1

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.

BitWriter
  • 35
  • 7
  • Does it work if you comment out the noteOn/Off calls for the second channel? – CL. May 27 '17 at 16:08
  • @CL. Yes, the instrument on channel 1 starts playing when I comment out the second channel. Infact on commenting out the first channel the note on the second channel works. All in all it seems the channels are pretty much functional. ThankYou for responding. – BitWriter May 28 '17 at 03:49
  • How do you get the channel objects? – CL. May 28 '17 at 07:22
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 28 '17 at 08:53
  • @AndrewThompson thanks for the help. – BitWriter May 29 '17 at 07:00
  • @CL. I have done some edits and added the code which I have used to get the channel objects. I was thinking whether or not running two parallel threads to play the instruments could be a possible solution. Let me know if I am on the right track. Thanks. – BitWriter May 29 '17 at 07:04
  • *"thanks for the help"* That's odd, you're thanking me for help & have not done what I suggested.. – Andrew Thompson May 29 '17 at 07:30
  • In theory, this is how `getChannels()` is supposed to be used, and channels should not interfere with each other. Please provide a [mcve], i.e., a *small* and *complete* program that just play two fixed notes with fixed instruments. – CL. May 29 '17 at 09:02
  • @CL. Yeah I get your point. I've tried minimising the code. But the problem still seems to exist. Is there a possible way of playing the 2 noteOns simultaneously? I'm trying to achieve the feel of an orchestra. With multiple number of instruments playing a music all at one go. – BitWriter May 29 '17 at 13:25
  • Does it work if you use a different note number for the other channel? – CL. May 29 '17 at 13:44
  • It worked. I could differentiate between the two instruments by playing a different note number on the other channel. Realised what was going wrong. Couldn't differentiate between the two instruments earlier, playing the same note. Thanks. – BitWriter May 29 '17 at 14:16

0 Answers0