I want to send a MIDI note to a virtual port using Java. To send a MIDI note middle C (60), I do this:
public static void main(String[] main) throws MidiUnavailableException, InvalidMidiDataException, InterruptedException {
final MidiDevice.Info[] midiDeviceInfo = MidiSystem.getMidiDeviceInfo();
for (MidiDevice.Info info : midiDeviceInfo) {
System.out.println(info.getName());
}
MidiDevice.Info midiInfo = Arrays.stream(midiDeviceInfo)
.filter(midiDefInfo -> main[0].equalsIgnoreCase(midiDefInfo.getName()))
.findFirst().orElseThrow();
final MidiDevice midiDevice = MidiSystem.getMidiDevice(midiInfo);
midiDevice.open();
Receiver receiver = midiDevice.getReceiver();
final ShortMessage messageOn = new ShortMessage();
final ShortMessage messageOff = new ShortMessage();
messageOn.setMessage(ShortMessage.NOTE_ON, 1, 60, 112);
messageOff.setMessage(ShortMessage.NOTE_OFF, 1, 60, 0);
receiver.send(messageOn, -1);
Thread.sleep(1000L);
receiver.send(messageOff, -1);
}
Using MIDI-studio from MacOS, I created 'MyVirtualPort':
When running the program, it prints the available devices:
Gervill
Real Time Sequencer
MyVirtualPort
MyVirtualPort
When I pass "Gervill" as param, I hear the middle C for 1 second from my speakers. When picking the second "MyVirtualPort", an Exception is thrown for MIDI IN not available. When picking the first "MyVirtualPort", the program runs fine and it seems to send the messages. However, I see no activity on the virtual port.
I tried to run the program on Windows using loopMidi (https://www.tobias-erichsen.de/software/loopmidi.html), but the same thing happens: program runs fine and seems to send messages correctly, but nothing happens on the virtual device. Setting it to the default plays the middle C successfully on Windows as well.
What am I missing here? I am a newby to MIDI, so it is perfectly possible this is something trivial.
