Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a MidiDevice in Java

Tags:

java

midi

I'm currently trying to connect my piano's midiport to my computer. I've read everything I could find about that, but somehow I'm missing something, so I hope someone here can help me. I'm trying to do this for a week now and its getting realy frustrating.

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, thats what i've got so far. I just wanted to get the piano connected so I can 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'd greatly appreciate any help, thanks.

P.S.: And sorry for my english, I'm no native speaker.

Edit1: According to the comment:

I expect the programm to print something in the console when i hit a key while System.in() is running, because in the Receiver's send(Midimessage, long) method the last line is Prinstream.print(midimsg). I am right, thinking that the send method of the Receiver interface class is always called if a Note is played on the Transmitter that the Receiver is connected to, ain't I? If only that wouldn't work I could figure it out, but there are also some membervariables of the receiver that should save the number of keys that were hit, but these membervariables are always null. So my main problem here is, that the send method is never called. I hope i made clear what my main problem is.

Edit2: If you are into this whole "midi programming in java"-thing and you don't see any major mistakes, then please tell me. I just found out that I can't get any Midisignals in Steinbergs Cubase either. Perhaps this time, the problem wasn't in front of the screen.

like image 973
user1240362 Avatar asked Feb 29 '12 13:02

user1240362


1 Answers

Ok I figured it out. The code i posted is totaly right and working. The problem was, that the MIDI IN Plug of my usb midi interface belongs to the MIDI OUT plug of my piano. Guess I'm gonna smash my head against a wall now for a few hours.

Thanks for reading.

like image 94
user1240362 Avatar answered Sep 20 '22 23:09

user1240362