]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord.cc
patch::: 1.3.41.jcn2
[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--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
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"
16
17
18 /*
19   construct from parser output
20 */
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)
23 {
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++)
27     {
28       Musical_pitch* p = &(*add_arr_p)[i];
29       if (p->octave_i_ == -100)
30         {
31           p->octave_i_ = 0;
32           dim_b = true;
33         }
34     }
35   Chord::rebuild_transpose (add_arr_p, tonic, true);
36   Chord::rebuild_transpose (sub_arr_p, tonic, true);
37
38   Musical_pitch fifth = Chord::base_arr (tonic).top ();
39
40   /*
41     remove double adds (urg: sus4)
42    */
43   for (int i = add_arr_p->size () - 1; i >= 0 ; i--)
44     {
45       int j = Chord::find_pitch_i (add_arr_p, (*add_arr_p)[i]);
46       if ((j != -1) && (i != j))
47         {
48             add_arr_p->get (i);
49         } 
50     }
51
52   /*
53     default chord includes upto 5: <1, 3, 5>
54    */
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)
61     {
62       Musical_pitch* p = &add_arr_p->top ();
63       p->accidental_i_--;
64     }
65
66   /*
67     find missing thirds
68    */
69   Array<Musical_pitch> missing_arr = Chord::missing_thirds_pitch_arr (&tmp);
70   if (highest_step < 5)
71     missing_arr.push (fifth);
72
73   /*
74     if dim modifier is given: lower all missing
75    */
76   if (dim_b)
77     {
78       for (int i=0; i < missing_arr.size (); i++)
79         {
80           missing_arr[i].accidental_i_--;
81         }
82     }
83
84   /*
85     if additions include some 3, don't add third
86    */
87   Musical_pitch third = Chord::base_arr (tonic)[1];
88   if (Chord::find_notename_i (add_arr_p, third) != -1)
89     {
90       int i = Chord::find_pitch_i (&missing_arr, third);
91       if (i != -1)
92         missing_arr.get (i);
93     }
94   
95   /*
96     if additions include 4, assume sus4 and don't add third implicitely
97      C-sus (4) = c f g (1 4 5)
98    */
99   Musical_pitch sus = tonic;
100   sus.transpose (Musical_pitch (3));
101   if (Chord::find_pitch_i (add_arr_p, sus) != -1)
102     {
103       int i = Chord::find_pitch_i (&missing_arr, third);
104       if (i != -1)
105         missing_arr.get (i);
106     }
107
108   /*
109     if additions include some 5, don't add fifth
110    */
111   if (Chord::find_notename_i (add_arr_p, fifth) != -1)
112     {
113       int i = Chord::find_pitch_i (&missing_arr, fifth);
114       if (i != -1)
115         missing_arr.get (i);
116     }
117   
118   
119   /*
120     complete the list of thirds to be added
121    */
122   add_arr_p->concat (missing_arr);
123   add_arr_p->sort (Musical_pitch::compare);
124
125   Array<Musical_pitch> pitch_arr;
126   /*
127    add all that aren't subtracted
128    */
129   for (int i = 0; i < add_arr_p->size (); i++)
130     {
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])
135           {
136             sub_arr_p->del (j);
137             j = -1;
138             break;
139           }
140       if (j == sub_arr_p->size ())
141         pitch_arr.push (p);
142     }
143
144   pitch_arr.sort (Musical_pitch::compare);
145
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 ()));
149  
150   return Chord (pitch_arr, inversion_p, bass_p);
151 }
152
153 /*
154   Construct from list of pitches and requests
155  */
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)
158 {
159   Musical_pitch* inversion_p = 0;
160   Musical_pitch* bass_p = 0;
161
162   if (bass_req)
163     {
164       assert (pitch_arr[0].notename_i_ == bass_req->pitch_.notename_i_);
165       bass_p = new Musical_pitch (pitch_arr.get (0));
166     }
167     
168   if (inversion_req)
169     {
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);
176     }
177     
178   if (find_inversion_b && !inversion_p)
179     {
180       int tonic_i = tonic_req
181         ? Chord::find_notename_i (&pitch_arr, tonic_req->pitch_) 
182         : Chord::find_tonic_i (&pitch_arr);
183         
184       if (tonic_i)
185         {
186           inversion_p = &pitch_arr[0];
187           Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
188         }
189     }
190
191   if (tonic_req)
192     {
193       assert (pitch_arr[0].notename_i_ == tonic_req->pitch_.notename_i_);
194     }
195
196   Chord chord (pitch_arr, inversion_p, bass_p);
197   delete inversion_p;
198   delete bass_p;
199   return chord;
200 }
201
202 Chord::Chord ()
203 {
204   inversion_b_ = false;
205   bass_b_ = false;
206 }
207
208 Chord::Chord (Array<Musical_pitch> pitch_arr, Musical_pitch* inversion_p, Musical_pitch* bass_p)
209 {
210   pitch_arr_ = pitch_arr;
211   inversion_b_ = false;
212   bass_b_ = false;
213   if (inversion_p)
214     {
215       inversion_pitch_ = *inversion_p;
216       inversion_b_ = true;
217       delete inversion_p;
218     }
219   if (bass_p)
220     {
221       bass_pitch_ = *bass_p;
222       bass_b_ = true;
223       delete bass_p;
224     }
225 }
226   
227 Chord::Chord (Chord const& chord)
228 {
229   pitch_arr_ = chord.pitch_arr_;
230   inversion_b_ = chord.inversion_b_;
231   inversion_pitch_ = chord.inversion_pitch_;
232   bass_b_ = chord.bass_b_;
233   bass_pitch_ = chord.bass_pitch_;
234 }
235   
236
237 Array<Musical_pitch>
238 Chord::base_arr (Musical_pitch p)
239 {
240   Array<Musical_pitch> base;
241   base.push (p);
242   p.transpose (Musical_pitch (2));
243   base.push (p);
244   p.transpose (Musical_pitch (2, -1));
245   base.push (p);
246   return base;
247 }
248
249 void
250 Chord::rebuild_transpose (Array<Musical_pitch>* pitch_arr_p, Musical_pitch tonic, bool fix7_b)
251 {
252   for (int i = 0; i < pitch_arr_p->size (); i++)
253     {
254       Musical_pitch p = tonic;
255       Musical_pitch q = (*pitch_arr_p)[i];
256       p.transpose (q);
257       // duh, c7 should mean <c bes>
258       if (fix7_b && (step_i (tonic, p) == 7))
259         p.accidental_i_--;
260       (*pitch_arr_p)[i] = p;
261     }
262   pitch_arr_p->sort (Musical_pitch::compare);
263 }
264
265 int
266 Chord::find_pitch_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
267 {
268   for (int i = 0; i < pitch_arr_p->size (); i++)
269     if (p == (*pitch_arr_p)[i])
270       return i;
271   return -1;
272 }
273
274 int
275 Chord::find_notename_i (Array<Musical_pitch> const* pitch_arr_p, Musical_pitch p)
276 {
277   int i = find_pitch_i (pitch_arr_p, p);
278   if (i == -1)
279     {
280       for (int i = 0; i < pitch_arr_p->size (); i++)
281         {
282           p.octave_i_ = (*pitch_arr_p)[i].octave_i_;
283           if (p == (*pitch_arr_p)[i])
284             return i;
285         }
286     }
287   return i;
288 }
289
290 int
291 Chord::step_i (Musical_pitch tonic, Musical_pitch p)
292 {
293   int i = p.notename_i_ - tonic.notename_i_
294     + (p.octave_i_ - tonic.octave_i_) * 7;
295   while (i < 0)
296     i += 7;
297   i++;
298   return i;
299 }
300
301 Array<Musical_pitch>
302 Chord::missing_thirds_pitch_arr (Array<Musical_pitch> const* pitch_arr_p)
303 {
304   Array<Musical_pitch> thirds;
305
306   /* is the third c-e, d-f, etc. small or large? */
307   int minormajor_a[] = {0, -1, -1, 0,0,-1,-1};
308   for (int i=0; i < 7; i++)
309     thirds.push (Musical_pitch( 2, minormajor_a[i]));
310
311   Musical_pitch tonic = (*pitch_arr_p)[0];
312   Musical_pitch last = tonic;
313   Array<Musical_pitch> missing_arr;
314
315   for (int i = 0; i < pitch_arr_p->size ();)
316     {
317       Musical_pitch p = (*pitch_arr_p)[i];
318       int step = step_i (tonic, p);
319       if (last.notename_i_ == p.notename_i_)
320         last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
321       if (step > step_i (tonic, last))
322         {
323           while (step > step_i (tonic, last))
324             {
325               if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
326                 {
327                   Musical_pitch special_seven = last;
328                   Musical_pitch lower (0, -1);
329                   special_seven.transpose (lower);
330                   missing_arr.push (special_seven);
331                 }
332               else
333                 {
334                   missing_arr.push (last);
335                 }
336               last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
337             }
338         }
339       else
340         {
341           i++;
342         }
343     }
344   return missing_arr;
345 }
346
347
348 /*
349  Mangle into list of pitches.
350  For normal chord entry, inversion and bass pitches are retained in
351  specific *_requests
352  */
353 Array<Musical_pitch>
354 Chord::to_pitch_arr () const
355 {
356   Array<Musical_pitch> pitch_arr = pitch_arr_;
357   if (inversion_b_)
358     {
359       int i = 0;
360       for (; i < pitch_arr.size (); i++)
361         {
362           if ((pitch_arr[i].notename_i_ == inversion_pitch_.notename_i_)
363               && (pitch_arr[i].accidental_i_ == inversion_pitch_.accidental_i_))
364             break;
365         }
366       if (i == pitch_arr.size ())
367         {
368           warning (_f ("invalid inversion pitch: not part of chord: %s",
369                        inversion_pitch_.str ()));
370         }
371       else
372         rebuild_with_bass (&pitch_arr, i);
373     }
374
375   if (bass_b_)
376     {
377       pitch_arr.insert (bass_pitch_, 0);
378       rebuild_with_bass (&pitch_arr, 0);
379     }
380   return pitch_arr;
381 }
382
383 void
384 Chord::find_additions_and_subtractions (Array<Musical_pitch> pitch_arr, Array<Musical_pitch>* add_arr_p, Array<Musical_pitch>* sub_arr_p)
385 {
386   Musical_pitch tonic = pitch_arr[0];
387   /*
388     construct an array of thirds for a normal chord
389    */
390   Array<Musical_pitch> all_arr;
391   all_arr.push (tonic);
392   if (step_i (tonic, pitch_arr.top ()) >= 5)
393     all_arr.push (pitch_arr.top ());
394   else
395     all_arr.push (base_arr (tonic).top ());
396   all_arr.concat (missing_thirds_pitch_arr (&all_arr));
397   all_arr.sort (Musical_pitch::compare);
398   
399   int i = 0;
400   int j = 0;
401   Musical_pitch last_extra = tonic;
402   while ((i < all_arr.size ()) || (j < pitch_arr.size ()))
403     {
404       Musical_pitch a = all_arr [i <? all_arr.size () - 1];
405       Musical_pitch p = pitch_arr[j <? pitch_arr.size () - 1];
406       /*
407         this pitch is present: do nothing, check next
408        */
409       if (a == p)
410         {
411           i++;
412           j++;
413           last_extra = tonic;
414         }
415       /*
416         found an extra pitch: chord addition
417        */
418       else if ((p < a) || (p.notename_i_ == a.notename_i_))
419         {
420           add_arr_p->push (p);
421           last_extra = p;
422           (j < pitch_arr.size ()) ? j++ : i++;
423         }
424       /*
425         a third is missing: chord subtraction
426        */
427       else
428         {
429           if (last_extra.notename_i_ != a.notename_i_)
430             sub_arr_p->push (a);
431           (i < all_arr.size ()) ? i++ : j++;
432           last_extra = tonic;
433         }
434     }
435       
436   /* add missing basic steps */
437   if (step_i (tonic, pitch_arr.top ()) < 3)
438     sub_arr_p->push (base_arr (tonic)[1]);
439   if (step_i (tonic, pitch_arr.top ()) < 5)
440     sub_arr_p->push (base_arr (tonic).top ());
441
442   /*
443     add highest addition, because it names chord, if greater than 5
444     or non-standard
445     (1, 3 and) 5 not additions: part of normal chord
446    */
447   if ((step_i (tonic, pitch_arr.top ()) > 5)
448        || pitch_arr.top ().accidental_i_)
449     add_arr_p->push (pitch_arr.top ());
450 }
451
452
453 /*
454   This routine tries to guess tonic in a possibly inversed chord, ie
455   <e g c'> should produce: C.
456   This is only used for chords that are entered as simultaneous notes,
457   chords entered in \chord mode are fully defined.
458  */
459 int
460 Chord::find_tonic_i (Array<Musical_pitch> const* pitch_arr_p)
461 {
462   /*
463     find tonic
464     
465     first try: base of longest line of thirds
466    */
467   int tonic_i = 0;
468   int longest_i = 0;
469   for (int i = 0; i < pitch_arr_p->size (); i++)
470     {
471       int no_third_i = 0;
472       int last_i = (*pitch_arr_p)[i % pitch_arr_p->size ()].notename_i_;
473       int j = 0;
474       for (; j < pitch_arr_p->size (); j++)
475         {
476           int cur_i = (*pitch_arr_p)[(i + j + 1) % pitch_arr_p->size ()].notename_i_;
477           int gap = cur_i - last_i;
478           while (gap < 0)
479             gap += 7;
480           gap %= 7;
481           if (gap == 2)
482             last_i = cur_i;
483           else
484             no_third_i++;
485         }
486       if (j - no_third_i > longest_i)
487         {
488           longest_i = j - no_third_i;
489           tonic_i = i;
490         }
491     }
492
493   /*
494     second try: note after biggest gap
495    */
496   int biggest_i = 0;
497   //  if (longest_i)
498   if (longest_i <= 1)
499     for (int i = 0; i < pitch_arr_p->size (); i++)
500       {
501         int gap = (*pitch_arr_p)[i].notename_i_
502           - (*pitch_arr_p)[(i - 1 + pitch_arr_p->size ()) 
503           % pitch_arr_p->size ()].notename_i_;
504         while (gap < 0)
505           gap += 7;
506         gap %= 7;
507         if (gap > biggest_i)
508           {
509             biggest_i = gap;
510             tonic_i = i;
511           }
512       }
513   return tonic_i;
514 }
515
516 void
517 Chord::rebuild_from_base (Array<Musical_pitch>* pitch_arr_p, int base_i)
518 {
519   assert (base_i >= 0);
520   Musical_pitch last (0, 0, -5);
521   Array<Musical_pitch> new_arr;
522   for (int i = 0; i < pitch_arr_p->size (); i++)
523     {
524       Musical_pitch p = (*pitch_arr_p)[(base_i + i) % pitch_arr_p->size ()];
525       if (p < last)
526         {
527           p.octave_i_ = last.octave_i_;
528           if (p < last)
529             p.octave_i_++;
530         }
531       new_arr.push (p);
532       last = p;
533     }
534   *pitch_arr_p = new_arr;
535 }
536
537 void
538 Chord::rebuild_insert_inversion (Array<Musical_pitch>* pitch_arr_p, int tonic_i)
539 {
540   assert (tonic_i > 0);
541   Musical_pitch inversion = pitch_arr_p->get (0);
542   rebuild_from_base (pitch_arr_p, tonic_i - 1);
543   if (pitch_arr_p->size ())
544     {
545       inversion.octave_i_ = (*pitch_arr_p)[0].octave_i_ - 1;
546       while (inversion < (*pitch_arr_p)[0])
547         inversion.octave_i_++;
548     }
549   for (int i = 0; i < pitch_arr_p->size (); i++)
550     if ((*pitch_arr_p)[i] > inversion)
551       {
552         pitch_arr_p->insert (inversion, i);
553         break;
554       }
555 }
556
557 void
558 Chord::rebuild_with_bass (Array<Musical_pitch>* pitch_arr_p, int bass_i)
559 {
560   assert (bass_i >= 0);
561   Musical_pitch bass = pitch_arr_p->get (bass_i);
562   // is lowering fine, or should others be raised?
563   if (pitch_arr_p->size ())
564     while (bass > (*pitch_arr_p)[0])
565       bass.octave_i_--;
566   pitch_arr_p->insert (bass, 0);
567 }
568