- midi_stream_r << Midi_header (1, tracks_i, clocks_per_4_i);
- output_header_track (midi_stream_r);
- int n = 1;
- for (PCursor<Audio_staff*> i (audio_staff_l_list_); i.ok(); i++)
- i->output (midi_stream_r, n++);
+
+ midi_stream << Midi_header (1, tracks_i, clocks_per_4_i);
+ output_header_track (midi_stream);
+ message (_ ("Track...") + " ");
+ int channel = 0;
+ for (int i = 0; i < audio_staffs_.size (); i++)
+ {
+ Audio_staff *s = audio_staffs_[i];
+ if (be_verbose_global)
+ progress_indication ("[" + to_string (i));
+
+ /*
+ MIDI players tend to ignore instrument settings on
+ channel 10, the percussion channel by default.
+ */
+ if (channel % 16 == 9)
+ channel++;
+
+ /*
+ Huh? Why does each staff also have a separate channel? We
+ should map channels to voices, not staves. --hwn.
+ */
+ if (s->channel_ < 0)
+ {
+ s->channel_ = channel % 16;
+ if (channel > 15)
+ {
+ warning (_ ("MIDI channel wrapped around"));
+ warning (_ ("remapping modulo 16"));
+ }
+ }
+
+ s->output (midi_stream, channel++);
+ if (be_verbose_global)
+ progress_indication ("]");
+ }