6

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.

user1240362
  • 131
  • 1
  • 6
  • *"i can't get it to work"* Maybe it is on strike, or sleepy, or just plain lazy. For the first, negotiate. For the second, give it a good night's rest and try it in the morning. For the 3rd, try the carrot & stick approach. For anything else, try describing 1) What you expected to happen 2) What actually happened, and for utility 3) Why you expected (1) to happen. Also, for better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 29 '12 at 14:11

1 Answers1

7

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.

user1240362
  • 131
  • 1
  • 6