Java MIDI合成器 - 无法改变乐器

我似乎无法让乐器改变。我切换了仪器的值,但输出没有任何不同。无论我尝试什么价值,我都只能拿钢琴乐器演奏。这是下面的简单代码。有没有人有什么建议?或者我错过了仪器对象的基本原理?
import javax.sound.midi.*;
//import javax.sound.*;

public class Drum {
    static int instrument = 45;
    static int note = 100;
    static int timbre = 0;
    static int force = 100;
    public static void main(String[] args) {        
        Synthesizer synth = null;
        try {
            synth = MidiSystem.getSynthesizer();
            synth.open();
        }
        catch (Exception e) {
            System.out.println(e);
        }
        Soundbank soundbank = synth.getDefaultSoundbank();
        Instrument[] instr = soundbank.getInstruments();
        synth.loadInstrument(instr[instrument]);    //Changing this int (instrument) does nothing
        MidiChannel[] mc = synth.getChannels();
        mc[4].noteOn(note, force);
        try { Thread.sleep(1000); } 
        catch(InterruptedException e) {}
        System.out.println(instr[instrument].getName());

        synth.close();

    }
} 
    
已邀请:
您需要告诉频道使用该乐器。我承认我从来没有在Java中使用MIDI,但是像
mc.programChange(instr.getPatch().getProgram())
这样的东西听起来很有希望。     
要播放打击乐器,您必须使用通道10,该通道仅用于打击乐器。 (http://en.wikipedia.org/wiki/General_MIDI) 例如:
int instrument = 36;

Sequence sequence = new Sequence(Sequence.PPQ, 1);

Track track = sequence.createTrack();


ShortMessage sm = new ShortMessage( );
sm.setMessage(ShortMessage.PROGRAM_CHANGE, 9, instrument, 0); //9 ==> is the channel 10.
track.add(new MidiEvent(sm, 0));
然后你添加它的每一个音符都会敲击打击声。     
您需要将程序更改事件发送到顺控程序。怎么样?发送短信。
sound.setMessage(ShortMessage.PROGRAM_CHANGE, channel, instrument, channel);
            long timeStam1p = -1;
            Receiver rcv1r = MidiSystem.getReceiver();
            rcv1r.send(sound, timeStam1p);
            sound.setMessage(ShortMessage.NOTE_ON, channel, note, velocity);
            long timeStamp = -1;
            Receiver rcvr = MidiSystem.getReceiver();
            rcvr.send(sound, timeStamp);
变量是channel(int)note(int),instrument(int),velocity(int)。 另外,我建议学习midi事件。事件是midi如何演奏音符,停止音符,改变乐器,节奏变化,控制变化等。我花了两年时间使用midi程序。     

要回复问题请先登录注册