]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/dynamic-performer.cc
Run `make grand-replace'.
[lilypond.git] / lily / dynamic-performer.cc
index 71ce23fb7415fd9b39b130aa4655da7a3eb79bac..c80d64111ee1dad5dd034b07408d6881dc757d44 100644 (file)
 
   source file of the GNU LilyPond music typesetter
 
-  (c) 2000--2005 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 2000--2008 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "performer.hh"
 #include "audio-item.hh"
+#include "stream-event.hh"
 
-/*
-  TODO:
-
-  handle multiple events
+#include "translator.icc"
 
-  perform absolute (text) dynamics
-*/
 class Dynamic_performer : public Performer
 {
 public:
   TRANSLATOR_DECLARATIONS (Dynamic_performer);
 protected:
-  virtual bool try_music (Music *req);
-  virtual void stop_translation_timestep ();
-  virtual void create_audio_elements ();
+  void stop_translation_timestep ();
+  void process_music ();
+  Real equalize_volume (Real);
 
+  DECLARE_TRANSLATOR_LISTENER (decrescendo);
+  DECLARE_TRANSLATOR_LISTENER (crescendo);
+  DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
 private:
-  Music *script_req_;
-  Audio_dynamic *audio_;
+  Stream_event *script_event_;
+  Drul_array<Stream_event*> span_events_; 
+  Drul_array<Direction> grow_dir_; 
+  Real last_volume_;
+  Audio_dynamic *absolute_;
+  Audio_span_dynamic *span_dynamic_;
+  Audio_span_dynamic *finished_span_dynamic_;
 };
 
 Dynamic_performer::Dynamic_performer ()
 {
-  script_req_ = 0;
-  audio_ = 0;
+  last_volume_ = 0.5;
+  script_event_ = 0;
+  absolute_ = 0;
+  span_events_[LEFT] = 
+    span_events_[RIGHT] = 0;
+  span_dynamic_ = 0;
+  finished_span_dynamic_ = 0;
 }
 
-void
-Dynamic_performer::create_audio_elements ()
+Real
+Dynamic_performer::equalize_volume (Real volume)
 {
-  if (script_req_)
+  /*
+    properties override default equaliser setting
+  */
+  SCM min = get_property ("midiMinimumVolume");
+  SCM max = get_property ("midiMaximumVolume");
+  if (scm_is_number (min) || scm_is_number (max))
     {
-      SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
+      Interval iv (0, 1);
+      if (scm_is_number (min))
+       iv[MIN] = scm_to_double (min);
+      if (scm_is_number (max))
+       iv[MAX] = scm_to_double (max);
+      volume = iv[MIN] + iv.length () * volume;
+    }
+  else
+    {
+      /*
+       urg, code duplication:: staff_performer
+      */
+      SCM s = get_property ("midiInstrument");
 
-      SCM svolume = SCM_EOL;
-      if (ly_c_procedure_p (proc))
-       {
-         // urg
-         svolume = scm_call_1 (proc, script_req_->get_property ("text"));
-       }
+      if (!scm_is_string (s))
+       s = get_property ("instrumentName");
 
-      Real volume = robust_scm2double (svolume, 0.5);
+      if (!scm_is_string (s))
+       s = scm_from_locale_string ("piano");
 
-      /*
-       properties override default equaliser setting
-      */
-      SCM min = get_property ("midiMinimumVolume");
-      SCM max = get_property ("midiMaximumVolume");
-      if (scm_is_number (min) || scm_is_number (max))
+      SCM eq = get_property ("instrumentEqualizer");
+      if (ly_is_procedure (eq))
+       s = scm_call_1 (eq, s);
+
+      if (is_number_pair (s))
        {
-         Interval iv (0, 1);
-         if (scm_is_number (min))
-           iv[MIN] = scm_to_double (min);
-         if (scm_is_number (max))
-           iv[MAX] = scm_to_double (max);
+         Interval iv = ly_scm2interval (s);
          volume = iv[MIN] + iv.length () * volume;
        }
-      else
-       {
-         /*
-           urg, code duplication:: staff_performer
-         */
-         SCM s = get_property ("midiInstrument");
+    }
+  return volume;
+}
 
-         if (!scm_is_string (s))
-           s = get_property ("instrument");
 
-         if (!scm_is_string (s))
-           s = scm_makfrom0str ("piano");
+void
+Dynamic_performer::process_music ()
+{
+  if (span_events_[STOP] || script_event_)
+    {
+      finished_span_dynamic_ = span_dynamic_;
+      span_dynamic_ = 0;
+    }
 
-         SCM eq = get_property ("instrumentEqualizer");
-         if (ly_c_procedure_p (eq))
-           {
-             s = scm_call_1 (eq, s);
-           }
+  if (span_events_[START])
+    {
+      span_dynamic_ = new Audio_span_dynamic ();
+      announce_element (Audio_element_info (span_dynamic_, span_events_[START]));
+
+      span_dynamic_->grow_dir_ = grow_dir_[START];
+    }
+
+  if (script_event_
+      || span_dynamic_
+      || finished_span_dynamic_)
+    {
+      absolute_ = new Audio_dynamic ();
+
+      if (script_event_)
+       {
+         SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
 
-         if (is_number_pair (s))
+         SCM svolume = SCM_EOL;
+         if (ly_is_procedure (proc))
            {
-             Interval iv = ly_scm2interval (s);
-             volume = iv[MIN] + iv.length () * volume;
+             // urg
+             svolume = scm_call_1 (proc, script_event_->get_property ("text"));
            }
-       }
 
-      audio_ = new Audio_dynamic (volume);
-      Audio_element_info info (audio_, script_req_);
+         Real volume = robust_scm2double (svolume, 0.5);
+
+         last_volume_
+           = absolute_->volume_ = equalize_volume (volume);
+       }
+      
+      Audio_element_info info (absolute_, script_event_);
       announce_element (info);
-      script_req_ = 0;
     }
+
+
+  if (span_dynamic_)
+    span_dynamic_->add_absolute (absolute_);
+
+  if (finished_span_dynamic_)
+    finished_span_dynamic_->add_absolute (absolute_);
 }
 
 void
 Dynamic_performer::stop_translation_timestep ()
 {
-  if (audio_)
+  if (finished_span_dynamic_)
+    {
+      finished_span_dynamic_->render ();
+      finished_span_dynamic_ = 0;
+    }
+  
+  if (absolute_ && absolute_->volume_ < 0)
+    {
+      absolute_->volume_ = last_volume_;
+    }
+  else if (absolute_)
     {
-      play_element (audio_);
-      audio_ = 0;
+      last_volume_ = absolute_->volume_;
     }
+  
+  absolute_ = 0;
+  script_event_ = 0;
+  span_events_[LEFT] = 
+    span_events_[RIGHT] = 0;
 }
 
-bool
-Dynamic_performer::try_music (Music *r)
+IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, decrescendo);
+void
+Dynamic_performer::listen_decrescendo (Stream_event *r)
 {
-  if (!script_req_)
-    {
-      if (r->is_mus_type ("absolute-dynamic-event")) // fixme.
-       {
-         script_req_ = r;
-         return true;
-       }
-    }
-  return false;
+  Direction d = to_dir (r->get_property ("span-direction"));
+  span_events_[d] = r;
+  grow_dir_[d] = SMALLER;
+}
+
+IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, crescendo);
+void
+Dynamic_performer::listen_crescendo (Stream_event *r)
+{
+  Direction d = to_dir (r->get_property ("span-direction"));
+  span_events_[d] = r;
+  grow_dir_[d] = BIGGER;
+}
+
+IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, absolute_dynamic);
+void
+Dynamic_performer::listen_absolute_dynamic (Stream_event *r)
+{
+  if (!script_event_)
+    script_event_ = r;
 }
 
 ADD_TRANSLATOR (Dynamic_performer,
-               /*descr*/                "",
-               /* creats*/ "",
-               /* accepts */ "absolute-dynamic-event",
-               /* acks */ "",
-               /*reads */"dynamicAbsoluteVolumeFunction midiMaximumVolume midiMinimumVolume midiInstrument instrumentEqualizer",
-               /*writes*/"");
+               /* doc */
+               "",
+
+               /* create */
+               "",
+
+               /* read */
+               "dynamicAbsoluteVolumeFunction "
+               "instrumentEqualizer "
+               "midiMaximumVolume "
+               "midiMinimumVolume "
+               "midiInstrument ",
+
+               /* write */
+               ""
+               );