Java MIDI Synthesizer - Unable to change instruments

I can not make the tool change. I switch the value of the tool, but I get nothing in the output. I can only get a piano instrument, no matter what value I try. Below is a simple code. Anyone have any suggestions? Or am I missing a fundamental tool object?

import javax.sound.midi.*; //import javax.sound.*; public class Drum { static int instrument = 45; static int note = 100; static int timbre = 0; static int force = 100; public static void main(String[] args) { Synthesizer synth = null; try { synth = MidiSystem.getSynthesizer(); synth.open(); } catch (Exception e) { System.out.println(e); } Soundbank soundbank = synth.getDefaultSoundbank(); Instrument[] instr = soundbank.getInstruments(); synth.loadInstrument(instr[instrument]); //Changing this int (instrument) does nothing MidiChannel[] mc = synth.getChannels(); mc[4].noteOn(note, force); try { Thread.sleep(1000); } catch(InterruptedException e) {} System.out.println(instr[instrument].getName()); synth.close(); } } 
+8
java
source share
2 answers

You need to specify the channel to use the tool. I admit that I have never used MIDI in Java, but something like mc.programChange(instr.getPatch().getProgram()) sounds promising.

+10
source share

To play percussion instruments you need to use channel 10, this channel is used only for percussion instruments. (Http://en.wikipedia.org/wiki/General_MIDI)

For example:

 int instrument = 36; Sequence sequence = new Sequence(Sequence.PPQ, 1); Track track = sequence.createTrack(); ShortMessage sm = new ShortMessage( ); sm.setMessage(ShortMessage.PROGRAM_CHANGE, 9, instrument, 0); //9 ==> is the channel 10. track.add(new MidiEvent(sm, 0)); 

then each note you add will sound with percussion.

+3
source share

All Articles