]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-performer.cc
* lily/*-performer.cc: Converted try_music to listen_*
[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--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "performer.hh"
10
11 #include "audio-item.hh"
12 #include "international.hh"
13 #include "stream-event.hh"
14 #include "warn.hh"
15
16 #include "translator.icc"
17
18 #define SOSTENUTO       0
19 #define SUSTAIN         1
20 #define UNA_CORDA       2
21 #define NUM_PEDAL_TYPES 3
22
23 /**
24    perform Piano pedals
25 */
26 class Piano_pedal_performer : public Performer
27 {
28   struct Pedal_info
29   {
30     Stream_event *start_event_;
31     Drul_array<Stream_event *> event_drul_;
32   };
33
34 public:
35   TRANSLATOR_DECLARATIONS (Piano_pedal_performer);
36
37 protected:
38   virtual void initialize ();
39   static const char *pedal_type_str (int t);
40   void process_music ();
41   void stop_translation_timestep ();
42   void start_translation_timestep ();
43   DECLARE_TRANSLATOR_LISTENER (sustain);
44   DECLARE_TRANSLATOR_LISTENER (una_corda);
45   DECLARE_TRANSLATOR_LISTENER (sostenuto);
46 private:
47   vector<Audio_piano_pedal*> audios_;
48   Pedal_info info_alist_[NUM_PEDAL_TYPES];
49 };
50
51 Piano_pedal_performer::Piano_pedal_performer ()
52 {
53 }
54
55 const char *
56 Piano_pedal_performer::pedal_type_str (int t)
57 {
58   switch (t)
59     {
60     case SOSTENUTO: 
61       return "Sostenuto";
62     case SUSTAIN:
63       return "Sustain";
64     case UNA_CORDA: 
65       return "UnaCorda";
66     default:
67       programming_error ("Unknown pedal type");
68       return 0;
69     }
70 }
71
72 void
73 Piano_pedal_performer::initialize ()
74 {
75   Pedal_info *p = info_alist_;
76
77   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
78     {
79       p->event_drul_[START] = 0;
80       p->event_drul_[STOP] = 0;
81       p->start_event_ = 0;
82     }
83 }
84
85 void
86 Piano_pedal_performer::process_music ()
87 {
88   Pedal_info *p = info_alist_;
89
90   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
91     {
92       if (p->event_drul_[STOP])
93         {
94           if (!p->start_event_)
95             p->event_drul_[STOP]->origin ()->warning (_f ("can't find start of piano pedal: `%s'", pedal_type_str (i)));
96           else
97             {
98               Audio_piano_pedal *a = new Audio_piano_pedal;
99               a->type_string_ = string (pedal_type_str (i));
100               a->dir_ = STOP;
101               audios_.push_back (a);
102               Audio_element_info info(a, p->event_drul_[STOP]);
103               announce_element (info);
104             }
105           p->start_event_ = 0;
106         }
107
108       if (p->event_drul_[START])
109         {
110           p->start_event_ = p->event_drul_[START];
111           Audio_piano_pedal *a = new Audio_piano_pedal;
112           a->type_string_ = string (pedal_type_str (i));
113           a->dir_ = START;
114           audios_.push_back (a);
115           Audio_element_info info(a, p->event_drul_[START]);
116           announce_element (info);
117         }
118       p->event_drul_[START] = 0;
119       p->event_drul_[STOP] = 0;
120     }
121 }
122
123 void
124 Piano_pedal_performer::stop_translation_timestep ()
125 {
126   for (vsize i = 0; i < audios_.size (); i++)
127     play_element (audios_[i]);
128   audios_.clear ();
129 }
130
131 void
132 Piano_pedal_performer::start_translation_timestep ()
133 {
134   Pedal_info *p = info_alist_;
135   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
136     {
137       p->event_drul_[STOP] = 0;
138       p->event_drul_[START] = 0;
139     }
140 }
141
142 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sostenuto);
143 void
144 Piano_pedal_performer::listen_sostenuto (Stream_event *r)
145 {
146   Direction d = to_dir (r->get_property ("span-direction"));
147   info_alist_[SOSTENUTO].event_drul_[d] = r;
148 }
149
150 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sustain);
151 void
152 Piano_pedal_performer::listen_sustain (Stream_event *r)
153 {
154   Direction d = to_dir (r->get_property ("span-direction"));
155   info_alist_[SUSTAIN].event_drul_[d] = r;
156 }
157
158 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, una_corda);
159 void
160 Piano_pedal_performer::listen_una_corda (Stream_event *r)
161 {
162   Direction d = to_dir (r->get_property ("span-direction"));
163   info_alist_[UNA_CORDA].event_drul_[d] = r;
164 }
165
166 ADD_TRANSLATOR (Piano_pedal_performer, "", "",
167                 "pedal-event",
168                 "", "");