2 chord.cc -- implement Chord
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
12 // doesn't seem common, and we should know about this during purple hit
13 // #define INVERSION_ADDED_AS_BASE 1
15 Chord::Chord (Array<Musical_pitch> pitch_arr)
17 pitch_arr_ = pitch_arr;
21 rebuild_transpose (Musical_pitch tonic, Array<Musical_pitch>* pitch_arr_p)
23 for (int i = 0; i < pitch_arr_p->size (); i++)
25 Musical_pitch p = tonic;
26 Musical_pitch q = (*pitch_arr_p)[i];
27 // duh, c7 should mean <c bes>
28 if (q.notename_i_ == 6)
31 (*pitch_arr_p)[i] = p;
33 pitch_arr_p->sort (Musical_pitch::compare);
37 find_pitch_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
39 for (int i = 0; i < pitch_arr_p->size (); i++)
40 if (p == (*pitch_arr_p)[i])
46 find_notename_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
48 int i = find_pitch_i (pitch_arr_p, p);
51 for (int i = 0; i < pitch_arr_p->size (); i++)
53 p.octave_i_ = (*pitch_arr_p)[i].octave_i_;
54 if (p == (*pitch_arr_p)[i])
62 trap_i (Musical_pitch tonic, Musical_pitch p)
64 int i = p.notename_i_ - tonic.notename_i_
65 + (p.octave_i_ - tonic.octave_i_) * 7;
72 static Array<Musical_pitch>
73 missing_triads_pitch_arr (Array<Musical_pitch>const* pitch_arr_p)
75 Musical_pitch third (2);
76 Musical_pitch mthird (2, -1);
78 Array<Musical_pitch> triads;
79 triads.push (third); // c e
80 triads.push (mthird); // d f
81 triads.push (mthird); // e g
82 triads.push (third); // f a
83 triads.push (third); // g b
84 triads.push (mthird); // a c
85 triads.push (mthird); // b d
87 Musical_pitch tonic = (*pitch_arr_p)[0];
88 Musical_pitch last = tonic;
89 Array<Musical_pitch> missing_arr;
91 for (int i = 0; i < pitch_arr_p->size ();)
93 Musical_pitch p = (*pitch_arr_p)[i];
94 int trap = trap_i (tonic, p);
95 if (last.notename_i_ == p.notename_i_)
96 last.transpose (triads[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
97 if (trap > trap_i (tonic, last))
99 while (trap > trap_i (tonic, last))
101 if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
103 Musical_pitch special_seven = last;
104 Musical_pitch lower (0, -1);
105 special_seven.transpose (lower);
106 missing_arr.push (special_seven);
110 missing_arr.push (last);
112 last.transpose (triads[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
125 construct from parser output
127 Chord::Chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Musical_pitch* inversion_p)
129 rebuild_transpose (tonic, add_arr_p);
130 rebuild_transpose (tonic, sub_arr_p);
132 Musical_pitch fifth = tonic;
133 fifth.transpose (Musical_pitch (2));
134 fifth.transpose (Musical_pitch (2, -1));
137 default chord includes upto 5: <1, 3, 5>
139 add_arr_p->insert (tonic, 0);
140 int highest_trap = trap_i (tonic, add_arr_p->top ());
141 if (highest_trap < 5)
142 add_arr_p->push (fifth);
147 Array<Musical_pitch> missing_arr = missing_triads_pitch_arr (add_arr_p);
150 if additions include 4, assume sus4 and don't add third implicitely
152 Musical_pitch third = tonic;
153 third.transpose (Musical_pitch (2));
154 Musical_pitch sus = tonic;
155 sus.transpose (Musical_pitch (3));
156 if (::find_pitch_i (add_arr_p, sus) != -1)
158 int i = ::find_pitch_i (&missing_arr, third);
164 complete the list of triads to be added
166 add_arr_p->concat (missing_arr);
167 add_arr_p->sort (Musical_pitch::compare);
170 add all that aren't subtracted
172 for (int i = 0; i < add_arr_p->size (); i++)
174 Musical_pitch p = (*add_arr_p)[i];
176 for (; j < sub_arr_p->size (); j++)
177 if (p == (*sub_arr_p)[j])
183 if (j == sub_arr_p->size ())
187 pitch_arr_.sort (Musical_pitch::compare);
189 for (int i = 0; i < sub_arr_p->size (); i++)
190 warning (_f ("invalid subtraction: not part of chord: %s",
191 (*sub_arr_p)[i].str ()));
196 for (; i < pitch_arr_.size (); i++)
198 if ((pitch_arr_[i].notename_i_ == inversion_p->notename_i_)
199 && (pitch_arr_[i].accidental_i_ == inversion_p->accidental_i_))
202 if (i == pitch_arr_.size ())
204 warning (_f ("invalid inversion pitch: not part of chord: %s",
205 inversion_p->str ()));
209 #if INVERSION_ADDED_AS_BASE
210 pitch_arr_.insert (pitch_arr_[i], 0);
211 rebuild_with_bass (0);
213 rebuild_with_bass (i);
222 Chord::find_additions_and_subtractions(Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p)
224 Musical_pitch tonic = pitch_arr_[0];
226 all the triads that should be there
228 Array<Musical_pitch> all_arr;
229 all_arr.push (tonic);
230 all_arr.push (pitch_arr_.top ());
231 all_arr.concat (missing_triads_pitch_arr (&all_arr));
232 all_arr.sort (Musical_pitch::compare);
236 while ((i < all_arr.size ()) || (j < pitch_arr_.size ()))
238 i = i <? all_arr.size () - 1;
239 j = j <? pitch_arr_.size () - 1;
240 Musical_pitch a = all_arr[i];
241 Musical_pitch p = pitch_arr_[j];
247 else if ((p < a) || (p.notename_i_ == a.notename_i_))
260 add highest addition, because it names chord
262 if (trap_i (tonic, pitch_arr_.top () > 5))
263 add_arr_p->push (pitch_arr_.top ());
267 Chord::banter_str (Musical_pitch* inversion) const
269 Musical_pitch tonic = pitch_arr_[0];
271 //urg, should do translation in scheme.
272 char const *acc[] = {"\\textflat\\textflat ", "\\textflat ", "", "\\textsharp " , "\\textsharp\\textsharp "};
273 String tonic_str = tonic.str ();
274 tonic_str = tonic_str.left_str (1).upper_str ()
275 + acc[tonic.accidental_i_ + 2];
277 Array<Musical_pitch> add_arr;
278 Array<Musical_pitch> sub_arr;
279 find_additions_and_subtractions (&add_arr, &sub_arr);
281 Array<Musical_pitch> scale;
282 scale.push (Musical_pitch (0)); // c
283 scale.push (Musical_pitch (1)); // d
284 scale.push (Musical_pitch (2)); // e
285 scale.push (Musical_pitch (3)); // f
286 scale.push (Musical_pitch (4)); // g
287 scale.push (Musical_pitch (5)); // a
288 scale.push (Musical_pitch (6)); // b
289 // 7 always means 7-...
290 // scale.push (Musical_pitch (6, -1)); // b
292 rebuild_transpose (tonic, &scale);
294 bool has3m_b = false;
298 for (int i = 0; i < add_arr.size (); i++)
300 Musical_pitch p = add_arr[i];
301 int trap = trap_i (tonic, p);
304 int accidental = p.accidental_i_ - scale[(trap - 1) % 7].accidental_i_;
305 if ((trap == 3) && (accidental == -1))
311 || (!(trap % 2) || ((i + 1 == add_arr.size ()) && (trap > 5))))
314 if ((trap == 7) && (accidental == 1))
318 str += to_str (trap);
320 str += accidental < 0 ? "-" : "+";
326 for (int i = 0; i < sub_arr.size (); i++)
328 Musical_pitch p = sub_arr[i];
329 int trap = trap_i (tonic, p);
331 if chord has 3-, assume minor and don't display 'no3'
332 if additions include 4, assume sus4 and don't display 'no3'
334 if (!((trap == 3) && (has3m_b || has4_b)))
336 str += sep_str + "no" + to_str (trap);
341 String inversion_str;
344 inversion_str = inversion->str ();
345 inversion_str = "/" + inversion_str.left_str (1).upper_str ()
346 + acc[inversion->accidental_i_ + 2];
350 return tonic_str + "$^{" + str + "}$" + inversion_str;
354 Chord::find_notename_i (Musical_pitch p) const
356 return ::find_notename_i (&pitch_arr_, p);
360 Chord::find_pitch_i (Musical_pitch p) const
362 return ::find_pitch_i (&pitch_arr_, p);
366 Chord::find_tonic_i () const
371 first try: base of longest line of triads
375 for (int i = 0; i < pitch_arr_.size (); i++)
378 int last_i = pitch_arr_[i % pitch_arr_.size ()].notename_i_;
380 for (; j < pitch_arr_.size (); j++)
382 int cur_i = pitch_arr_[(i + j + 1) % pitch_arr_.size ()].notename_i_;
383 int gap = cur_i - last_i;
392 if (j - no_triad_i > longest_i)
394 longest_i = j - no_triad_i;
400 second try: note after biggest gap
405 for (int i = 0; i < pitch_arr_.size (); i++)
407 int gap = pitch_arr_[i].notename_i_
408 - pitch_arr_[(i - 1 + pitch_arr_.size ())
409 % pitch_arr_.size ()].notename_i_;
423 Chord::rebuild_from_base (int base_i)
425 assert (base_i >= 0);
426 Musical_pitch last (0, 0, -5);
427 Array<Musical_pitch> new_arr;
428 for (int i = 0; i < pitch_arr_.size (); i++)
430 Musical_pitch p = pitch_arr_[(base_i + i) % pitch_arr_.size ()];
433 p.octave_i_ = last.octave_i_;
440 pitch_arr_ = new_arr;
444 Chord::rebuild_insert_inversion (int tonic_i)
446 assert (tonic_i > 0);
447 #if INVERSION_ADDED_AS_BASE
448 // inversion was added; don't insert
449 Musical_pitch inversion = pitch_arr_.get (0);
452 Musical_pitch inversion = pitch_arr_.get (0);
453 rebuild_from_base (tonic_i - 1);
454 if (pitch_arr_.size ())
456 inversion.octave_i_ = pitch_arr_[0].octave_i_ - 1;
457 while (inversion < pitch_arr_[0])
458 inversion.octave_i_++;
460 for (int i = 0; i < pitch_arr_.size (); i++)
461 if (pitch_arr_[i] > inversion)
463 pitch_arr_.insert (inversion, i);
470 Chord::rebuild_with_bass (int bass_i)
472 assert (bass_i >= 0);
473 Musical_pitch inversion = pitch_arr_.get (bass_i);
474 // is lowering fine, or should others be raised?
475 if (pitch_arr_.size ())
476 while (inversion > pitch_arr_[0])
477 inversion.octave_i_--;
478 pitch_arr_.insert (inversion, 0);