+int
+Staff_performer::get_channel (const string &instrument)
+{
+ SCM channel_mapping = get_property ("midiChannelMapping");
+ map<string, int> &channel_map
+ = (!scm_is_eq (channel_mapping, ly_symbol2scm ("instrument")))
+ ? channel_map_
+ : static_channel_map_;
+
+ if (scm_is_eq (channel_mapping, ly_symbol2scm ("staff"))
+ && channel_ >= 0)
+ return channel_;
+
+ map<string, int>::const_iterator i = channel_map.find (instrument);
+ if (i != channel_map.end ())
+ return i->second;
+
+ int channel = (scm_is_eq (channel_mapping, ly_symbol2scm ("staff")))
+ ? channel_count_++
+ : channel_map.size ();
+
+ /* MIDI players tend to ignore instrument settings on channel
+ 10, the percussion channel. */
+ if (channel % 16 == 9)
+ {
+ channel_map["percussion"] = channel++;
+ channel_count_++;
+ }
+
+ if (channel > 15)
+ {
+ warning (_ ("MIDI channel wrapped around"));
+ warning (_ ("remapping modulo 16"));
+ channel = channel % 16;
+ }
+
+ channel_map[instrument] = channel;
+ return channel;
+}
+