5 Replies - 317 Views - Last Post: 21 June 2013 - 07:08 AM Rate Topic: -----

#1 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

How to change the Instrument?

Posted 21 June 2013 - 05:49 AM

Hello, I am trying to change the instrument but i'm unable to do it, always i hear a piano voice.
what's wrong in the following code?

import javax.sound.midi.*;

public class SoundApp {
    public void play(){
        try{
            Sequencer player = MidiSystem.getSequencer();
            player.open();
            
            Sequence seq = new Sequence(Sequence.PPQ, 4);
            
            Track track = seq.createTrack();
            
            ShortMessage first = new ShortMessage();
            first.setMessage(ShortMessage.PROGRAM_CHANGE, 1, 54, 0);
            MidiEvent change = new MidiEvent(first, 0);
            track.add(change);
            
            ShortMessage a = new ShortMessage();
            a.setMessage(144, 1, 30, 100);
            MidiEvent noteOn = new MidiEvent(a, 1);
            track.add(noteOn);
            
            ShortMessage b = new ShortMessage();
            a.setMessage(128, 1, 30, 100);
            MidiEvent noteOff = new MidiEvent(b, 16);
            track.add(noteOff);
            
            player.setSequence(seq);
            
            player.start();
     
              
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }


Thanks :)

Is This A Good Question/Topic? 0
  • +

Replies To: How to change the Instrument?

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: How to change the Instrument?

Posted 21 June 2013 - 06:14 AM

I don't see your code to change the actual instrument, and I haven't looked at MIDI before in Java. However, have you checked out this method?

http://docs.oracle.c...idi.Instrument)
Was This Post Helpful? 0
  • +
  • -

#3 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: How to change the Instrument?

Posted 21 June 2013 - 06:24 AM

The first setMessage method assigns the instrument in the above code but however the the only instrument it plays is a piano. is something wrong with the change instrument method?

ShortMessage first = new ShortMessage();
            first.setMessage(ShortMessage.PROGRAM_CHANGE, 1, 54, 0);
            MidiEvent change = new MidiEvent(first, 0);
            track.add(change);


Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: How to change the Instrument?

Posted 21 June 2013 - 06:32 AM

If I'm not mistaken, the 54 literal is specifying the instrument type. According to my Google searches, you're doing the rest of this right. The only thing that might be happening is that instrument 54 is not available. Have you tried others that you know to exist and seen the same issue?
Was This Post Helpful? 1
  • +
  • -

#5 NDragger  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 10-August 11

Re: How to change the Instrument?

Posted 21 June 2013 - 06:52 AM

Issue resolved, i tried changing the reference variable of ShortMessage and MidiEvent and it worked this time. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: How to change the Instrument?

Posted 21 June 2013 - 07:08 AM

I'm glad you solved it. Can you, just for the forum's sake, showing what you changed from post #3?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1