]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-performer.cc
74643891894e3b25b486e202ae5f54f80a7bb4b6
[lilypond.git] / lily / piano-pedal-performer.cc
1 /*
2   piano-pedal-performer.cc -- implement Piano_pedal_performer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performer.hh"
10 #include "audio-item.hh"
11 #include "music.hh"
12
13 /**
14    perform Piano pedals
15 */
16 class Piano_pedal_performer : public Performer
17 {
18   struct Pedal_info
19   {
20     char const *name_;
21     Music *start_event_;
22     Drul_array<Music *> event_drul_;
23   };
24
25 public:
26   TRANSLATOR_DECLARATIONS (Piano_pedal_performer);
27   ~Piano_pedal_performer ();
28
29 protected:
30   virtual void initialize ();
31   virtual bool try_music (Music *);
32   void process_music ();
33   void stop_translation_timestep ();
34   void start_translation_timestep ();
35
36 private:
37   Link_array<Audio_piano_pedal> audios_;
38   Pedal_info *info_alist_;
39 };
40
41 Piano_pedal_performer::Piano_pedal_performer ()
42 {
43   info_alist_ = 0;
44 }
45
46 Piano_pedal_performer::~Piano_pedal_performer ()
47 {
48   delete[] info_alist_;
49 }
50
51 void
52 Piano_pedal_performer::initialize ()
53 {
54   info_alist_ = new Pedal_info[4];
55   Pedal_info *p = info_alist_;
56
57   char *names [] = { "Sostenuto", "Sustain", "UnaCorda", 0 };
58   char **np = names;
59   do
60     {
61       p->name_ = *np;
62       p->event_drul_[START] = 0;
63       p->event_drul_[STOP] = 0;
64       p->start_event_ = 0;
65
66       p++;
67     }
68   while (* (np++));
69 }
70
71 void
72 Piano_pedal_performer::process_music ()
73 {
74   for (Pedal_info *p = info_alist_; p && p->name_; p++)
75
76     {
77       if (p->event_drul_[STOP])
78         {
79           if (!p->start_event_)
80             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", String (p->name_)));
81           else
82             {
83               Audio_piano_pedal *a = new Audio_piano_pedal;
84               a->type_string_ = String (p->name_);
85               a->dir_ = STOP;
86               audios_.push (a);
87               Audio_element_info info(a, p->event_drul_[STOP]);
88               announce_element (info);
89             }
90           p->start_event_ = 0;
91         }
92
93       if (p->event_drul_[START])
94         {
95           p->start_event_ = p->event_drul_[START];
96           Audio_piano_pedal *a = new Audio_piano_pedal;
97           a->type_string_ = String (p->name_);
98           a->dir_ = START;
99           audios_.push (a);
100           Audio_element_info info(a, p->event_drul_[START]);
101           announce_element (info);
102         }
103       p->event_drul_[START] = 0;
104       p->event_drul_[STOP] = 0;
105     }
106 }
107
108 void
109 Piano_pedal_performer::stop_translation_timestep ()
110 {
111   for (int i = 0; i < audios_.size (); i++)
112     play_element (audios_[i]);
113   audios_.clear ();
114 }
115
116 void
117 Piano_pedal_performer::start_translation_timestep ()
118 {
119   for (Pedal_info *p = info_alist_; p && p->name_; p++)
120     {
121       p->event_drul_[STOP] = 0;
122       p->event_drul_[START] = 0;
123     }
124 }
125
126 bool
127 Piano_pedal_performer::try_music (Music *r)
128 {
129   if (r->is_mus_type ("pedal-event"))
130     {
131       for (Pedal_info *p = info_alist_; p->name_; p++)
132         {
133           String nm = p->name_ + String ("Event");
134           if (ly_is_equal (r->get_property ("name"),
135                            scm_str2symbol (nm.to_str0 ())))
136             {
137               Direction d = to_dir (r->get_property ("span-direction"));
138               p->event_drul_[d] = r;
139               return true;
140             }
141         }
142     }
143   return false;
144 }
145
146 #include "translator.icc"
147
148 ADD_TRANSLATOR (Piano_pedal_performer, "", "",
149                 "pedal-event",
150                 "", "");