I've been trying to play more than 1 instrument on a soft synthesizer using the javax.sound.midi.
package. Among the the 16 channels available with the synthesizer I tried using two of them in this manner.
try {
// Locating the default synthesizer
Synthesizer synth = MidiSystem.getSynthesizer();
// Opening the synthesizer
synth.open();
// Geting the available Midi channels
MidiChannel channels[] = synth.getChannels();
// Geting the synth's soundbank where all the sounds are stored
Soundbank bank = synth.getDefaultSoundbank();
// Loading all the available instruments
synth.loadAllInstruments(bank);
// Geting a list of the available instruments
Instrument instrs[] = synth.getLoadedInstruments();
Instrument shanai = null;
Instrument flute = null;
// Looping through the instruments
for (int i=0; i < instrs.length; i++)
{
if (instrs[i].getName().equals("Shanai"))
{
shanai = instrs[i];
}
else if(instrs[i].getName().equals("Flute"))
{
flute=instrs[i];
}
}
// patch containing the soundbank and program number
Patch shanaiPatch = shanai.getPatch();
Patch flutePatch = flute.getPatch();
channels[0].programChange(shanaiPatch.getBank(),
shanaiPatch.getProgram());
channels[1].programChange(flutePatch.getBank(),
flutePatch.getProgram());
channels[0].noteOn(32, 127);
channels[1].noteOn(32, 127);
Thread.sleep(1500);
channels[0].noteOff(32);
channels[1].noteOff(32);
}
The problem here is that this code only plays the instrument on channel 2 and not the one on channel 1. Possibly because the sleep function is after the second noteOn()
message. Then the question is as to how to run the two noteOns simulataneously?? Is that possible or is there a different approach altogether to play multiple instruments?
Any insight is greatly appreciated. Thanks.