3

Hey I am trying to send MIDI data from a Java class to a MIDI device connected via USB. I did this once like 2 years ago and it worked, but I somehow can't find the project anymore.

The example Java code runs fine;

   myMsg = new ShortMessage();
   myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
   timeStamp = -1;
   Receiver rcvr = MidiSystem.getReceiver();
   rcvr.send(myMsg, timeStamp);

Simple stuff. 5 lines of code and the message appears on the device. The problem is, that this way, only the standard device is set up and ready to receive MIDI. I can't ask my users to set the device of desire as standard device every time they want to use my application. (the Receiver acts as output destination/port to the input of the physical device I am connected to)

I am now trying to set up the Receiver by doing the following:

   MidiDevice.Info[] infoA=MidiSystem.getMidiDeviceInfo();//array where all the device info goes

    for (int x=0;x<infoA.length;x++){
        System.out.println("in "+infoA[x]); //this displays all the devices
        }
        MidiSystem.getMidiDevice(infoA[d]); //d is set to an integer that represents the item number on the device list of the device I wanna send MIDI to
        System.out.println(infoA[d]);//last check if the correct device is selected
        MidiDevice MidiOutDevice = MidiSystem.getMidiDevice(infoA[d]); //setting this to "my" device in order to set the Receiver
        maschineReceiver= MidiOutDevice.getReceiver();
        Sequencer MidiOutSequencer = MidiSystem.getSequencer(); 
        MidiOutSequencer.getTransmitter().setReceiver(maschineReceiver); //probably unnecessary last 2 line but I gave this a try in case it helps 

if I now do maschineReceiver.send(myMsg, timeStamp);, nothing happens at all. I also tried different devices but it didn't get any better. I am sure it can't be a very hard difficult thing to do as it something I actually achieved 2 years ago when my coding skills were awful but I just can't find the mistake right now, no matter how often I reread the Java documentation, it just won't work whatever I do.

Thanks in advance

user2875404
  • 3,048
  • 3
  • 25
  • 47

1 Answers1

1

To actually reserve a device for your program, you need to use the MidiDevice method open:

if (!(device.isOpen())) {
    try {
      device.open();
  } catch (MidiUnavailableException e) {
          // Handle or throw exception...
  }
}
sasankad
  • 3,543
  • 3
  • 24
  • 31