]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/chord.cc
release: 1.5.29
[lilypond.git] / lily / chord.cc
index aecc8eb45b13cee5e6ea04922afa9b823715b073..908d8f4432eaca03ec5bf0819046c3b3ede8f5ab 100644 (file)
@@ -3,7 +3,7 @@
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  1999--2000 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c)  1999--2002 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "chord.hh"
@@ -24,39 +24,33 @@ SCM
 ly_unique (SCM list)
 {
   SCM unique = SCM_EOL;
-  for (SCM i = list; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
     {
-      if (!gh_pair_p (gh_cdr (i))
-         || !gh_equal_p (gh_car (i), gh_cadr (i)))
-       unique = gh_cons (gh_car (i), unique);
+      if (!gh_pair_p (ly_cdr (i))
+         || !gh_equal_p (ly_car (i), ly_cadr (i)))
+       unique = gh_cons (ly_car (i), unique);
     }
   return gh_reverse (unique);
 }
 
 /* Hmm, rewrite this using ly_split_list? */
 SCM
-ly_delete1 (SCM s, SCM list)
+ly_remove_member (SCM s, SCM list)
 {
   SCM removed = SCM_EOL;
-  for (SCM i = list; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = list; gh_pair_p (i); i = ly_cdr (i))
     {
-      if (!gh_equal_p (gh_car (i), s))
-       removed = gh_cons (gh_car (i), removed);
+      if (!gh_equal_p (ly_car (i), s))
+       removed = gh_cons (ly_car (i), removed);
     }
   return gh_reverse (removed);
 }
 
-SCM
-ly_last (SCM list)
-{
-  return gh_car (scm_last_pair (list));
-}
-
 /* tail add */
 SCM
 ly_snoc (SCM s, SCM list)
 {
-  return gh_append2 (list, gh_list (s, SCM_UNDEFINED));
+  return gh_append2 (list, scm_list_n (s, SCM_UNDEFINED));
 }
 
 
@@ -69,8 +63,8 @@ ly_split_list (SCM s, SCM list)
   SCM after = list;
   for (; gh_pair_p (after);)
     {
-      SCM i = gh_car (after);
-      after = gh_cdr (after);
+      SCM i = ly_car (after);
+      after = ly_cdr (after);
       if (gh_equal_p (i, s))
        break;
       before = gh_cons (i, before);
@@ -78,59 +72,6 @@ ly_split_list (SCM s, SCM list)
   return gh_cons (gh_reverse (before), after);
 }
 
-
-/* Construct from list of pitches and requests:
-
-  (PITCHES . (INVERSION . BASS))
-
-
-  Note, the pitches here, are all inclusive.
-  We must identify tonic, filter-out (and maybe detect) inversion and bass. */
-
-SCM
-Chord::pitches_and_requests_to_chord (SCM pitches,
-                                     SCM tonic_req,
-                                     SCM inversion_req,
-                                     SCM bass_req,
-                                     bool find_inversion_b)
-{
-  pitches = scm_sort_list (pitches, Pitch::less_p_proc);
-                          
-  if (bass_req != SCM_EOL)
-    {
-      assert (unsmob_pitch (gh_car (pitches))->notename_i_
-             == unsmob_pitch (bass_req)->notename_i_);
-      pitches = gh_cdr (pitches);
-    }
-    
-  if (inversion_req != SCM_EOL)
-    {
-      assert (unsmob_pitch (gh_car (pitches))->notename_i_
-             == unsmob_pitch (inversion_req)->notename_i_);
-      /* huh ? */
-      assert (tonic_req != SCM_EOL);
-      
-      SCM tonic = member_notename (tonic_req, pitches);
-      if (tonic != SCM_EOL)
-       pitches = add_above_tonic (gh_car (pitches), gh_cdr (pitches));
-    }
-  else if (find_inversion_b)
-    {
-      SCM tonic = (tonic_req != SCM_EOL)
-       ? member_notename (pitches, tonic_req)
-       : guess_tonic (pitches);
-       
-      if (tonic != SCM_EOL)
-       pitches = add_above_tonic (gh_car (pitches), gh_cdr (pitches));
-    }
-
-  if (tonic_req != SCM_EOL)
-      assert (unsmob_pitch (gh_car (pitches))->notename_i_
-             == unsmob_pitch (tonic_req)->notename_i_);
-
-  return gh_cons (pitches, gh_cons (inversion_req, bass_req));
-}
-
 /*
   JUNKME. 
   do something smarter.
@@ -145,8 +86,8 @@ Chord::base_pitches (SCM tonic)
   SCM minor = Pitch (0, 2, -1).smobbed_copy ();
 
   base = gh_cons (tonic, base);
-  base = gh_cons (Pitch::transpose (gh_car (base), major), base);
-  base = gh_cons (Pitch::transpose (gh_car (base), minor), base);
+  base = gh_cons (Pitch::transpose (ly_car (base), major), base);
+  base = gh_cons (Pitch::transpose (ly_car (base), minor), base);
 
   return gh_reverse (base);
 }
@@ -158,9 +99,9 @@ Chord::transpose_pitches (SCM tonic, SCM pitches)
      hoe doe je lambda in C?
   */
   SCM transposed = SCM_EOL;
-  for (SCM i = pitches; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
     {
-      transposed = gh_cons (Pitch::transpose (tonic, gh_car (i)),
+      transposed = gh_cons (Pitch::transpose (tonic, ly_car (i)),
                            transposed);
     }
   return gh_reverse (transposed);
@@ -176,10 +117,10 @@ SCM
 Chord::lower_step (SCM tonic, SCM pitches, SCM step)
 {
   SCM lowered = SCM_EOL;
-  for (SCM i = pitches; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
     {
-      SCM p = gh_car (i);
-      if (gh_equal_p (step_scm (tonic, gh_car (i)), step)
+      SCM p = ly_car (i);
+      if (gh_equal_p (step_scm (tonic, ly_car (i)), step)
          || gh_scm2int (step) == 0)
        {
          p = Pitch::transpose (p, Pitch (0, 0, -1).smobbed_copy ());
@@ -197,20 +138,22 @@ Chord::member_notename (SCM p, SCM pitches)
   SCM member = gh_member (p, pitches);
   if (member == SCM_BOOL_F)
     {
-      for (SCM i = pitches; gh_pair_p (i); i = gh_cdr (i))
+      for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
        {
          /*
            Urg, eindelijk gevonden: () != #f, kan maar niet aan wennen.
            Anders kon iets korter...
           */
          if (unsmob_pitch (p)->notename_i_
-             == unsmob_pitch (gh_car (i))->notename_i_)
+             == unsmob_pitch (ly_car (i))->notename_i_)
            {
-             member = gh_car (i);
+             member = ly_car (i);
              break;
            }
        }
     }
+  else
+    member = ly_car (member);
   return member;
 }
 
@@ -222,38 +165,35 @@ Chord::member_pitch (SCM p, SCM pitches)
   SCM member = gh_member (p, pitches);
   if (member == SCM_BOOL_F)
     {
-      for (SCM i = pitches; gh_pair_p (i); i = gh_cdr (i))
+      for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
        {
          if (unsmob_pitch (p)->notename_i_
-             == unsmob_pitch (gh_car (i))->notename_i_
+             == unsmob_pitch (ly_car (i))->notename_i_
              && unsmob_pitch (p)->alteration_i_
-             == unsmob_pitch (gh_car (i))->alteration_i_)
+             == unsmob_pitch (ly_car (i))->alteration_i_)
            {
-             member = gh_car (i);
+             member = ly_car (i);
              break;
            }
        }
     }
+  else
+    member = ly_car (member);
   return member;
 }
 
-
-
-int
-Chord::step_i (Pitch tonic, Pitch p)
+SCM
+Chord::step_scm (SCM tonic, SCM p)
 {
-  int i = p.notename_i_ - tonic.notename_i_
-    + (p.octave_i ()  - tonic.octave_i () ) * 7;
+  /* De Pitch intervaas is nog beetje sleutelgat? */
+  int i = unsmob_pitch (p)->notename_i_
+    - unsmob_pitch (tonic)->notename_i_
+    + (unsmob_pitch (p)->octave_i_
+       - unsmob_pitch (tonic)->octave_i_) * 7;
   while (i < 0)
     i += 7;
   i++;
-  return i;
-}
-
-SCM
-Chord::step_scm (SCM tonic, SCM p)
-{
-  return gh_int2scm (step_i (*unsmob_pitch (tonic), *unsmob_pitch (p)));
+  return gh_int2scm (i);
 }
 
 /*
@@ -279,13 +219,13 @@ Chord::missing_thirds (SCM pitches)
                      thirds);
   thirds = scm_vector (gh_reverse (thirds));
   
-  SCM tonic = gh_car (pitches);
+  SCM tonic = ly_car (pitches);
   SCM last = tonic;
   SCM missing = SCM_EOL;
 
   for (SCM i = pitches; gh_pair_p (i);)
     {
-      SCM p = gh_car (i);
+      SCM p = ly_car (i);
       int step = gh_scm2int (step_scm (tonic, p));
       
       if (unsmob_pitch (last)->notename_i_ == unsmob_pitch (p)->notename_i_)
@@ -308,74 +248,20 @@ Chord::missing_thirds (SCM pitches)
        }
       else
        {
-         i = gh_cdr (i);
+         i = ly_cdr (i);
        }
     }
   
   return lower_step (tonic, missing, gh_int2scm (7));
 }
 
-
-/* Mangle
-
-     (PITCHES . (INVERSION . BASS))
- into full list of pitches.
-
- This means:
-   - delete INVERSION and add as lowest note of PITCHES
-   - add BASS as lowest note of PITCHES */
-
-SCM
-Chord::to_pitches (SCM chord)
-{
-  SCM pitches = gh_car (chord);
-  SCM modifiers = gh_cdr (chord);
-  SCM inversion = gh_car (modifiers);
-  SCM bass = gh_cdr (modifiers);
-
-  if (inversion != SCM_EOL)
-    {
-      /* If inversion requested, check first if the note is part of chord */
-      SCM s = member_pitch (inversion, pitches);
-      if (s != SCM_BOOL_F)
-       {
-         /* Then, delete and add as base note, ie: the inversion */
-         scm_delete (s, pitches);
-         pitches = add_below_tonic (s, pitches);
-       }
-      else
-       warning (_f ("invalid inversion pitch: not part of chord: %s",
-                    unsmob_pitch (inversion)->str ()));
-    }
-
-  /* Bass is easy, just add if requested */
-  if (bass != SCM_EOL)
-    pitches = add_below_tonic (bass, pitches);
-    
-  return pitches;
-}
-
-/*
-  This routine tries to guess tonic in a possibly inversed chord, ie
-  <e g c'> should produce: C.
-  This is only used for chords that are entered as simultaneous notes,
-  chords entered in \chord mode are fully defined.
- */
-
-SCM
-Chord::guess_tonic (SCM pitches)
-{
-  return gh_car (scm_sort_list (pitches, Pitch::less_p_proc)); 
-} 
-
 /* Return PITCHES with PITCH added not as lowest note */
 SCM
 Chord::add_above_tonic (SCM pitch, SCM pitches)
 {
   /* Should we maybe first make sure that PITCH is below tonic? */
   if (pitches != SCM_EOL)
-    while (Pitch::less_p (pitch, gh_car (pitches)) == SCM_BOOL_T)
+    while (Pitch::less_p (pitch, ly_car (pitches)) == SCM_BOOL_T)
       pitch = Pitch::transpose (pitch, Pitch (1, 0, 0).smobbed_copy ());
    
   pitches = gh_cons (pitch, pitches);
@@ -387,34 +273,35 @@ SCM
 Chord::add_below_tonic (SCM pitch, SCM pitches)
 {
   if (pitches != SCM_EOL)
-    while (Pitch::less_p (gh_car (pitches), pitch) == SCM_BOOL_T)
+    while (Pitch::less_p (ly_car (pitches), pitch) == SCM_BOOL_T)
       pitch = Pitch::transpose (pitch, Pitch (-1, 0, 0).smobbed_copy ());
   return gh_cons (pitch, pitches);
 }
 
 
 
-/*****
-      Parser stuff 
-
-      Construct from parser output:
-
-      (PITCHES . (INVERSION . BASS))
-
-      PITCHES is the plain chord, it does not include bass or inversion
-
-      Part of Chord:: namespace for now, because we do lots of
-      chord-manipulating stuff. */
+/*
+  Parser stuff 
+  
+  Construct from parser output:
 
+  PITCHES is the plain chord, it does not include bass or inversion
+  
+  Part of Chord:: namespace for now, because we do lots of
+  chord-manipulating stuff.
+*/
 SCM
-Chord::tonic_add_sub_inversion_bass_to_scm (SCM tonic, SCM add, SCM sub,
-                                           SCM inversion, SCM bass)
+Chord::tonic_add_sub_to_pitches (SCM tonic, SCM add, SCM sub)
 {
   /* urg: catch dim modifier: 3rd, 5th, 7th, .. should be lowered */
   bool dim_b = false;
-  for (SCM i = add; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = add; gh_pair_p (i); i = ly_cdr (i))
     {
-      Pitch* p = unsmob_pitch (gh_car (i));
+      Pitch* p = unsmob_pitch (ly_car (i));
+      /* Ugr
+       This chord modifier stuff should really be fixed
+       Cmaj7 yields C 7/7-
+      */
       if (p->octave_i ()  == -100)
         {
           p->octave_i_ = 0;
@@ -439,7 +326,10 @@ Chord::tonic_add_sub_inversion_bass_to_scm (SCM tonic, SCM add, SCM sub,
   if (highest_step < 5)
     tmp = ly_snoc (fifth, tmp);
   else if (dim_b)
-    add = lower_step (tonic, add, gh_int2scm (5));
+    {
+      add = lower_step (tonic, add, gh_int2scm (5));
+      add = lower_step (tonic, add, gh_int2scm (7));
+    }
 
   /* find missing thirds */
   SCM missing = missing_thirds (tmp);
@@ -451,7 +341,7 @@ Chord::tonic_add_sub_inversion_bass_to_scm (SCM tonic, SCM add, SCM sub,
     missing = lower_step (tonic, missing, gh_int2scm (0));
   
   /* if additions include any 3, don't add third */
-  SCM third = gh_cadr (base_pitches (tonic));
+  SCM third = ly_cadr (base_pitches (tonic));
   if (member_notename (third, add) != SCM_BOOL_F)
     missing = scm_delete (third, missing);
 
@@ -471,9 +361,9 @@ Chord::tonic_add_sub_inversion_bass_to_scm (SCM tonic, SCM add, SCM sub,
   
   SCM pitches = SCM_EOL;
   /* Add all that aren't subtracted */
-  for (SCM i = add; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = add; gh_pair_p (i); i = ly_cdr (i))
     {
-      SCM p = gh_car (i);
+      SCM p = ly_car (i);
       SCM s = member_notename (p, sub);
       if (s != SCM_BOOL_F)
        sub = scm_delete (s, sub);
@@ -482,70 +372,62 @@ Chord::tonic_add_sub_inversion_bass_to_scm (SCM tonic, SCM add, SCM sub,
     }
   pitches = scm_sort_list (pitches, Pitch::less_p_proc);
   
-  for (SCM i = sub; gh_pair_p (i); i = gh_cdr (i))
+  for (SCM i = sub; gh_pair_p (i); i = ly_cdr (i))
     warning (_f ("invalid subtraction: not part of chord: %s",
-                unsmob_pitch (gh_car (i))->str ()));
+                unsmob_pitch (ly_car (i))->str ()));
 
-  return gh_cons (pitches, gh_cons (inversion, bass));
+  return pitches;
 }
 
 
-/*
-  --Het lijkt me dat dit in het paarse gedeelte moet.
-
-  Zo-en-zo, lijktme dat je ipv. Inversion_req een (inversion . #t) aan
-  de betreffende Noot_req kan hangen
-*/
-
+/* --Het lijkt me dat dit in het paarse gedeelte moet. */
 Simultaneous_music *
 Chord::get_chord (SCM tonic, SCM add, SCM sub, SCM inversion, SCM bass, SCM dur)
 {
-  SCM chord = tonic_add_sub_inversion_bass_to_scm (tonic, add, sub,
-                                                  inversion, bass);
-                                                  
-  Tonic_req* t = new Tonic_req;
-  t->set_mus_property ("pitch",  tonic);
-  SCM l = gh_cons (t->self_scm (), SCM_EOL);
-
-  SCM modifiers = gh_cdr (chord);
-  inversion = gh_car (modifiers);
-  bass = gh_cdr (modifiers);
-
-  /* This sucks.
-     Should add (inversion . #t) to the pitch that is an inversion
-   */
+  SCM pitches = tonic_add_sub_to_pitches (tonic, add, sub);
+  SCM list = SCM_EOL;
   if (inversion != SCM_EOL)
     {
-      Inversion_req* i = new Inversion_req;
-      i->set_mus_property ("pitch",  inversion);
-      l = gh_cons (i->self_scm (), l);
-      scm_unprotect_object (i->self_scm ());
+      /* If inversion requested, check first if the note is part of chord */
+      SCM s = member_pitch (inversion, pitches);
+      if (s != SCM_BOOL_F)
+       {
+         /* Then, delete and add as base note, ie: the inversion */
+         pitches = scm_delete (s, pitches);
+         Note_req* n = new Note_req;
+         n->set_mus_property ("pitch", ly_car (add_below_tonic (s, pitches)));
+         n->set_mus_property ("duration", dur);
+         n->set_mus_property ("inversion", SCM_BOOL_T);
+         list = gh_cons (n->self_scm (), list);
+         scm_gc_unprotect_object (n->self_scm ());
+       }
+      else
+       warning (_f ("invalid inversion pitch: not part of chord: %s",
+                    unsmob_pitch (inversion)->str ()));
     }
 
-  /*
-    Should add (base . #t) to the pitch that is an added base
-   */
+  /* Bass is easy, just add if requested */
   if (bass != SCM_EOL)
     {
-      Bass_req* b = new Bass_req;
-      b->set_mus_property ("pitch", bass);
-
-      l = gh_cons (b->self_scm (), l);
-      scm_unprotect_object (b->self_scm ());      
+      Note_req* n = new Note_req;
+      n->set_mus_property ("pitch", ly_car (add_below_tonic (bass, pitches)));
+      n->set_mus_property ("duration", dur);
+      n->set_mus_property ("bass", SCM_BOOL_T);
+      list = gh_cons (n->self_scm (), list);
+      scm_gc_unprotect_object (n->self_scm ());
     }
-
-  SCM pitches = Chord::to_pitches (chord);
-  for (SCM i = pitches; gh_pair_p (i); i = gh_cdr (i))
+  
+  for (SCM i = pitches; gh_pair_p (i); i = ly_cdr (i))
     {
       Note_req* n = new Note_req;
-      n->set_mus_property ("pitch", gh_car (i));
+      n->set_mus_property ("pitch", ly_car (i));
       n->set_mus_property ("duration", dur);
-      l = gh_cons (n->self_scm (), l);
-
-      scm_unprotect_object (n->self_scm ());
+      list = gh_cons (n->self_scm (), list);
+      scm_gc_unprotect_object (n->self_scm ());
     }
 
-  Simultaneous_music*v = new Request_chord (l);
+  Simultaneous_music*v = new Request_chord (SCM_EOL);
+  v->set_mus_property ("elements", list);
 
   return v;
 }