2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2000--2015 Jan Nieuwenhuizen <janneke@gnu.org>
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.
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.
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/>.
20 #include "performer.hh"
21 #include "audio-item.hh"
22 #include "stream-event.hh"
23 #include "international.hh"
25 #include "translator.icc"
27 class Dynamic_performer : public Performer
30 TRANSLATOR_DECLARATIONS (Dynamic_performer);
32 void stop_translation_timestep ();
33 void process_music ();
34 Real equalize_volume (Real);
36 void listen_decrescendo (Stream_event *);
37 void listen_crescendo (Stream_event *);
38 void listen_absolute_dynamic (Stream_event *);
40 Stream_event *script_event_;
41 Drul_array<Stream_event *> span_events_;
42 Drul_array<Direction> grow_dir_;
44 bool last_volume_initialized_;
45 Audio_dynamic *absolute_;
46 Audio_span_dynamic *span_dynamic_;
47 Audio_span_dynamic *finished_span_dynamic_;
50 Dynamic_performer::Dynamic_performer ()
53 last_volume_initialized_ = false;
57 = span_events_[RIGHT] = 0;
59 finished_span_dynamic_ = 0;
63 Dynamic_performer::equalize_volume (Real volume)
66 properties override default equaliser setting
68 SCM min = get_property ("midiMinimumVolume");
69 SCM max = get_property ("midiMaximumVolume");
70 if (scm_is_number (min) || scm_is_number (max))
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;
82 urg, code duplication:: staff_performer
84 SCM s = get_property ("midiInstrument");
86 if (!scm_is_string (s))
87 s = get_property ("instrumentName");
89 if (!scm_is_string (s))
90 s = scm_from_ascii_string ("piano");
92 SCM eq = get_property ("instrumentEqualizer");
93 if (ly_is_procedure (eq))
94 s = scm_call_1 (eq, s);
96 if (is_number_pair (s))
98 Interval iv = ly_scm2interval (s);
99 volume = iv[MIN] + iv.length () * volume;
106 Dynamic_performer::process_music ()
108 if (span_events_[STOP] || script_event_)
110 // End of a dynamic spanner, or an explicit dynamic script event.
111 finished_span_dynamic_ = span_dynamic_;
115 if (span_events_[START])
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]));
122 span_dynamic_->grow_dir_ = grow_dir_[START];
127 || finished_span_dynamic_)
129 // New change in dynamics.
130 absolute_ = new Audio_dynamic ();
134 // Explicit dynamic script event: determine the volume.
135 SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
137 SCM svolume = SCM_EOL;
138 if (ly_is_procedure (proc))
141 svolume = scm_call_1 (proc, script_event_->get_property ("text"));
144 Real volume = robust_scm2double (svolume, 0.5);
147 = absolute_->volume_ = equalize_volume (volume);
148 last_volume_initialized_ = true;
151 Audio_element_info info (absolute_, script_event_);
152 announce_element (info);
155 if (!last_volume_initialized_)
157 absolute_ = new Audio_dynamic ();
160 = absolute_->volume_ = equalize_volume (0.71); // Backward compatible
161 last_volume_initialized_ = true;
163 Audio_element_info info (absolute_, script_event_);
164 announce_element (info);
168 span_dynamic_->add_absolute (absolute_);
170 if (finished_span_dynamic_)
171 finished_span_dynamic_->add_absolute (absolute_);
175 Dynamic_performer::stop_translation_timestep ()
177 if (finished_span_dynamic_)
179 finished_span_dynamic_->render ();
180 finished_span_dynamic_ = 0;
183 if (absolute_ && absolute_->volume_ < 0)
185 absolute_->volume_ = last_volume_;
189 last_volume_ = absolute_->volume_;
190 last_volume_initialized_ = true;
196 = span_events_[RIGHT] = 0;
200 Dynamic_performer::listen_decrescendo (Stream_event *r)
202 Direction d = to_dir (r->get_property ("span-direction"));
204 grow_dir_[d] = SMALLER;
208 Dynamic_performer::listen_crescendo (Stream_event *r)
210 Direction d = to_dir (r->get_property ("span-direction"));
212 grow_dir_[d] = BIGGER;
216 Dynamic_performer::listen_absolute_dynamic (Stream_event *r)
223 Dynamic_performer::boot ()
225 ADD_LISTENER (Dynamic_performer, decrescendo);
226 ADD_LISTENER (Dynamic_performer, crescendo);
227 ADD_LISTENER (Dynamic_performer, absolute_dynamic);
230 ADD_TRANSLATOR (Dynamic_performer,
238 "dynamicAbsoluteVolumeFunction "
239 "instrumentEqualizer "