]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord.cc
patch::: 1.1.63.lu1
[lilypond.git] / lily / chord.cc
1 /*
2   chord.cc -- implement Chord
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1999 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "chord.hh"
10 #include "warn.hh"
11
12 // doesn't seem common, and we should know about this during parsing
13 // #define INVERSION_ADDED_AS_BASE 1
14
15 Chord::Chord (Array<Musical_pitch> pitch_arr)
16 {
17   pitch_arr_ = pitch_arr;
18 }
19
20 static void
21 rebuild_transpose (Musical_pitch tonic, Array<Musical_pitch>* pitch_arr_p)
22 {
23   for (int i = 0; i < pitch_arr_p->size (); i++)
24     {
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)
29         q.accidental_i_--;
30       p.transpose (q);
31       (*pitch_arr_p)[i] = p;
32     }
33   pitch_arr_p->sort (Musical_pitch::compare);
34 }
35
36 static int
37 find_pitch_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
38 {
39   for (int i = 0; i < pitch_arr_p->size (); i++)
40     if (p == (*pitch_arr_p)[i])
41       return i;
42   return -1;
43 }
44
45 static int
46 find_notename_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
47 {
48   int i = find_pitch_i (pitch_arr_p, p);
49   if (i == -1)
50     {
51       for (int i = 0; i < pitch_arr_p->size (); i++)
52         {
53           p.octave_i_ = (*pitch_arr_p)[i].octave_i_;
54           if (p == (*pitch_arr_p)[i])
55             return i;
56         }
57     }
58   return i;
59 }
60
61 static int
62 trap_i (Musical_pitch tonic, Musical_pitch p)
63 {
64   int i = p.notename_i_ - tonic.notename_i_
65     + (p.octave_i_ - tonic.octave_i_) * 7;
66   while (i < 0)
67     i += 7;
68   i++;
69   return i;
70 }
71
72 static Array<Musical_pitch>
73 missing_triads_pitch_arr (Array<Musical_pitch>const* pitch_arr_p)
74 {
75   Array<Musical_pitch> triads;
76
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]));
81
82   Musical_pitch tonic = (*pitch_arr_p)[0];
83   Musical_pitch last = tonic;
84   Array<Musical_pitch> missing_arr;
85
86   for (int i = 0; i < pitch_arr_p->size ();)
87     {
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))
93         {
94           while (trap > trap_i (tonic, last))
95             {
96               if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
97                 {
98                   Musical_pitch special_seven = last;
99                   Musical_pitch lower (0, -1);
100                   special_seven.transpose (lower);
101                   missing_arr.push (special_seven);
102                 }
103               else
104                 {
105                   missing_arr.push (last);
106                 }
107               last.transpose (triads[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
108             }
109         }
110       else
111         {
112           i++;
113         }
114     }
115   return missing_arr;
116 }
117
118
119 /*
120   construct from parser output
121 */
122 Chord::Chord (Musical_pitch tonic, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p, Musical_pitch* inversion_p)
123 {
124   rebuild_transpose (tonic, add_arr_p);
125   rebuild_transpose (tonic, sub_arr_p);
126
127   Musical_pitch fifth = tonic;
128   fifth.transpose (Musical_pitch (2));
129   fifth.transpose (Musical_pitch (2, -1));
130
131   /*
132     remove double adds (urg: sus4)
133    */
134   for (int i = add_arr_p->size () - 1; i >= 0 ; i--)
135     {
136       int j = ::find_pitch_i (add_arr_p, (*add_arr_p)[i]);
137       if ((j != -1) && (i != j))
138         {
139             add_arr_p->get (i);
140         } 
141     }
142
143   /*
144     default chord includes upto 5: <1, 3, 5>
145    */
146   add_arr_p->insert (tonic, 0);
147   Array<Musical_pitch> tmp = *add_arr_p;
148   int highest_trap = trap_i (tonic, tmp.top ());
149   if (highest_trap < 5)
150     tmp.push (fifth);
151
152   /*
153     find missing triads
154    */
155   Array<Musical_pitch> missing_arr = missing_triads_pitch_arr (&tmp);
156   if (highest_trap < 5)
157     missing_arr.push (fifth);
158
159   /*
160     if additions include some 3, don't add third
161    */
162   Musical_pitch third = tonic;
163   third.transpose (Musical_pitch (2));
164   if (::find_notename_i (add_arr_p, third) != -1)
165     {
166       int i = ::find_pitch_i (&missing_arr, third);
167       if (i != -1)
168         missing_arr.get (i);
169     }
170   
171   /*
172     if additions include 4, assume sus4 and don't add third implicitely
173      C-sus (4) = c f g (1 4 5)
174    */
175   Musical_pitch sus = tonic;
176   sus.transpose (Musical_pitch (3));
177   if (::find_pitch_i (add_arr_p, sus) != -1)
178     {
179       int i = ::find_pitch_i (&missing_arr, third);
180       if (i != -1)
181         missing_arr.get (i);
182     }
183
184   /*
185     if additions include some 5, don't add fifth
186    */
187   if (::find_notename_i (add_arr_p, fifth) != -1)
188     {
189       int i = ::find_pitch_i (&missing_arr, fifth);
190       if (i != -1)
191         missing_arr.get (i);
192     }
193   
194   
195   /*
196     complete the list of triads to be added
197    */
198   add_arr_p->concat (missing_arr);
199   add_arr_p->sort (Musical_pitch::compare);
200
201   /*
202    add all that aren't subtracted
203    */
204   for (int i = 0; i < add_arr_p->size (); i++)
205     {
206       Musical_pitch p = (*add_arr_p)[i];
207       int j = 0;
208       for (; j < sub_arr_p->size (); j++)
209         if (p == (*sub_arr_p)[j])
210           {
211             sub_arr_p->del (j);
212             j = -1;
213             break;
214           }
215       if (j == sub_arr_p->size ())
216         pitch_arr_.push (p);
217     }
218
219   pitch_arr_.sort (Musical_pitch::compare);
220
221   for (int i = 0; i < sub_arr_p->size (); i++)
222     warning (_f ("invalid subtraction: not part of chord: %s",
223                  (*sub_arr_p)[i].str ()));
224
225   if (inversion_p)
226     {
227       int i = 0;
228       for (; i < pitch_arr_.size (); i++)
229         {
230           if ((pitch_arr_[i].notename_i_ == inversion_p->notename_i_)
231               && (pitch_arr_[i].accidental_i_ == inversion_p->accidental_i_))
232             break;
233         }
234       if (i == pitch_arr_.size ())
235         {
236           warning (_f ("invalid inversion pitch: not part of chord: %s",
237                        inversion_p->str ()));
238         }
239       else
240         {
241 #if INVERSION_ADDED_AS_BASE
242           pitch_arr_.insert (pitch_arr_[i], 0);
243           rebuild_with_bass (0);
244 #else
245           rebuild_with_bass (i);
246 #endif
247           
248         }
249       delete inversion_p;
250     }
251 }
252
253 void
254 Chord::find_additions_and_subtractions(Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p)
255 {
256   Musical_pitch tonic = pitch_arr_[0];
257   /*
258     all the triads that should be there
259    */
260   Array<Musical_pitch> all_arr;
261   all_arr.push (tonic);
262   all_arr.push (pitch_arr_.top ());
263   all_arr.concat (missing_triads_pitch_arr (&all_arr));
264   all_arr.sort (Musical_pitch::compare);
265   
266   int i = 0;
267   int j = 0;
268   while ((i < all_arr.size ()) || (j < pitch_arr_.size ()))
269     {
270       i = i <? all_arr.size () - 1;
271       j = j <? pitch_arr_.size () - 1;
272       Musical_pitch a = all_arr[i];
273       Musical_pitch p = pitch_arr_[j];
274       if (a == p)
275         {
276           i++;
277           j++;
278         }
279       else if ((p < a) || (p.notename_i_ == a.notename_i_))
280         {
281           add_arr_p->push (p);
282           j++;
283         }
284       else
285         {
286           sub_arr_p->push (a);
287           i++;
288         }
289     }
290       
291   /*
292     add highest addition, because it names chord
293    */
294   if (trap_i (tonic, pitch_arr_.top () > 5))
295     add_arr_p->push (pitch_arr_.top ());
296 }
297
298 String
299 Chord::banter_str (Musical_pitch* inversion) const
300 {
301   Musical_pitch tonic = pitch_arr_[0];
302
303   //urg, should do translation in scheme.
304   char const *acc[] = {"\\textflat\\textflat ", "\\textflat ", "", "\\textsharp " , "\\textsharp\\textsharp "};
305   String tonic_str = tonic.str ();
306   tonic_str = tonic_str.left_str (1).upper_str ()
307     + acc[tonic.accidental_i_ + 2];
308
309   Array<Musical_pitch> add_arr;
310   Array<Musical_pitch> sub_arr;
311   find_additions_and_subtractions (&add_arr, &sub_arr);
312                            
313
314   Array<Musical_pitch> scale;
315   for (int i=0; i < 7; i++)
316     scale.push (Musical_pitch (i));
317
318   // 7 always means 7-...
319   //  scale.push (Musical_pitch (6, -1)); // b
320
321   rebuild_transpose (tonic, &scale);
322   
323   bool has3m_b = false;
324   bool has4_b = false;
325   String str;
326   String sep_str;
327   for (int i = 0; i < add_arr.size (); i++)
328     {
329       Musical_pitch p = add_arr[i];
330       int trap = trap_i (tonic, p);
331       if (trap == 4)
332         has4_b = true;
333       int accidental = p.accidental_i_ - scale[(trap - 1) % 7].accidental_i_;
334       if ((trap == 3) && (accidental == -1))
335         {
336           tonic_str += "m";
337           has3m_b = true;
338         }
339       else if (accidental
340                || (!(trap % 2) || ((i + 1 == add_arr.size ()) && (trap > 5))))
341         {
342           str += sep_str;
343           if ((trap == 7) && (accidental == 1))
344             str += "maj7";
345           else
346             {
347               str += to_str (trap);
348               if (accidental)
349                 str += accidental < 0 ? "-" : "+";
350             }
351           sep_str = "/";
352         }
353     }
354
355   for (int i = 0; i < sub_arr.size (); i++)
356     {
357       Musical_pitch p = sub_arr[i];
358       int trap = trap_i (tonic, p);
359       /*
360         if chord has 3-, assume minor and don't display 'no3'
361         if additions include 4, assume sus4 and don't display 'no3'
362       */
363       if (!((trap == 3) && (has3m_b || has4_b)))
364         {
365           str += sep_str + "no" + to_str (trap);
366           sep_str = "/";
367         }
368     }
369
370   String inversion_str;
371   if (inversion)
372     {
373       inversion_str = inversion->str ();
374       inversion_str = "/" + inversion_str.left_str (1).upper_str ()
375         + acc[inversion->accidental_i_ + 2];
376
377     }
378
379   return tonic_str + "$^{" + str + "}$" + inversion_str;
380 }
381
382 int
383 Chord::find_notename_i (Musical_pitch p) const
384 {
385   return ::find_notename_i (&pitch_arr_, p);
386 }
387
388 int
389 Chord::find_pitch_i (Musical_pitch p) const
390 {
391   return ::find_pitch_i (&pitch_arr_, p);
392 }
393
394 int
395 Chord::find_tonic_i () const
396 {
397   /*
398     find tonic
399     
400     first try: base of longest line of triads
401    */
402   int tonic_i = 0;
403   int longest_i = 0;
404   for (int i = 0; i < pitch_arr_.size (); i++)
405     {
406       int no_triad_i = 0;
407       int last_i = pitch_arr_[i % pitch_arr_.size ()].notename_i_;
408       int j = 0;
409       for (; j < pitch_arr_.size (); j++)
410         {
411           int cur_i = pitch_arr_[(i + j + 1) % pitch_arr_.size ()].notename_i_;
412           int gap = cur_i - last_i;
413           while (gap < 0)
414             gap += 7;
415           gap %= 7;
416           if (gap == 2)
417             last_i = cur_i;
418           else
419             no_triad_i++;
420         }
421       if (j - no_triad_i > longest_i)
422         {
423           longest_i = j - no_triad_i;
424           tonic_i = i;
425         }
426     }
427
428   /*
429     second try: note after biggest gap
430    */
431   int biggest_i = 0;
432   //  if (longest_i)
433   if (longest_i <= 1)
434     for (int i = 0; i < pitch_arr_.size (); i++)
435       {
436         int gap = pitch_arr_[i].notename_i_
437           - pitch_arr_[(i - 1 + pitch_arr_.size ()) 
438           % pitch_arr_.size ()].notename_i_;
439         while (gap < 0)
440           gap += 7;
441         gap %= 7;
442         if (gap > biggest_i)
443           {
444             biggest_i = gap;
445             tonic_i = i;
446           }
447       }
448   return tonic_i;
449 }
450
451 void
452 Chord::rebuild_from_base (int base_i)
453 {
454   assert (base_i >= 0);
455   Musical_pitch last (0, 0, -5);
456   Array<Musical_pitch> new_arr;
457   for (int i = 0; i < pitch_arr_.size (); i++)
458     {
459       Musical_pitch p = pitch_arr_[(base_i + i) % pitch_arr_.size ()];
460       if (p < last)
461         {
462           p.octave_i_ = last.octave_i_;
463           if (p < last)
464             p.octave_i_++;
465         }
466       new_arr.push (p);
467       last = p;
468     }
469   pitch_arr_ = new_arr;
470 }
471
472 void
473 Chord::rebuild_insert_inversion (int tonic_i)
474 {
475   assert (tonic_i > 0);
476 #if INVERSION_ADDED_AS_BASE
477   // inversion was added; don't insert
478   Musical_pitch inversion = pitch_arr_.get (0);
479   (void)inversion;
480 #else
481   Musical_pitch inversion = pitch_arr_.get (0);
482   rebuild_from_base (tonic_i - 1);
483   if (pitch_arr_.size ())
484     {
485       inversion.octave_i_ = pitch_arr_[0].octave_i_ - 1;
486       while (inversion < pitch_arr_[0])
487         inversion.octave_i_++;
488     }
489   for (int i = 0; i < pitch_arr_.size (); i++)
490     if (pitch_arr_[i] > inversion)
491       {
492         pitch_arr_.insert (inversion, i);
493         break;
494       }
495 #endif
496 }
497
498 void
499 Chord::rebuild_with_bass (int bass_i)
500 {
501   assert (bass_i >= 0);
502   Musical_pitch inversion = pitch_arr_.get (bass_i);
503   // is lowering fine, or should others be raised?
504   if (pitch_arr_.size ())
505     while (inversion > pitch_arr_[0])
506       inversion.octave_i_--;
507   pitch_arr_.insert (inversion, 0);
508 }