]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/part-combine-music-iterator.cc
* input/mutopia/F.Schubert/standchen.ly (trebleThrough): fixes
[lilypond.git] / lily / part-combine-music-iterator.cc
index 1f813a7445afb87b2222fc1e85689fb5980bc531..b249224f3b368ed4da4d051daf2e0b8597b19e13 100644 (file)
@@ -3,52 +3,63 @@
 
   source file of the GNU LilyPond music typesetter
   
-  (c) 2000 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 2000--2003 Jan Nieuwenhuizen <janneke@gnu.org>
  */
 
-#include "part-combine-music.hh"
 #include "part-combine-music-iterator.hh"
 #include "translator-group.hh"
-#include "musical-request.hh"
+#include "event.hh"
 #include "music-sequence.hh"
+#include "lily-guile.hh"
 #include "warn.hh"
 
+Part_combine_music_iterator::Part_combine_music_iterator ()
+{
+  first_iter_ = 0;
+  second_iter_ = 0;
+  first_until_ = 0;
+  second_until_ = 0;
+  state_ = 0;
+}
 
-/*
-  DOCUMENTME
- */
-int
-compare (Array<Duration> const * left, Array<Duration> const * right)
+void
+Part_combine_music_iterator::derived_mark () const
 {
-  assert (left);
-  assert (right);
-  
-  if (left->size () == right->size ())
-    {
-      for (int i = 0; i < left->size (); i++)
-       {
-         int r = Duration::compare ((*left)[i], (*right)[i]);
-         if (r)
-           return r;
-       }
-    }
-  else
-    return 1;
-  return 0;
+  if (first_iter_)
+    scm_gc_mark (first_iter_->self_scm());
+  if (second_iter_)
+    scm_gc_mark(second_iter_->self_scm());
 }
 
-Part_combine_music_iterator::Part_combine_music_iterator ()
+void
+Part_combine_music_iterator::do_quit ()
 {
-  first_iter_p_ = 0;
-  second_iter_p_ = 0;
-  first_until_ = 0;
-  second_until_ = 0;
+  if (first_iter_)
+    first_iter_->quit();
+  if (second_iter_)
+    second_iter_->quit();
 }
 
-Part_combine_music_iterator::~Part_combine_music_iterator ()
+Part_combine_music_iterator::Part_combine_music_iterator (Part_combine_music_iterator const &src)
+  : Music_iterator (src)
 {
-  delete second_iter_p_;
-  delete first_iter_p_;
+  first_iter_ = 0;
+  second_iter_ = 0;
+
+  if(src.first_iter_)
+    first_iter_ = src.first_iter_->clone ();
+  if (src.second_iter_)
+    second_iter_ = src.second_iter_->clone ();
+
+  first_until_ = src.first_until_;
+  second_until_ = src.second_until_;
+  state_ = src.state_;
+  suffix_ = src.suffix_;
+
+  if (first_iter_)
+    scm_gc_unprotect_object (first_iter_->self_scm());
+  if (second_iter_)
+    scm_gc_unprotect_object (second_iter_->self_scm());
 }
 
 Moment
@@ -56,34 +67,35 @@ Part_combine_music_iterator::pending_moment () const
 {
   Moment p;
   p.set_infinite (1);
-  if (first_iter_p_->ok ())
-    p = p <? first_iter_p_->pending_moment ();
+  if (first_iter_->ok ())
+    p = p <? first_iter_->pending_moment ();
 
-  if (second_iter_p_->ok ())
-    p = p <? second_iter_p_->pending_moment ();
+  if (second_iter_->ok ())
+    p = p <? second_iter_->pending_moment ();
   return p;
 }
 
 bool
 Part_combine_music_iterator::ok () const
 {
-  return first_iter_p_->ok () || second_iter_p_->ok ();
+  return first_iter_->ok () || second_iter_->ok ();
 }
 
+
 void
 Part_combine_music_iterator::construct_children ()
 {
-  Part_combine_music const * m = dynamic_cast<Part_combine_music const*> (music_l_);
-  
-  first_iter_p_ = get_iterator_p (m->first_l ());
-  second_iter_p_ = get_iterator_p (m->second_l ());
+  SCM lst = get_music ()->get_mus_property ("elements");
+
+  first_iter_ = unsmob_iterator (get_iterator (unsmob_music (gh_car (lst))));
+  second_iter_ = unsmob_iterator (get_iterator (unsmob_music (gh_cadr (lst))));
 }
 
 void
 Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
                                        String to_id)
 {
-  Translator_group * current = it->report_to_l ();
+  Translator_group * current = it->report_to ();
   Translator_group * last = 0;
 
   /*
@@ -96,13 +108,13 @@ Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
      
      If \translator Staff = bass, then look for Staff = *
    */
-  while  (current && current->type_str_ != to_type)
+  while (current && current->type_string_ != to_type)
     {
       last = current;
-      current = current->daddy_trans_l_;
+      current = current->daddy_trans_;
     }
 
-  if (current && current->id_str_ == to_id)
+  if (current && current->id_string_ == to_id)
     {
       String msg;
       msg += _ ("Can't switch translators, I'm there already");
@@ -112,9 +124,9 @@ Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
     if (last)
       {
        Translator_group * dest = 
-         it->report_to_l ()->find_create_translator_l (to_type, to_id);
-       current->remove_translator_p (last);
-       dest->add_group_translator (last);
+         it->report_to ()->find_create_translator (to_type, to_id);
+       current->remove_translator (last);
+       dest->add_used_group_translator (last);
       }
     else
       {
@@ -122,52 +134,56 @@ Part_combine_music_iterator::change_to (Music_iterator *it, String to_type,
          We could change the current translator's id, but that would make 
          errors hard to catch
          
-          last->translator_id_str_  = change_l ()->change_to_id_str_;
+          last->translator_id_string_  = get_change ()->change_to_id_string_;
        */
-       error (_f ("I'm one myself: `%s'", to_type.ch_C ()));
+       error (_f ("I'm one myself: `%s'", to_type.to_str0 ()));
       }
   else
-    error (_f ("none of these in my family: `%s'", to_id.ch_C ()));
+    error (_f ("none of these in my family: `%s'", to_id.to_str0 ()));
 }
 
 
+// SCM*, moet / kan dat niet met set_x ofzo?
 static void
-get_music_info (Moment m, Music_iterator* iter, Array<Musical_pitch> *pitches, Array<Duration> *durations)
+get_music_info (Moment m, Music_iterator* iter, SCM *pitches, SCM *durations)
 {
   if (iter->ok ())
     {
-      for (SCM i = iter->get_music (m); gh_pair_p (i); i = gh_cdr (i))
+      for (SCM i = iter->get_pending_events (m); gh_pair_p (i); i = ly_cdr (i))
        {
-         Music *m = unsmob_music (gh_car (i));
-         if (Melodic_req *r = dynamic_cast<Melodic_req *> (m))
-           pitches->push (*unsmob_pitch (r->get_mus_property("pitch")));
-         if (Rhythmic_req *r = dynamic_cast<Rhythmic_req *> (m))
-           durations->push (*unsmob_duration (r->get_mus_property("duration")));
+         Music *m = unsmob_music (ly_car (i));
+         SCM p = m->get_mus_property ("pitch");
+         SCM d = m->get_mus_property ("duration");
+         if (unsmob_pitch (p))
+           *pitches = gh_cons (p, *pitches);
+         if (unsmob_duration (d))
+           *durations = gh_cons (d, *durations);
        }
     }
 }
-  
+
 int
 Part_combine_music_iterator::get_state (Moment)
 {
   int state = UNKNOWN;
-  Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (music_l_);
+  
+  Music *p = get_music ();
 
   String w = ly_scm2string (p->get_mus_property ("what"));
     
   
-  Translator_group *first_translator = first_iter_p_->report_to_l ()->find_create_translator_l (w, "one" + suffix_);
+  Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
 
-  SCM s = first_translator->get_property (ly_symbol2scm ("changeMoment"));
+  SCM s = first_translator->get_property ("changeMoment");
   if (!gh_pair_p (s))
     return state;
 
-  Moment change_mom = *unsmob_moment (gh_car (s));
-  Moment diff_mom = *unsmob_moment (gh_cdr (s));
+  Moment change_mom = *unsmob_moment (ly_car (s));
+  Moment diff_mom = *unsmob_moment (ly_cdr (s));
   
   Moment now = pending_moment ();
 
-  if (!now.mod_rat (change_mom))
+  if (!now.main_part_.mod_rat (change_mom.main_part_))
     {
       SCM interval = SCM_BOOL_F;
       if (first_until_ < now)
@@ -179,10 +195,11 @@ Part_combine_music_iterator::get_state (Moment)
       Moment second_mom = second_until_;
       Moment diff_until = diff_mom + now;
 
-      bool first = true;
-      Music_iterator *first_iter = first_iter_p_->clone ();
-      Music_iterator *second_iter = second_iter_p_->clone ();
 
+      bool first = true;
+      Music_iterator *first_iter = first_iter_->clone ();
+      Music_iterator *second_iter = second_iter_->clone ();
+      
       Moment last_pending (-1);
       Moment pending = now;
       while (now < diff_until
@@ -199,104 +216,110 @@ Part_combine_music_iterator::get_state (Moment)
            pending = first_iter->pending_moment () <? second_iter->pending_moment ();
          last_pending = pending;
 
-         Array<Musical_pitch> first_pitches;
-         Array<Duration> first_durations;
-         get_music_info (pending, first_iter, &first_pitches, &first_durations);
+         SCM first_pitches = SCM_EOL;
+         SCM first_durations = SCM_EOL;
+         get_music_info (pending, first_iter,
+                         &first_pitches, &first_durations);
       
-         Array<Musical_pitch> second_pitches;
-         Array<Duration> second_durations;
-         get_music_info (pending, second_iter, &second_pitches, &second_durations);
+         SCM second_pitches = SCM_EOL;
+         SCM second_durations = SCM_EOL;
+         get_music_info (pending, second_iter,
+                         &second_pitches, &second_durations);
 
-         if (first_pitches.size () && second_pitches.size ())
+         if (first_pitches != SCM_EOL && second_pitches != SCM_EOL)
            {
-             first_pitches.sort (Musical_pitch::compare);
-             second_pitches.sort (Musical_pitch::compare);
-             interval = gh_int2scm (first_pitches.top ().steps ()
-                                    - second_pitches[0].steps ());
+             scm_sort_list_x (first_pitches, Pitch::less_p_proc);
+             scm_sort_list_x (second_pitches, Pitch::less_p_proc);
+
+             interval = gh_int2scm (unsmob_pitch (ly_car (first_pitches))->steps ()
+                                    - unsmob_pitch (ly_car (scm_last_pair (second_pitches)))->steps ());
            }
-         if (first_durations.size ())
+
+         if (first_durations != SCM_EOL)
            {
-             first_durations.sort (Duration::compare);
-             first_mom += first_durations.top ().length_mom ();
+             scm_sort_list_x (first_durations,
+                              Duration::less_p_proc);
+             first_mom += unsmob_duration (ly_car (first_durations))->get_length ();
            }
-
-         if (second_durations.size ())
+         
+         if (second_durations != SCM_EOL)
            {
-             second_durations.sort (Duration::compare);
-             second_mom += second_durations.top ().length_mom ();
+             scm_sort_list_x (second_durations,
+                              Duration::less_p_proc);
+             second_mom += unsmob_duration (ly_car (second_durations))->get_length ();
            }
-
-         if (!first_pitches.empty () && second_pitches.empty ()
-              && !(second_until_ > now))
+         
+         if (first_pitches != SCM_EOL && second_pitches == SCM_EOL
+                 && ! (second_until_ > now))
            {
              state |= UNRELATED;
              state &= ~UNISILENCE;
-             if (!(state & ~(UNRELATED | SOLO1 | UNISILENCE)))
+             if (! (state & ~ (UNRELATED | SOLO1 | UNISILENCE)))
                state |= SOLO1;
            }
          else
            state &= ~SOLO1;
 
-         if (first_pitches.empty () && !second_pitches.empty ()
-             && !(first_until_ > now))
+         if (first_pitches == SCM_EOL && second_pitches != SCM_EOL
+             && ! (first_until_ > now))
            {
              state |= UNRELATED;
              state &= ~UNISILENCE;
-             if (!(state & ~(UNRELATED | SOLO2 | UNISILENCE)))
+             if (! (state & ~ (UNRELATED | SOLO2 | UNISILENCE)))
                state |= SOLO2;
            }
          else
            state &= ~SOLO2;
 
-         if (!compare (&first_durations, &second_durations))
+         if (gh_equal_p (first_durations, second_durations))
            {
              state &= ~UNISILENCE;
-             if (!(state & ~(UNIRHYTHM | UNISON)))
+             if (! (state & ~ (UNIRHYTHM | UNISON)))
                state |= UNIRHYTHM;
            }
          else
-           state &= ~(UNIRHYTHM | UNISILENCE);
+           state &= ~ (UNIRHYTHM | UNISILENCE);
          
-         if (!first_pitches.empty ()
-             &&!compare (&first_pitches, &second_pitches))
+         if (first_pitches != SCM_EOL
+             && gh_equal_p (first_pitches, second_pitches))
            {
              state &= ~UNISILENCE;
-             if (!(state & ~(UNIRHYTHM | UNISON)))
+             if (! (state & ~ (UNIRHYTHM | UNISON)))
                state |= UNISON;
            }
          else
-           state &= ~(UNISON);
+           state &= ~ (UNISON);
            
-         if (first_pitches.empty () && second_pitches.empty ())
+         if (first_pitches == SCM_EOL && second_pitches == SCM_EOL)
            {
-             if (!(state & ~(UNIRHYTHM | UNISILENCE)))
+             if (! (state & ~ (UNIRHYTHM | UNISILENCE)))
                state |= UNISILENCE;
            }
          else if (!state)
            state |= UNRELATED;
          else
-           state &= ~(UNISILENCE);
+           state &= ~ (UNISILENCE);
 
          if (gh_number_p (interval))
            {
-             SCM s = first_translator->get_property (ly_symbol2scm ("splitInterval"));
+             SCM s = first_translator->get_property ("splitInterval");
              int i = gh_scm2int (interval);
              if (gh_pair_p (s)
-                 && gh_number_p (gh_car (s))
-                 && gh_number_p (gh_cdr (s))
-                 && i >= gh_scm2int (gh_car (s))
-                 && i <= gh_scm2int (gh_cdr (s)))
+                 && gh_number_p (ly_car (s))
+                 && gh_number_p (ly_cdr (s))
+                 && i >= gh_scm2int (ly_car (s))
+                 && i <= gh_scm2int (ly_cdr (s)))
                {
-                 if (!(state & ~(SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
+                 if (! (state & ~ (SPLIT_INTERVAL | UNIRHYTHM | UNISON)))
                    state |= SPLIT_INTERVAL;
                }
              else
-               state &= ~(SPLIT_INTERVAL);
+               state &= ~ (SPLIT_INTERVAL);
            }
 
-         if (first && !first_pitches.empty ())
+         if (first && first_pitches != SCM_EOL)
            first_until_ = first_mom;
-         if (first && !second_pitches.empty ())
+         if (first && second_pitches != SCM_EOL)
            second_until_ = second_mom;
          first = false;
 
@@ -306,13 +329,14 @@ Part_combine_music_iterator::get_state (Moment)
            second_iter->skip (pending);
          now = pending;
        }
-      delete first_iter;
-      delete second_iter;
+      scm_gc_unprotect_object (first_iter->self_scm ());
+      scm_gc_unprotect_object (second_iter->self_scm ());
     }
+
   return state;
 }
 
-static Span_req* abort_req = NULL;
+static Music* abort_req = NULL;
 
 void
 Part_combine_music_iterator::process (Moment m)
@@ -326,13 +350,14 @@ Part_combine_music_iterator::process (Moment m)
 
       **** Tried this, but won't work:
 
-      Consider thread switching: threads "one", "two" and "both".
+s      Consider thread switching: threads "one", "two" and "both".
       User can't pre-set the (most important) stem direction at
       thread level!
    */
  
   if (suffix_.empty_b ())
-    suffix_ = first_iter_p_->report_to_l ()->daddy_trans_l_->id_str_.cut_str (3, INT_MAX);
+    suffix_ = first_iter_->report_to ()
+      ->daddy_trans_->id_string_.cut_string (3, INT_MAX);
 
   int state = get_state (m);
   if (state)
@@ -340,15 +365,15 @@ Part_combine_music_iterator::process (Moment m)
   else
     state = state_;
   
-  Part_combine_music const *p = dynamic_cast<Part_combine_music const* > (music_l_);
+  Music *p =get_music ();
 
 
-  bool previously_combined_b = first_iter_p_->report_to_l ()->daddy_trans_l_
-    == second_iter_p_->report_to_l ()->daddy_trans_l_;
+  bool previously_combined_b = first_iter_->report_to ()->daddy_trans_
+    == second_iter_->report_to ()->daddy_trans_;
 
   bool combine_b = previously_combined_b;
 
-  if (!(state & UNIRHYTHM)
+  if (! (state & UNIRHYTHM)
       || (state & SPLIT_INTERVAL)
       || (state & (SOLO1 | SOLO2)))
     combine_b = false;
@@ -361,26 +386,29 @@ Part_combine_music_iterator::process (Moment m)
 
   if (!abort_req)
     {
-      abort_req = new Span_req;
-      abort_req->set_mus_property ("span-type", ly_str02scm ("abort"));
+      abort_req = make_music_by_name (ly_symbol2scm ("AbortEvent"));
     }
   
   if (combine_b && combine_b != previously_combined_b)
     {
-      if (second_iter_p_ && second_iter_p_->ok ())
-       second_iter_p_->try_music (abort_req);
+      if (second_iter_ && second_iter_->ok ())
+       second_iter_->try_music (abort_req);
      }
   String w = ly_scm2string (p->get_mus_property ("what"));
   if (combine_b != previously_combined_b)
-    change_to (second_iter_p_, w, (combine_b ? "one" : "two")
+    change_to (second_iter_, w, (combine_b ? "one" : "two")
               + suffix_);
+  
+  Translator_group *first_translator = first_iter_->report_to ()->find_create_translator (w, "one" + suffix_);
+  Translator_group *second_translator = second_iter_->report_to ()->find_create_translator (w, "two" + suffix_);
+  
 
-  Translator_group *first_translator = first_iter_p_->report_to_l ()->find_create_translator_l (w, "one" + suffix_);
-  Translator_group *second_translator = second_iter_p_->report_to_l ()->find_create_translator_l (w, "two" + suffix_);
-
-  /*
-    hmm
-   */
+  /* Hmm */
+  first_translator->set_property ("combineParts", SCM_BOOL_T);
+  second_translator ->set_property ("combineParts", SCM_BOOL_T);
+  /* hmm */
   SCM b = (state & UNIRHYTHM) ? SCM_BOOL_T : SCM_BOOL_F;
   first_translator->set_property ("unirhythm", b);
   second_translator->set_property ("unirhythm", b);
@@ -404,33 +432,37 @@ Part_combine_music_iterator::process (Moment m)
   first_translator->set_property ("solo", b1);
   second_translator->set_property ("solo", b2);
 
-  if (first_iter_p_->ok ())
-    first_iter_p_->process (m);
+  /* Can't these be computed? */
+  first_translator->set_property ("othersolo", b2);
+  second_translator->set_property ("othersolo", b1);
+
+  if (first_iter_->ok ())
+    first_iter_->process (m);
   
-  if (second_iter_p_->ok ())
-    second_iter_p_->process (m);
+  if (second_iter_->ok ())
+    second_iter_->process (m);
 }
 
 Music_iterator*
 Part_combine_music_iterator::try_music_in_children (Music *m) const
 {
-  Music_iterator * i =  first_iter_p_->try_music (m);
+  Music_iterator * i =  first_iter_->try_music (m);
   if (i)
     return i;
   else
-    return second_iter_p_->try_music (m);
+    return second_iter_->try_music (m);
 }
 
 
 SCM
-Part_combine_music_iterator::get_music (Moment m)const
+Part_combine_music_iterator::get_pending_events (Moment m)const
 {
   SCM s = SCM_EOL;
-  if (first_iter_p_)
-    s = gh_append2 (s,first_iter_p_->get_music (m));
-  if (second_iter_p_)
-    s = gh_append2 (second_iter_p_->get_music (m),s);
+  if (first_iter_)
+    s = gh_append2 (s,first_iter_->get_pending_events (m));
+  if (second_iter_)
+    s = gh_append2 (second_iter_->get_pending_events (m),s);
   return s;
 }
 
-IMPLEMENT_CTOR_CALLBACK(Part_combine_music_iterator);
+IMPLEMENT_CTOR_CALLBACK (Part_combine_music_iterator);