]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-performer.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / dynamic-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2014 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 #include "audio-item.hh"
22 #include "stream-event.hh"
23 #include "international.hh"
24
25 #include "translator.icc"
26
27 class Dynamic_performer : public Performer
28 {
29 public:
30   TRANSLATOR_DECLARATIONS (Dynamic_performer);
31 protected:
32   void stop_translation_timestep ();
33   void process_music ();
34   Real equalize_volume (Real);
35
36   DECLARE_TRANSLATOR_LISTENER (decrescendo);
37   DECLARE_TRANSLATOR_LISTENER (crescendo);
38   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
39 private:
40   Stream_event *script_event_;
41   Drul_array<Stream_event *> span_events_;
42   Drul_array<Direction> grow_dir_;
43   Real last_volume_;
44   bool last_volume_initialized_;
45   Audio_dynamic *absolute_;
46   Audio_span_dynamic *span_dynamic_;
47   Audio_span_dynamic *finished_span_dynamic_;
48 };
49
50 Dynamic_performer::Dynamic_performer ()
51 {
52   last_volume_ = 0.0;
53   last_volume_initialized_ = false;
54   script_event_ = 0;
55   absolute_ = 0;
56   span_events_[LEFT]
57     = span_events_[RIGHT] = 0;
58   span_dynamic_ = 0;
59   finished_span_dynamic_ = 0;
60 }
61
62 Real
63 Dynamic_performer::equalize_volume (Real volume)
64 {
65   /*
66     properties override default equaliser setting
67   */
68   SCM min = get_property ("midiMinimumVolume");
69   SCM max = get_property ("midiMaximumVolume");
70   if (scm_is_number (min) || scm_is_number (max))
71     {
72       Interval iv (0, 1);
73       if (scm_is_number (min))
74         iv[MIN] = scm_to_double (min);
75       if (scm_is_number (max))
76         iv[MAX] = scm_to_double (max);
77       volume = iv[MIN] + iv.length () * volume;
78     }
79   else
80     {
81       /*
82         urg, code duplication:: staff_performer
83       */
84       SCM s = get_property ("midiInstrument");
85
86       if (!scm_is_string (s))
87         s = get_property ("instrumentName");
88
89       if (!scm_is_string (s))
90         s = scm_from_locale_string ("piano");
91
92       SCM eq = get_property ("instrumentEqualizer");
93       if (ly_is_procedure (eq))
94         s = scm_call_1 (eq, s);
95
96       if (is_number_pair (s))
97         {
98           Interval iv = ly_scm2interval (s);
99           volume = iv[MIN] + iv.length () * volume;
100         }
101     }
102   return volume;
103 }
104
105 void
106 Dynamic_performer::process_music ()
107 {
108   if (span_events_[STOP] || script_event_)
109     {
110       // End of a dynamic spanner, or an explicit dynamic script event.
111       finished_span_dynamic_ = span_dynamic_;
112       span_dynamic_ = 0;
113     }
114
115   if (span_events_[START])
116     {
117       // Start of a dynamic spanner.  Create a new Audio_span_dynamic for
118       // collecting changes in dynamics within this spanner.
119       span_dynamic_ = new Audio_span_dynamic (equalize_volume (0.1), equalize_volume (1.0));
120       announce_element (Audio_element_info (span_dynamic_, span_events_[START]));
121
122       span_dynamic_->grow_dir_ = grow_dir_[START];
123
124       if (!last_volume_initialized_ && !script_event_)
125         {
126           // No explicit dynamic script events have occurred yet, but there is
127           // nevertheless a dynamic spanner.  Initialize last_volume_ to a
128           // value within the available range.
129           span_events_[START]->origin ()->warning (_ ("(De)crescendo with unspecified starting volume in MIDI."));
130           last_volume_ = equalize_volume (0.5);
131           last_volume_initialized_ = true;
132         }
133     }
134
135   if (script_event_
136       || span_dynamic_
137       || finished_span_dynamic_)
138     {
139       // New change in dynamics.
140       absolute_ = new Audio_dynamic ();
141
142       if (script_event_)
143         {
144           // Explicit dynamic script event: determine the volume.
145           SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
146
147           SCM svolume = SCM_EOL;
148           if (ly_is_procedure (proc))
149             {
150               // urg
151               svolume = scm_call_1 (proc, script_event_->get_property ("text"));
152             }
153
154           Real volume = robust_scm2double (svolume, 0.5);
155
156           last_volume_
157             = absolute_->volume_ = equalize_volume (volume);
158           last_volume_initialized_ = true;
159         }
160
161       Audio_element_info info (absolute_, script_event_);
162       announce_element (info);
163     }
164
165   if (!last_volume_initialized_)
166     {
167       absolute_ = new Audio_dynamic ();
168
169       last_volume_
170         = absolute_->volume_ = equalize_volume (0.71); // Backward compatible
171       last_volume_initialized_ = true;
172
173       Audio_element_info info (absolute_, script_event_);
174       announce_element (info);
175     }
176
177   if (span_dynamic_)
178     span_dynamic_->add_absolute (absolute_);
179
180   if (finished_span_dynamic_)
181     finished_span_dynamic_->add_absolute (absolute_);
182 }
183
184 void
185 Dynamic_performer::stop_translation_timestep ()
186 {
187   if (finished_span_dynamic_)
188     {
189       finished_span_dynamic_->render ();
190       finished_span_dynamic_ = 0;
191     }
192
193   if (absolute_ && absolute_->volume_ < 0)
194     {
195       absolute_->volume_ = last_volume_;
196     }
197   else if (absolute_)
198     {
199       last_volume_ = absolute_->volume_;
200       last_volume_initialized_ = true;
201     }
202
203   absolute_ = 0;
204   script_event_ = 0;
205   span_events_[LEFT]
206     = span_events_[RIGHT] = 0;
207 }
208
209 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, decrescendo);
210 void
211 Dynamic_performer::listen_decrescendo (Stream_event *r)
212 {
213   Direction d = to_dir (r->get_property ("span-direction"));
214   span_events_[d] = r;
215   grow_dir_[d] = SMALLER;
216 }
217
218 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, crescendo);
219 void
220 Dynamic_performer::listen_crescendo (Stream_event *r)
221 {
222   Direction d = to_dir (r->get_property ("span-direction"));
223   span_events_[d] = r;
224   grow_dir_[d] = BIGGER;
225 }
226
227 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, absolute_dynamic);
228 void
229 Dynamic_performer::listen_absolute_dynamic (Stream_event *r)
230 {
231   if (!script_event_)
232     script_event_ = r;
233 }
234
235 ADD_TRANSLATOR (Dynamic_performer,
236                 /* doc */
237                 "",
238
239                 /* create */
240                 "",
241
242                 /* read */
243                 "dynamicAbsoluteVolumeFunction "
244                 "instrumentEqualizer "
245                 "midiMaximumVolume "
246                 "midiMinimumVolume "
247                 "midiInstrument ",
248
249                 /* write */
250                 ""
251                );