1 Reply Latest reply on Nov 18, 2010 7:42 PM by captfoss

    Can't get Sysex to work in Java, any ideas?

    814565
      Can anyone please tell me what I'm doing wrong here. I'm trying to send Sysex from Java. But, whatever I try, no Sysex gets sent.

      I've written a small test application. This sends ShortMessage's easily. I can create ShortMessage's for note-ons and note-offs, and send them. I've been sending them to MaxMSP running on my Mac, and also to an external sound module (Yamaha TX7). I see the data arrive in MaxMSP, and hear the note ons and offs play on the TX.

      I can use a MIDI librarian to send Sysex data to Max/MSP and to the TX, and it all works fine.

      But, my Java program won't show any sign of sending Sysex. Nothing at all, not a single byte, arrives at MaxMSP, and there is no indication of any data being received by the TX. As you'll see in the code, I've been trying two different MIDI devices in addition to Max/MSP. Both send the note on and note offs through fine, but no sysex. But an external Sysex librarian program sends sysex through these devices fine, and the Yamaha TX is receiving it fine. Hence, I'm fairly sure that Java just isn't sending any Sysex.

      Here's my program, I've gone through the documentation, tutorials, and sample programs for this over and over again, and I just can't see any faults. Can anyone help?

      Thanks in anticipation,

      RPC

      import javax.sound.midi.*;
      import java.io.*;

      public class TestMIDI
      {
      private Receiver myReceiver;

      public TestMIDI( Receiver r )
      {
      myReceiver = r;
      }

      public void playNote() throws Exception
      {
      ShortMessage noteOn = new ShortMessage();
      noteOn.setMessage( (0x9<<4) | 0, 60, 64 );

      ShortMessage noteOff = new ShortMessage();
      noteOff.setMessage( (0x9<<4) | 0, 60, 0 );

      if ( myReceiver != null )
      {
      try { Thread.sleep( 1000 ); } catch( Exception e ) {}
           System.out.println( "Sending " );
      myReceiver.send( noteOn, 0 ); // not using timestamp

      try { Thread.sleep( 1000 ); } catch( Exception e ) {}

           System.out.println( "And off" );
      myReceiver.send( noteOff, 0 ); // not using timestamp

      try { Thread.sleep( 1000 ); } catch( Exception e ) {}
      }
      }

      public void sendSysex() throws Exception
      {
      byte data[] =
      {
      (byte) 0xF0, (byte) 0x43, (byte) 0x10, (byte) 0x01, (byte) 0x11,
      (byte) 0x58, (byte) 0xF7
      };

      SysexMessage sm = new SysexMessage();
      sm.setMessage( data, data.length );

      System.out.println( "Sysex message" );
      myReceiver.send( sm, 0 );
      try { Thread.sleep( 5000 ); } catch( Exception e ) {}
      System.out.println( "Sent the message" );
      }

      public static void main( String args[] ) throws Exception
      {
      Receiver r = null;

      MidiDevice.Info[] devs = MidiSystem.getMidiDeviceInfo();

      MidiDevice storeDevice = null;

      for ( int index = 0; index < devs.length; index++ )
      {
      System.out.println( devs[index].getName() );

      if ( storeDevice == null &&
           devs[index].getName().startsWith( "to MaxMSP" ))
           //devs[index].getName().startsWith( "USB" ))
           //devs[index].getName().startsWith( "FastTrack" ))
      {
           System.out.println( "Potential device" );

      MidiDevice theDevice = MidiSystem.getMidiDevice( devs[index] );

      theDevice.open();

           try
           {
           r = theDevice.getReceiver();
           System.out.println( "I got a receiver " + r );
           storeDevice = theDevice;
           }
           catch( Exception e )
           {
           theDevice.close();
           System.out.println( "I couldn't get a receiver" );
           }
      }
      }

      if ( storeDevice != null )
      {
      TestMIDI tm = new TestMIDI( r );

      tm.playNote();

      tm.sendSysex();

      tm.playNote();

      storeDevice.close();
      }
      }
      }
        • 1. Re: Can't get Sysex to work in Java, any ideas?
          captfoss
          I think perhaps you have to set the status bytes separately from the message...

          IE,
          public void sendSysex() throws Exception
          {
          byte data[] =
          { 
          (byte) 0x43, (byte) 0x10, (byte) 0x01, (byte) 0x11, 
          (byte) 0x58, (byte) 0xF7
          };
          
          SysexMessage sm = new SysexMessage();
          sm.setMessage( SysexMessage.SYSTEM_EXCLUSIVE, data, data.length );
          
          System.out.println( "Sysex message" );
          myReceiver.send( sm, 0 );
          try { Thread.sleep( 5000 ); } catch( Exception e ) {}
          System.out.println( "Sent the message" );
          }