]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
Issue 4957: parser.yy: loc_on_music -> loc_on_copy
[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 ()
116   : channel_ (-1),
117     instrument_ (0),
118     instrument_name_ (0),
119     name_ (0),
120     tempo_ (0)
121 {
122 }
123
124 Staff_performer::~Staff_performer ()
125 {
126 }
127
128 void
129 Staff_performer::initialize ()
130 {
131   ++staff_performer_count_;
132 }
133
134 Audio_staff *
135 Staff_performer::new_audio_staff (const string &voice)
136 {
137   Audio_staff *audio_staff = new Audio_staff;
138   audio_staff->merge_unisons_
139     = to_boolean (get_property ("midiMergeUnisons"));
140   string track_name = context ()->id_string () + ":" + voice;
141   if (track_name != ":")
142     {
143       name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
144                               + ":" + voice);
145       audio_staff->add_audio_item (name_);
146       announce_element (Audio_element_info (name_, 0));
147     }
148   announce_element (Audio_element_info (audio_staff, 0));
149   staff_map_[voice] = audio_staff;
150   if (!instrument_string_.empty ())
151     set_instrument (channel_, voice);
152   // Set initial values (if any) for MIDI controls.
153   Midi_control_initializer i (this, audio_staff, channel_);
154   i.announce_control_changes ();
155   return audio_staff;
156 }
157
158 Audio_staff *
159 Staff_performer::get_audio_staff (const string &voice)
160 {
161   SCM channel_mapping = get_property ("midiChannelMapping");
162   if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument"))
163       && staff_map_.size ())
164     return staff_map_.begin ()->second;
165
166   map<string, Audio_staff *>::const_iterator i = staff_map_.find (voice);
167   if (i != staff_map_.end ())
168     return i->second;
169   map<string, Audio_staff *>::const_iterator e = staff_map_.find ("");
170   if (staff_map_.size () == 1 && e != staff_map_.end ())
171     {
172       staff_map_[voice] = e->second;
173       return e->second;
174     }
175   return new_audio_staff (voice);
176 }
177
178 void
179 Staff_performer::process_music ()
180 {
181 }
182
183 void
184 Staff_performer::set_instrument (int channel, const string &voice)
185 {
186   instrument_ = new Audio_instrument (instrument_string_);
187   instrument_->channel_ = channel;
188   announce_element (Audio_element_info (instrument_, 0));
189   Audio_staff *audio_staff = get_audio_staff (voice);
190   audio_staff->add_audio_item (instrument_);
191   SCM drums = Lily::percussion_p (ly_symbol2scm (instrument_string_.c_str ()));
192   audio_staff->percussion_ = to_boolean (drums);
193 }
194
195 void
196 Staff_performer::set_instrument_name (const string &voice)
197 {
198   instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
199                                      instrument_string_);
200   announce_element (Audio_element_info (instrument_name_, 0));
201   get_audio_staff (voice)->add_audio_item (instrument_name_);
202 }
203
204 void
205 Staff_performer::stop_translation_timestep ()
206 {
207   name_ = 0;
208   tempo_ = 0;
209   instrument_name_ = 0;
210   instrument_ = 0;
211 }
212
213 void
214 Staff_performer::finalize ()
215 {
216   staff_map_.clear ();
217   channel_map_.clear ();
218   if (staff_performer_count_)
219     --staff_performer_count_;
220   if (0 == staff_performer_count_)
221     {
222       static_channel_map_.clear ();
223       channel_count_ = 0;
224     }
225 }
226
227 string
228 Staff_performer::new_instrument_string ()
229 {
230   // mustn't ask Score for instrument: it will return piano!
231   SCM minstr = get_property ("midiInstrument");
232
233   if (!scm_is_string (minstr)
234       || ly_scm2string (minstr) == instrument_string_)
235     return "";
236
237   instrument_string_ = ly_scm2string (minstr);
238
239   return instrument_string_;
240 }
241
242 int
243 Staff_performer::get_channel (const string &instrument)
244 {
245   SCM channel_mapping = get_property ("midiChannelMapping");
246   map<string, int> &channel_map
247     = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
248       ? channel_map_
249       : static_channel_map_;
250
251   if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
252       && channel_ >= 0)
253     return channel_;
254
255   map<string, int>::const_iterator i = channel_map.find (instrument);
256   if (i != channel_map.end ())
257     return i->second;
258
259   int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
260                 ? channel_count_++
261                 : channel_map.size ();
262
263   /* MIDI players tend to ignore instrument settings on channel
264      10, the percussion channel.  */
265   if (channel % 16 == 9)
266     {
267       channel_map["percussion"] = channel++;
268       channel_count_++;
269     }
270
271   if (channel > 15)
272     {
273       warning (_ ("MIDI channel wrapped around"));
274       warning (_ ("remapping modulo 16"));
275       channel = channel % 16;
276     }
277
278   channel_map[instrument] = channel;
279   return channel;
280 }
281
282 void
283 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
284 {
285   /* map each context (voice) to its own track */
286   Context *c = inf.origin_contexts (this)[0];
287   string voice;
288   if (c->is_alias (ly_symbol2scm ("Voice")))
289     voice = c->id_string ();
290   SCM channel_mapping = get_property ("midiChannelMapping");
291   string str = new_instrument_string ();
292   if (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
293     channel_ = get_channel (voice);
294   else if (channel_ < 0 && str.empty ())
295     channel_ = get_channel (str);
296   if (str.length ())
297     {
298       if (!scm_is_eq (channel_mapping, ly_symbol2scm ("voice")))
299         channel_ = get_channel (str);
300       set_instrument (channel_, voice);
301       set_instrument_name (voice);
302     }
303   Audio_staff *audio_staff = get_audio_staff (voice);
304   if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
305     {
306       ai->channel_ = channel_;
307       audio_staff->add_audio_item (ai);
308     }
309 }
310
311 Staff_performer::Midi_control_initializer::Midi_control_initializer
312 (Staff_performer *performer, Audio_staff *audio_staff, int channel)
313   : performer_ (performer),
314     audio_staff_ (audio_staff),
315     channel_ (channel)
316 {
317 }
318
319 SCM Staff_performer::Midi_control_initializer::get_property_value
320 (const char *property_name)
321 {
322   return performer_->get_property (property_name);
323 }
324
325 void Staff_performer::Midi_control_initializer::do_announce
326 (Audio_control_change *item)
327 {
328   item->channel_ = channel_;
329   audio_staff_->add_audio_item (item);
330   performer_->announce_element (Audio_element_info (item, 0));
331 }