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

860 Views Asked by At

I'm loading a Soundbank of a TR-808 into Java and want to play that kit - the problem is I can't disable the default soundbank from playing along with the drums. Everything works when just using the GM sounds, but when loading a SoundBank everything gets mashed together.

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);
}

Anything look off here?

EDIT

I just found this, apparently I'm not the only one with this problem:

A guy writes

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.

Sadly, the link to Sun's forum doesn't work anymore (I'm looking at you, Oracle!) and the post is from 2009, so it's quite old in Internet years and I would've hoped this issue has been resolved - sadly it hasn't, it would seem.

I switched to using the Beads Project (http://beadsproject.net) and they also offer Midi support. I have the audio samples working perfectly (so much better than Java Audio!) and will try my luck with their Midi implementation in the near(-ish) future. If anyone's interested, I can post updates to this question as soon as I get results.

1

There are 1 best solutions below

0
On BEST ANSWER

According to the Javadocs, this is expected behaviour of the sequencer.

When calling:

MidiSystem.getSequencer()

Java will connect this sequencer instance with the default synthesizer. This is will output sound using the low-res default soundfonts. When you connect your custom synthesizer to the sequencer, the sequencer will send the sounds to both the default synthesizer and your custom one, creating two sounds.

On the other hand, when calling:

MidiSystem.getSequencer(false)

Java creates a sequencer that is not connected to any synthesizer. By default this sequencer would output no sound. Connecting it to the custom synthesizer that you create will only output the sounds from the loaded instruments.