]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/piano-pedal-performer.cc
Run `make grand-replace'.
[lilypond.git] / lily / piano-pedal-performer.cc
index 444028b8dee20d7640061a05615ddae086d5081a..2552e6a32f3039ebce57d82be1c6b646ed5868c2 100644 (file)
 /*
- piano-pedal-performer.cc -- implement Piano_pedal_performer
 piano-pedal-performer.cc -- implement Piano_pedal_performer
 
   source file of the GNU LilyPond music typesetter
 
-  (c)  2000 Jan Nieuwenhuizen <janneke@gnu.org>
+  (c) 2000--2008 Jan Nieuwenhuizen <janneke@gnu.org>
 */
 
 #include "performer.hh"
-#include "command-request.hh"
-#include "musical-request.hh"
+
 #include "audio-item.hh"
+#include "international.hh"
+#include "stream-event.hh"
+#include "warn.hh"
 
+#include "translator.icc"
 
-/*
-  TODO:
-    sostenuto
-    una-chorda / tre-corde
- */
+typedef enum Pedal_type {SOSTENUTO, SUSTAIN, UNA_CORDA, NUM_PEDAL_TYPES};
 
 /**
    perform Piano pedals
- */
+*/
 class Piano_pedal_performer : public Performer
 {
+  struct Pedal_info
+  {
+    Stream_event *start_event_;
+    Drul_array<Stream_event *> event_drul_;
+  };
+
 public:
-  VIRTUAL_COPY_CONS (Translator);
-  
-  Piano_pedal_performer ();
+  TRANSLATOR_DECLARATIONS (Piano_pedal_performer);
 
 protected:
-  virtual bool do_try_music (Music*);
-  virtual void do_process_music ();
-  virtual void do_pre_move_processing ();
-  virtual void do_post_move_processing ();
-
+  virtual void initialize ();
+  static const char *pedal_type_str (int t);
+  void process_music ();
+  void stop_translation_timestep ();
+  void start_translation_timestep ();
+  DECLARE_TRANSLATOR_LISTENER (sustain);
+  DECLARE_TRANSLATOR_LISTENER (una_corda);
+  DECLARE_TRANSLATOR_LISTENER (sostenuto);
 private:
-  Link_array<Audio_piano_pedal> audio_p_arr_;
-  Span_req* sustain_start_req_l_;
-  Drul_array<Span_req*> sustain_req_l_drul_;
+  vector<Audio_piano_pedal*> audios_;
+  Pedal_info info_alist_[NUM_PEDAL_TYPES];
 };
 
-ADD_THIS_TRANSLATOR (Piano_pedal_performer);
-
 Piano_pedal_performer::Piano_pedal_performer ()
 {
-  sustain_req_l_drul_[START] = 0;
-  sustain_req_l_drul_[STOP] = 0;
-  sustain_start_req_l_ = 0;
+}
+
+const char *
+Piano_pedal_performer::pedal_type_str (int t)
+{
+  switch (t)
+    {
+    case SOSTENUTO: 
+      return "Sostenuto";
+    case SUSTAIN:
+      return "Sustain";
+    case UNA_CORDA: 
+      return "UnaCorda";
+    default:
+      programming_error ("Unknown pedal type");
+      return 0;
+    }
+}
+
+void
+Piano_pedal_performer::initialize ()
+{
+  Pedal_info *p = info_alist_;
+
+  for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
+    {
+      p->event_drul_[START] = 0;
+      p->event_drul_[STOP] = 0;
+      p->start_event_ = 0;
+    }
 }
 
 void
-Piano_pedal_performer::do_process_music ()
+Piano_pedal_performer::process_music ()
 {
-  if (sustain_req_l_drul_[STOP])
+  Pedal_info *p = info_alist_;
+
+  for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
     {
-      if (!sustain_start_req_l_)
+      string pedal_type = pedal_type_str (i);
+      if (p->event_drul_[STOP])
        {
-         sustain_req_l_drul_[STOP]->warning (_ ("can't find start of piano_pedal"));
+         if (!p->start_event_)
+           p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", pedal_type));
+         else
+           {
+             Audio_piano_pedal *a = new Audio_piano_pedal;
+             a->type_string_ = pedal_type;
+             a->dir_ = STOP;
+             audios_.push_back (a);
+              Audio_element_info info (a, p->event_drul_[STOP]);
+              announce_element (info);
+           }
+         p->start_event_ = 0;
        }
-      else
+
+      if (p->event_drul_[START])
        {
-         Audio_piano_pedal* p = new Audio_piano_pedal;
-         p->type_b_ = false;
-         audio_p_arr_.push (p);
+         p->start_event_ = p->event_drul_[START];
+         Audio_piano_pedal *a = new Audio_piano_pedal;
+         a->type_string_ = pedal_type;
+         a->dir_ = START;
+         audios_.push_back (a);
+          Audio_element_info info (a, p->event_drul_[START]);
+          announce_element (info);
        }
-      sustain_start_req_l_ = 0;
+      p->event_drul_[START] = 0;
+      p->event_drul_[STOP] = 0;
     }
+}
 
-  if (sustain_req_l_drul_[START])
+void
+Piano_pedal_performer::stop_translation_timestep ()
+{
+  audios_.clear ();
+}
+
+void
+Piano_pedal_performer::start_translation_timestep ()
+{
+  Pedal_info *p = info_alist_;
+  for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
     {
-      sustain_start_req_l_ = sustain_req_l_drul_[START];
-      Audio_piano_pedal* p = new Audio_piano_pedal;
-      p->type_b_ = true;
-      audio_p_arr_.push (p);
+      p->event_drul_[STOP] = 0;
+      p->event_drul_[START] = 0;
     }
 }
 
+IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sostenuto);
 void
-Piano_pedal_performer::do_pre_move_processing ()
+Piano_pedal_performer::listen_sostenuto (Stream_event *r)
 {
-  for (int i=0; i < audio_p_arr_.size (); i++)
-    play_element (audio_p_arr_[i]);
-  audio_p_arr_.clear ();
+  Direction d = to_dir (r->get_property ("span-direction"));
+  info_alist_[SOSTENUTO].event_drul_[d] = r;
 }
 
+IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sustain);
 void
-Piano_pedal_performer::do_post_move_processing ()
+Piano_pedal_performer::listen_sustain (Stream_event *r)
 {
-  sustain_req_l_drul_[STOP] = 0;
-  sustain_req_l_drul_[START] = 0;
+  Direction d = to_dir (r->get_property ("span-direction"));
+  info_alist_[SUSTAIN].event_drul_[d] = r;
 }
 
-bool
-Piano_pedal_performer::do_try_music (Music* r)
+IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, una_corda);
+void
+Piano_pedal_performer::listen_una_corda (Stream_event *r)
 {
-  if (Span_req * s = dynamic_cast<Span_req*>(r))
-    {
-      if (s->span_type_str_ == "sustain")
-       {
-         sustain_req_l_drul_[s->span_dir_] = s;
-         return true;
-       }
-    }
-  return false;
+  Direction d = to_dir (r->get_property ("span-direction"));
+  info_alist_[UNA_CORDA].event_drul_[d] = r;
 }
+
+ADD_TRANSLATOR (Piano_pedal_performer,
+               /* doc */
+               "",
+
+               /* create */
+               "",
+
+               /* read */
+               "",
+
+               /* write */
+               ""
+               );