2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2011 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/>.
22 #include "audio-column.hh"
23 #include "audio-item.hh"
24 #include "audio-staff.hh"
26 #include "international.hh"
27 #include "performer-group.hh"
30 /* Perform a staff. Individual notes should have their instrument
31 (staff-wide) set, so we override play_element ()
33 class Staff_performer : public Performer
36 TRANSLATOR_DECLARATIONS (Staff_performer);
40 virtual void acknowledge_audio_element (Audio_element_info info);
41 virtual void finalize ();
42 virtual void initialize ();
43 void process_music ();
44 void stop_translation_timestep ();
47 string new_instrument_string ();
48 void set_instrument_name (string voice);
49 void set_instrument (int channel, string voice);
50 int get_channel (string instrument);
51 Audio_staff* get_audio_staff (string voice);
52 Audio_staff* new_audio_staff (string voice);
53 Real get_dynamic (string voice);
55 string instrument_string_;
57 Audio_instrument *instrument_;
58 Audio_text *instrument_name_;
61 map<string, Audio_staff*> staff_map_;
62 map<string, int> channel_map_;
63 map<string, Real> dynamic_map_;
64 static map<string, int> static_channel_map_;
65 static int channel_count_;
68 map<string, int> Staff_performer::static_channel_map_;
69 int Staff_performer::channel_count_ = 0;
71 #include "translator.icc"
73 ADD_TRANSLATOR (Staff_performer,
86 Staff_performer::Staff_performer ()
89 , instrument_name_ (0)
95 Staff_performer::~Staff_performer ()
100 Staff_performer::initialize ()
105 Staff_performer::new_audio_staff (string voice)
107 Audio_staff* audio_staff = new Audio_staff;
108 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
110 audio_staff->add_audio_item (name_);
111 announce_element (Audio_element_info (audio_staff, 0));
112 announce_element (Audio_element_info (name_, 0));
113 staff_map_[voice] = audio_staff;
118 Staff_performer::get_audio_staff (string voice)
120 map<string, Audio_staff*>::const_iterator i = staff_map_.find (voice);
121 if (i != staff_map_.end ())
123 map<string, Audio_staff*>::const_iterator e = staff_map_.find ("");
124 if (staff_map_.size () == 1 && e != staff_map_.end ())
126 staff_map_[voice] = e->second;
129 return new_audio_staff (voice);
133 Staff_performer::get_dynamic (string voice)
135 map<string, Real>::const_iterator i = dynamic_map_.find (voice);
136 if (i != dynamic_map_.end ())
142 Staff_performer::process_music ()
147 Staff_performer::set_instrument (int channel, string voice)
149 instrument_ = new Audio_instrument (instrument_string_);
150 instrument_->channel_ = channel;
151 announce_element (Audio_element_info (instrument_, 0));
152 Audio_staff* audio_staff = get_audio_staff (voice);
153 audio_staff->add_audio_item (instrument_);
154 SCM proc = ly_lily_module_constant ("percussion?");
155 SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.c_str ()));
156 audio_staff->percussion_ = (drums == SCM_BOOL_T);
160 Staff_performer::set_instrument_name (string voice)
162 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
164 announce_element (Audio_element_info (instrument_name_, 0));
165 get_audio_staff (voice)->add_audio_item (instrument_name_);
169 Staff_performer::stop_translation_timestep ()
173 instrument_name_ = 0;
178 Staff_performer::finalize ()
181 channel_map_.clear ();
183 static_channel_map_.clear ();
187 Staff_performer::new_instrument_string ()
189 // mustn't ask Score for instrument: it will return piano!
190 SCM minstr = get_property ("midiInstrument");
192 if (!scm_is_string (minstr)
193 || ly_scm2string (minstr) == instrument_string_)
196 instrument_string_ = ly_scm2string (minstr);
198 return instrument_string_;
202 Staff_performer::get_channel (string instrument)
204 SCM channel_per_staff = get_property ("midiChannelPerStaff");
205 map<string, int>& channel_map = (channel_per_staff == SCM_BOOL_T)
207 : static_channel_map_;
209 map<string, int>::const_iterator i = channel_map.find (instrument);
210 if (i != channel_map.end ())
213 int channel = (channel_per_staff == SCM_BOOL_T)
215 :channel_map.size ();
217 /* MIDI players tend to ignore instrument settings on channel
218 10, the percussion channel. */
219 if (channel % 16 == 9)
220 channel_map["percussion"] = channel++;
224 warning (_ ("MIDI channel wrapped around"));
225 warning (_ ("remapping modulo 16"));
226 channel = channel % 16;
229 channel_map[instrument] = channel;
234 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
236 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
238 /* map each context (voice) to its own track */
239 Context* c = inf.origin_contexts (this)[0];
241 if (c->is_alias (ly_symbol2scm ("Voice")))
242 voice = c->id_string ();
243 string str = new_instrument_string ();
246 channel_ = get_channel (str);
247 set_instrument (channel_, voice);
248 set_instrument_name (voice);
250 Audio_staff* audio_staff = get_audio_staff (voice);
251 ai->channel_ = channel_;
252 if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (inf.elem_))
253 dynamic_map_[voice] = d->volume_;
254 if (Real d = get_dynamic (voice))
255 if (Audio_note *n = dynamic_cast<Audio_note *> (inf.elem_))
257 audio_staff->add_audio_item (ai);