Access MidiDevice in Java

I am currently trying to connect my midiport piano to my computer. I read everything I could find about it, but for some reason I am missing something, so I hope someone here can help me. I have been trying to do this for a week, and it complicates things.

public class MidiDeviceGetter { public MidiDeviceGetter() {} public static void listTransmitterDevices() throws MidiUnavailableException { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if (device.getMaxTransmitters() != 0) System.out.println(device.getDeviceInfo().getName().toString() + " has transmitters"); } } // should get me my USB MIDI Interface. There are two of them but only one // has Transmitters so the if statement should get me the one i want public static MidiDevice getInputDevice() throws MidiUnavailableException { MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo(); for (int i = 0; i < infos.length; i++) { MidiDevice device = MidiSystem.getMidiDevice(infos[i]); if (device.getMaxTransmitters() != 0 && device.getDeviceInfo().getName().contains("USB")) { System.out.println(device.getDeviceInfo().getName().toString() + " was chosen"); return device; } } return null; } public static void main(String[] args) throws MidiUnavailableException, IOException { MidiDevice inputDevice; // MidiDeviceGetter.listTransmitterDevices(); inputDevice = MidiDeviceGetter.getInputDevice(); // just to make sure that i got the right one System.out.println(inputDevice.getDeviceInfo().getName().toString()); System.out.println(inputDevice.getMaxTransmitters()); // opening the device System.out.println("open inputDevice: " + inputDevice.getDeviceInfo().toString()); inputDevice.open(); System.out.println("connect Transmitter to Receiver"); // Creating a Dumpreceiver and setting up the Midi wiring Receiver r = new DumpReceiver(System.out); Transmitter t = inputDevice.getTransmitter(); t.setReceiver(r); System.out.println("connected."); System.out.println("running..."); System.in.read(); // at this point the console should print out at least something, as the // send method of the receiver should be called when i hit a key on my // keyboard System.out.println("close inputDevice: " + inputDevice.getDeviceInfo().toString()); inputDevice.close(); System.out.println(("Received " + ((DumpReceiver) r).seCount + " sysex messages with a total of " + ((DumpReceiver) r).seByteCount + " bytes")); System.out.println(("Received " + ((DumpReceiver) r).smCount + " short messages with a total of " + ((DumpReceiver) r).smByteCount + " bytes")); System.out.println(("Received a total of " + (((DumpReceiver) r).smByteCount + ((DumpReceiver) r).seByteCount) + " bytes")); } } 

Well, that’s what I have so far. I just wanted to tie the piano so that I could go further from there, but, as I said, I can’t get it to work.

For testing, I took the DumpReceiver class from http://www.jsresources.org/examples/DumpReceiver.java.html .

I am very grateful for any help, thanks.

PS: And sorry for my English, I'm not a native speaker.

Edit1: According to the comment:

I expect the program to print something in the console when I press the System.in () key is running, because in the Receiver send (Midimessage, long) method, the last line is Prinstream.print (midimsg). I’m right in assuming that the method of sending the receiver interface class is always called if the note to which the receiver is connected is played on the transmitter, isn’t it? If this did not work, I could understand this, but there are also some recipient membervariables that must retain the number of deleted keys, but these membervariables are always zero. So my main problem is that the send method is never called. I hope I made it clear that my main problem.

Edit2: If you are in all this "midi programming in java" and you do not see any serious errors, please tell me. I just found out that I cannot get any Midisignals in Steinbergs Cubase. Perhaps this time the problem was not in front of the screen.

+6
source share
1 answer

Ok, I figured it out. The code I posted is complete and correct. The problem was that the MIDI IN Plug of my usb midi interface belongs to my piano's MIDI OUT connector. Guess that for a moment I hit my head against the wall.

Thank you for reading.

+7
source

Source: https://habr.com/ru/post/909632/


All Articles