- time_t t = time (0);
-
- // perhaps multiple text events?
- String str = String ("Creator: ") + get_version_str() + "\n";
-
- Midi_text creator (Midi_text::TEXT, str);
- midi_track.add (Moment (0), &creator);
-
- str = "Automatically generated at ";
- str += ctime (&t);
- str = str.left_str (str.length_i() - 1);
- str += "\n";
- Midi_text generate (Midi_text::TEXT, str);
- midi_track.add (Moment (0), &generate);
-
- str = "from musical definition: ";
-
- str += origin_str_;
- Midi_text from (Midi_text::TEXT, str);
- midi_track.add (Moment (0), &from);
-
- Midi_text track_name (Midi_text::TRACK_NAME, "Track "
- + String_convert::i2dec_str (0, 0, '0'));
- midi_track.add (Moment (0), &track_name);
-
- Midi_tempo tempo (midi_l_->get_tempo_i (Moment (1, 4)));
- midi_track.add (Moment (0), &tempo);
-
- midi_stream_r << midi_track;
+ int channel = 0;
+ for (vsize i = 0; i < audio_staffs_.size (); i++)
+ {
+ Audio_staff *s = audio_staffs_[i];
+ if (be_verbose_global)
+ progress_indication ("[" + to_string (i));
+
+ int midi_channel = s->channel_;
+
+ if (midi_channel < 0)
+ {
+ midi_channel = channel;
+ channel ++;
+ /*
+ MIDI players tend to ignore instrument settings on
+ channel 10, the percussion channel.
+ */
+ 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 (midi_channel > 15)
+ {
+ warning (_ ("MIDI channel wrapped around"));
+ warning (_ ("remapping modulo 16"));
+
+ midi_channel = midi_channel % 16;
+ }
+
+ s->output (midi_stream, midi_channel);
+ if (be_verbose_global)
+ progress_indication ("]");
+ }