]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/repeated-music.cc
release: 1.2.14
[lilypond.git] / lily / repeated-music.cc
index e34a5c8da64606079c1aaa11d0cc74bb02f670e4..f7cfefac53022388397f8d5d04ab4cfa55d0dfe0 100644 (file)
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1998 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
   
  */
 
 #include "repeated-music.hh"
+#include "music-list.hh"
 #include "musical-pitch.hh"
+#include "debug.hh"
 
-Repeated_music::Repeated_music (Music* r, int n, Music_sequence* a)
+Repeated_music::Repeated_music(Music *beg, int times, Music_sequence * alts)
 {
-  repeats_i_ = n;
-  unfold_b_ = false;
-  repeat_p_ = r;
-  alternative_p_ = a;
+  repeat_body_p_ = beg;
+  fold_b_ = false;
+  repeats_i_ = times;
+  alternatives_p_ = alts;
+  volta_fold_b_ = true;
+  if (alts)
+    alts->music_p_list_p_->truncate (times);
 }
 
-Repeated_music::~Repeated_music ()
+Repeated_music::Repeated_music (Repeated_music const &s)
+  : Music (s)
 {
-  delete repeat_p_;
-  delete alternative_p_;
+  repeats_i_ = s.repeats_i_;
+  fold_b_ = s.fold_b_;
+  volta_fold_b_ = s.volta_fold_b_;
+  
+  repeat_body_p_ = s.repeat_body_p_ ? s.repeat_body_p_->clone () : 0;
+  alternatives_p_ = s.alternatives_p_
+    ? dynamic_cast<Music_sequence*> (s.alternatives_p_->clone ()):0;
 }
 
-Repeated_music::Repeated_music (Repeated_music const& s)
-  : Music (s)
+Repeated_music::~Repeated_music ()
 {
-  repeat_p_ = (s.repeat_p_) ? s.repeat_p_->clone () : 0;
-  // urg?
-  alternative_p_ = (s.alternative_p_) ? dynamic_cast <Music_sequence*> (s.alternative_p_->clone ()) : 0;
+  delete repeat_body_p_;
+  delete alternatives_p_;
 }
 
 void
 Repeated_music::do_print () const
 {
-  if (repeat_p_)
-    repeat_p_->print ();
-  if (alternative_p_)
-    alternative_p_->print ();
+#ifndef NPRINT
+  DEBUG_OUT << "Fold = " << fold_b_ << " reps: " << repeats_i_;
+
+  if (repeat_body_p_)
+    repeat_body_p_->print();
+  
+  if (alternatives_p_)
+    alternatives_p_->print();
+#endif
+}
+
+Musical_pitch
+Repeated_music::to_relative_octave (Musical_pitch p)
+{
+  if (repeat_body_p_)
+    p = repeat_body_p_->to_relative_octave (p);
+
+  if (alternatives_p_)
+    p = alternatives_p_->do_relative_octave (p, false);
+  return p;
 }
 
+
+
 void
 Repeated_music::transpose (Musical_pitch p)
 {
-  if (repeat_p_)
-    repeat_p_->transpose (p);
-  if (alternative_p_)
-    alternative_p_->transpose (p);
+  if (repeat_body_p_)
+    repeat_body_p_->transpose (p);
+
+  if (alternatives_p_)
+    alternatives_p_->transpose (p);  
 }
 
-Moment
-Repeated_music::length_mom () const
+void
+Repeated_music::compress (Moment p)
 {
-  Moment m;
-  if (repeat_p_)
-    m += repeat_p_->length_mom ();
-  if (alternative_p_)
-    m += alternative_p_->length_mom ();
-  return m;
-}
+  if (repeat_body_p_)
+    repeat_body_p_->compress (p);
 
+  if (alternatives_p_)
+    alternatives_p_->compress (p);  
+}
 
-Musical_pitch
-Repeated_music::to_relative_octave (Musical_pitch p)
+Moment
+Repeated_music::alternatives_length_mom () const
 {
-  p = repeat_p_->to_relative_octave (p);
-
-  p = alternative_p_->do_relative_octave (p, false); 
-  return p;
+  if (!alternatives_p_ )
+    return 0;
   
-  /* ugh.  Should 
-     \relative c'' \repeat 2  { c4 } { < ... > }
+  if  (fold_b_)
+    return alternatives_p_->maximum_length ();
 
-     and 
-     
-     \relative c'' \repeat 2  { c4 }
-     { { ...} }
+  Moment m =0;
+  int done =0;
+  Cons<Music> *p = alternatives_p_->music_p_list_p_->head_;
+  while (p && done < repeats_i_)
+    {
+      m = m + p->car_->length_mom ();
+      done ++;
+      if (volta_fold_b_
+         || repeats_i_ - done < alternatives_p_->length_i ())
+       p = p->next_;
+    }
+  return m;
+}
 
-     behave differently?
-   */
+Moment
+Repeated_music::length_mom () const
+{
+  Moment m =0;
+  if (fold_b_)
+    {
+      if (repeat_body_p_)
+       m += repeat_body_p_->length_mom ();
+    }
+  else
+    {
+      Moment beg = (repeat_body_p_) ? repeat_body_p_->length_mom () : Rational(0);
+      if (!volta_fold_b_)
+       beg *=  Rational (repeats_i_);
+      m += beg;
+    }
+
+  m += alternatives_length_mom ();
+  return m;
 }
+