lilypond-1.3.16
[lilypond.git] / lily / chord.cc
blob89d4c4590ede0f1299db3c0a6be06477a4806cbd
1 /*
2 chord.cc -- implement Chord
4 source file of the GNU LilyPond music typesetter
6 (c) 1999 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "chord.hh"
10 #include "musical-request.hh"
11 #include "warn.hh"
12 #include "debug.hh"
13 #include "molecule.hh"
14 #include "paper-def.hh"
15 #include "lookup.hh"
19 construct from parser output
21 Chord
22 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)
24 // urg: catch dim modifier: 3rd, 5th, 7th, .. should be lowered
25 bool dim_b = false;
26 for (int i=0; i < add_arr_p->size (); i++)
28 Musical_pitch* p = &(*add_arr_p)[i];
29 if (p->octave_i_ == -100)
31 p->octave_i_ = 0;
32 dim_b = true;
35 Chord::rebuild_transpose (add_arr_p, tonic, true);
36 Chord::rebuild_transpose (sub_arr_p, tonic, true);
38 Musical_pitch fifth = Chord::base_arr (tonic).top ();
41 remove double adds (urg: sus4)
43 for (int i = add_arr_p->size () - 1; i >= 0 ; i--)
45 int j = Chord::find_pitch_i (add_arr_p, (*add_arr_p)[i]);
46 if ((j != -1) && (i != j))
48 add_arr_p->get (i);
53 default chord includes upto 5: <1, 3, 5>
55 add_arr_p->insert (tonic, 0);
56 Array<Musical_pitch> tmp = *add_arr_p;
57 int highest_step = Chord::step_i (tonic, tmp.top ());
58 if (highest_step < 5)
59 tmp.push (fifth);
60 else if (dim_b)
62 Musical_pitch* p = &add_arr_p->top ();
63 p->accidental_i_--;
67 find missing thirds
69 Array<Musical_pitch> missing_arr = Chord::missing_thirds_pitch_arr (&tmp);
70 if (highest_step < 5)
71 missing_arr.push (fifth);
74 if dim modifier is given: lower all missing
76 if (dim_b)
78 for (int i=0; i < missing_arr.size (); i++)
80 missing_arr[i].accidental_i_--;
85 if additions include some 3, don't add third
87 Musical_pitch third = Chord::base_arr (tonic)[1];
88 if (Chord::find_notename_i (add_arr_p, third) != -1)
90 int i = Chord::find_pitch_i (&missing_arr, third);
91 if (i != -1)
92 missing_arr.get (i);
96 if additions include 4, assume sus4 and don't add third implicitely
97 C-sus (4) = c f g (1 4 5)
99 Musical_pitch sus = tonic;
100 sus.transpose (Musical_pitch (3));
101 if (Chord::find_pitch_i (add_arr_p, sus) != -1)
103 int i = Chord::find_pitch_i (&missing_arr, third);
104 if (i != -1)
105 missing_arr.get (i);
109 if additions include some 5, don't add fifth
111 if (Chord::find_notename_i (add_arr_p, fifth) != -1)
113 int i = Chord::find_pitch_i (&missing_arr, fifth);
114 if (i != -1)
115 missing_arr.get (i);
120 complete the list of thirds to be added
122 add_arr_p->concat (missing_arr);
123 add_arr_p->sort (Musical_pitch::compare);
125 Array<Musical_pitch> pitch_arr;
127 add all that aren't subtracted
129 for (int i = 0; i < add_arr_p->size (); i++)
131 Musical_pitch p = (*add_arr_p)[i];
132 int j = 0;
133 for (; j < sub_arr_p->size (); j++)
134 if (p == (*sub_arr_p)[j])
136 sub_arr_p->del (j);
137 j = -1;
138 break;
140 if (j == sub_arr_p->size ())
141 pitch_arr.push (p);
144 pitch_arr.sort (Musical_pitch::compare);
146 for (int i = 0; i < sub_arr_p->size (); i++)
147 warning (_f ("invalid subtraction: not part of chord: %s",
148 (*sub_arr_p)[i].str ()));
150 return Chord (pitch_arr, inversion_p, bass_p);
154 Construct from list of pitches and requests
156 Chord
157 to_chord (Array<Musical_pitch> pitch_arr, Tonic_req* tonic_req, Inversion_req* inversion_req, Bass_req* bass_req, bool find_inversion_b)
159 Musical_pitch* inversion_p = 0;
160 Musical_pitch* bass_p = 0;
162 if (bass_req)
164 assert (pitch_arr[0].notename_i_ == bass_req->pitch_.notename_i_);
165 bass_p = new Musical_pitch (pitch_arr.get (0));
168 if (inversion_req)
170 assert (pitch_arr[0].notename_i_ == inversion_req->pitch_.notename_i_);
171 inversion_p = new Musical_pitch (inversion_req->pitch_);
172 assert (tonic_req);
173 int tonic_i = Chord::find_notename_i (&pitch_arr, tonic_req->pitch_);
174 if (tonic_i)
175 Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
178 if (find_inversion_b && !inversion_p)
180 int tonic_i = tonic_req
181 ? Chord::find_notename_i (&pitch_arr, tonic_req->pitch_)
182 : Chord::find_tonic_i (&pitch_arr);
184 if (tonic_i)
186 inversion_p = &pitch_arr[0];
187 Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
191 if (tonic_req)
193 assert (pitch_arr[0].notename_i_ == tonic_req->pitch_.notename_i_);
196 return Chord (pitch_arr, inversion_p, bass_p);
199 Chord::Chord ()
201 inversion_b_ = false;
202 bass_b_ = false;
205 Chord::Chord (Array<Musical_pitch> pitch_arr, Musical_pitch* inversion_p, Musical_pitch* bass_p)
207 pitch_arr_ = pitch_arr;
208 inversion_b_ = false;
209 bass_b_ = false;
210 if (inversion_p)
212 inversion_pitch_ = *inversion_p;
213 inversion_b_ = true;
214 delete inversion_p;
216 if (bass_p)
218 bass_pitch_ = *bass_p;
219 bass_b_ = true;
220 delete bass_p;
224 Chord::Chord (Chord const& chord)
226 pitch_arr_ = chord.pitch_arr_;
227 inversion_b_ = chord.inversion_b_;
228 inversion_pitch_ = chord.inversion_pitch_;
229 bass_b_ = chord.bass_b_;
230 bass_pitch_ = chord.bass_pitch_;
234 Array<Musical_pitch>
235 Chord::base_arr (Musical_pitch p)
237 Array<Musical_pitch> base;
238 base.push (p);
239 p.transpose (Musical_pitch (2));
240 base.push (p);
241 p.transpose (Musical_pitch (2, -1));
242 base.push (p);
243 return base;
246 void
247 Chord::rebuild_transpose (Array<Musical_pitch>* pitch_arr_p, Musical_pitch tonic, bool fix7_b)
249 for (int i = 0; i < pitch_arr_p->size (); i++)
251 Musical_pitch p = tonic;
252 Musical_pitch q = (*pitch_arr_p)[i];
253 p.transpose (q);
254 // duh, c7 should mean <c bes>
255 if (fix7_b && (step_i (tonic, p) == 7))
256 p.accidental_i_--;
257 (*pitch_arr_p)[i] = p;
259 pitch_arr_p->sort (Musical_pitch::compare);
263 Chord::find_pitch_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
265 for (int i = 0; i < pitch_arr_p->size (); i++)
266 if (p == (*pitch_arr_p)[i])
267 return i;
268 return -1;
272 Chord::find_notename_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
274 int i = find_pitch_i (pitch_arr_p, p);
275 if (i == -1)
277 for (int i = 0; i < pitch_arr_p->size (); i++)
279 p.octave_i_ = (*pitch_arr_p)[i].octave_i_;
280 if (p == (*pitch_arr_p)[i])
281 return i;
284 return i;
288 Chord::step_i (Musical_pitch tonic, Musical_pitch p)
290 int i = p.notename_i_ - tonic.notename_i_
291 + (p.octave_i_ - tonic.octave_i_) * 7;
292 while (i < 0)
293 i += 7;
294 i++;
295 return i;
298 Array<Musical_pitch>
299 Chord::missing_thirds_pitch_arr (Array<Musical_pitch> const* pitch_arr_p)
301 Array<Musical_pitch> thirds;
303 /* is the third c-e, d-f, etc. small or large? */
304 int minormajor_a[] = {0, -1, -1, 0,0,-1,-1};
305 for (int i=0; i < 7; i++)
306 thirds.push (Musical_pitch( 2, minormajor_a[i]));
308 Musical_pitch tonic = (*pitch_arr_p)[0];
309 Musical_pitch last = tonic;
310 Array<Musical_pitch> missing_arr;
312 for (int i = 0; i < pitch_arr_p->size ();)
314 Musical_pitch p = (*pitch_arr_p)[i];
315 int step = step_i (tonic, p);
316 if (last.notename_i_ == p.notename_i_)
317 last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
318 if (step > step_i (tonic, last))
320 while (step > step_i (tonic, last))
322 if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
324 Musical_pitch special_seven = last;
325 Musical_pitch lower (0, -1);
326 special_seven.transpose (lower);
327 missing_arr.push (special_seven);
329 else
331 missing_arr.push (last);
333 last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
336 else
338 i++;
341 return missing_arr;
346 Mangle into list of pitches.
347 For normal chord entry, inversion and bass pitches are retained in
348 specific *_requests
350 Array<Musical_pitch>
351 Chord::to_pitch_arr () const
353 Array<Musical_pitch> pitch_arr = pitch_arr_;
354 if (inversion_b_)
356 int i = 0;
357 for (; i < pitch_arr.size (); i++)
359 if ((pitch_arr[i].notename_i_ == inversion_pitch_.notename_i_)
360 && (pitch_arr[i].accidental_i_ == inversion_pitch_.accidental_i_))
361 break;
363 if (i == pitch_arr.size ())
365 warning (_f ("invalid inversion pitch: not part of chord: %s",
366 inversion_pitch_.str ()));
368 else
369 rebuild_with_bass (&pitch_arr, i);
372 if (bass_b_)
374 pitch_arr.insert (bass_pitch_, 0);
375 rebuild_with_bass (&pitch_arr, 0);
377 return pitch_arr;
380 void
381 Chord::find_additions_and_subtractions (Array<Musical_pitch> pitch_arr, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p)
383 Musical_pitch tonic = pitch_arr[0];
385 construct an array of thirds for a normal chord
387 Array<Musical_pitch> all_arr;
388 all_arr.push (tonic);
389 if (step_i (tonic, pitch_arr.top ()) >= 5)
390 all_arr.push (pitch_arr.top ());
391 else
392 all_arr.push (base_arr (tonic).top ());
393 all_arr.concat (missing_thirds_pitch_arr (&all_arr));
394 all_arr.sort (Musical_pitch::compare);
396 int i = 0;
397 int j = 0;
398 Musical_pitch last_extra = tonic;
399 while ((i < all_arr.size ()) || (j < pitch_arr.size ()))
401 Musical_pitch a = all_arr [i <? all_arr.size () - 1];
402 Musical_pitch p = pitch_arr[j <? pitch_arr.size () - 1];
404 this pitch is present: do nothing, check next
406 if (a == p)
408 i++;
409 j++;
410 last_extra = tonic;
413 found an extra pitch: chord addition
415 else if ((p < a) || (p.notename_i_ == a.notename_i_))
417 add_arr_p->push (p);
418 last_extra = p;
419 (j < pitch_arr.size ()) ? j++ : i++;
422 a third is missing: chord subtraction
424 else
426 if (last_extra.notename_i_ != a.notename_i_)
427 sub_arr_p->push (a);
428 (i < all_arr.size ()) ? i++ : j++;
429 last_extra = tonic;
433 /* add missing basic steps */
434 if (step_i (tonic, pitch_arr.top ()) < 3)
435 sub_arr_p->push (base_arr (tonic)[1]);
436 if (step_i (tonic, pitch_arr.top ()) < 5)
437 sub_arr_p->push (base_arr (tonic).top ());
440 add highest addition, because it names chord, if greater than 5
441 or non-standard
442 (1, 3 and) 5 not additions: part of normal chord
444 if ((step_i (tonic, pitch_arr.top ()) > 5)
445 || pitch_arr.top ().accidental_i_)
446 add_arr_p->push (pitch_arr.top ());
451 This routine tries to guess tonic in a possibly inversed chord, ie
452 <e g c'> should produce: C.
453 This is only used for chords that are entered as simultaneous notes,
454 chords entered in \chord mode are fully defined.
457 Chord::find_tonic_i (Array<Musical_pitch> const* pitch_arr_p)
460 find tonic
462 first try: base of longest line of thirds
464 int tonic_i = 0;
465 int longest_i = 0;
466 for (int i = 0; i < pitch_arr_p->size (); i++)
468 int no_third_i = 0;
469 int last_i = (*pitch_arr_p)[i % pitch_arr_p->size ()].notename_i_;
470 int j = 0;
471 for (; j < pitch_arr_p->size (); j++)
473 int cur_i = (*pitch_arr_p)[(i + j + 1) % pitch_arr_p->size ()].notename_i_;
474 int gap = cur_i - last_i;
475 while (gap < 0)
476 gap += 7;
477 gap %= 7;
478 if (gap == 2)
479 last_i = cur_i;
480 else
481 no_third_i++;
483 if (j - no_third_i > longest_i)
485 longest_i = j - no_third_i;
486 tonic_i = i;
491 second try: note after biggest gap
493 int biggest_i = 0;
494 // if (longest_i)
495 if (longest_i <= 1)
496 for (int i = 0; i < pitch_arr_p->size (); i++)
498 int gap = (*pitch_arr_p)[i].notename_i_
499 - (*pitch_arr_p)[(i - 1 + pitch_arr_p->size ())
500 % pitch_arr_p->size ()].notename_i_;
501 while (gap < 0)
502 gap += 7;
503 gap %= 7;
504 if (gap > biggest_i)
506 biggest_i = gap;
507 tonic_i = i;
510 return tonic_i;
513 void
514 Chord::rebuild_from_base (Array<Musical_pitch>* pitch_arr_p, int base_i)
516 assert (base_i >= 0);
517 Musical_pitch last (0, 0, -5);
518 Array<Musical_pitch> new_arr;
519 for (int i = 0; i < pitch_arr_p->size (); i++)
521 Musical_pitch p = (*pitch_arr_p)[(base_i + i) % pitch_arr_p->size ()];
522 if (p < last)
524 p.octave_i_ = last.octave_i_;
525 if (p < last)
526 p.octave_i_++;
528 new_arr.push (p);
529 last = p;
531 *pitch_arr_p = new_arr;
534 void
535 Chord::rebuild_insert_inversion (Array<Musical_pitch>* pitch_arr_p, int tonic_i)
537 assert (tonic_i > 0);
538 Musical_pitch inversion = pitch_arr_p->get (0);
539 rebuild_from_base (pitch_arr_p, tonic_i - 1);
540 if (pitch_arr_p->size ())
542 inversion.octave_i_ = (*pitch_arr_p)[0].octave_i_ - 1;
543 while (inversion < (*pitch_arr_p)[0])
544 inversion.octave_i_++;
546 for (int i = 0; i < pitch_arr_p->size (); i++)
547 if ((*pitch_arr_p)[i] > inversion)
549 pitch_arr_p->insert (inversion, i);
550 break;
554 void
555 Chord::rebuild_with_bass (Array<Musical_pitch>* pitch_arr_p, int bass_i)
557 assert (bass_i >= 0);
558 Musical_pitch bass = pitch_arr_p->get (bass_i);
559 // is lowering fine, or should others be raised?
560 if (pitch_arr_p->size ())
561 while (bass > (*pitch_arr_p)[0])
562 bass.octave_i_--;
563 pitch_arr_p->insert (bass, 0);