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"
16 #include "lily-guile.icc"
19 ugh, move to chord-name-engraver
21 Hmm, why not represent complete chord as list?
22 ((tonic third fifth) (inversion bass))
25 Chord_name::set (Chord const& c)
27 set_elt_property ("pitches", array_to_scm (c.pitch_arr_));
29 set_elt_property ("inversion", to_scm (c.inversion_pitch_));
31 set_elt_property ("bass", to_scm (c.bass_pitch_));
39 notename2scm (Musical_pitch p)
41 return gh_cons (gh_int2scm (p.notename_i_), gh_int2scm (p.accidental_i_));
45 word is roman text or styled text:
50 Chord_name::ly_word2molecule (SCM scm) const
55 style = ly_scm2string (gh_car (scm));
58 String text = ly_scm2string (scm);
59 return lookup_l ()->text (style, text, paper_l ());
63 scm is word or list of words:
68 Chord_name::ly_text2molecule (SCM scm) const
73 while (gh_cdr (scm) != SCM_EOL)
75 mol.add_at_edge (X_AXIS, RIGHT,
76 ly_word2molecule (gh_car (scm)), 0);
81 mol.add_at_edge (X_AXIS, RIGHT,
82 ly_word2molecule (scm), 0);
87 Chord_name::pitch2molecule (Musical_pitch p) const
89 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-pitch-name"), ly_quote_scm (notename2scm (p)), SCM_UNDEFINED));
91 if (name != SCM_UNSPECIFIED)
93 return ly_text2molecule (name);
96 Molecule mol = lookup_l ()->text ("", p.str ().left_str (1).upper_str (), paper_l ());
99 We want the smaller size, even if we're big ourselves.
102 mol.add_at_edge (X_AXIS, RIGHT,
104 paper_l ()->lookup_l (-2)->afm_find (String ("accidentals-") + to_str (p.accidental_i_)), 0.0);
109 diff_pitch (Musical_pitch tonic, Musical_pitch p)
111 Musical_pitch diff (p.notename_i_ - tonic.notename_i_,
112 p.accidental_i_ - tonic.accidental_i_,
113 p.octave_i_ - tonic.octave_i_);
115 while (diff.notename_i_ >= 7)
117 diff.notename_i_ -= 7;
120 while (diff.notename_i_ < 0)
122 diff.notename_i_ += 7;
126 diff.accidental_i_ -= (tonic.semitone_pitch () + diff.semitone_pitch ())
127 - p.semitone_pitch ();
133 Chord_name::user_chord_name (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
136 Array<Musical_pitch> chord_type = pitch_arr;
137 Chord::rebuild_transpose (&chord_type, diff_pitch (pitch_arr[0], Musical_pitch (0)), false);
139 for (int i= chord_type.size (); i--; )
140 chord = gh_cons (notename2scm (chord_type[i]), chord);
142 SCM name = scm_eval (gh_list (ly_symbol2scm ("user-chord-name"), ly_quote_scm (chord), SCM_UNDEFINED));
143 if (gh_pair_p (name))
145 name_p->modifier_mol = ly_text2molecule (gh_car (name));
146 name_p->addition_mol = ly_text2molecule (gh_cdr (name));
153 Chord_name::banter (Array<Musical_pitch> pitch_arr, Chord_mol* name_p) const
155 Array<Musical_pitch> add_arr;
156 Array<Musical_pitch> sub_arr;
157 Chord::find_additions_and_subtractions (pitch_arr, &add_arr, &sub_arr);
159 Array<Musical_pitch> scale;
160 for (int i=0; i < 7; i++)
161 scale.push (Musical_pitch (i));
163 Musical_pitch tonic = pitch_arr[0];
164 Chord::rebuild_transpose (&scale, tonic, true);
167 Does chord include this step? -1 if flat
170 for (int i=0; i<16; i++)
176 for (int i = 0; i < add_arr.size (); i++)
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))
190 || ((i == add_arr.size () - 1) && (step > 5))))
194 if ((step == 7) && (accidental == 1))
200 add_str += to_str (step);
202 add_str += accidental < 0 ? "-" : "+";
207 for (int i = 0; i < sub_arr.size (); i++)
209 Musical_pitch p = sub_arr[i];
210 int step = Chord::step_i (tonic, p);
212 if additions include 2 or 4, assume sus2/4 and don't display 'no3'
214 if (!((step == 3) && (has[2] || has[4])))
216 add_str += sep_str + "no" + to_str (step);
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 ())
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);
235 fix silly to-and-fro scm conversions
238 Chord_name::do_brew_molecule_p () const
240 Array<Musical_pitch> pitch_arr;
241 scm_to_array (get_elt_property ("pitches"), &pitch_arr);
242 Musical_pitch tonic = pitch_arr[0];
245 name.tonic_mol = pitch2molecule (tonic);
248 if user has explicitely listed chord name, use that
252 maybe we should check all sub-lists of pitches, not
253 just full list and base triad?
255 if (!user_chord_name (pitch_arr, &name))
258 else, check if user has listed base triad
259 use user base name and add banter for remaining part
261 if ((pitch_arr.size () > 2)
262 && user_chord_name (pitch_arr.slice (0, 3), &name))
264 Array<Musical_pitch> base = Chord::base_arr (tonic);
265 base.concat (pitch_arr.slice (3, pitch_arr.size ()));
266 banter (base, &name);
269 else, use pure banter
273 banter (pitch_arr, &name);
277 SCM s = get_elt_property ("inversion");
278 if (s != SCM_UNDEFINED)
280 name.inversion_mol = lookup_l ()->text ("", "/", paper_l ());
283 Molecule mol = pitch2molecule (p);
284 name.inversion_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
287 s = get_elt_property ("bass");
288 if (s != SCM_UNDEFINED)
290 name.bass_mol = lookup_l ()->text ("", "/", paper_l ());
293 Molecule mol = pitch2molecule (p);
294 name.bass_mol.add_at_edge (X_AXIS, RIGHT, mol, 0);
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));
302 Molecule* mol_p = new Molecule;
303 mol_p->add_at_edge (X_AXIS, RIGHT, name.tonic_mol, 0);
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);