]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
Web-ja: update introduction
[lilypond.git] / lily / staff-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5
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.
10
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.
15
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/>.
18 */
19
20 #include <map>
21 #include <deque>
22
23 #include "audio-column.hh"
24 #include "audio-item.hh"
25 #include "audio-staff.hh"
26 #include "context.hh"
27 #include "international.hh"
28 #include "midi-cc-announcer.hh"
29 #include "performer-group.hh"
30 #include "warn.hh"
31 #include "lily-imports.hh"
32
33 #include "translator.icc"
34
35 /* Perform a staff. Individual notes should have their instrument
36   (staff-wide) set, so we override play_element ()
37 */
38 class Staff_performer : public Performer
39 {
40 public:
41   TRANSLATOR_DECLARATIONS (Staff_performer);
42   ~Staff_performer ();
43
44 protected:
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 ();
50
51 private:
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);
58
59   class Midi_control_initializer : public Midi_control_change_announcer
60   {
61   public:
62     Midi_control_initializer (Staff_performer *performer,
63                               Audio_staff *audio_staff,
64                               int channel);
65
66     SCM get_property_value (const char *property_name);
67     void do_announce (Audio_control_change *item);
68
69   private:
70     Staff_performer *performer_;
71     Audio_staff *audio_staff_;
72     int channel_;
73   };
74
75   string instrument_string_;
76   int channel_;
77   Audio_instrument *instrument_;
78   Audio_text *instrument_name_;
79   Audio_text *name_;
80   Audio_tempo *tempo_;
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_;
90 };
91
92 map<string, int> Staff_performer::static_channel_map_;
93 int Staff_performer::channel_count_ = 0;
94 int Staff_performer::staff_performer_count_ = 0;
95
96 void
97 Staff_performer::boot ()
98 {
99
100 }
101
102 ADD_TRANSLATOR (Staff_performer,
103                 /* doc */
104                 "",
105
106                 /* create */
107                 "",
108
109                 /* read */
110                 "",
111
112                 /* write */
113                 "");
114
115 Staff_performer::Staff_performer (Context *c)
116   : Performer (c),
117     channel_ (-1),
118     instrument_ (0),
119     instrument_name_ (0),
120     name_ (0),
121     tempo_ (0)
122 {
123 }
124
125 Staff_performer::~Staff_performer ()
126 {
127 }
128
129 void
130 Staff_performer::initialize ()
131 {
132   ++staff_performer_count_;
133 }
134
135 Audio_staff *
136 Staff_performer::new_audio_staff (const string &voice)
137 {
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 != ":")
143     {
144       name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
145                               + ":" + voice);
146       audio_staff->add_audio_item (name_);
147       announce_element (Audio_element_info (name_, 0));
148     }
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 ();
156   return audio_staff;
157 }
158
159 Audio_staff *
160 Staff_performer::get_audio_staff (const string &voice)
161 {
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;
166
167   map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
168   if (i != staff_map_.end ())
169     return i->second;
170   map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
171   if (staff_map_.size () == 1 && e != staff_map_.end ())
172     {
173       staff_map_[voice] = e->second;
174       return e->second;
175     }
176   return new_audio_staff (voice);
177 }
178
179 void
180 Staff_performer::process_music ()
181 {
182 }
183
184 void
185 Staff_performer::set_instrument (int channel, const string &voice)
186 {
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);
194 }
195
196 void
197 Staff_performer::set_instrument_name (const string &voice)
198 {
199   instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
200                                      instrument_string_);
201   announce_element (Audio_element_info (instrument_name_, 0));
202   get_audio_staff (voice)->add_audio_item (instrument_name_);
203 }
204
205 void
206 Staff_performer::stop_translation_timestep ()
207 {
208   name_ = 0;
209   tempo_ = 0;
210   instrument_name_ = 0;
211   instrument_ = 0;
212 }
213
214 void
215 Staff_performer::finalize ()
216 {
217   staff_map_.clear ();
218   channel_map_.clear ();
219   if (staff_performer_count_)
220     --staff_performer_count_;
221   if (0 == staff_performer_count_)
222     {
223       static_channel_map_.clear ();
224       channel_count_ = 0;
225     }
226 }
227
228 string
229 Staff_performer::new_instrument_string ()
230 {
231   // mustn't ask Score for instrument: it will return piano!
232   SCM minstr = get_property ("midiInstrument");
233
234   if (!scm_is_string (minstr)
235       || ly_scm2string (minstr) == instrument_string_)
236     return "";
237
238   instrument_string_ = ly_scm2string (minstr);
239
240   return instrument_string_;
241 }
242
243 int
244 Staff_performer::get_channel (const string &instrument)
245 {
246   SCM channel_mapping = get_property ("midiChannelMapping");
247   map<string, int> &channel_map
248     = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
249       ? channel_map_
250       : static_channel_map_;
251
252   if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
253       && channel_ >= 0)
254     return channel_;
255
256   map<string, int>::const_iterator i = channel_map.find (instrument);
257   if (i != channel_map.end ())
258     return i->second;
259
260   int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
261                 ? channel_count_++
262                 : channel_map.size ();
263
264   /* MIDI players tend to ignore instrument settings on channel
265      10, the percussion channel.  */
266   if (channel % 16 == 9)
267     {
268       channel_map["percussion"] = channel++;
269       channel_count_++;
270     }
271
272   if (channel > 15)
273     {
274       warning (_ ("MIDI channel wrapped around"));
275       warning (_ ("remapping modulo 16"));
276       channel = channel % 16;
277     }
278
279   channel_map[instrument] = channel;
280   return channel;
281 }
282
283 void
284 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
285 {
286   /* map each context (voice) to its own track */
287   Context *c = inf.origin_contexts (this)[0];
288   string voice;
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);
297   if (str.length ())
298     {
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);
303     }
304   Audio_staff *audio_staff = get_audio_staff (voice);
305   if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
306     {
307       ai->channel_ = channel_;
308       audio_staff->add_audio_item (ai);
309     }
310 }
311
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),
316     channel_ (channel)
317 {
318 }
319
320 SCM Staff_performer::Midi_control_initializer::get_property_value
321 (const char *property_name)
322 {
323   return performer_->get_property (property_name);
324 }
325
326 void Staff_performer::Midi_control_initializer::do_announce
327 (Audio_control_change *item)
328 {
329   item->channel_ = channel_;
330   audio_staff_->add_audio_item (item);
331   performer_->announce_element (Audio_element_info (item, 0));
332 }