2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--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/>.
23 #include "audio-column.hh"
24 #include "audio-item.hh"
25 #include "audio-staff.hh"
27 #include "international.hh"
28 #include "performer-group.hh"
30 #include "lily-imports.hh"
32 #include "translator.icc"
34 /* Perform a staff. Individual notes should have their instrument
35 (staff-wide) set, so we override play_element ()
37 class Staff_performer : public Performer
40 TRANSLATOR_DECLARATIONS (Staff_performer);
44 virtual void acknowledge_audio_element (Audio_element_info info);
45 virtual void finalize ();
46 virtual void initialize ();
47 void process_music ();
48 void stop_translation_timestep ();
51 string new_instrument_string ();
52 void set_instrument_name (const string &voice);
53 void set_instrument (int channel, const string &voice);
54 int get_channel (const string &instrument);
55 Audio_staff *get_audio_staff (const string &voice);
56 Audio_staff *new_audio_staff (const string &voice);
57 Audio_span_dynamic *get_dynamic (const string &voice);
59 string instrument_string_;
61 Audio_instrument *instrument_;
62 Audio_text *instrument_name_;
65 map<string, deque<Audio_note *> > note_map_;
66 map<string, Audio_staff *> staff_map_;
67 map<string, int> channel_map_;
68 map<string, Audio_span_dynamic *> dynamic_map_;
69 // Would prefer to have the following two items be
70 // members of the containing class Performance,
71 // so they can be reset for each new midi file output.
72 static map<string, int> static_channel_map_;
73 static int channel_count_;
74 // For now, ask the last Staff_performer clean up during its finalize method
75 static int staff_performer_count_;
78 map<string, int> Staff_performer::static_channel_map_;
79 int Staff_performer::channel_count_ = 0;
80 int Staff_performer::staff_performer_count_ = 0;
83 Staff_performer::boot ()
88 ADD_TRANSLATOR (Staff_performer,
101 Staff_performer::Staff_performer ()
104 instrument_name_ (0),
110 Staff_performer::~Staff_performer ()
115 Staff_performer::initialize ()
117 ++staff_performer_count_;
121 Staff_performer::new_audio_staff (const string &voice)
123 Audio_staff *audio_staff = new Audio_staff;
124 audio_staff->merge_unisons_
125 = to_boolean (get_property ("midiMergeUnisons"));
126 string track_name = context ()->id_string () + ":" + voice;
127 if (track_name != ":")
129 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
131 audio_staff->add_audio_item (name_);
132 announce_element (Audio_element_info (name_, 0));
134 announce_element (Audio_element_info (audio_staff, 0));
135 staff_map_[voice] = audio_staff;
136 if (!instrument_string_.empty ())
137 set_instrument (channel_, voice);
138 // Set initial values (if any) for control functions.
139 for (const Audio_control_function_value_change::Context_property *p
140 = Audio_control_function_value_change::context_properties_;
143 SCM value = get_property (p->name_);
144 if (scm_is_number (value))
146 Real val = scm_to_double (value);
147 if (val >= p->range_min_ && val <= p->range_max_)
149 // Normalize the value to the 0.0 to 1.0 range.
150 val = ((val - p->range_min_)
151 / (p->range_max_ - p->range_min_));
152 Audio_control_function_value_change *item
153 = new Audio_control_function_value_change (p->control_, val);
154 item->channel_ = channel_;
155 audio_staff->add_audio_item (item);
156 announce_element (Audio_element_info (item, 0));
159 warning (_f ("ignoring out-of-range value change for MIDI "
168 Staff_performer::get_audio_staff (const string &voice)
170 SCM channel_mapping = get_property ("midiChannelMapping");
171 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument"))
172 && staff_map_.size ())
173 return staff_map_.begin ()->second;
175 map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
176 if (i != staff_map_.end ())
178 map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
179 if (staff_map_.size () == 1 && e != staff_map_.end ())
181 staff_map_[voice] = e->second;
184 return new_audio_staff (voice);
188 Staff_performer::get_dynamic (const string &voice)
190 map<string, Audio_span_dynamic *>::const_iterator i = dynamic_map_.find (voice);
191 if (i != dynamic_map_.end ())
197 Staff_performer::process_music ()
202 Staff_performer::set_instrument (int channel, const string &voice)
204 instrument_ = new Audio_instrument (instrument_string_);
205 instrument_->channel_ = channel;
206 announce_element (Audio_element_info (instrument_, 0));
207 Audio_staff *audio_staff = get_audio_staff (voice);
208 audio_staff->add_audio_item (instrument_);
209 SCM drums = Lily::percussion_p (ly_symbol2scm (instrument_string_.c_str ()));
210 audio_staff->percussion_ = to_boolean (drums);
214 Staff_performer::set_instrument_name (const string &voice)
216 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
218 announce_element (Audio_element_info (instrument_name_, 0));
219 get_audio_staff (voice)->add_audio_item (instrument_name_);
223 Staff_performer::stop_translation_timestep ()
227 instrument_name_ = 0;
229 // For each voice with a note played in the current translation time step,
230 // check if the voice has a dynamic registered: if yes, apply the dynamic
231 // to every note played in the voice in the current translation time step.
232 for (map<string, deque<Audio_note *> >::iterator vi = note_map_.begin ();
233 vi != note_map_.end (); ++vi)
235 Audio_span_dynamic *d = get_dynamic (vi->first);
238 for (deque<Audio_note *>::iterator ni = vi->second.begin ();
239 ni != vi->second.end (); ++ni)
247 Staff_performer::finalize ()
250 channel_map_.clear ();
251 if (staff_performer_count_)
252 --staff_performer_count_;
253 if (0 == staff_performer_count_)
255 static_channel_map_.clear ();
261 Staff_performer::new_instrument_string ()
263 // mustn't ask Score for instrument: it will return piano!
264 SCM minstr = get_property ("midiInstrument");
266 if (!scm_is_string (minstr)
267 || ly_scm2string (minstr) == instrument_string_)
270 instrument_string_ = ly_scm2string (minstr);
272 return instrument_string_;
276 Staff_performer::get_channel (const string &instrument)
278 SCM channel_mapping = get_property ("midiChannelMapping");
279 map<string, int> &channel_map
280 = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
282 : static_channel_map_;
284 if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
288 map<string, int>::const_iterator i = channel_map.find (instrument);
289 if (i != channel_map.end ())
292 int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
294 : channel_map.size ();
296 /* MIDI players tend to ignore instrument settings on channel
297 10, the percussion channel. */
298 if (channel % 16 == 9)
300 channel_map["percussion"] = channel++;
306 warning (_ ("MIDI channel wrapped around"));
307 warning (_ ("remapping modulo 16"));
308 channel = channel % 16;
311 channel_map[instrument] = channel;
316 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
318 /* map each context (voice) to its own track */
319 Context *c = inf.origin_contexts (this)[0];
321 if (c->is_alias (ly_symbol2scm ("Voice")))
322 voice = c->id_string ();
323 SCM channel_mapping = get_property ("midiChannelMapping");
324 string str = new_instrument_string ();
325 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
326 channel_ = get_channel (voice);
327 else if (channel_ < 0 && str.empty ())
328 channel_ = get_channel (str);
331 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("voice")))
332 channel_ = get_channel (str);
333 set_instrument (channel_, voice);
334 set_instrument_name (voice);
336 Audio_staff *audio_staff = get_audio_staff (voice);
337 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
339 ai->channel_ = channel_;
340 if (Audio_note *n = dynamic_cast<Audio_note *> (inf.elem_))
342 // Keep track of the notes played in the current voice in this
343 // translation time step (for adjusting their dynamics later in
344 // stop_translation_timestep).
345 note_map_[voice].push_back (n);
347 audio_staff->add_audio_item (ai);
349 else if (Audio_span_dynamic *d = dynamic_cast<Audio_span_dynamic *> (inf.elem_))
351 dynamic_map_[voice] = d;