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/>.
23 #include "audio-column.hh"
24 #include "audio-item.hh"
25 #include "audio-staff.hh"
26 #include "performer-group.hh"
29 /* Perform a staff. Individual notes should have their instrument
30 (staff-wide) set, so we override play_element ()
32 class Staff_performer : public Performer
35 TRANSLATOR_DECLARATIONS (Staff_performer);
38 string new_instrument_string ();
39 string instrument_string_;
42 virtual void acknowledge_audio_element (Audio_element_info info);
43 virtual void finalize ();
44 virtual void initialize ();
45 void process_music ();
46 void stop_translation_timestep ();
47 void set_instrument_name ();
48 void set_instrument (int channel);
51 Audio_staff *audio_staff_;
52 Audio_instrument *instrument_;
53 Audio_text *instrument_name_;
56 map<string, int> channel_map_;
59 #include "translator.icc"
61 ADD_TRANSLATOR (Staff_performer,
74 Staff_performer::Staff_performer ()
83 Staff_performer::~Staff_performer ()
88 Staff_performer::initialize ()
90 audio_staff_ = new Audio_staff;
91 name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ());
93 audio_staff_->add_audio_item (name_);
95 announce_element (Audio_element_info (audio_staff_, 0));
96 announce_element (Audio_element_info (name_, 0));
100 Staff_performer::process_music ()
102 string str = new_instrument_string ();
106 set_instrument_name ();
111 Staff_performer::set_instrument (int channel)
113 instrument_ = new Audio_instrument (instrument_string_);
114 instrument_->channel_ = channel;
115 announce_element (Audio_element_info (instrument_, 0));
116 audio_staff_->add_audio_item (instrument_);
120 Staff_performer::set_instrument_name ()
122 instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
124 announce_element (Audio_element_info (instrument_name_, 0));
125 audio_staff_->add_audio_item (instrument_name_);
129 Staff_performer::stop_translation_timestep ()
131 SCM proc = ly_lily_module_constant ("percussion?");
132 SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.c_str ()));
133 audio_staff_->percussion_ = (drums == SCM_BOOL_T);
137 instrument_name_ = 0;
142 Staff_performer::finalize ()
148 Staff_performer::new_instrument_string ()
150 // mustn't ask Score for instrument: it will return piano!
151 SCM minstr = get_property ("midiInstrument");
153 if (!scm_is_string (minstr)
154 || ly_scm2string (minstr) == instrument_string_)
157 instrument_string_ = ly_scm2string (minstr);
159 return instrument_string_;
163 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
165 if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
167 /* map each context (voice) to its own channel */
168 Context *c = inf.origin_contexts (this)[0];
169 string id = c->id_string ();
170 int channel = channel_map_.size ();
171 /* MIDI players tend to ignore instrument settings on channel
172 10, the percussion channel. */
173 if (channel % 16 == 9)
174 channel_map_[""] = channel++;
176 map<string, int>::const_iterator i = channel_map_.find (id);
177 if (i != channel_map_.end ())
181 channel_map_[id] = channel;
183 set_instrument (channel);
186 ai->channel_ = channel;
187 audio_staff_->add_audio_item (ai);