1

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);
Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85

2 Answers2

1

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • The hint with the bank was just what I needed. In the Debug view I could see, that instruments[215] has bank number 2048. So I added this number to the programChange method call and it worked: mandolinChannel.programChange(2048, instruments[215].getPatch().getProgram()); – Poguemahone21 Mar 09 '19 at 05:52
  • If the code is intended to run on other systems then do use `getBank()` to find that number - there's no guarantee whatsoever that it'll always be 2048. – Alnitak Mar 11 '19 at 09:16
0

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.

tadge
  • 116
  • 6
  • *"There are a list of all the available midi instruments on Wikipedia"* The Java Sound API can provide its own [list of instruments](https://stackoverflow.com/a/7810530/418556). – Andrew Thompson Mar 01 '19 at 23:44