2 chord-name.cc -- implement Chord_name
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "chord-name.hh"
10 #include "musical-request.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
18 to_scm (Musical_pitch p)
20 return gh_list (gh_int2scm (p.notename_i_), gh_int2scm (p.accidental_i_), gh_int2scm (p.octave_i_), SCM_UNDEFINED);
26 return Musical_pitch (gh_scm2int (gh_car (s)),
27 gh_scm2int (gh_cadr (s)),
28 gh_scm2int (gh_caddr (s)));
32 array_to_scm (Array<T> arr)
35 for (int i = arr.size (); i--;)
36 list = gh_cons (to_scm (arr[i]), list);
42 Array<T> scm_to_array (SCM s)
45 scm_to_array (SCM s, Array<T>* arr)
47 for (; gh_pair_p (s); s= gh_cdr (s))
48 arr->push (from_scm (gh_car (s)));
52 ugh, move to chord-name-engraver
54 Hmm, why not represent complete chord as list?
55 ((tonic third fifth) (inversion bass))
58 Chord_name::set (Chord const& c)
60 set_elt_property ("pitches", array_to_scm (c.pitch_arr_));
62 set_elt_property ("inversion", to_scm (c.inversion_pitch_));
64 set_elt_property ("bass", to_scm (c.bass_pitch_));
68 notename2scm (Musical_pitch p)
70 return gh_cons (gh_int2scm (p.notename_i_), gh_int2scm (p.accidental_i_));
74 word is roman text or styled text:
79 Chord_name::ly_word2molecule (SCM scm) const
84 style = ly_scm2string (gh_car (scm));
87 String text = ly_scm2string (scm);
88 return lookup_l ()->text (style, text, paper_l ());
92 scm is word or list of words:
97 Chord_name::ly_text2molecule (SCM scm) const
102 while (gh_cdr (scm) != SCM_EOL)
104 mol.add_at_edge (X_AXIS, RIGHT,
105 ly_word2molecule (gh_car (scm)), 0);
110 mol.add_at_edge (X_AXIS, RIGHT,
111 ly_word2molecule (scm), 0);
116 Chord_name::pitch2molecule (Musical_pitch p) const
118 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-pitch-name"), ly_quote_scm (notename2scm (p)), SCM_UNDEFINED));
120 if (name != SCM_UNSPECIFIED)
122 return ly_text2molecule (name);
125 Molecule mol = lookup_l ()->text ("", p.str ().left_str (1).upper_str (), paper_l ());
128 We want the smaller size, even if we're big ourselves.
131 mol.add_at_edge (X_AXIS, RIGHT,
133 paper_l ()->lookup_l (-2)->afm_find (String ("accidentals-") + to_str (p.accidental_i_)), 0.0);
138 diff_pitch (Musical_pitch tonic, Musical_pitch p)
140 Musical_pitch diff (p.notename_i_ - tonic.notename_i_,
141 p.accidental_i_ - tonic.accidental_i_,
142 p.octave_i_ - tonic.octave_i_);
144 while (diff.notename_i_ >= 7)
146 diff.notename_i_ -= 7;
149 while (diff.notename_i_ < 0)
151 diff.notename_i_ += 7;
155 diff.accidental_i_ -= (tonic.semitone_pitch () + diff.semitone_pitch ())
156 - p.semitone_pitch ();
162 Chord_name::user_chord_name (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
165 Array<Musical_pitch> chord_type = pitch_arr;
166 Chord::rebuild_transpose (&chord_type, diff_pitch (pitch_arr[0], Musical_pitch (0)), false);
168 for (int i= chord_type.size (); i--; )
169 chord = gh_cons (notename2scm (chord_type[i]), chord);
171 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-chord-name"), ly_quote_scm (chord), SCM_UNDEFINED));
172 if (gh_pair_p (name))
174 name_p->modifier_mol = ly_text2molecule (gh_car (name));
175 name_p->addition_mol = ly_text2molecule (gh_cdr (name));
182 Chord_name::banter (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
184 Array<Musical_pitch> add_arr;
185 Array<Musical_pitch> sub_arr;
186 Chord::find_additions_and_subtractions (pitch_arr, &add_arr, &sub_arr);
188 Array<Musical_pitch> scale;
189 for (int i=0; i < 7; i++)
190 scale.push (Musical_pitch (i));
192 Musical_pitch tonic = pitch_arr[0];
193 Chord::rebuild_transpose (&scale, tonic, true);
196 Does chord include this step? -1 if flat
199 for (int i=0; i<16; i++)
205 for (int i = 0; i < add_arr.size (); i++)
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))
219 || ((i == add_arr.size () - 1) && (step > 5))))
223 if ((step == 7) && (accidental == 1))
229 add_str += to_str (step);
231 add_str += accidental < 0 ? "-" : "+";
236 for (int i = 0; i < sub_arr.size (); i++)
238 Musical_pitch p = sub_arr[i];
239 int step = Chord::step_i (tonic, p);
241 if additions include 2 or 4, assume sus2/4 and don't display 'no3'
243 if (!((step == 3) && (has[2] || has[4])))
245 add_str += sep_str + "no" + to_str (step);
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 ())
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);
264 fix silly to-and-fro scm conversions
267 Chord_name::do_brew_molecule_p () const
269 Array<Musical_pitch> pitch_arr;
270 scm_to_array (get_elt_property ("pitches"), &pitch_arr);
271 Musical_pitch tonic = pitch_arr[0];
274 name.tonic_mol = pitch2molecule (tonic);
277 if user has explicitely listed chord name, use that
281 maybe we should check all sub-lists of pitches, not
282 just full list and base triad?
284 if (!user_chord_name (pitch_arr, &name))
287 else, check if user has listed base triad
288 use user base name and add banter for remaining part
290 if ((pitch_arr.size () > 2)
291 && user_chord_name (pitch_arr.slice (0, 3), &name))
293 Array<Musical_pitch> base = Chord::base_arr (tonic);
294 base.concat (pitch_arr.slice (3, pitch_arr.size ()));
295 banter (base, &name);
298 else, use pure banter
302 banter (pitch_arr, &name);
306 SCM s = get_elt_property ("inversion");
307 if (s != SCM_UNDEFINED)
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);
314 s = get_elt_property ("bass");
315 if (s != SCM_UNDEFINED)
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);
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));
327 Molecule* mol_p = new Molecule;
328 mol_p->add_at_edge (X_AXIS, RIGHT, name.tonic_mol, 0);
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);