Java Midi 默认音库与新音库同时播放

Java Midi default soundbank playing at the same time as the new one

我正在将 TR-808 的 Soundbank 加载到 Java 并想演奏该套件 - 问题是我无法禁用默认音库与鼓一起演奏。 仅使用 GM 声音时一切正常,但加载 SoundBank 时一切都混在一起。

player = MidiSystem.getSequencer();
player.open();
seqTrans = player.getTransmitter();
synth   = MidiSystem.getSynthesizer();
synthRcvr = synth.getReceiver(); 
seqTrans.setReceiver(synthRcvr);  
synth.open();

File file = new File("resources/TR-808.sf2");
Soundbank soundbank = MidiSystem.getSoundbank(file);
// synth.unloadAllInstruments(synth.getDefaultSoundbank()); don't work:(
synth.loadAllInstruments(soundbank);

Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();

ShortMessage instr = new ShortMessage();
instr.setMessage(192, channel, 35, 0);
MidiEvent changeInstrument = new MidiEvent(instr, 1);
track.add(changeInstrument);

for(int i = 1; i < 30; i++){
    ShortMessage a = new ShortMessage();
    a.setMessage(144, channel, 35, 100);
    MidiEvent noteOn = new MidiEvent(a, 4+4*i);
    track.add(noteOn);
}

这里有什么好看的吗?

编辑

我刚发现这个,显然我不是唯一遇到 this 问题的人:

一个人写

Someone here seems to have had the same problem, with the default soundbank playing at the same time as the new one: http://forums.sun.com/thread.jspa?threadID=5182082. Their "solved" reply doesn't give much info though. Something to do with the location of their new soundbank's .gm file.

可悲的是,Sun 论坛的 link 不再有效(我在看着你,Oracle!)而 post 是 2009 年的,所以在互联网时代已经很老了我希望这个问题已经得到解决 - 遗憾的是它似乎没有。

我转而使用 Beads Project (http://beadsproject.net),他们也提供 Midi 支持。我的音频样本工作得很好(比 Java 音频好得多!),我会在不久的将来试试他们的 Midi 实现。如果有人感兴趣,我可以在得到结果后立即post更新这个问题。

根据 Java文档,这是音序器的预期行为。

调用时:

MidiSystem.getSequencer()

Java 会将此音序器实例与默认合成器连接。这将使用低分辨率默认声音字体输出声音。当您将自定义合成器连接到音序器时,音序器会将声音发送到默认合成器和您的自定义合成器,从而创建两种声音。

另一方面,调用时:

MidiSystem.getSequencer(false)

Java 创建一个未连接到任何合成器的音序器。默认情况下,此音序器不会输出任何声音。将它连接到您创建的自定义合成器将仅输出来自加载乐器的声音。