Changing an instrument while playing a track - MIDI / Java

I use the track to play noteOn / noteOff events, and everything works as planned, the problem I am facing is that I would like to change the tools that are used in the track.

I came up with the following code, which is designed to invoke the β€œprogram change” command on all MIDI channels with a new instrument, the problem I am facing is how to apply this to the track so that the instruments are different.

public void LoadInstrument() { for(int i = 0; i < instruments.length; i++) { if(instruments[i].getName() == "Clean Guitar") { instrumentToLoad = instruments[i]; } } drumPatch = instrumentToLoad.getPatch(); } 

I saw that you can send the PROGRAM_CHANGE event to the track to show that the tool is changing, but I'm not sure how to do this to create an object to store the necessary information and add it to the Track. Many thanks.

+1
source share
2 answers

After I worked a little, I found a solution:

  try { ShortMessage instrumentChange = new ShortMessage(); instrumentChange.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 6,0); //MidiEvent instrumentChange = new MidiEvent(ShortMessage.PROGRAM_CHANGE,drumPatch.getBank(),drumPatch.getProgram()); track.add(new MidiEvent(instrumentChange,0)); } catch(Exception e) { //Handle } 

Note. The " 6 " parameter in the .setMessage method is the number of the instrument to play.

+4
source

I assume that drumPatch.getBank() and drumPatch.getProgram() will apply the same program to the current channel that is currently being played. You should probably pass the new bank and program number to your LoadInstrument() method and pass this instead of the program change argument.

0
source

Source: https://habr.com/ru/post/1309690/


All Articles