Question

I am making a simple application to playback MIDI files in Java.
I am using the javax.sound.midi library.

I can retrieve my MIDI Out Device correctly (USB to MIDI Cable).

But when I playback the sequence it always plays in the default sound synthesizer which is the host OS's General Midi (Soundfont) playback.

The problem I am having is changing the default MIDI Out.

When I execute

Receiver MidiOutReceiver; //instantiated earlier
Sequencer MidiOutSequencer = MidiSystem.getSequencer();
MidiOutSequencer.open();
MidiOutSequencer.start();

I hear the MIDI sequence reproduction out of the speakers.

After referring to the documentation

3.10.
The "Java Sound Sequencer" is implicitly connected to the default Synthesizer, 
3.5.
In your program you can obtain a Synthesizer instance yourself and connect 
it to a sequencer explicitly using a Transmitter / Receiver pair. 
So you know which Synthesizer is used. 
In case you obtained a Sequencer that is pre-connected to a Synthesizer,
you can find out the Synthesizer by using the method getTransmitters().
But there is not mention of how to explicitly use this Transmitter / Receiver pair and instantiate the Sequencer object with the new Default Synthesizer.

I have the correct Transmitter and Receiver. But they are not playing out of the MIDI out.

Also when I try

MidiSystem.getTransmitter().setReceiver(MidiOutReceiver);

It does not seem to change anything.

How do I explicitly use this Receiver / Transmitter Pair with a Synthesizer so that the Default Sequencer will use my MIDI out port?

Was it helpful?

Solution

I overlooked something simple. The default Sequencer returned by MidiSystem.getSequencer(); has a Transmitter which in turn has a setReceiver() method to add a MIDI Out device. But you must have the correct MIDI Out device. I had chosen a wrong device by accident.

So first the correct output device is chosen by looking through the DeviceInfos array, picking the correct device, then instatiating the the Sequencer with the correct Receiver.

MidiDevice.Info[] MidiDeviceInfos = MidiSystem.getMidiDeviceInfo();
//find the suitable device number here, based on some criteria
MidiDevice MidiOutDevice = MidiSystem.getMidiDevice(MidiDeviceInfos[DEVICE_NUMBER]);
Receiver MidiOutReceiver = MidiOutDevice.getReceiver();
Sequencer MidiOutSequencer = MidiSystem.getSequencer();
//Add the new MIDI out device here.
MidiOutSequencer.getTransmitter().setReceiver(MidiOutReceiver);
MidiOutSequencer.open();

Now you can open() a file and play() it, but since the default MIDI Out device is also used you may hear General MIDI/Soundfont MIDI sounds too. This threw me off at first but now the MIDI device will also be sending the opened sequence out of your added Receiver.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top