Poguemahone21
Poguemahone21

Reputation: 13

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);

Upvotes: 1

Views: 482

Answers (2)

Alnitak
Alnitak

Reputation: 339776

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.

Upvotes: 1

tadge
tadge

Reputation: 96

There are only 128 total instrument programs in General MIDI, so you will not be able to use instrument 215, which probably doesn't exist.

There are a list of all the available midi instruments on Wikipedia, you might be able to find something quite close to what your looking for there.

Upvotes: 0

Related Questions