]> git.donarmstrong.com Git - lilypond.git/blob - lily/piano-pedal-performer.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / piano-pedal-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "performer.hh"
21
22 #include "audio-item.hh"
23 #include "international.hh"
24 #include "stream-event.hh"
25 #include "warn.hh"
26
27 #include "translator.icc"
28
29 using std::string;
30 using std::vector;
31
32 enum Pedal_type {SOSTENUTO, SUSTAIN, UNA_CORDA, NUM_PEDAL_TYPES};
33
34 /**
35    perform Piano pedals
36 */
37 class Piano_pedal_performer : public Performer
38 {
39   struct Pedal_info
40   {
41     Stream_event *start_event_;
42     Drul_array<Stream_event *> event_drul_;
43   };
44
45 public:
46   TRANSLATOR_DECLARATIONS (Piano_pedal_performer);
47
48 protected:
49   virtual void initialize ();
50   static const char *pedal_type_str (int t);
51   void process_music ();
52   void stop_translation_timestep ();
53   void start_translation_timestep ();
54   DECLARE_TRANSLATOR_LISTENER (sustain);
55   DECLARE_TRANSLATOR_LISTENER (una_corda);
56   DECLARE_TRANSLATOR_LISTENER (sostenuto);
57 private:
58   vector<Audio_piano_pedal *> audios_;
59   Pedal_info info_alist_[NUM_PEDAL_TYPES];
60 };
61
62 Piano_pedal_performer::Piano_pedal_performer ()
63 {
64 }
65
66 const char *
67 Piano_pedal_performer::pedal_type_str (int t)
68 {
69   switch (t)
70     {
71     case SOSTENUTO:
72       return "Sostenuto";
73     case SUSTAIN:
74       return "Sustain";
75     case UNA_CORDA:
76       return "UnaCorda";
77     default:
78       programming_error ("Unknown pedal type");
79       return 0;
80     }
81 }
82
83 void
84 Piano_pedal_performer::initialize ()
85 {
86   Pedal_info *p = info_alist_;
87
88   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
89     {
90       p->event_drul_[START] = 0;
91       p->event_drul_[STOP] = 0;
92       p->start_event_ = 0;
93     }
94 }
95
96 void
97 Piano_pedal_performer::process_music ()
98 {
99   Pedal_info *p = info_alist_;
100
101   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
102     {
103       string pedal_type = pedal_type_str (i);
104       if (p->event_drul_[STOP])
105         {
106           if (!p->start_event_)
107             p->event_drul_[STOP]->origin ()->warning (_f ("cannot find start of piano pedal: `%s'", pedal_type));
108           else
109             {
110               Audio_piano_pedal *a = new Audio_piano_pedal;
111               a->type_string_ = pedal_type;
112               a->dir_ = STOP;
113               audios_.push_back (a);
114               Audio_element_info info (a, p->event_drul_[STOP]);
115               announce_element (info);
116             }
117           p->start_event_ = 0;
118         }
119
120       if (p->event_drul_[START])
121         {
122           p->start_event_ = p->event_drul_[START];
123           Audio_piano_pedal *a = new Audio_piano_pedal;
124           a->type_string_ = pedal_type;
125           a->dir_ = START;
126           audios_.push_back (a);
127           Audio_element_info info (a, p->event_drul_[START]);
128           announce_element (info);
129         }
130       p->event_drul_[START] = 0;
131       p->event_drul_[STOP] = 0;
132     }
133 }
134
135 void
136 Piano_pedal_performer::stop_translation_timestep ()
137 {
138   audios_.clear ();
139 }
140
141 void
142 Piano_pedal_performer::start_translation_timestep ()
143 {
144   Pedal_info *p = info_alist_;
145   for (int i = 0; i < NUM_PEDAL_TYPES; i++, p++)
146     {
147       p->event_drul_[STOP] = 0;
148       p->event_drul_[START] = 0;
149     }
150 }
151
152 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sostenuto);
153 void
154 Piano_pedal_performer::listen_sostenuto (Stream_event *r)
155 {
156   Direction d = to_dir (r->get_property ("span-direction"));
157   info_alist_[SOSTENUTO].event_drul_[d] = r;
158 }
159
160 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, sustain);
161 void
162 Piano_pedal_performer::listen_sustain (Stream_event *r)
163 {
164   Direction d = to_dir (r->get_property ("span-direction"));
165   info_alist_[SUSTAIN].event_drul_[d] = r;
166 }
167
168 IMPLEMENT_TRANSLATOR_LISTENER (Piano_pedal_performer, una_corda);
169 void
170 Piano_pedal_performer::listen_una_corda (Stream_event *r)
171 {
172   Direction d = to_dir (r->get_property ("span-direction"));
173   info_alist_[UNA_CORDA].event_drul_[d] = r;
174 }
175
176 ADD_TRANSLATOR (Piano_pedal_performer,
177                 /* doc */
178                 "",
179
180                 /* create */
181                 "",
182
183                 /* read */
184                 "",
185
186                 /* write */
187                 ""
188                );