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