]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-performer.cc
Run grand-replace for 2012
[lilypond.git] / lily / dynamic-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2012 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
24 #include "translator.icc"
25
26 class Dynamic_performer : public Performer
27 {
28 public:
29   TRANSLATOR_DECLARATIONS (Dynamic_performer);
30 protected:
31   void stop_translation_timestep ();
32   void process_music ();
33   Real equalize_volume (Real);
34
35   DECLARE_TRANSLATOR_LISTENER (decrescendo);
36   DECLARE_TRANSLATOR_LISTENER (crescendo);
37   DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
38 private:
39   Stream_event *script_event_;
40   Drul_array<Stream_event *> span_events_;
41   Drul_array<Direction> grow_dir_;
42   Real last_volume_;
43   Audio_dynamic *absolute_;
44   Audio_span_dynamic *span_dynamic_;
45   Audio_span_dynamic *finished_span_dynamic_;
46 };
47
48 Dynamic_performer::Dynamic_performer ()
49 {
50   last_volume_ = 0.5;
51   script_event_ = 0;
52   absolute_ = 0;
53   span_events_[LEFT]
54     = span_events_[RIGHT] = 0;
55   span_dynamic_ = 0;
56   finished_span_dynamic_ = 0;
57 }
58
59 Real
60 Dynamic_performer::equalize_volume (Real volume)
61 {
62   /*
63     properties override default equaliser setting
64   */
65   SCM min = get_property ("midiMinimumVolume");
66   SCM max = get_property ("midiMaximumVolume");
67   if (scm_is_number (min) || scm_is_number (max))
68     {
69       Interval iv (0, 1);
70       if (scm_is_number (min))
71         iv[MIN] = scm_to_double (min);
72       if (scm_is_number (max))
73         iv[MAX] = scm_to_double (max);
74       volume = iv[MIN] + iv.length () * volume;
75     }
76   else
77     {
78       /*
79         urg, code duplication:: staff_performer
80       */
81       SCM s = get_property ("midiInstrument");
82
83       if (!scm_is_string (s))
84         s = get_property ("instrumentName");
85
86       if (!scm_is_string (s))
87         s = scm_from_locale_string ("piano");
88
89       SCM eq = get_property ("instrumentEqualizer");
90       if (ly_is_procedure (eq))
91         s = scm_call_1 (eq, s);
92
93       if (is_number_pair (s))
94         {
95           Interval iv = ly_scm2interval (s);
96           volume = iv[MIN] + iv.length () * volume;
97         }
98     }
99   return volume;
100 }
101
102 void
103 Dynamic_performer::process_music ()
104 {
105   if (span_events_[STOP] || script_event_)
106     {
107       finished_span_dynamic_ = span_dynamic_;
108       span_dynamic_ = 0;
109     }
110
111   if (span_events_[START])
112     {
113       span_dynamic_ = new Audio_span_dynamic (equalize_volume (0.1), equalize_volume (1.0));
114       announce_element (Audio_element_info (span_dynamic_, span_events_[START]));
115
116       span_dynamic_->grow_dir_ = grow_dir_[START];
117     }
118
119   if (script_event_
120       || span_dynamic_
121       || finished_span_dynamic_)
122     {
123       absolute_ = new Audio_dynamic ();
124
125       if (script_event_)
126         {
127           SCM proc = get_property ("dynamicAbsoluteVolumeFunction");
128
129           SCM svolume = SCM_EOL;
130           if (ly_is_procedure (proc))
131             {
132               // urg
133               svolume = scm_call_1 (proc, script_event_->get_property ("text"));
134             }
135
136           Real volume = robust_scm2double (svolume, 0.5);
137
138           last_volume_
139             = absolute_->volume_ = equalize_volume (volume);
140         }
141
142       Audio_element_info info (absolute_, script_event_);
143       announce_element (info);
144     }
145
146   if (span_dynamic_)
147     span_dynamic_->add_absolute (absolute_);
148
149   if (finished_span_dynamic_)
150     finished_span_dynamic_->add_absolute (absolute_);
151 }
152
153 void
154 Dynamic_performer::stop_translation_timestep ()
155 {
156   if (finished_span_dynamic_)
157     {
158       finished_span_dynamic_->render ();
159       finished_span_dynamic_ = 0;
160     }
161
162   if (absolute_ && absolute_->volume_ < 0)
163     {
164       absolute_->volume_ = last_volume_;
165     }
166   else if (absolute_)
167     {
168       last_volume_ = absolute_->volume_;
169     }
170
171   absolute_ = 0;
172   script_event_ = 0;
173   span_events_[LEFT]
174     = span_events_[RIGHT] = 0;
175 }
176
177 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, decrescendo);
178 void
179 Dynamic_performer::listen_decrescendo (Stream_event *r)
180 {
181   Direction d = to_dir (r->get_property ("span-direction"));
182   span_events_[d] = r;
183   grow_dir_[d] = SMALLER;
184 }
185
186 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, crescendo);
187 void
188 Dynamic_performer::listen_crescendo (Stream_event *r)
189 {
190   Direction d = to_dir (r->get_property ("span-direction"));
191   span_events_[d] = r;
192   grow_dir_[d] = BIGGER;
193 }
194
195 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_performer, absolute_dynamic);
196 void
197 Dynamic_performer::listen_absolute_dynamic (Stream_event *r)
198 {
199   if (!script_event_)
200     script_event_ = r;
201 }
202
203 ADD_TRANSLATOR (Dynamic_performer,
204                 /* doc */
205                 "",
206
207                 /* create */
208                 "",
209
210                 /* read */
211                 "dynamicAbsoluteVolumeFunction "
212                 "instrumentEqualizer "
213                 "midiMaximumVolume "
214                 "midiMinimumVolume "
215                 "midiInstrument ",
216
217                 /* write */
218                 ""
219                );