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 "midi-cc-announcer.hh"
29 #include "performer-group.hh"
31 #include "lily-imports.hh"
33 #include "translator.icc"
35 /* Perform a staff. Individual notes should have their instrument
36 (staff-wide) set, so we override play_element ()
38 class Staff_performer : public Performer
41 TRANSLATOR_DECLARATIONS (Staff_performer);
45 virtual void acknowledge_audio_element (Audio_element_info info);
46 virtual void finalize ();
47 virtual void initialize ();
48 void process_music ();
49 void stop_translation_timestep ();
52 string new_instrument_string ();
53 void set_instrument_name (const string &voice);
54 void set_instrument (int channel, const string &voice);
55 int get_channel (const string &instrument);
56 Audio_staff *get_audio_staff (const string &voice);
57 Audio_staff *new_audio_staff (const string &voice);
59 class Midi_control_initializer : public Midi_control_change_announcer
62 Midi_control_initializer (Staff_performer *performer,
63 Audio_staff *audio_staff,
66 SCM get_property_value (const char *property_name);
67 void do_announce (Audio_control_change *item);
70 Staff_performer *performer_;
71 Audio_staff *audio_staff_;
75 string instrument_string_;
77 Audio_instrument *instrument_;
78 Audio_text *instrument_name_;
81 map<string, Audio_staff *> staff_map_;
82 map<string, int> channel_map_;
83 // Would prefer to have the following two items be
84 // members of the containing class Performance,
85 // so they can be reset for each new midi file output.
86 static map<string, int> static_channel_map_;
87 static int channel_count_;
88 // For now, ask the last Staff_performer clean up during its finalize method
89 static int staff_performer_count_;
92 map<string, int> Staff_performer::static_channel_map_;
93 int Staff_performer::channel_count_ = 0;
94 int Staff_performer::staff_performer_count_ = 0;
97 Staff_performer::boot ()
102 ADD_TRANSLATOR (Staff_performer,
115 Staff_performer::Staff_performer ()
118 instrument_name_ (0),
124 Staff_performer::~Staff_performer ()
129 Staff_performer::initialize ()
131 ++staff_performer_count_;
135 Staff_performer::new_audio_staff (const string &voice)
137 Audio_staff *audio_staff = new Audio_staff;
138 audio_staff->merge_unisons_
139 = to_boolean (get_property ("midiMergeUnisons"));
140 string track_name = context ()->id_string () + ":" + voice;
141 if (track_name != ":")
143 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
145 audio_staff->add_audio_item (name_);
146 announce_element (Audio_element_info (name_, 0));
148 announce_element (Audio_element_info (audio_staff, 0));
149 staff_map_[voice] = audio_staff;
150 if (!instrument_string_.empty ())
151 set_instrument (channel_, voice);
152 // Set initial values (if any) for MIDI controls.
153 Midi_control_initializer i (this, audio_staff, channel_);
154 i.announce_control_changes ();
159 Staff_performer::get_audio_staff (const string &voice)
161 SCM channel_mapping = get_property ("midiChannelMapping");
162 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument"))
163 && staff_map_.size ())
164 return staff_map_.begin ()->second;
166 map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
167 if (i != staff_map_.end ())
169 map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
170 if (staff_map_.size () == 1 && e != staff_map_.end ())
172 staff_map_[voice] = e->second;
175 return new_audio_staff (voice);
179 Staff_performer::process_music ()
184 Staff_performer::set_instrument (int channel, const string &voice)
186 instrument_ = new Audio_instrument (instrument_string_);
187 instrument_->channel_ = channel;
188 announce_element (Audio_element_info (instrument_, 0));
189 Audio_staff *audio_staff = get_audio_staff (voice);
190 audio_staff->add_audio_item (instrument_);
191 SCM drums = Lily::percussion_p (ly_symbol2scm (instrument_string_.c_str ()));
192 audio_staff->percussion_ = to_boolean (drums);
196 Staff_performer::set_instrument_name (const string &voice)
198 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
200 announce_element (Audio_element_info (instrument_name_, 0));
201 get_audio_staff (voice)->add_audio_item (instrument_name_);
205 Staff_performer::stop_translation_timestep ()
209 instrument_name_ = 0;
214 Staff_performer::finalize ()
217 channel_map_.clear ();
218 if (staff_performer_count_)
219 --staff_performer_count_;
220 if (0 == staff_performer_count_)
222 static_channel_map_.clear ();
228 Staff_performer::new_instrument_string ()
230 // mustn't ask Score for instrument: it will return piano!
231 SCM minstr = get_property ("midiInstrument");
233 if (!scm_is_string (minstr)
234 || ly_scm2string (minstr) == instrument_string_)
237 instrument_string_ = ly_scm2string (minstr);
239 return instrument_string_;
243 Staff_performer::get_channel (const string &instrument)
245 SCM channel_mapping = get_property ("midiChannelMapping");
246 map<string, int> &channel_map
247 = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
249 : static_channel_map_;
251 if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
255 map<string, int>::const_iterator i = channel_map.find (instrument);
256 if (i != channel_map.end ())
259 int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
261 : channel_map.size ();
263 /* MIDI players tend to ignore instrument settings on channel
264 10, the percussion channel. */
265 if (channel % 16 == 9)
267 channel_map["percussion"] = channel++;
273 warning (_ ("MIDI channel wrapped around"));
274 warning (_ ("remapping modulo 16"));
275 channel = channel % 16;
278 channel_map[instrument] = channel;
283 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
285 /* map each context (voice) to its own track */
286 Context *c = inf.origin_contexts (this)[0];
288 if (c->is_alias (ly_symbol2scm ("Voice")))
289 voice = c->id_string ();
290 SCM channel_mapping = get_property ("midiChannelMapping");
291 string str = new_instrument_string ();
292 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
293 channel_ = get_channel (voice);
294 else if (channel_ < 0 && str.empty ())
295 channel_ = get_channel (str);
298 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("voice")))
299 channel_ = get_channel (str);
300 set_instrument (channel_, voice);
301 set_instrument_name (voice);
303 Audio_staff *audio_staff = get_audio_staff (voice);
304 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
306 ai->channel_ = channel_;
307 audio_staff->add_audio_item (ai);
311 Staff_performer::Midi_control_initializer::Midi_control_initializer
312 (Staff_performer *performer, Audio_staff *audio_staff, int channel)
313 : performer_ (performer),
314 audio_staff_ (audio_staff),
319 SCM Staff_performer::Midi_control_initializer::get_property_value
320 (const char *property_name)
322 return performer_->get_property (property_name);
325 void Staff_performer::Midi_control_initializer::do_announce
326 (Audio_control_change *item)
328 item->channel_ = channel_;
329 audio_staff_->add_audio_item (item);
330 performer_->announce_element (Audio_element_info (item, 0));