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