]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-performer.cc
Midi: allow alternate channel mappings: per instrument, staff, voice.
[lilypond.git] / lily / staff-performer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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
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"
29
30 /* Perform a staff. Individual notes should have their instrument
31   (staff-wide) set, so we override play_element ()
32 */
33 class Staff_performer : public Performer
34 {
35 public:
36   TRANSLATOR_DECLARATIONS (Staff_performer);
37   ~Staff_performer ();
38
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 ();
45
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);
54
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_;
66 };
67
68 map<string, int> Staff_performer::static_channel_map_;
69 int Staff_performer::channel_count_ = 0;
70
71 #include "translator.icc"
72
73 ADD_TRANSLATOR (Staff_performer,
74                 /* doc */
75                 "",
76
77                 /* create */
78                 "",
79
80                 /* read */
81                 "",
82
83                 /* write */
84                 "");
85
86 Staff_performer::Staff_performer ()
87   : channel_ (0)
88   , instrument_ (0)
89   , instrument_name_ (0)
90   , name_ (0)
91   , tempo_ (0)
92 {
93 }
94
95 Staff_performer::~Staff_performer ()
96 {
97 }
98
99 void
100 Staff_performer::initialize ()
101 {
102 }
103
104 Audio_staff*
105 Staff_performer::new_audio_staff (string voice)
106 {
107   Audio_staff* audio_staff = new Audio_staff;
108   name_ = new Audio_text (Audio_text::TRACK_NAME, context ()->id_string ()
109                           + ":" + voice);
110   audio_staff->add_audio_item (name_);
111   announce_element (Audio_element_info (audio_staff, 0));
112   announce_element (Audio_element_info (name_, 0));
113   staff_map_[voice] = audio_staff;
114   return audio_staff;
115 }
116
117 Audio_staff*
118 Staff_performer::get_audio_staff (string voice)
119 {
120   SCM channel_mapping = get_property ("midiChannelMapping");
121   if (channel_mapping == ly_symbol2scm ("voice")
122       && staff_map_.size ())
123     return staff_map_.begin ()->second;
124
125   map<string, Audio_staff*>::const_iterator i = staff_map_.find (voice);
126   if (i != staff_map_.end ())
127     return i->second;
128   map<string, Audio_staff*>::const_iterator e = staff_map_.find ("");
129   if (staff_map_.size () == 1 && e != staff_map_.end ())
130     {
131       staff_map_[voice] = e->second;
132       return e->second;
133     }
134   return new_audio_staff (voice);
135 }
136
137 Real
138 Staff_performer::get_dynamic (string voice)
139 {
140   map<string, Real>::const_iterator i = dynamic_map_.find (voice);
141   if (i != dynamic_map_.end ())
142     return i->second;
143   return 0;
144 }
145
146 void
147 Staff_performer::process_music ()
148 {
149 }
150
151 void
152 Staff_performer::set_instrument (int channel, string voice)
153 {
154   instrument_ = new Audio_instrument (instrument_string_);
155   instrument_->channel_ = channel;
156   announce_element (Audio_element_info (instrument_, 0));
157   Audio_staff* audio_staff = get_audio_staff (voice);
158   audio_staff->add_audio_item (instrument_);
159   SCM proc = ly_lily_module_constant ("percussion?");
160   SCM drums = scm_call_1 (proc, ly_symbol2scm (instrument_string_.c_str ()));
161   audio_staff->percussion_ = (drums == SCM_BOOL_T);
162 }
163
164 void
165 Staff_performer::set_instrument_name (string voice)
166 {
167   instrument_name_ = new Audio_text (Audio_text::INSTRUMENT_NAME,
168                                      instrument_string_);
169   announce_element (Audio_element_info (instrument_name_, 0));
170   get_audio_staff (voice)->add_audio_item (instrument_name_);
171 }
172
173 void
174 Staff_performer::stop_translation_timestep ()
175 {
176   name_ = 0;
177   tempo_ = 0;
178   instrument_name_ = 0;
179   instrument_ = 0;
180 }
181
182 void
183 Staff_performer::finalize ()
184 {
185   staff_map_.clear ();
186   channel_map_.clear ();
187   channel_count_ = 0;
188   static_channel_map_.clear ();
189 }
190
191 string
192 Staff_performer::new_instrument_string ()
193 {
194   // mustn't ask Score for instrument: it will return piano!
195   SCM minstr = get_property ("midiInstrument");
196
197   if (!scm_is_string (minstr)
198       || ly_scm2string (minstr) == instrument_string_)
199     return "";
200
201   instrument_string_ = ly_scm2string (minstr);
202
203   return instrument_string_;
204 }
205
206 int
207 Staff_performer::get_channel (string instrument)
208 {
209   SCM channel_mapping = get_property ("midiChannelMapping");
210   map<string, int>& channel_map
211     = (channel_mapping != ly_symbol2scm ("instrument"))
212     ? channel_map_
213     : static_channel_map_;
214
215   map<string, int>::const_iterator i = channel_map.find (instrument);
216   if (i != channel_map.end ())
217     return i->second;
218  
219   int channel = (channel_mapping == ly_symbol2scm ("staff"))
220     ? channel_count_++
221     : channel_map.size ();
222
223   /* MIDI players tend to ignore instrument settings on channel
224      10, the percussion channel.  */
225   if (channel % 16 == 9)
226     channel_map["percussion"] = channel++;
227
228   if (channel > 15)
229     {
230       warning (_ ("MIDI channel wrapped around"));
231       warning (_ ("remapping modulo 16"));
232       channel = channel % 16; 
233     }
234
235   channel_map[instrument] = channel;
236   return channel;
237 }
238
239 void
240 Staff_performer::acknowledge_audio_element (Audio_element_info inf)
241 {
242   if (Audio_item *ai = dynamic_cast<Audio_item *> (inf.elem_))
243     {
244       /* map each context (voice) to its own track */
245       Context* c = inf.origin_contexts (this)[0];
246       string voice;
247       if (c->is_alias (ly_symbol2scm ("Voice")))
248         voice = c->id_string ();
249       SCM channel_mapping = get_property ("midiChannelMapping");
250       if (channel_mapping == ly_symbol2scm ("voice"))
251         channel_ = get_channel (voice);
252       string str = new_instrument_string ();
253       if (str.length ())
254         {
255           if (channel_mapping != ly_symbol2scm ("voice"))
256             channel_ = get_channel (str);
257           set_instrument (channel_, voice);
258           set_instrument_name (voice);
259         }
260       Audio_staff* audio_staff = get_audio_staff (voice);
261       ai->channel_ = channel_;
262       if (Audio_dynamic *d = dynamic_cast<Audio_dynamic *> (inf.elem_))
263         dynamic_map_[voice] = d->volume_;
264       if (Real d = get_dynamic (voice))
265         if (Audio_note *n = dynamic_cast<Audio_note *> (inf.elem_))
266           n->volume_ = d;
267       audio_staff->add_audio_item (ai);
268     }
269 }
270