1 Reply Latest reply: Jan 23, 2011 2:12 PM by 833558 RSS

    Changing instrument during Track playback

    833558
      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.


      public void LoadInstrument()
      {
      for(int i = 0; i < instruments.length; i++)
      {
      if(instruments.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.

      (Apologies for the poor code layout, I can't seem to find a suitable markup type)
        • 1. Re: Changing instrument during Track playback
          833558
          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.