javamidijavasoundsynthesizer

MIDI instrument not working for javax.sound.midi.MidiChannel


I am currently working on an application to play some MIDI with different instruments. I am using the javax.sound.midi.MidiChannel for this and it works fine with guitar (index 25) and bass (index 32). Now I want to add a Mandolin channel; according to synthesizer.getDefaultSoundbank().getInstruments(), this is index 215. But with the code below, the program of the Mandolin channel gets set to 25 (same as guitar channel). According to the documentation, only values between 0 and 127 are allowed at the programChange() method... Any ideas how I can configure my channel to work with Mandolin as instrument?

        Synthesizer synthesizer = MidiSystem.getSynthesizer();
        synthesizer.open();

        final Instrument[] instruments = synthesizer.getDefaultSoundbank().getInstruments();

        MidiChannel guitarChannel = synthesizer.getChannels()[0];
        guitarChannel.programChange(instruments[25].getPatch().getProgram());

        MidiChannel bassChannel = synthesizer.getChannels()[1];
        bassChannel.programChange(instruments[32].getPatch().getProgram());

        MidiChannel mandolinChannel = synthesizer.getChannels()[2];
        mandolinChannel.programChange(instruments[215].getPatch().getProgram());

        mandolinChannel.noteOn(note, 100);

Solution

  • In theory you'll need to use the .getBank() method of the patch and supply both the bank number and the program number to .programChange().

    In practise, when I try to replicate your issue myself, it seems that the default com.sun.media.sound.SoftSynthesizer doesn't appear to support bank changes at all.