2 chord.cc -- implement Chord
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
10 #include "musical-request.hh"
15 construct from parser output
18 to_chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Musical_pitch* inversion_p, Musical_pitch* bass_p)
20 // urg: catch dim modifier: 5th and 7th should be lowered
22 for (int i=0; i < add_arr_p->size (); i++)
24 Musical_pitch* p = &(*add_arr_p)[i];
25 if (p->octave_i_ == -100)
31 Chord::rebuild_transpose (add_arr_p, tonic);
32 Chord::rebuild_transpose (sub_arr_p, tonic);
34 Musical_pitch fifth = tonic;
35 fifth.transpose (Musical_pitch (2));
36 fifth.transpose (Musical_pitch (2, -1));
39 remove double adds (urg: sus4)
41 for (int i = add_arr_p->size () - 1; i >= 0 ; i--)
43 int j = Chord::find_pitch_i (add_arr_p, (*add_arr_p)[i]);
44 if ((j != -1) && (i != j))
51 default chord includes upto 5: <1, 3, 5>
53 add_arr_p->insert (tonic, 0);
54 Array<Musical_pitch> tmp = *add_arr_p;
55 int highest_step = Chord::step_i (tonic, tmp.top ());
60 Musical_pitch* p = &add_arr_p->top ();
67 Array<Musical_pitch> missing_arr = Chord::missing_thirds_pitch_arr (&tmp);
69 missing_arr.push (fifth);
73 for (int i=0; i < missing_arr.size (); i++)
75 missing_arr[i].accidental_i_--;
80 if additions include some 3, don't add third
82 Musical_pitch third = tonic;
83 third.transpose (Musical_pitch (2));
84 if (Chord::find_notename_i (add_arr_p, third) != -1)
86 int i = Chord::find_pitch_i (&missing_arr, third);
92 if additions include 4, assume sus4 and don't add third implicitely
93 C-sus (4) = c f g (1 4 5)
95 Musical_pitch sus = tonic;
96 sus.transpose (Musical_pitch (3));
97 if (Chord::find_pitch_i (add_arr_p, sus) != -1)
99 int i = Chord::find_pitch_i (&missing_arr, third);
105 if additions include some 5, don't add fifth
107 if (Chord::find_notename_i (add_arr_p, fifth) != -1)
109 int i = Chord::find_pitch_i (&missing_arr, fifth);
116 complete the list of thirds to be added
118 add_arr_p->concat (missing_arr);
119 add_arr_p->sort (Musical_pitch::compare);
121 Array<Musical_pitch> pitch_arr;
123 add all that aren't subtracted
125 for (int i = 0; i < add_arr_p->size (); i++)
127 Musical_pitch p = (*add_arr_p)[i];
129 for (; j < sub_arr_p->size (); j++)
130 if (p == (*sub_arr_p)[j])
136 if (j == sub_arr_p->size ())
140 pitch_arr.sort (Musical_pitch::compare);
142 for (int i = 0; i < sub_arr_p->size (); i++)
143 warning (_f ("invalid subtraction: not part of chord: %s",
144 (*sub_arr_p)[i].str ()));
146 return Chord (pitch_arr, inversion_p, bass_p);
150 Construct from list of pitches and requests
153 to_chord (Array<Musical_pitch> pitch_arr, Tonic_req* tonic_req, Inversion_req* inversion_req, Bass_req* bass_req, bool find_inversion_b)
155 Musical_pitch* inversion_p = 0;
156 Musical_pitch* bass_p = 0;
160 assert (pitch_arr[0].notename_i_ == bass_req->pitch_.notename_i_);
161 bass_p = new Musical_pitch (pitch_arr.get (0));
166 assert (pitch_arr[0].notename_i_ == inversion_req->pitch_.notename_i_);
167 inversion_p = new Musical_pitch (inversion_req->pitch_);
169 int tonic_i = Chord::find_notename_i (&pitch_arr, tonic_req->pitch_);
171 Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
174 if (find_inversion_b && !inversion_p)
176 int tonic_i = tonic_req
177 ? Chord::find_notename_i (&pitch_arr, tonic_req->pitch_)
178 : Chord::find_tonic_i (&pitch_arr);
182 inversion_p = &pitch_arr[0];
183 Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
189 assert (pitch_arr[0].notename_i_ == tonic_req->pitch_.notename_i_);
192 return Chord (pitch_arr, inversion_p, bass_p);
195 Chord::Chord (Array<Musical_pitch> pitch_arr, Musical_pitch* inversion_p, Musical_pitch* bass_p)
197 pitch_arr_ = pitch_arr;
198 inversion_p_ = inversion_p;
202 Chord::Chord (Chord const& chord)
204 pitch_arr_ = chord.pitch_arr_;
205 inversion_p_ = chord.inversion_p_ ? new Musical_pitch (*chord.inversion_p_) : 0;
206 bass_p_ = chord.bass_p_ ? new Musical_pitch (*chord.bass_p_) : 0;
216 Chord::rebuild_transpose (Array<Musical_pitch>* pitch_arr_p, Musical_pitch tonic)
218 for (int i = 0; i < pitch_arr_p->size (); i++)
220 Musical_pitch p = tonic;
221 Musical_pitch q = (*pitch_arr_p)[i];
222 // duh, c7 should mean <c bes>
223 if (q.notename_i_ == 6)
226 (*pitch_arr_p)[i] = p;
228 pitch_arr_p->sort (Musical_pitch::compare);
232 Chord::find_pitch_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
234 for (int i = 0; i < pitch_arr_p->size (); i++)
235 if (p == (*pitch_arr_p)[i])
241 Chord::find_notename_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
243 int i = find_pitch_i (pitch_arr_p, p);
246 for (int i = 0; i < pitch_arr_p->size (); i++)
248 p.octave_i_ = (*pitch_arr_p)[i].octave_i_;
249 if (p == (*pitch_arr_p)[i])
257 Chord::step_i (Musical_pitch tonic, Musical_pitch p)
259 int i = p.notename_i_ - tonic.notename_i_
260 + (p.octave_i_ - tonic.octave_i_) * 7;
268 Chord::missing_thirds_pitch_arr (Array<Musical_pitch> const* pitch_arr_p)
270 Array<Musical_pitch> thirds;
272 /* is the third c-e, d-f, etc. small or large? */
273 int minormajor_a[] = {0, -1, -1, 0,0,-1,-1};
274 for (int i=0; i < 7; i++)
275 thirds.push (Musical_pitch( 2, minormajor_a[i]));
277 Musical_pitch tonic = (*pitch_arr_p)[0];
278 Musical_pitch last = tonic;
279 Array<Musical_pitch> missing_arr;
281 for (int i = 0; i < pitch_arr_p->size ();)
283 Musical_pitch p = (*pitch_arr_p)[i];
284 int step = step_i (tonic, p);
285 if (last.notename_i_ == p.notename_i_)
286 last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
287 if (step > step_i (tonic, last))
289 while (step > step_i (tonic, last))
291 if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
293 Musical_pitch special_seven = last;
294 Musical_pitch lower (0, -1);
295 special_seven.transpose (lower);
296 missing_arr.push (special_seven);
300 missing_arr.push (last);
302 last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
315 Mangle into list of pitches.
316 For normal chord entry, inversion and bass pitches are retained in
320 Chord::to_pitch_arr () const
322 Array<Musical_pitch> pitch_arr = pitch_arr_;
326 for (; i < pitch_arr.size (); i++)
328 if ((pitch_arr[i].notename_i_ == inversion_p_->notename_i_)
329 && (pitch_arr[i].accidental_i_ == inversion_p_->accidental_i_))
332 if (i == pitch_arr.size ())
334 warning (_f ("invalid inversion pitch: not part of chord: %s",
335 inversion_p_->str ()));
338 rebuild_with_bass (&pitch_arr, i);
343 pitch_arr.insert (*bass_p_, 0);
344 rebuild_with_bass (&pitch_arr, 0);
350 Chord::find_additions_and_subtractions (Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p) const
352 Musical_pitch tonic = pitch_arr_[0];
354 construct an array of thirds for a normal chord
356 Array<Musical_pitch> all_arr;
357 all_arr.push (tonic);
358 all_arr.push (pitch_arr_.top ());
359 all_arr.concat (missing_thirds_pitch_arr (&all_arr));
360 all_arr.sort (Musical_pitch::compare);
364 while ((i < all_arr.size ()) || (j < pitch_arr_.size ()))
366 Musical_pitch a = all_arr [i <? all_arr.size () - 1];
367 Musical_pitch p = pitch_arr_ [j <? pitch_arr_.size () - 1];
369 this pitch is present: do nothing, check next
377 found an extra pitch: chord addition
379 else if ((p < a) || (p.notename_i_ == a.notename_i_))
382 (j < pitch_arr_.size ()) ? j++ : i++;
385 a third is missing: chord subtraction
390 (i < all_arr.size ()) ? i++ : j++;
395 add highest addition, because it names chord
396 (1, 3 and) 5 not an addition: part of normal chord
398 if (step_i (tonic, pitch_arr_.top () > 5))
399 add_arr_p->push (pitch_arr_.top ());
404 reduce guess work: dim chord
405 other naming conventions `American'?
406 don't use TeX constructs
407 user defined chords-names for specific chords:
408 tonic, additions, subtractions, inversion, bass -> "my-chord-name"
411 Chord::banter_str () const
413 Musical_pitch tonic = pitch_arr_[0];
415 //urg, should do translation in scheme.
416 char const *acc[] = {"\\textflat\\textflat ", "\\textflat ", "", "\\textsharp " , "\\textsharp\\textsharp "};
417 String tonic_str = tonic.str ();
418 tonic_str = tonic_str.left_str (1).upper_str ()
419 + acc[tonic.accidental_i_ + 2];
421 Array<Musical_pitch> add_arr;
422 Array<Musical_pitch> sub_arr;
423 find_additions_and_subtractions (&add_arr, &sub_arr);
426 Array<Musical_pitch> scale;
427 for (int i=0; i < 7; i++)
428 scale.push (Musical_pitch (i));
430 // 7 always means 7-...
431 // scale.push (Musical_pitch (6, -1)); // b
433 rebuild_transpose (&scale, tonic);
435 bool has3m_b = false;
437 bool has5m_b = false;
441 for (int i = 0; i < add_arr.size (); i++)
443 Musical_pitch p = add_arr[i];
444 int step = step_i (tonic, p);
447 int accidental = p.accidental_i_ - scale[(step - 1) % 7].accidental_i_;
448 if ((step == 3) && (accidental == -1))
454 have Cdim rather than Cm5-, even if it's a prefix
456 else if ((step == 5) && (accidental == -1) && has3m_b)
462 || (!(step % 2) || ((i + 1 == add_arr.size ()) && (step > 5))))
466 if ((step == 7) && (accidental == 1))
473 if has3m_b and has5m_b, assume dim
474 don't mention dim-addition, except for chord-namer
476 if (((step/2) && (accidental == -1))
477 && has3m_b && has5m_b)
479 if (i == add_arr.size () - 1)
480 str += to_str (step);
486 str += to_str (step);
488 str += accidental < 0 ? "-" : "+";
494 for (int i = 0; i < sub_arr.size (); i++)
496 Musical_pitch p = sub_arr[i];
497 int step = step_i (tonic, p);
499 if chord has 3-, assume minor and don't display 'no3'
500 if additions include 4, assume sus4 and don't display 'no3'
501 if has3m_b and has5m_b, assume 'dim' chord
503 if (!((step == 3) && (has3m_b || has4_b))
504 && !((step/2) && (step !=3) && (step !=7 ) && (p.accidental_i_ == 0) && has3m_b && has5m_b)
505 && !((step == 7) && (p.accidental_i_ == -1) && has3m_b && has5m_b))
507 str += sep_str + "no" + to_str (step);
513 have Co rather than Cdim7
515 if (minor_str + str == "dim7")
522 String inversion_str;
525 inversion_str = inversion_p_->str ();
526 inversion_str = "/" + inversion_str.left_str (1).upper_str ()
527 + acc[inversion_p_->accidental_i_ + 2];
533 bass_str = bass_p_->str ();
534 bass_str = "/" + bass_str.left_str (1).upper_str ()
535 + acc[bass_p_->accidental_i_ + 2];
539 return tonic_str + minor_str + "$^{" + str + "}$" + inversion_str + bass_str;
543 Chord::find_tonic_i (Array<Musical_pitch> const* pitch_arr_p)
548 first try: base of longest line of thirds
552 for (int i = 0; i < pitch_arr_p->size (); i++)
555 int last_i = (*pitch_arr_p)[i % pitch_arr_p->size ()].notename_i_;
557 for (; j < pitch_arr_p->size (); j++)
559 int cur_i = (*pitch_arr_p)[(i + j + 1) % pitch_arr_p->size ()].notename_i_;
560 int gap = cur_i - last_i;
569 if (j - no_third_i > longest_i)
571 longest_i = j - no_third_i;
577 second try: note after biggest gap
582 for (int i = 0; i < pitch_arr_p->size (); i++)
584 int gap = (*pitch_arr_p)[i].notename_i_
585 - (*pitch_arr_p)[(i - 1 + pitch_arr_p->size ())
586 % pitch_arr_p->size ()].notename_i_;
600 Chord::rebuild_from_base (Array<Musical_pitch>* pitch_arr_p, int base_i)
602 assert (base_i >= 0);
603 Musical_pitch last (0, 0, -5);
604 Array<Musical_pitch> new_arr;
605 for (int i = 0; i < pitch_arr_p->size (); i++)
607 Musical_pitch p = (*pitch_arr_p)[(base_i + i) % pitch_arr_p->size ()];
610 p.octave_i_ = last.octave_i_;
617 *pitch_arr_p = new_arr;
621 Chord::rebuild_insert_inversion (Array<Musical_pitch>* pitch_arr_p, int tonic_i)
623 assert (tonic_i > 0);
624 Musical_pitch inversion = pitch_arr_p->get (0);
625 rebuild_from_base (pitch_arr_p, tonic_i - 1);
626 if (pitch_arr_p->size ())
628 inversion.octave_i_ = (*pitch_arr_p)[0].octave_i_ - 1;
629 while (inversion < (*pitch_arr_p)[0])
630 inversion.octave_i_++;
632 for (int i = 0; i < pitch_arr_p->size (); i++)
633 if ((*pitch_arr_p)[i] > inversion)
635 pitch_arr_p->insert (inversion, i);
641 Chord::rebuild_with_bass (Array<Musical_pitch>* pitch_arr_p, int bass_i)
643 assert (bass_i >= 0);
644 Musical_pitch bass = pitch_arr_p->get (bass_i);
645 // is lowering fine, or should others be raised?
646 if (pitch_arr_p->size ())
647 while (bass > (*pitch_arr_p)[0])
649 pitch_arr_p->insert (bass, 0);