]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-performer.cc
Issue 4911/3: Don't treat context modification identifiers special
[lilypond.git] / lily / dynamic-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 #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   void listen_decrescendo (Stream_event *);
37   void listen_crescendo (Stream_event *);
38   void listen_absolute_dynamic (Stream_event *);
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   Audio_dynamic *absolute_;
45   Audio_span_dynamic *span_dynamic_;
46   Audio_span_dynamic *finished_span_dynamic_;
47 };
48
49 Dynamic_performer::Dynamic_performer ()
50 {
51   last_volume_ = -1;
52   script_event_ = 0;
53   absolute_ = 0;
54   span_events_[LEFT]
55     = span_events_[RIGHT] = 0;
56   span_dynamic_ = 0;
57   finished_span_dynamic_ = 0;
58 }
59
60 Real
61 Dynamic_performer::equalize_volume (Real volume)
62 {
63   /*
64     properties override default equaliser setting
65   */
66   SCM min = get_property ("midiMinimumVolume");
67   SCM max = get_property ("midiMaximumVolume");
68   if (scm_is_number (min) || scm_is_number (max))
69     {
70       Interval iv (0, 1);
71       if (scm_is_number (min))
72         iv[MIN] = scm_to_double (min);
73       if (scm_is_number (max))
74         iv[MAX] = scm_to_double (max);
75       volume = iv[MIN] + iv.length () * volume;
76     }
77   else
78     {
79       /*
80         urg, code duplication:: staff_performer
81       */
82       SCM s = get_property ("midiInstrument");
83
84       if (!scm_is_string (s))
85         s = get_property ("instrumentName");
86
87       if (!scm_is_string (s))
88         s = scm_from_ascii_string ("piano");
89
90       SCM eq = get_property ("instrumentEqualizer");
91       if (ly_is_procedure (eq))
92         s = scm_call_1 (eq, s);
93
94       if (is_number_pair (s))
95         {
96           Interval iv = ly_scm2interval (s);
97           volume = iv[MIN] + iv.length () * volume;
98         }
99     }
100   return volume;
101 }
102
103 void
104 Dynamic_performer::process_music ()
105 {
106   if (span_events_[START] || span_events_[STOP] || script_event_)
107     {
108       // End the previous spanner when a new one begins or at an explicit stop
109       // or absolute dynamic.
110       finished_span_dynamic_ = span_dynamic_;
111       span_dynamic_ = 0;
112     }
113
114   if (span_events_[START])
115     {
116       // Start of a dynamic spanner.  Create a new Audio_span_dynamic for
117       // collecting changes in dynamics within this spanner.
118       span_dynamic_ = new Audio_span_dynamic (equalize_volume (0.1), equalize_volume (1.0));
119       announce_element (Audio_element_info (span_dynamic_, span_events_[START]));
120
121       span_dynamic_->grow_dir_ = grow_dir_[START];
122     }
123
124   if (script_event_
125       || span_dynamic_
126       || finished_span_dynamic_)
127     {
128       // New change in dynamics.
129       absolute_ = new Audio_dynamic ();
130
131       if (script_event_)
132         {
133           // Explicit dynamic script event: determine the volume.
134           SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
135
136           SCM svolume = SCM_EOL;
137           if (ly_is_procedure (proc))
138             {
139               // urg
140               svolume = scm_call_1 (proc, script_event_->get_property ("text"));
141             }
142
143           Real volume = robust_scm2double (svolume, 0.5);
144
145           last_volume_
146             = absolute_->volume_ = equalize_volume (volume);
147         }
148
149       Audio_element_info info (absolute_, script_event_);
150       announce_element (info);
151     }
152
153   if (last_volume_ < 0)
154     {
155       absolute_ = new Audio_dynamic ();
156
157       last_volume_
158         = absolute_->volume_ = equalize_volume (0.71); // Backward compatible
159
160       Audio_element_info info (absolute_, script_event_);
161       announce_element (info);
162     }
163
164   if (span_dynamic_)
165     span_dynamic_->add_absolute (absolute_);
166
167   if (finished_span_dynamic_)
168     finished_span_dynamic_->add_absolute (absolute_);
169 }
170
171 void
172 Dynamic_performer::stop_translation_timestep ()
173 {
174   if (finished_span_dynamic_)
175     {
176       finished_span_dynamic_->render ();
177       finished_span_dynamic_ = 0;
178     }
179
180   if (absolute_)
181     {
182       if (absolute_->volume_ < 0)
183         {
184           absolute_->volume_ = last_volume_;
185         }
186       else
187         {
188           last_volume_ = absolute_->volume_;
189         }
190     }
191
192   absolute_ = 0;
193   script_event_ = 0;
194   span_events_[LEFT]
195     = span_events_[RIGHT] = 0;
196 }
197
198 void
199 Dynamic_performer::listen_decrescendo (Stream_event *r)
200 {
201   Direction d = to_dir (r->get_property ("span-direction"));
202   span_events_[d] = r;
203   grow_dir_[d] = SMALLER;
204 }
205
206 void
207 Dynamic_performer::listen_crescendo (Stream_event *r)
208 {
209   Direction d = to_dir (r->get_property ("span-direction"));
210   span_events_[d] = r;
211   grow_dir_[d] = BIGGER;
212 }
213
214 void
215 Dynamic_performer::listen_absolute_dynamic (Stream_event *r)
216 {
217   if (!script_event_)
218     script_event_ = r;
219 }
220
221 void
222 Dynamic_performer::boot ()
223 {
224   ADD_LISTENER (Dynamic_performer, decrescendo);
225   ADD_LISTENER (Dynamic_performer, crescendo);
226   ADD_LISTENER (Dynamic_performer, absolute_dynamic);
227 }
228
229 ADD_TRANSLATOR (Dynamic_performer,
230                 /* doc */
231                 "",
232
233                 /* create */
234                 "",
235
236                 /* read */
237                 "dynamicAbsoluteVolumeFunction "
238                 "instrumentEqualizer "
239                 "midiMaximumVolume "
240                 "midiMinimumVolume "
241                 "midiInstrument ",
242
243                 /* write */
244                 ""
245                );