Midi: add addlyrics test with mid2ly fix.
[lilypond/patrick.git] / lily / staff-performer.cc
blobdf2697060bff31ad56f5755870ab84ab96be1925
1 /*
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/>.
20 #include <map>
22 #include "audio-column.hh"
23 #include "audio-item.hh"
24 #include "audio-staff.hh"
25 #include "context.hh"
26 #include "international.hh"
27 #include "performer-group.hh"
28 #include "warn.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
35 public:
36 TRANSLATOR_DECLARATIONS (Staff_performer);
37 ~Staff_performer ();
39 protected:
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 ();
46 private:
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_;
56 int channel_;
57 Audio_instrument *instrument_;
58 Audio_text *instrument_name_;
59 Audio_text *name_;
60 Audio_tempo *tempo_;
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,
74 /* doc */
75 "",
77 /* create */
78 "",
80 /* read */
81 "",
83 /* write */
84 "");
86 Staff_performer::Staff_performer ()
87 : channel_ (0)
88 , instrument_ (0)
89 , instrument_name_ (0)
90 , name_ (0)
91 , tempo_ (0)
95 Staff_performer::~Staff_performer ()
99 void
100 Staff_performer::initialize ()
104 Audio_staff*
105 Staff_performer::new_audio_staff (string voice)
107 Audio_staff* audio_staff = new Audio_staff;
108 string track_name = context ()->id_string () + ":" + voice;
109 if (track_name != ":")
111 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
112 + ":" + voice);
113 audio_staff->add_audio_item (name_);
115 announce_element (Audio_element_info (audio_staff, 0));
116 announce_element (Audio_element_info (name_, 0));
117 staff_map_[voice] = audio_staff;
118 return audio_staff;
121 Audio_staff*
122 Staff_performer::get_audio_staff (string voice)
124 SCM channel_mapping = get_property ("midiChannelMapping");
125 if (channel_mapping != ly_symbol2scm ("voice")
126 && staff_map_.size ())
127 return staff_map_.begin ()->second;
129 map<string, Audio_staff*>::const_iterator i = staff_map_.find (voice);
130 if (i != staff_map_.end ())
131 return i->second;
132 map<string, Audio_staff*>::const_iterator e = staff_map_.find ("");
133 if (staff_map_.size () == 1 && e != staff_map_.end ())
135 staff_map_[voice] = e->second;
136 return e->second;
138 return new_audio_staff (voice);
141 Real
142 Staff_performer::get_dynamic (string voice)
144 map<string, Real>::const_iterator i = dynamic_map_.find (voice);
145 if (i != dynamic_map_.end ())
146 return i->second;
147 return 0;
150 void
151 Staff_performer::process_music ()
155 void
156 Staff_performer::set_instrument (int channel, string voice)
158 instrument_ = new Audio_instrument (instrument_string_);
159 instrument_->channel_ = channel;
160 announce_element (Audio_element_info (instrument_, 0));
161 Audio_staff* audio_staff = get_audio_staff (voice);
162 audio_staff->add_audio_item (instrument_);
163 SCM proc = ly_lily_module_constant ("percussion?");
164 SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.c_str ()));
165 audio_staff->percussion_ = (drums == SCM_BOOL_T);
168 void
169 Staff_performer::set_instrument_name (string voice)
171 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
172 instrument_string_);
173 announce_element (Audio_element_info (instrument_name_, 0));
174 get_audio_staff (voice)->add_audio_item (instrument_name_);
177 void
178 Staff_performer::stop_translation_timestep ()
180 name_ = 0;
181 tempo_ = 0;
182 instrument_name_ = 0;
183 instrument_ = 0;
186 void
187 Staff_performer::finalize ()
189 staff_map_.clear ();
190 channel_map_.clear ();
193 string
194 Staff_performer::new_instrument_string ()
196 // mustn't ask Score for instrument: it will return piano!
197 SCM minstr = get_property ("midiInstrument");
199 if (!scm_is_string (minstr)
200 || ly_scm2string (minstr) == instrument_string_)
201 return "";
203 instrument_string_ = ly_scm2string (minstr);
205 return instrument_string_;
209 Staff_performer::get_channel (string instrument)
211 SCM channel_mapping = get_property ("midiChannelMapping");
212 map<string, int>& channel_map
213 = (channel_mapping != ly_symbol2scm ("instrument"))
214 ? channel_map_
215 : static_channel_map_;
217 map<string, int>::const_iterator i = channel_map.find (instrument);
218 if (i != channel_map.end ())
219 return i->second;
221 int channel = (channel_mapping == ly_symbol2scm ("staff"))
222 ? channel_count_++
223 : channel_map.size ();
225 /* MIDI players tend to ignore instrument settings on channel
226 10, the percussion channel. */
227 if (channel % 16 == 9)
229 channel_map["percussion"] = channel++;
230 channel_count_++;
233 if (channel > 15)
235 warning (_ ("MIDI channel wrapped around"));
236 warning (_ ("remapping modulo 16"));
237 channel = channel % 16;
240 channel_map[instrument] = channel;
241 return channel;
244 void
245 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
247 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
249 /* map each context (voice) to its own track */
250 Context* c = inf.origin_contexts (this)[0];
251 string voice;
252 if (c->is_alias (ly_symbol2scm ("Voice")))
253 voice = c->id_string ();
254 SCM channel_mapping = get_property ("midiChannelMapping");
255 if (channel_mapping == ly_symbol2scm ("voice"))
256 channel_ = get_channel (voice);
257 string str = new_instrument_string ();
258 if (str.length ())
260 if (channel_mapping != ly_symbol2scm ("voice"))
261 channel_ = get_channel (str);
262 set_instrument (channel_, voice);
263 set_instrument_name (voice);
265 Audio_staff* audio_staff = get_audio_staff (voice);
266 ai->channel_ = channel_;
267 if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (inf.elem_))
268 dynamic_map_[voice] = d->volume_;
269 if (Real d = get_dynamic (voice))
270 if (Audio_note *n = dynamic_cast<Audio_note *> (inf.elem_))
271 n->volume_ = d;
272 audio_staff->add_audio_item (ai);