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