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 Array<Musical_pitch> triads;
77 /* is the third c-e, d-f, etc. small or large? */
78 int minormajor_a[] = {0, -1, -1, 0,0,-1,-1};
79 for (int i=0; i < 7; i++)
80 triads.push (Musical_pitch( 2, minormajor_a[i]));
82 Musical_pitch tonic = (*pitch_arr_p)[0];
83 Musical_pitch last = tonic;
84 Array<Musical_pitch> missing_arr;
86 for (int i = 0; i < pitch_arr_p->size ();)
88 Musical_pitch p = (*pitch_arr_p)[i];
89 int trap = trap_i (tonic, p);
90 if (last.notename_i_ == p.notename_i_)
91 last.transpose (triads[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
92 if (trap > trap_i (tonic, last))
94 while (trap > trap_i (tonic, last))
96 if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
98 Musical_pitch special_seven = last;
99 Musical_pitch lower (0, -1);
100 special_seven.transpose (lower);
101 missing_arr.push (special_seven);
105 missing_arr.push (last);
107 last.transpose (triads[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
120 construct from parser output
122 Chord::Chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Musical_pitch* inversion_p)
124 rebuild_transpose (tonic, add_arr_p);
125 rebuild_transpose (tonic, sub_arr_p);
127 Musical_pitch fifth = tonic;
128 fifth.transpose (Musical_pitch (2));
129 fifth.transpose (Musical_pitch (2, -1));
132 default chord includes upto 5: <1, 3, 5>
134 add_arr_p->insert (tonic, 0);
135 int highest_trap = trap_i (tonic, add_arr_p->top ());
136 if (highest_trap < 5)
137 add_arr_p->push (fifth);
142 Array<Musical_pitch> missing_arr = missing_triads_pitch_arr (add_arr_p);
145 if additions include 4, assume sus4 and don't add third implicitely
147 Musical_pitch third = tonic;
148 third.transpose (Musical_pitch (2));
149 Musical_pitch sus = tonic;
150 sus.transpose (Musical_pitch (3));
151 if (::find_pitch_i (add_arr_p, sus) != -1)
153 int i = ::find_pitch_i (&missing_arr, third);
159 complete the list of triads to be added
161 add_arr_p->concat (missing_arr);
162 add_arr_p->sort (Musical_pitch::compare);
165 add all that aren't subtracted
167 for (int i = 0; i < add_arr_p->size (); i++)
169 Musical_pitch p = (*add_arr_p)[i];
171 for (; j < sub_arr_p->size (); j++)
172 if (p == (*sub_arr_p)[j])
178 if (j == sub_arr_p->size ())
182 pitch_arr_.sort (Musical_pitch::compare);
184 for (int i = 0; i < sub_arr_p->size (); i++)
185 warning (_f ("invalid subtraction: not part of chord: %s",
186 (*sub_arr_p)[i].str ()));
191 for (; i < pitch_arr_.size (); i++)
193 if ((pitch_arr_[i].notename_i_ == inversion_p->notename_i_)
194 && (pitch_arr_[i].accidental_i_ == inversion_p->accidental_i_))
197 if (i == pitch_arr_.size ())
199 warning (_f ("invalid inversion pitch: not part of chord: %s",
200 inversion_p->str ()));
204 #if INVERSION_ADDED_AS_BASE
205 pitch_arr_.insert (pitch_arr_[i], 0);
206 rebuild_with_bass (0);
208 rebuild_with_bass (i);
217 Chord::find_additions_and_subtractions(Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p)
219 Musical_pitch tonic = pitch_arr_[0];
221 all the triads that should be there
223 Array<Musical_pitch> all_arr;
224 all_arr.push (tonic);
225 all_arr.push (pitch_arr_.top ());
226 all_arr.concat (missing_triads_pitch_arr (&all_arr));
227 all_arr.sort (Musical_pitch::compare);
231 while ((i < all_arr.size ()) || (j < pitch_arr_.size ()))
233 i = i <? all_arr.size () - 1;
234 j = j <? pitch_arr_.size () - 1;
235 Musical_pitch a = all_arr[i];
236 Musical_pitch p = pitch_arr_[j];
242 else if ((p < a) || (p.notename_i_ == a.notename_i_))
255 add highest addition, because it names chord
257 if (trap_i (tonic, pitch_arr_.top () > 5))
258 add_arr_p->push (pitch_arr_.top ());
262 Chord::banter_str (Musical_pitch* inversion) const
264 Musical_pitch tonic = pitch_arr_[0];
266 //urg, should do translation in scheme.
267 char const *acc[] = {"\\textflat\\textflat ", "\\textflat ", "", "\\textsharp " , "\\textsharp\\textsharp "};
268 String tonic_str = tonic.str ();
269 tonic_str = tonic_str.left_str (1).upper_str ()
270 + acc[tonic.accidental_i_ + 2];
272 Array<Musical_pitch> add_arr;
273 Array<Musical_pitch> sub_arr;
274 find_additions_and_subtractions (&add_arr, &sub_arr);
277 Array<Musical_pitch> scale;
278 for (int i=0; i < 7; i++)
279 scale.push (Musical_pitch (i));
281 // 7 always means 7-...
282 // scale.push (Musical_pitch (6, -1)); // b
284 rebuild_transpose (tonic, &scale);
286 bool has3m_b = false;
290 for (int i = 0; i < add_arr.size (); i++)
292 Musical_pitch p = add_arr[i];
293 int trap = trap_i (tonic, p);
296 int accidental = p.accidental_i_ - scale[(trap - 1) % 7].accidental_i_;
297 if ((trap == 3) && (accidental == -1))
303 || (!(trap % 2) || ((i + 1 == add_arr.size ()) && (trap > 5))))
306 if ((trap == 7) && (accidental == 1))
310 str += to_str (trap);
312 str += accidental < 0 ? "-" : "+";
318 for (int i = 0; i < sub_arr.size (); i++)
320 Musical_pitch p = sub_arr[i];
321 int trap = trap_i (tonic, p);
323 if chord has 3-, assume minor and don't display 'no3'
324 if additions include 4, assume sus4 and don't display 'no3'
326 if (!((trap == 3) && (has3m_b || has4_b)))
328 str += sep_str + "no" + to_str (trap);
333 String inversion_str;
336 inversion_str = inversion->str ();
337 inversion_str = "/" + inversion_str.left_str (1).upper_str ()
338 + acc[inversion->accidental_i_ + 2];
342 return tonic_str + "$^{" + str + "}$" + inversion_str;
346 Chord::find_notename_i (Musical_pitch p) const
348 return ::find_notename_i (&pitch_arr_, p);
352 Chord::find_pitch_i (Musical_pitch p) const
354 return ::find_pitch_i (&pitch_arr_, p);
358 Chord::find_tonic_i () const
363 first try: base of longest line of triads
367 for (int i = 0; i < pitch_arr_.size (); i++)
370 int last_i = pitch_arr_[i % pitch_arr_.size ()].notename_i_;
372 for (; j < pitch_arr_.size (); j++)
374 int cur_i = pitch_arr_[(i + j + 1) % pitch_arr_.size ()].notename_i_;
375 int gap = cur_i - last_i;
384 if (j - no_triad_i > longest_i)
386 longest_i = j - no_triad_i;
392 second try: note after biggest gap
397 for (int i = 0; i < pitch_arr_.size (); i++)
399 int gap = pitch_arr_[i].notename_i_
400 - pitch_arr_[(i - 1 + pitch_arr_.size ())
401 % pitch_arr_.size ()].notename_i_;
415 Chord::rebuild_from_base (int base_i)
417 assert (base_i >= 0);
418 Musical_pitch last (0, 0, -5);
419 Array<Musical_pitch> new_arr;
420 for (int i = 0; i < pitch_arr_.size (); i++)
422 Musical_pitch p = pitch_arr_[(base_i + i) % pitch_arr_.size ()];
425 p.octave_i_ = last.octave_i_;
432 pitch_arr_ = new_arr;
436 Chord::rebuild_insert_inversion (int tonic_i)
438 assert (tonic_i > 0);
439 #if INVERSION_ADDED_AS_BASE
440 // inversion was added; don't insert
441 Musical_pitch inversion = pitch_arr_.get (0);
444 Musical_pitch inversion = pitch_arr_.get (0);
445 rebuild_from_base (tonic_i - 1);
446 if (pitch_arr_.size ())
448 inversion.octave_i_ = pitch_arr_[0].octave_i_ - 1;
449 while (inversion < pitch_arr_[0])
450 inversion.octave_i_++;
452 for (int i = 0; i < pitch_arr_.size (); i++)
453 if (pitch_arr_[i] > inversion)
455 pitch_arr_.insert (inversion, i);
462 Chord::rebuild_with_bass (int bass_i)
464 assert (bass_i >= 0);
465 Musical_pitch inversion = pitch_arr_.get (bass_i);
466 // is lowering fine, or should others be raised?
467 if (pitch_arr_.size ())
468 while (inversion > pitch_arr_[0])
469 inversion.octave_i_--;
470 pitch_arr_.insert (inversion, 0);