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 (Context *c)
119 instrument_name_ (0),
125 Staff_performer::~Staff_performer ()
130 Staff_performer::initialize ()
132 ++staff_performer_count_;
136 Staff_performer::new_audio_staff (const string &voice)
138 Audio_staff *audio_staff = new Audio_staff;
139 audio_staff->merge_unisons_
140 = to_boolean (get_property ("midiMergeUnisons"));
141 string track_name = context ()->id_string () + ":" + voice;
142 if (track_name != ":")
144 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
146 audio_staff->add_audio_item (name_);
147 announce_element (Audio_element_info (name_, 0));
149 announce_element (Audio_element_info (audio_staff, 0));
150 staff_map_[voice] = audio_staff;
151 if (!instrument_string_.empty ())
152 set_instrument (channel_, voice);
153 // Set initial values (if any) for MIDI controls.
154 Midi_control_initializer i (this, audio_staff, channel_);
155 i.announce_control_changes ();
160 Staff_performer::get_audio_staff (const string &voice)
162 SCM channel_mapping = get_property ("midiChannelMapping");
163 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument"))
164 && staff_map_.size ())
165 return staff_map_.begin ()->second;
167 map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
168 if (i != staff_map_.end ())
170 map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
171 if (staff_map_.size () == 1 && e != staff_map_.end ())
173 staff_map_[voice] = e->second;
176 return new_audio_staff (voice);
180 Staff_performer::process_music ()
185 Staff_performer::set_instrument (int channel, const string &voice)
187 instrument_ = new Audio_instrument (instrument_string_);
188 instrument_->channel_ = channel;
189 announce_element (Audio_element_info (instrument_, 0));
190 Audio_staff *audio_staff = get_audio_staff (voice);
191 audio_staff->add_audio_item (instrument_);
192 SCM drums = Lily::percussion_p (ly_symbol2scm (instrument_string_.c_str ()));
193 audio_staff->percussion_ = to_boolean (drums);
197 Staff_performer::set_instrument_name (const string &voice)
199 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
201 announce_element (Audio_element_info (instrument_name_, 0));
202 get_audio_staff (voice)->add_audio_item (instrument_name_);
206 Staff_performer::stop_translation_timestep ()
210 instrument_name_ = 0;
215 Staff_performer::finalize ()
218 channel_map_.clear ();
219 if (staff_performer_count_)
220 --staff_performer_count_;
221 if (0 == staff_performer_count_)
223 static_channel_map_.clear ();
229 Staff_performer::new_instrument_string ()
231 // mustn't ask Score for instrument: it will return piano!
232 SCM minstr = get_property ("midiInstrument");
234 if (!scm_is_string (minstr)
235 || ly_scm2string (minstr) == instrument_string_)
238 instrument_string_ = ly_scm2string (minstr);
240 return instrument_string_;
244 Staff_performer::get_channel (const string &instrument)
246 SCM channel_mapping = get_property ("midiChannelMapping");
247 map<string, int> &channel_map
248 = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
250 : static_channel_map_;
252 if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
256 map<string, int>::const_iterator i = channel_map.find (instrument);
257 if (i != channel_map.end ())
260 int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
262 : channel_map.size ();
264 /* MIDI players tend to ignore instrument settings on channel
265 10, the percussion channel. */
266 if (channel % 16 == 9)
268 channel_map["percussion"] = channel++;
274 warning (_ ("MIDI channel wrapped around"));
275 warning (_ ("remapping modulo 16"));
276 channel = channel % 16;
279 channel_map[instrument] = channel;
284 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
286 /* map each context (voice) to its own track */
287 Context *c = inf.origin_contexts (this)[0];
289 if (c->is_alias (ly_symbol2scm ("Voice")))
290 voice = c->id_string ();
291 SCM channel_mapping = get_property ("midiChannelMapping");
292 string str = new_instrument_string ();
293 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
294 channel_ = get_channel (voice);
295 else if (channel_ < 0 && str.empty ())
296 channel_ = get_channel (str);
299 if (!scm_is_eq (channel_mapping, ly_symbol2scm ("voice")))
300 channel_ = get_channel (str);
301 set_instrument (channel_, voice);
302 set_instrument_name (voice);
304 Audio_staff *audio_staff = get_audio_staff (voice);
305 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
307 ai->channel_ = channel_;
308 audio_staff->add_audio_item (ai);
312 Staff_performer::Midi_control_initializer::Midi_control_initializer
313 (Staff_performer *performer, Audio_staff *audio_staff, int channel)
314 : performer_ (performer),
315 audio_staff_ (audio_staff),
320 SCM Staff_performer::Midi_control_initializer::get_property_value
321 (const char *property_name)
323 return performer_->get_property (property_name);
326 void Staff_performer::Midi_control_initializer::do_announce
327 (Audio_control_change *item)
329 item->channel_ = channel_;
330 audio_staff_->add_audio_item (item);
331 performer_->announce_element (Audio_element_info (item, 0));