Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Applying a MIDI instrument change to a Track

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

         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.
    Last edited by AnkleSpankle; January 23rd, 2011 at 01:53 PM. Reason: More information


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Applying a MIDI instrument change to a Track

    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.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Applying a MIDI instrument change to a Track

    Glad you found a solution, and thanks for posting your solution to the forums.

  4. The Following User Says Thank You to copeg For This Useful Post:

    AnkleSpankle (January 23rd, 2011)

  5. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Applying a MIDI instrument change to a Track

    Quote Originally Posted by copeg View Post
    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.

Similar Threads

  1. [SOLVED] Can't get boolean to change!!
    By Day2Day in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 2nd, 2010, 07:20 PM
  2. Change to JOptionPane
    By Liuric in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 12:07 AM
  3. how to using button to change linechart
    By NARs in forum AWT / Java Swing
    Replies: 3
    Last Post: October 30th, 2009, 12:53 PM
  4. Track the user session list
    By kalees in forum Java Servlet
    Replies: 3
    Last Post: October 24th, 2009, 02:34 PM
  5. [SOLVED] Change the size of an image
    By subhvi in forum Algorithms & Recursion
    Replies: 4
    Last Post: August 23rd, 2009, 11:44 PM

Tags for this Thread