]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
Merge branch 'master' of /home/jcharles/GIT/Lily/. into translation
[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   Audio_span_dynamic *get_dynamic (const string &voice);
59
60   class Midi_control_initializer : public Midi_control_change_announcer
61   {
62   public:
63     Midi_control_initializer (Staff_performer *performer,
64                               Audio_staff *audio_staff,
65                               int channel);
66
67     SCM get_property_value (const char *property_name);
68     void do_announce (Audio_control_change *item);
69
70   private:
71     Staff_performer *performer_;
72     Audio_staff *audio_staff_;
73     int channel_;
74   };
75
76   string instrument_string_;
77   int channel_;
78   Audio_instrument *instrument_;
79   Audio_text *instrument_name_;
80   Audio_text *name_;
81   Audio_tempo *tempo_;
82   map<string, deque<Audio_note *> > note_map_;
83   map<string, Audio_staff *> staff_map_;
84   map<string, int> channel_map_;
85   map<string, Audio_span_dynamic *> dynamic_map_;
86   // Would prefer to have the following two items be
87   // members of the containing class Performance,
88   // so they can be reset for each new midi file output.
89   static map<string, int> static_channel_map_;
90   static int channel_count_;
91   // For now, ask the last Staff_performer clean up during its finalize method
92   static int staff_performer_count_;
93 };
94
95 map<string, int> Staff_performer::static_channel_map_;
96 int Staff_performer::channel_count_ = 0;
97 int Staff_performer::staff_performer_count_ = 0;
98
99 void
100 Staff_performer::boot ()
101 {
102
103 }
104
105 ADD_TRANSLATOR (Staff_performer,
106                 /* doc */
107                 "",
108
109                 /* create */
110                 "",
111
112                 /* read */
113                 "",
114
115                 /* write */
116                 "");
117
118 Staff_performer::Staff_performer ()
119   : channel_ (-1),
120     instrument_ (0),
121     instrument_name_ (0),
122     name_ (0),
123     tempo_ (0)
124 {
125 }
126
127 Staff_performer::~Staff_performer ()
128 {
129 }
130
131 void
132 Staff_performer::initialize ()
133 {
134   ++staff_performer_count_;
135 }
136
137 Audio_staff *
138 Staff_performer::new_audio_staff (const string &voice)
139 {
140   Audio_staff *audio_staff = new Audio_staff;
141   audio_staff->merge_unisons_
142     = to_boolean (get_property ("midiMergeUnisons"));
143   string track_name = context ()->id_string () + ":" + voice;
144   if (track_name != ":")
145     {
146       name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
147                               + ":" + voice);
148       audio_staff->add_audio_item (name_);
149       announce_element (Audio_element_info (name_, 0));
150     }
151   announce_element (Audio_element_info (audio_staff, 0));
152   staff_map_[voice] = audio_staff;
153   if (!instrument_string_.empty ())
154     set_instrument (channel_, voice);
155   // Set initial values (if any) for MIDI controls.
156   Midi_control_initializer i (this, audio_staff, channel_);
157   i.announce_control_changes ();
158   return audio_staff;
159 }
160
161 Audio_staff *
162 Staff_performer::get_audio_staff (const string &voice)
163 {
164   SCM channel_mapping = get_property ("midiChannelMapping");
165   if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument"))
166       && staff_map_.size ())
167     return staff_map_.begin ()->second;
168
169   map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
170   if (i != staff_map_.end ())
171     return i->second;
172   map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
173   if (staff_map_.size () == 1 && e != staff_map_.end ())
174     {
175       staff_map_[voice] = e->second;
176       return e->second;
177     }
178   return new_audio_staff (voice);
179 }
180
181 Audio_span_dynamic *
182 Staff_performer::get_dynamic (const string &voice)
183 {
184   map<string, Audio_span_dynamic *>::const_iterator i = dynamic_map_.find (voice);
185   if (i != dynamic_map_.end ())
186     return i->second;
187   return 0;
188 }
189
190 void
191 Staff_performer::process_music ()
192 {
193 }
194
195 void
196 Staff_performer::set_instrument (int channel, const string &voice)
197 {
198   instrument_ = new Audio_instrument (instrument_string_);
199   instrument_->channel_ = channel;
200   announce_element (Audio_element_info (instrument_, 0));
201   Audio_staff *audio_staff = get_audio_staff (voice);
202   audio_staff->add_audio_item (instrument_);
203   SCM drums = Lily::percussion_p (ly_symbol2scm (instrument_string_.c_str ()));
204   audio_staff->percussion_ = to_boolean (drums);
205 }
206
207 void
208 Staff_performer::set_instrument_name (const string &voice)
209 {
210   instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
211                                      instrument_string_);
212   announce_element (Audio_element_info (instrument_name_, 0));
213   get_audio_staff (voice)->add_audio_item (instrument_name_);
214 }
215
216 void
217 Staff_performer::stop_translation_timestep ()
218 {
219   name_ = 0;
220   tempo_ = 0;
221   instrument_name_ = 0;
222   instrument_ = 0;
223   // For each voice with a note played in the current translation time step,
224   // check if the voice has a dynamic registered: if yes, apply the dynamic
225   // to every note played in the voice in the current translation time step.
226   for (map<string, deque<Audio_note *> >::iterator vi = note_map_.begin ();
227        vi != note_map_.end (); ++vi)
228     {
229       Audio_span_dynamic *d = get_dynamic (vi->first);
230       if (d)
231         {
232           for (deque<Audio_note *>::iterator ni = vi->second.begin ();
233                ni != vi->second.end (); ++ni)
234             (*ni)->dynamic_ = d;
235         }
236     }
237   note_map_.clear ();
238 }
239
240 void
241 Staff_performer::finalize ()
242 {
243   staff_map_.clear ();
244   channel_map_.clear ();
245   if (staff_performer_count_)
246     --staff_performer_count_;
247   if (0 == staff_performer_count_)
248     {
249       static_channel_map_.clear ();
250       channel_count_ = 0;
251     }
252 }
253
254 string
255 Staff_performer::new_instrument_string ()
256 {
257   // mustn't ask Score for instrument: it will return piano!
258   SCM minstr = get_property ("midiInstrument");
259
260   if (!scm_is_string (minstr)
261       || ly_scm2string (minstr) == instrument_string_)
262     return "";
263
264   instrument_string_ = ly_scm2string (minstr);
265
266   return instrument_string_;
267 }
268
269 int
270 Staff_performer::get_channel (const string &instrument)
271 {
272   SCM channel_mapping = get_property ("midiChannelMapping");
273   map<string, int> &channel_map
274     = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
275       ? channel_map_
276       : static_channel_map_;
277
278   if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
279       && channel_ >= 0)
280     return channel_;
281
282   map<string, int>::const_iterator i = channel_map.find (instrument);
283   if (i != channel_map.end ())
284     return i->second;
285
286   int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
287                 ? channel_count_++
288                 : channel_map.size ();
289
290   /* MIDI players tend to ignore instrument settings on channel
291      10, the percussion channel.  */
292   if (channel % 16 == 9)
293     {
294       channel_map["percussion"] = channel++;
295       channel_count_++;
296     }
297
298   if (channel > 15)
299     {
300       warning (_ ("MIDI channel wrapped around"));
301       warning (_ ("remapping modulo 16"));
302       channel = channel % 16;
303     }
304
305   channel_map[instrument] = channel;
306   return channel;
307 }
308
309 void
310 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
311 {
312   /* map each context (voice) to its own track */
313   Context *c = inf.origin_contexts (this)[0];
314   string voice;
315   if (c->is_alias (ly_symbol2scm ("Voice")))
316     voice = c->id_string ();
317   SCM channel_mapping = get_property ("midiChannelMapping");
318   string str = new_instrument_string ();
319   if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
320     channel_ = get_channel (voice);
321   else if (channel_ < 0 && str.empty ())
322     channel_ = get_channel (str);
323   if (str.length ())
324     {
325       if (!scm_is_eq (channel_mapping, ly_symbol2scm ("voice")))
326         channel_ = get_channel (str);
327       set_instrument (channel_, voice);
328       set_instrument_name (voice);
329     }
330   Audio_staff *audio_staff = get_audio_staff (voice);
331   if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
332     {
333       ai->channel_ = channel_;
334       if (Audio_note *n = dynamic_cast<Audio_note *> (inf.elem_))
335         {
336           // Keep track of the notes played in the current voice in this
337           // translation time step (for adjusting their dynamics later in
338           // stop_translation_timestep).
339           note_map_[voice].push_back (n);
340         }
341       audio_staff->add_audio_item (ai);
342     }
343   else if (Audio_span_dynamic *d = dynamic_cast<Audio_span_dynamic *> (inf.elem_))
344     {
345       dynamic_map_[voice] = d;
346     }
347 }
348
349 Staff_performer::Midi_control_initializer::Midi_control_initializer
350 (Staff_performer *performer, Audio_staff *audio_staff, int channel)
351   : performer_ (performer),
352     audio_staff_ (audio_staff),
353     channel_ (channel)
354 {
355 }
356
357 SCM Staff_performer::Midi_control_initializer::get_property_value
358 (const char *property_name)
359 {
360   return performer_->get_property (property_name);
361 }
362
363 void Staff_performer::Midi_control_initializer::do_announce
364 (Audio_control_change *item)
365 {
366   item->channel_ = channel_;
367   audio_staff_->add_audio_item (item);
368   performer_->announce_element (Audio_element_info (item, 0));
369 }