]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord.cc
release: 1.3.56
[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   construct from parser output
19 */
20 Chord
21 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)
22 {
23   // urg: catch dim modifier: 3rd, 5th, 7th, .. should be lowered
24   bool dim_b = false;
25   for (int i=0; i < add_arr_p->size (); i++)
26     {
27       Musical_pitch* p = &(*add_arr_p)[i];
28       if (p->octave_i_ == -100)
29         {
30           p->octave_i_ = 0;
31           dim_b = true;
32         }
33     }
34   Chord::rebuild_transpose (add_arr_p, tonic, true);
35   Chord::rebuild_transpose (sub_arr_p, tonic, true);
36
37   Musical_pitch fifth = Chord::base_arr (tonic).top ();
38
39   /*
40     remove double adds (urg: sus4)
41    */
42   for (int i = add_arr_p->size () - 1; i >= 0 ; i--)
43     {
44       int j = Chord::find_pitch_i (add_arr_p, (*add_arr_p)[i]);
45       if ((j != -1) && (i != j))
46         {
47             add_arr_p->get (i);
48         } 
49     }
50
51   /*
52     default chord includes upto 5: <1, 3, 5>
53    */
54   add_arr_p->insert (tonic, 0);
55   Array<Musical_pitch> tmp = *add_arr_p;
56   int highest_step = Chord::step_i (tonic, tmp.top ());
57   if (highest_step < 5)
58     tmp.push (fifth);
59   else if (dim_b)
60     {
61       Musical_pitch* p = &add_arr_p->top ();
62       p->accidental_i_--;
63     }
64
65   /*
66     find missing thirds
67    */
68   Array<Musical_pitch> missing_arr = Chord::missing_thirds_pitch_arr (&tmp);
69   if (highest_step < 5)
70     missing_arr.push (fifth);
71
72   /*
73     if dim modifier is given: lower all missing
74    */
75   if (dim_b)
76     {
77       for (int i=0; i < missing_arr.size (); i++)
78         {
79           missing_arr[i].accidental_i_--;
80         }
81     }
82
83   /*
84     if additions include some 3, don't add third
85    */
86   Musical_pitch third = Chord::base_arr (tonic)[1];
87   if (Chord::find_notename_i (add_arr_p, third) != -1)
88     {
89       int i = Chord::find_pitch_i (&missing_arr, third);
90       if (i != -1)
91         missing_arr.get (i);
92     }
93   
94   /*
95     if additions include 4, assume sus4 and don't add third implicitely
96      C-sus (4) = c f g (1 4 5)
97    */
98   Musical_pitch sus = tonic;
99   sus.transpose (Musical_pitch (3));
100   if (Chord::find_pitch_i (add_arr_p, sus) != -1)
101     {
102       int i = Chord::find_pitch_i (&missing_arr, third);
103       if (i != -1)
104         missing_arr.get (i);
105     }
106
107   /*
108     if additions include some 5, don't add fifth
109    */
110   if (Chord::find_notename_i (add_arr_p, fifth) != -1)
111     {
112       int i = Chord::find_pitch_i (&missing_arr, fifth);
113       if (i != -1)
114         missing_arr.get (i);
115     }
116   
117   
118   /*
119     complete the list of thirds to be added
120    */
121   add_arr_p->concat (missing_arr);
122   add_arr_p->sort (Musical_pitch::compare);
123
124   Array<Musical_pitch> pitch_arr;
125   /*
126    add all that aren't subtracted
127    */
128   for (int i = 0; i < add_arr_p->size (); i++)
129     {
130       Musical_pitch p = (*add_arr_p)[i];
131       int j = 0;
132       for (; j < sub_arr_p->size (); j++)
133         if (p == (*sub_arr_p)[j])
134           {
135             sub_arr_p->del (j);
136             j = -1;
137             break;
138           }
139       if (j == sub_arr_p->size ())
140         pitch_arr.push (p);
141     }
142
143   pitch_arr.sort (Musical_pitch::compare);
144
145   for (int i = 0; i < sub_arr_p->size (); i++)
146     warning (_f ("invalid subtraction: not part of chord: %s",
147                  (*sub_arr_p)[i].str ()));
148  
149   return Chord (pitch_arr, inversion_p, bass_p);
150 }
151
152 /*
153   Construct from list of pitches and requests
154  */
155 Chord
156 to_chord (Array<Musical_pitch> pitch_arr, Tonic_req* tonic_req, Inversion_req* inversion_req, Bass_req* bass_req, bool find_inversion_b)
157 {
158   Musical_pitch* inversion_p = 0;
159   Musical_pitch* bass_p = 0;
160
161   if (bass_req)
162     {
163       assert (pitch_arr[0].notename_i_ == bass_req->pitch_.notename_i_);
164       bass_p = new Musical_pitch (pitch_arr.get (0));
165     }
166     
167   if (inversion_req)
168     {
169       assert (pitch_arr[0].notename_i_ == inversion_req->pitch_.notename_i_);
170       inversion_p = new Musical_pitch (inversion_req->pitch_);
171       assert (tonic_req);
172       int tonic_i = Chord::find_notename_i (&pitch_arr, tonic_req->pitch_);
173       if (tonic_i)
174         Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
175     }
176     
177   if (find_inversion_b && !inversion_p)
178     {
179       int tonic_i = tonic_req
180         ? Chord::find_notename_i (&pitch_arr, tonic_req->pitch_) 
181         : Chord::find_tonic_i (&pitch_arr);
182         
183       if (tonic_i)
184         {
185           inversion_p = &pitch_arr[0];
186           Chord::rebuild_insert_inversion (&pitch_arr, tonic_i);
187         }
188     }
189
190   if (tonic_req)
191     {
192       assert (pitch_arr[0].notename_i_ == tonic_req->pitch_.notename_i_);
193     }
194
195   return Chord (pitch_arr, inversion_p, bass_p);
196 }
197
198 Chord::Chord ()
199 {
200   inversion_b_ = false;
201   bass_b_ = false;
202 }
203
204 Chord::Chord (Array<Musical_pitch> pitch_arr, Musical_pitch* inversion_p, Musical_pitch* bass_p)
205 {
206   pitch_arr_ = pitch_arr;
207   inversion_b_ = false;
208   bass_b_ = false;
209   if (inversion_p)
210     {
211       inversion_pitch_ = *inversion_p;
212       inversion_b_ = true;
213       delete inversion_p;
214     }
215   if (bass_p)
216     {
217       bass_pitch_ = *bass_p;
218       bass_b_ = true;
219       delete bass_p;
220     }
221 }
222   
223 Chord::Chord (Chord const& chord)
224 {
225   pitch_arr_ = chord.pitch_arr_;
226   inversion_b_ = chord.inversion_b_;
227   inversion_pitch_ = chord.inversion_pitch_;
228   bass_b_ = chord.bass_b_;
229   bass_pitch_ = chord.bass_pitch_;
230 }
231   
232
233 /*
234   JUNKME. 
235   do something smarter.
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 /*
302   JUNKME. 
303   do something smarter.
304  */
305 Array<Musical_pitch>
306 Chord::missing_thirds_pitch_arr (Array<Musical_pitch> const* pitch_arr_p)
307 {
308   Array<Musical_pitch> thirds;
309
310   /* is the third c-e, d-f, etc. small or large? */
311   int minormajor_a[] = {0, -1, -1, 0,0,-1,-1};
312   for (int i=0; i < 7; i++)
313     thirds.push (Musical_pitch( 2, minormajor_a[i]));
314
315   Musical_pitch tonic = (*pitch_arr_p)[0];
316   Musical_pitch last = tonic;
317   Array<Musical_pitch> missing_arr;
318
319   for (int i = 0; i < pitch_arr_p->size ();)
320     {
321       Musical_pitch p = (*pitch_arr_p)[i];
322       int step = step_i (tonic, p);
323       if (last.notename_i_ == p.notename_i_)
324         last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
325       if (step > step_i (tonic, last))
326         {
327           while (step > step_i (tonic, last))
328             {
329               if ((last.notename_i_ - tonic.notename_i_ + 7) % 7 == 6)
330                 {
331                   Musical_pitch special_seven = last;
332                   Musical_pitch lower (0, -1);
333                   special_seven.transpose (lower);
334                   missing_arr.push (special_seven);
335                 }
336               else
337                 {
338                   missing_arr.push (last);
339                 }
340               last.transpose (thirds[(last.notename_i_ - tonic.notename_i_ + 7) % 7]);
341             }
342         }
343       else
344         {
345           i++;
346         }
347     }
348   return missing_arr;
349 }
350
351
352 /*
353  Mangle into list of pitches.
354  For normal chord entry, inversion and bass pitches are retained in
355  specific *_requests
356  */
357 Array<Musical_pitch>
358 Chord::to_pitch_arr () const
359 {
360   Array<Musical_pitch> pitch_arr = pitch_arr_;
361   if (inversion_b_)
362     {
363       int i = 0;
364       for (; i < pitch_arr.size (); i++)
365         {
366           if ((pitch_arr[i].notename_i_ == inversion_pitch_.notename_i_)
367               && (pitch_arr[i].accidental_i_ == inversion_pitch_.accidental_i_))
368             break;
369         }
370       if (i == pitch_arr.size ())
371         {
372           warning (_f ("invalid inversion pitch: not part of chord: %s",
373                        inversion_pitch_.str ()));
374         }
375       else
376         rebuild_with_bass (&pitch_arr, i);
377     }
378
379   if (bass_b_)
380     {
381       pitch_arr.insert (bass_pitch_, 0);
382       rebuild_with_bass (&pitch_arr, 0);
383     }
384   return pitch_arr;
385 }
386
387 /*
388   This routine tries to guess tonic in a possibly inversed chord, ie
389   <e g c'> should produce: C.
390   This is only used for chords that are entered as simultaneous notes,
391   chords entered in \chord mode are fully defined.
392  */
393 int
394 Chord::find_tonic_i (Array<Musical_pitch> const* pitch_arr_p)
395 {
396   /*
397     find tonic
398     
399     first try: base of longest line of thirds
400    */
401   int tonic_i = 0;
402   int longest_i = 0;
403   for (int i = 0; i < pitch_arr_p->size (); i++)
404     {
405       int no_third_i = 0;
406       int last_i = (*pitch_arr_p)[i % pitch_arr_p->size ()].notename_i_;
407       int j = 0;
408       for (; j < pitch_arr_p->size (); j++)
409         {
410           int cur_i = (*pitch_arr_p)[(i + j + 1) % pitch_arr_p->size ()].notename_i_;
411           int gap = cur_i - last_i;
412           while (gap < 0)
413             gap += 7;
414           gap %= 7;
415           if (gap == 2)
416             last_i = cur_i;
417           else
418             no_third_i++;
419         }
420       if (j - no_third_i > longest_i)
421         {
422           longest_i = j - no_third_i;
423           tonic_i = i;
424         }
425     }
426
427   /*
428     second try: note after biggest gap
429    */
430   int biggest_i = 0;
431   //  if (longest_i)
432   if (longest_i <= 1)
433     for (int i = 0; i < pitch_arr_p->size (); i++)
434       {
435         int gap = (*pitch_arr_p)[i].notename_i_
436           - (*pitch_arr_p)[(i - 1 + pitch_arr_p->size ()) 
437           % pitch_arr_p->size ()].notename_i_;
438         while (gap < 0)
439           gap += 7;
440         gap %= 7;
441         if (gap > biggest_i)
442           {
443             biggest_i = gap;
444             tonic_i = i;
445           }
446       }
447   return tonic_i;
448 }
449
450 void
451 Chord::rebuild_from_base (Array<Musical_pitch>* pitch_arr_p, int base_i)
452 {
453   assert (base_i >= 0);
454   Musical_pitch last (0, 0, -5);
455   Array<Musical_pitch> new_arr;
456   for (int i = 0; i < pitch_arr_p->size (); i++)
457     {
458       Musical_pitch p = (*pitch_arr_p)[(base_i + i) % pitch_arr_p->size ()];
459       if (p < last)
460         {
461           p.octave_i_ = last.octave_i_;
462           if (p < last)
463             p.octave_i_++;
464         }
465       new_arr.push (p);
466       last = p;
467     }
468   *pitch_arr_p = new_arr;
469 }
470
471 void
472 Chord::rebuild_insert_inversion (Array<Musical_pitch>* pitch_arr_p, int tonic_i)
473 {
474   assert (tonic_i > 0);
475   Musical_pitch inversion = pitch_arr_p->get (0);
476   rebuild_from_base (pitch_arr_p, tonic_i - 1);
477   if (pitch_arr_p->size ())
478     {
479       inversion.octave_i_ = (*pitch_arr_p)[0].octave_i_ - 1;
480       while (inversion < (*pitch_arr_p)[0])
481         inversion.octave_i_++;
482     }
483   for (int i = 0; i < pitch_arr_p->size (); i++)
484     if ((*pitch_arr_p)[i] > inversion)
485       {
486         pitch_arr_p->insert (inversion, i);
487         break;
488       }
489 }
490
491 void
492 Chord::rebuild_with_bass (Array<Musical_pitch>* pitch_arr_p, int bass_i)
493 {
494   assert (bass_i >= 0);
495   Musical_pitch bass = pitch_arr_p->get (bass_i);
496   // is lowering fine, or should others be raised?
497   if (pitch_arr_p->size ())
498     while (bass > (*pitch_arr_p)[0])
499       bass.octave_i_--;
500   pitch_arr_p->insert (bass, 0);
501 }
502