Changing Instrument during track playback
Hello,
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.
Code :
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.
Re: Applying a MIDI instrument change to a Track
After doing a little more digging around I've found the solution:
Code :
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.
Re: Applying a MIDI instrument change to a Track
Glad you found a solution, and thanks for posting your solution to the forums.
Re: Applying a MIDI instrument change to a Track
Quote:
Originally Posted by
copeg
Glad you found a solution, and thanks for posting your solution to the forums.
Not a problem, I had a pretty hard time trying to find the solution so it should make it easier for other people if they're stuck as well.