]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name.cc
release: 1.3.38
[lilypond.git] / lily / chord-name.cc
1 /*
2   chord-name.cc -- implement Chord_name
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "chord-name.hh"
10 #include "musical-request.hh"
11 #include "warn.hh"
12 #include "debug.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
15 #include "lookup.hh"
16
17
18 /*
19   TODO: move text lookup out of Chord_name
20  */
21
22 /*
23   word is roman text or styled text:
24    "text"
25    ("style" . "text")
26  */
27 Molecule
28 Chord_name::ly_word2molecule (SCM scm) const
29 {
30   String style;
31   if (gh_pair_p (scm))
32     {
33       style = ly_scm2string (gh_car (scm));
34       scm = gh_cdr (scm);
35     }
36   String text = ly_scm2string (scm);
37   return lookup_l ()->text (style, text, paper_l ());
38 }
39
40 /*
41  scm is word or list of words:
42    word
43    (word word)
44  */
45 Molecule
46 Chord_name::ly_text2molecule (SCM scm) const
47 {
48   Molecule mol;
49   if (gh_list_p (scm))
50     {
51       while (gh_cdr (scm) != SCM_EOL)
52         {
53           mol.add_at_edge (X_AXIS, RIGHT, ly_word2molecule (gh_car (scm)), 0);
54           scm = gh_cdr (scm);
55         }
56       scm = gh_car (scm);
57     }  
58   mol.add_at_edge (X_AXIS, RIGHT, ly_word2molecule (scm), 0);
59   return mol;
60 }
61
62 Molecule
63 Chord_name::pitch2molecule (Musical_pitch p) const
64 {
65   SCM name = scm_eval (gh_list (ly_symbol2scm ("user-pitch-name"),
66                                 ly_quote_scm (p.to_scm ()),
67                                 SCM_UNDEFINED));
68
69   if (name != SCM_UNSPECIFIED)
70     {
71       return ly_text2molecule (name);
72     }
73
74   Molecule mol = lookup_l ()->text ("", p.str ().left_str (1).upper_str (), paper_l ());
75
76   /*
77     We want the smaller size, even if we're big ourselves.
78    */
79   if (p.accidental_i_)
80     mol.add_at_edge (X_AXIS, RIGHT, 
81                      
82                      paper_l ()->lookup_l (-2)->afm_find (String ("accidentals-") + to_str (p.accidental_i_)), 0.0);
83   return mol;
84 }
85
86 Musical_pitch
87 diff_pitch (Musical_pitch tonic, Musical_pitch  p)
88 {
89   Musical_pitch diff (p.notename_i_ - tonic.notename_i_, 
90     p.accidental_i_ - tonic.accidental_i_, 
91     p.octave_i_ - tonic.octave_i_);
92
93   while  (diff.notename_i_ >= 7)
94     {
95       diff.notename_i_ -= 7;
96       diff.octave_i_ ++;
97     }
98   while  (diff.notename_i_ < 0)
99     {
100       diff.notename_i_ += 7;
101       diff.octave_i_ --;
102     }
103
104   diff.accidental_i_ -= (tonic.semitone_pitch () + diff.semitone_pitch ())
105     - p.semitone_pitch ();
106
107   return diff;
108 }
109
110 /*
111   JUNKME
112  */
113 bool
114 Chord_name::user_chord_name (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
115 {
116   Array<Musical_pitch> chord_type = pitch_arr;
117   Chord::rebuild_transpose (&chord_type, diff_pitch (pitch_arr[0], Musical_pitch (0)), false);
118
119   SCM chord = SCM_EOL;
120   for (int i= chord_type.size (); i--; )
121     chord = gh_cons (chord_type[i].to_scm (), chord);
122
123
124   SCM name = scm_eval (gh_list (ly_symbol2scm ("user-chord-name"),
125                                 ly_quote_scm (chord),
126                                 SCM_UNDEFINED));
127   if (gh_pair_p (name))
128     {
129       name_p->modifier_mol = ly_text2molecule (gh_car (name));
130       name_p->addition_mol = ly_text2molecule (gh_cdr (name));
131       return true;
132     }
133   return false;
134 }
135
136 void
137 Chord_name::banter (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
138 {
139   Array<Musical_pitch> add_arr;
140   Array<Musical_pitch> sub_arr;
141   Chord::find_additions_and_subtractions (pitch_arr, &add_arr, &sub_arr);
142                            
143   Array<Musical_pitch> scale;
144   for (int i=0; i < 7; i++)
145     scale.push (Musical_pitch (i));
146
147   Musical_pitch tonic = pitch_arr[0];
148   Chord::rebuild_transpose (&scale, tonic, true);
149   
150   /*
151     Does chord include this step?  -1 if flat
152    */
153   int has[16];
154   for (int i=0; i<16; i++)
155     has[i] = 0;
156
157   String mod_str;
158   String add_str;
159   String sep_str;
160   for (int i = 0; i < add_arr.size (); i++)
161     {
162       Musical_pitch p = add_arr[i];
163       int step = Chord::step_i (tonic, p);
164       int accidental = p.accidental_i_ - scale[(step - 1) % 7].accidental_i_;
165       if ((step < 16) && (has[step] != -1))
166         has[step] = accidental == -1 ? -1 : 1;
167       // only from guile table ?
168       if ((step == 3) && (accidental == -1))
169         {
170           mod_str = "m";
171         }
172       else if (accidental
173                || (!(step % 2) 
174                || ((i == add_arr.size () - 1) && (step > 5))))
175         {
176           add_str += sep_str;
177           sep_str = "/";
178           if ((step == 7) && (accidental == 1))
179             {
180               add_str += "maj7";
181             }
182           else
183             {
184               add_str += to_str (step);
185               if (accidental)
186                 add_str += accidental < 0 ? "-" : "+";
187             }
188         }
189     }
190
191   for (int i = 0; i < sub_arr.size (); i++)
192     {
193       Musical_pitch p = sub_arr[i];
194       int step = Chord::step_i (tonic, p);
195       /*
196         if additions include 2 or 4, assume sus2/4 and don't display 'no3'
197       */
198       if (!((step == 3) && (has[2] || has[4])))
199         {
200           add_str += sep_str + "no" + to_str (step);
201           sep_str = "/";
202         }
203     }
204
205   if (mod_str.length_i ())
206     name_p->modifier_mol.add_at_edge (X_AXIS, RIGHT, 
207       lookup_l ()->text ("roman", mod_str, paper_l ()), 0);
208   if (add_str.length_i ())
209     {
210       if (!name_p->addition_mol.empty_b ())
211         add_str = "/" + add_str;
212       name_p->addition_mol.add_at_edge (X_AXIS, RIGHT,
213        lookup_l ()->text ("script", add_str, paper_l ()), 0);
214     }
215 }
216
217 /*
218   TODO:
219     fix silly to-and-fro scm conversions
220  */
221 Molecule 
222 Chord_name::do_brew_molecule () const
223 {
224   Array<Musical_pitch> pitch_arr;
225   
226   for (SCM s = get_elt_property ("pitches"); s != SCM_EOL; s = gh_cdr (s))
227     pitch_arr.push (Musical_pitch (gh_car (s)));
228   
229   Musical_pitch tonic = pitch_arr[0];
230   
231   Chord_mol name;
232   name.tonic_mol = pitch2molecule (tonic);
233
234   /*
235     if user has explicitely listed chord name, use that
236     
237     TODO
238     urg
239     maybe we should check all sub-lists of pitches, not
240     just full list and base triad?
241    */
242   if (!user_chord_name (pitch_arr, &name))
243     {
244       /*
245         else, check if user has listed base triad
246         use user base name and add banter for remaining part
247        */
248       if ((pitch_arr.size () > 2)
249           && user_chord_name (pitch_arr.slice (0, 3), &name))
250         {
251           Array<Musical_pitch> base = Chord::base_arr (tonic);
252           base.concat (pitch_arr.slice (3, pitch_arr.size ()));
253           banter (base, &name);
254         }
255       /*
256         else, use pure banter
257        */
258       else
259         {
260           banter (pitch_arr, &name);
261         }
262     }
263
264   SCM s = get_elt_property ("inversion");
265   if (s != SCM_UNDEFINED)
266     {
267       name.inversion_mol = lookup_l ()->text ("", "/", paper_l ());
268       Musical_pitch p (s);
269
270       Molecule mol = pitch2molecule (p);
271       name.inversion_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
272     }
273
274   s = get_elt_property ("bass");
275   if (s != SCM_UNDEFINED)
276     {
277       name.bass_mol = lookup_l ()->text ("", "/", paper_l ());
278       Musical_pitch p (s);
279       Molecule mol = pitch2molecule (p);
280       name.bass_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
281     }
282
283   // urg, howto get a good superscript_y?
284   Real super_y = lookup_l ()->text ("", "x", paper_l ()).dim_.y ().length ()/2;
285   if (!name.addition_mol.empty_b ())
286     name.addition_mol.translate (Offset (0, super_y));
287
288   Molecule  mol;
289   mol.add_at_edge (X_AXIS, RIGHT, name.tonic_mol, 0);
290   // huh?
291   if (!name.modifier_mol.empty_b ())
292     mol.add_at_edge (X_AXIS, RIGHT, name.modifier_mol, 0);
293   if (!name.addition_mol.empty_b ())
294     mol.add_at_edge (X_AXIS, RIGHT, name.addition_mol, 0);
295   if (!name.inversion_mol.empty_b ())
296     mol.add_at_edge (X_AXIS, RIGHT, name.inversion_mol, 0);
297   if (!name.bass_mol.empty_b ())
298     mol.add_at_edge (X_AXIS, RIGHT, name.bass_mol, 0);
299   return mol;
300 }