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