]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/chord-tremolo-engraver.cc
*** empty log message ***
[lilypond.git] / lily / chord-tremolo-engraver.cc
index 769ec5a3fcba7d7bb50500326e0fe75541d14782..efc8e74a05aed28ffb57c77fb5ba02c3c06246b4 100644 (file)
-/*
-  chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
-
-  source file of the GNU LilyPond music typesetter
-
-  (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
-           Jan Nieuwenhuizen <janneke@gnu.org>
+/*   
+     new-chord-tremolo-engraver.cc --  implement Chord_tremolo_engraver
+  
+     source file of the GNU LilyPond music typesetter
+  
+     (c) 2000--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  
 */
 
-#include "timing-translator.hh"
-#include "stem.hh"
 #include "beam.hh"
-#include "musical-request.hh"
-#include "misc.hh"
+#include "repeated-music.hh"
+#include "stem.hh"
+#include "rhythmic-head.hh"
+#include "engraver-group-engraver.hh"
 #include "warn.hh"
-#include "score-engraver.hh"
-#include "engraver.hh"
-#include "drul-array.hh"
-#include "timing-engraver.hh"
-#include "beaming.hh"
+#include "misc.hh"
+#include "note-head.hh"
+#include "spanner.hh"
+#include "item.hh"
+#include "chord-tremolo-iterator.hh"
+#include "stem-tremolo.hh"
+#include "math.h"           // ceil
 
 /**
-  Generate a beam tremolo.  Eat stems.
 
-  UGH. Derive me from Beam_engraver.
-  
- */
-class Chord_tremolo_engraver : public Engraver
-{
-public:
-  VIRTUAL_COPY_CONS(Translator);
-  
+This acknowledges repeated music with "tremolo" style.  It typesets
+a beam.
 
-  Chord_tremolo_engraver();
+TODO:
 
-protected:
-  virtual void do_removal_processing();
-  virtual void do_process_music();
-  virtual bool do_try_music (Music*);
-  virtual void acknowledge_element (Score_element_info);
-  virtual void do_pre_move_processing();
-  virtual void do_post_move_processing();
-
-private:
+- perhaps use engraver this to steer other engravers? That would
+create dependencies between engravers, which is bad.
+
+- create dots if appropriate.
+
+- create TremoloBeam iso Beam?
+
+*/
+class Chord_tremolo_engraver : public Engraver
+{
   void typeset_beam ();
-  Drul_array<Chord_tremolo_req*> reqs_drul_;
-  Chord_tremolo_req* prev_start_req_;
-  Beam* beam_p_;
-  Beam* finished_beam_p_;
+  TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
+protected:
+  Music * repeat_;
 
+  /// moment (global time) where beam started.
+  Moment start_mom_;
+  Moment stop_mom_;
+  int flags_ ;
+  int total_duration_flags_;
+  
   /// location  within measure where beam started.
   Moment beam_start_location_;
 
-  /// moment (global time) where beam started.
-  Moment beam_start_mom_;
-  Beaming_info_list * beam_info_p_;
-  Beaming_info_list * finished_beam_info_p_;  
+  bool body_is_sequential_;
+  Spanner * beam_;
+  Spanner * finished_beam_;
+  Item * stem_tremolo_;
+protected:
+  virtual void finalize ();
+  virtual bool try_music (Music*);
+  virtual void acknowledge_grob (Grob_info);
+  virtual void stop_translation_timestep ();
+  virtual void start_translation_timestep ();
+  virtual void process_music ();
 };
 
-ADD_THIS_TRANSLATOR (Chord_tremolo_engraver);
-
 Chord_tremolo_engraver::Chord_tremolo_engraver ()
 {
-  reqs_drul_[LEFT] = reqs_drul_[RIGHT] = 0;
-  beam_p_ = 0;
-  finished_beam_p_ = 0;
-  prev_start_req_ = 0;
-  finished_beam_info_p_=0;
-  beam_info_p_ =0;
+  beam_  = finished_beam_ = 0;
+  repeat_ = 0;
+  flags_ = 0;
+  stem_tremolo_ = 0;
+  body_is_sequential_ = false;
 }
 
 bool
-Chord_tremolo_engraver::do_try_music (Music* m)
+Chord_tremolo_engraver::try_music (Music * m)
 {
-  if (Chord_tremolo_req* b = dynamic_cast <Chord_tremolo_req *> (m))
+  if (m->is_mus_type ("repeated-music")
+      && m->get_property ("iterator-ctor") == Chord_tremolo_iterator::constructor_proc
+      && !repeat_) 
     {
-      Direction d = b->span_dir_;
-      if (reqs_drul_[d] && !reqs_drul_[d]->equal_b (b))
-       return false;
+      Moment l = m->get_length ();
+      repeat_ = m;
+      start_mom_ = now_mom ();
+      stop_mom_ = start_mom_ + l;
 
-      if ((d == STOP) && !beam_p_)
+
+      Music *body = Repeated_music::body (m);
+      body_is_sequential_ = body->is_mus_type ("sequential-music");
+
+      int elt_count = body_is_sequential_ ? scm_ilength (body->get_property ("elements")) : 1;
+
+      if (body_is_sequential_ && elt_count != 2)
        {
-         m->warning (_ ("can't find start of chord tremolo"));
-         return false;
+         m->origin ()->warning (_f ("Chord tremolo with %d elements. Must have two elements.", elt_count));
        }
 
-      reqs_drul_[d] = b;
+      if (elt_count <= 0)
+       elt_count = 1;
+         
+      Rational total_dur = l.main_part_;
+      Rational note_dur = total_dur / Rational (elt_count * Repeated_music::repeat_count (repeat_));
+
+      total_duration_flags_ = 0 >? (intlog2 (total_dur.den ()) - 2);
+      
+      flags_ = intlog2 (note_dur.den ()) -2 ;
+      
       return true;
     }
 
@@ -92,147 +114,109 @@ Chord_tremolo_engraver::do_try_music (Music* m)
 }
 
 void
-Chord_tremolo_engraver::do_process_music ()
+Chord_tremolo_engraver::process_music ()
 {
-  if (reqs_drul_[STOP])
+  if (repeat_ && body_is_sequential_ && !beam_)
     {
-      if (!beam_p_)
-       reqs_drul_[STOP]->warning (_ ("can't find start of chord tremolo"));
-
-      prev_start_req_ = 0;
-
-      finished_beam_p_ = beam_p_;
-      beam_p_ = 0;
-
-      finished_beam_info_p_ = beam_info_p_;
-      beam_info_p_ = 0;
-    }
+      beam_ = make_spanner ("Beam", repeat_->self_scm ());
+      beam_->set_property ("chord-tremolo", SCM_BOOL_T);
 
-  if (beam_p_)
-    {
-      Score_engraver * e = 0;
-      Translator * t  =  daddy_grav_l ();
-      for (; !e && t;  t = t->daddy_trans_l_)
-       {
-         e = dynamic_cast<Score_engraver*> (t);
-       }
-      
-      if (!e)
-       programming_error ("No score engraver!");
-      else
-       e->forbid_breaks ();
+      beam_start_location_ = robust_scm2moment (get_property ("measurePosition"), Moment (0));
     }
-
-  if (reqs_drul_[START])
-    {
-      if (beam_p_)
-       {
-         reqs_drul_[START]->warning (_ ("already have a chord tremolo"));
-         return;
-       }
-
-      prev_start_req_ = reqs_drul_[START];
-
-      beam_p_ = new Beam;
-      beam_p_->set_elt_property ("chord-tremolo", SCM_BOOL_T);
-      Translator * t  = daddy_grav_l  ()->get_simple_translator ("Timing_engraver");
-      Timing_engraver *timer = dynamic_cast<Timing_engraver*> (t);
-      beam_start_location_ = (t) ?  timer->measure_position () : Moment (0);
-      beam_start_mom_ = now_mom();
-      beam_info_p_ = new Beaming_info_list;
-      
-      announce_element (Score_element_info (beam_p_, reqs_drul_[LEFT]));
-  }
 }
 
 void
-Chord_tremolo_engraver::do_post_move_processing ()
+Chord_tremolo_engraver::finalize ()
 {
-  reqs_drul_ [START] = 0;
+  typeset_beam ();
+  if (beam_)
+    {
+      repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
+      beam_->suicide ();
+    }
 }
 
-
 void
-Chord_tremolo_engraver::do_pre_move_processing ()
+Chord_tremolo_engraver::typeset_beam ()
 {
-  typeset_beam ();
+  finished_beam_ = 0;
 }
 
 void
-Chord_tremolo_engraver::typeset_beam ()
+Chord_tremolo_engraver::acknowledge_grob (Grob_info info)
 {
-  if (finished_beam_p_)
+  if (beam_ && Stem::has_interface (info.grob_))
     {
-      finished_beam_info_p_->beamify ();
-      finished_beam_p_->set_beaming (finished_beam_info_p_);
-      typeset_element (finished_beam_p_);
-      finished_beam_p_ = 0;
-      delete finished_beam_info_p_;
-      finished_beam_info_p_ =0;
-
-      reqs_drul_[STOP] = 0;
+      Grob * s = info.grob_;
+
+      if (start_mom_ == now_mom ())
+       Stem::set_beaming (s, flags_, RIGHT);
+      else
+       Stem::set_beaming (s, flags_, LEFT);
+         
+      if (Stem::duration_log (s) != 1)
+       {
+         beam_->set_property ("gap-count", scm_int2num (flags_ - total_duration_flags_));
+       }
+
+      if (info.music_cause ()->is_mus_type ("rhythmic-event"))
+       {
+         Beam::add_stem (beam_, s);
+       }
+      else
+       {
+         String s = _ ("stem must have Rhythmic structure");
+         if (info.music_cause ())
+           info.music_cause ()->origin ()->warning (s);
+         else
+           ::warning (s);
+       }
+    }
+  else if (repeat_ &&
+          flags_ && !body_is_sequential_ && Stem::has_interface (info.grob_))
+    {
+      stem_tremolo_ = make_item ("StemTremolo", repeat_->self_scm ());
+      stem_tremolo_->set_property ("flag-count",
+                                  scm_int2num (flags_));
+      stem_tremolo_->set_property ("stem",
+                                  info.grob_->self_scm ());
+      stem_tremolo_->set_parent (info.grob_, X_AXIS);
     }
 }
 
+
 void
-Chord_tremolo_engraver::do_removal_processing ()
+Chord_tremolo_engraver::start_translation_timestep ()
 {
-  typeset_beam ();
-  if (beam_p_)
+  if (beam_ && stop_mom_ == now_mom ())
     {
-      prev_start_req_->warning (_ ("unterminated chord tremolo"));
-      finished_beam_p_ = beam_p_;
-      finished_beam_info_p_ = beam_info_p_;
-      typeset_beam ();
+      finished_beam_ = beam_;
+      repeat_ = 0;
+      beam_ = 0;
     }
 }
 
+
 void
-Chord_tremolo_engraver::acknowledge_element (Score_element_info info)
+Chord_tremolo_engraver::stop_translation_timestep ()
 {
-  if (beam_p_)
+  if (stem_tremolo_)
     {
-      if (Stem* s = dynamic_cast<Stem *> (info.elem_l_))
-       {
-         int type_i = prev_start_req_->type_i_;
-         s->set_elt_property ("duration-log",  gh_int2scm (intlog2 (type_i) - 2));
-
-         s->set_beaming (s->flag_i (), LEFT);
-         s->set_beaming ( s->flag_i (), RIGHT);
-         
-         /*
-           URG: this sets the direction of the Stem s.
-           It's amazing Mike:
-           
-             Stem:: type_i () ->first_head ()->get_direction () ->
-                     directional_element (me).set (d);
-           
-          */
-         SCM d = s->get_elt_property ("direction");
-         if (s->type_i () != 1)
-           {
-             int gap_i =s->flag_i () - ((s->type_i () >? 2) - 2);
-             beam_p_->set_elt_property ("beam-gap", gh_int2scm(gap_i));
-           }
-         s->set_elt_property ("direction", d);
-
-         if (Rhythmic_req* r = dynamic_cast <Rhythmic_req *> (info.req_l_))
-           {
-             beam_p_->add_stem (s);
-             Moment stem_location = now_mom () -
-               beam_start_mom_ + beam_start_location_;
-             beam_info_p_->add_stem (stem_location,
-                                     (r->duration_.durlog_i_ - 2) >? 1);
-           }
-         else
-           {
-             String s = _ ("stem must have Rhythmic structure");
-             if (info.req_l_)
-               info.req_l_->warning (s);
-             else
-               ::warning (s);
-           }
-       }
+      repeat_ = 0;
+      if (beam_)
+       programming_error ("Huh, beam and stem tremolo?");
+      stem_tremolo_ = 0;
     }
+
+  typeset_beam ();
 }
 
+
+
+ADD_TRANSLATOR (Chord_tremolo_engraver,
+/* descr */       "Generates beams for  tremolo repeats.",
+/* creats*/       "Beam",
+/* accepts */     "repeated-music",
+/* acks  */      "stem-interface note-head-interface",
+/* reads */       "",
+/* write */       "");