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