2 chord-name.cc -- implement Chord_name
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "chord-name.hh"
10 #include "musical-request.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
16 #include "staff-symbol-referencer.hh"
20 TODO: move text lookup out of Chord_name
24 word is roman text or styled text:
29 Chord_name::ly_word2molecule (SCM scm) const
34 style = ly_scm2string (gh_car (scm));
37 String text = ly_scm2string (scm);
38 return lookup_l ()->text (style, text, paper_l ());
42 scm is word or list of words:
47 Chord_name::ly_text2molecule (SCM scm) const
52 while (gh_cdr (scm) != SCM_EOL)
54 mol.add_at_edge (X_AXIS, RIGHT, ly_word2molecule (gh_car (scm)), 0);
59 mol.add_at_edge (X_AXIS, RIGHT, ly_word2molecule (scm), 0);
64 Chord_name::pitch2molecule (Musical_pitch p) const
66 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-pitch-name"),
67 ly_quote_scm (p.to_scm ()),
70 if (name != SCM_UNSPECIFIED)
72 return ly_text2molecule (name);
75 Molecule mol = lookup_l ()->text ("", p.str ().left_str (1).upper_str (), paper_l ());
78 We want the smaller size, even if we're big ourselves.
82 Molecule acc = paper_l ()->lookup_l (-3)->afm_find
83 (String ("accidentals-") + to_str (p.accidental_i_));
84 // urg, howto get a good superscript_y?
85 Real super_y = lookup_l ()->text ("", "x", paper_l ()).extent
86 ()[Y_AXIS].length () / 2;
87 super_y += -acc.extent ()[Y_AXIS][MIN];
88 acc.translate_axis (super_y, Y_AXIS);
89 mol.add_at_edge (X_AXIS, RIGHT, acc, 0.0);
96 diff_pitch (Musical_pitch tonic, Musical_pitch p)
98 Musical_pitch diff (p.notename_i_ - tonic.notename_i_,
99 p.accidental_i_ - tonic.accidental_i_,
100 p.octave_i_ - tonic.octave_i_);
102 while (diff.notename_i_ >= 7)
104 diff.notename_i_ -= 7;
107 while (diff.notename_i_ < 0)
109 diff.notename_i_ += 7;
113 diff.accidental_i_ -= (tonic.semitone_pitch () + diff.semitone_pitch ())
114 - p.semitone_pitch ();
123 Chord_name::user_chord_name (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
125 Array<Musical_pitch> chord_type = pitch_arr;
126 Chord::rebuild_transpose (&chord_type, diff_pitch (pitch_arr[0], Musical_pitch (0)), false);
129 for (int i= chord_type.size (); i--; )
130 chord = gh_cons (chord_type[i].to_scm (), chord);
133 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-chord-name"),
134 ly_quote_scm (chord),
136 if (gh_pair_p (name))
138 name_p->modifier_mol = ly_text2molecule (gh_car (name));
139 name_p->addition_mol = ly_text2molecule (gh_cdr (name));
146 Chord_name::banter (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
148 Array<Musical_pitch> add_arr;
149 Array<Musical_pitch> sub_arr;
150 Chord::find_additions_and_subtractions (pitch_arr, &add_arr, &sub_arr);
152 Array<Musical_pitch> scale;
153 for (int i=0; i < 7; i++)
154 scale.push (Musical_pitch (i));
156 Musical_pitch tonic = pitch_arr[0];
157 Chord::rebuild_transpose (&scale, tonic, true);
160 Does chord include this step? -1 if flat
163 for (int i=0; i<16; i++)
169 for (int i = 0; i < add_arr.size (); i++)
171 Musical_pitch p = add_arr[i];
172 int step = Chord::step_i (tonic, p);
173 int accidental = p.accidental_i_ - scale[(step - 1) % 7].accidental_i_;
174 if ((step < 16) && (has[step] != -1))
175 has[step] = accidental == -1 ? -1 : 1;
176 // only from guile table ?
177 if ((step == 3) && (accidental == -1))
183 || ((i == add_arr.size () - 1) && (step > 5))))
187 if ((step == 7) && (accidental == 1))
193 add_str += to_str (step);
195 add_str += accidental < 0 ? "-" : "+";
200 for (int i = 0; i < sub_arr.size (); i++)
202 Musical_pitch p = sub_arr[i];
203 int step = Chord::step_i (tonic, p);
205 if additions include 2 or 4, assume sus2/4 and don't display 'no3'
207 if (!((step == 3) && (has[2] || has[4])))
209 add_str += sep_str + "no" + to_str (step);
214 if (mod_str.length_i ())
215 name_p->modifier_mol.add_at_edge (X_AXIS, RIGHT,
216 lookup_l ()->text ("roman", mod_str, paper_l ()), 0);
217 if (add_str.length_i ())
219 if (!name_p->addition_mol.empty_b ())
220 add_str = "/" + add_str;
221 name_p->addition_mol.add_at_edge (X_AXIS, RIGHT,
222 lookup_l ()->text ("script", add_str, paper_l ()), 0);
228 fix silly to-and-fro scm conversions
231 Chord_name::do_brew_molecule () const
233 Array<Musical_pitch> pitch_arr;
235 for (SCM s = get_elt_property ("pitches"); s != SCM_EOL; s = gh_cdr (s))
236 pitch_arr.push (Musical_pitch (gh_car (s)));
238 Musical_pitch tonic = pitch_arr[0];
241 name.tonic_mol = pitch2molecule (tonic);
244 if user has explicitely listed chord name, use that
248 maybe we should check all sub-lists of pitches, not
249 just full list and base triad?
251 if (!user_chord_name (pitch_arr, &name))
254 else, check if user has listed base triad
255 use user base name and add banter for remaining part
257 if ((pitch_arr.size () > 2)
258 && user_chord_name (pitch_arr.slice (0, 3), &name))
260 Array<Musical_pitch> base = Chord::base_arr (tonic);
261 base.concat (pitch_arr.slice (3, pitch_arr.size ()));
262 banter (base, &name);
265 else, use pure banter
269 banter (pitch_arr, &name);
273 SCM s = get_elt_property ("inversion");
274 if (s != SCM_UNDEFINED)
276 name.inversion_mol = lookup_l ()->text ("", "/", paper_l ());
279 Molecule mol = pitch2molecule (p);
280 name.inversion_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
283 s = get_elt_property ("bass");
284 if (s != SCM_UNDEFINED)
286 name.bass_mol = lookup_l ()->text ("", "/", paper_l ());
288 Molecule mol = pitch2molecule (p);
289 name.bass_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
292 // urg, howto get a good superscript_y?
293 Real super_y = lookup_l ()->text ("", "x", paper_l ()).extent
294 ()[Y_AXIS].length () / 2;
295 if (!name.addition_mol.empty_b ())
296 name.addition_mol.translate (Offset (0, super_y));
299 mol.add_at_edge (X_AXIS, RIGHT, name.tonic_mol, 0);
301 if (!name.modifier_mol.empty_b ())
302 mol.add_at_edge (X_AXIS, RIGHT, name.modifier_mol, 0);
303 if (!name.addition_mol.empty_b ())
304 mol.add_at_edge (X_AXIS, RIGHT, name.addition_mol, 0);
305 if (!name.inversion_mol.empty_b ())
306 mol.add_at_edge (X_AXIS, RIGHT, name.inversion_mol, 0);
307 if (!name.bass_mol.empty_b ())
308 mol.add_at_edge (X_AXIS, RIGHT, name.bass_mol, 0);
310 s = get_elt_property ("word-space");
312 mol.dim_.interval_a_[X_AXIS][RIGHT] += gh_scm2double (s)
313 * staff_symbol_referencer (this).staff_space ();