I would like to have multiple MIDI instruments playing at once with NAudio. I've found instructions for how to play a single MIDI instrument, and I've found instructions for how to export multiple tracks in a single MidiEventCollection
to a file. However, I can't seem to put these ideas together.
Here is some dumb example code I have that cycles through all my MIDI instruments, playing a major 3rd harmony for each one:
var midiOut = new MidiOut(0);
for (var i = 0; i <= 127; i++)
{
midiOut.Send(MidiMessage.ChangePatch(i, 1).RawData);
midiOut.Send(MidiMessage.StartNote(60, 127, 1).RawData);
midiOut.Send(MidiMessage.StartNote(64, 127, 1).RawData);
Thread.Sleep(500);
}
This works fine of course, but if I wanted that C
andE
to be played by different instruments, I'd be out of luck. I only have the one MIDI device and I can only have one connection to that open at a time, and MidiOut
does not appear to support adding multiple tracks.
On the other hand, the MidiEventController
code looks like it would be more or less what I want, but I only see examples of exporting that to a file, rather than actually playing the events. I put together something like this:
var events = new MidiEventCollection(1, 120);
var track = events.AddTrack();
var setInstrument = new PatchChangeEvent(0, 1, 66);
var play = new NoteOnEvent(0, 1, 60, 127, 1000);
track.Add(setInstrument);
track.Add(play);
But at this point I cannot figure out how to actually play the track, rather than export it.
If you want to play two different patches at the same time, this is what MIDI channels are for.
At your disposal are 16 channels, of which channel 10 is reserved for percussion if you're using a GM scheme.
In your first code snippet, you appear to be using only MIDI channel 1.
How about using more than one channel and loading different patches for each channel?