Reputation: 9966
I am using a track to play my noteOn/noteOff events and everything works as intended, the problem I am having is that I would like to change the instruments that is being used within the track.
I've come up with the following code which is designed invoke the "program change" command on all the MIDI channels with the new instrument, the problem I am having is working out how to apply this to the track so the instruments is 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've seen that you can send the Track a PROGRAM_CHANGE event to signify the instrument is changing but I'm not sure how to go about creating the object to hold the necessary information and adding it to the Track. Many Thanks.
Upvotes: 1
Views: 4647
Reputation: 9966
After doing a little more digging around I've found the 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.
Upvotes: 4
Reputation: 40390
My guess would be that drumPatch.getBank()
and drumPatch.getProgram()
will apply the same program to the current channel as is currently playing. You should probably pass in the new bank and program number to your LoadInstrument()
method and pass that to the program change argument instead.
Upvotes: 0