Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Receive Data from Midi Device with Java Sound API

    Hi,

    I haven't been able to find a cut and paste working example of how to receive MIDI data from a MIDI device, like my piano, and display something to System.out.
    There are all sorts of examples for synthesizers, sequencers and sending data to a MIDI device, but I have not been able to find a single example of how to receive
    MIDI data and just display a simple message. I just need something simple that works to start playing with it, and understanding how it works. Having a working example would also help understanding the Sound API descriptions. They read like differential equations texts books to me, i.e. not clear at all.

    I have a working example of how to poll all devices and getDevice.info, which is supposed to then be used to address a specific MIDI device.

    It's just all very confusing.
    I've also looked at a number of books, in the store and on line.
    I haven't seen a single example anywhere.

    the closest I found to working examples was on StackOverflow, but I didn't understand some of the other code in the examples, which may or may not have been working correctly anyway.

    Any help/guidance/patience from someone who understands would be tremendously appreciated.

    Thanks in advance,
    Mike

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Receive Data from Midi Device with Java Sound API

    I agree the API is difficult and counter intuitive. I still get receiver and transmitter backwards because I fail to alter my point of view of the audio system. However, I replied to some similar threads a while ago. Here is a link to one which may help. help with Java real-time midi
    Make certain you read the entire thread because it has several different code snippets.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: Receive Data from Midi Device with Java Sound API

    Hi Jim,

    Thank you very much for your reply.
    I will definitely check out your posts in their entirety.
    If it is not against this forum's policies, I also found some info on StackOverFlow recently, under the title, "Java MIDI - getting data from piano?"

    I'll try to provide some feedback to this post with my progress if that would help others.

    Thanks again,
    Mike

  4. #4
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: Receive Data from Midi Device with Java Sound API

    Hi Jim,

    I read your earlier posts all the way through and they show how you can:
    - Play a midi file on a midi capable device, and
    - Send messages to the Midi device.

    I want to receive messages from a Midi device.

    I understand about the MidiSystem.getMidiDevice(Info info) part.

    Besides being confused about the code structure to accomplish this, I wasn't sure if I need to create a listener or a loop to process incoming Midi Messages and keep the program Main from exiting.

    Thanks,
    Mike

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Receive Data from Midi Device with Java Sound API

    Here, try this. Note that you do not have to implement your own receiver if you just want the PC to accept MIDI tones. I did it so I
    could intercept and view the messages.

    Java Code:
    public class MidiDemo {
    
     public MidiDevice    input;
     public MidiDevice    output;
    
    public static void main(String[] args) {
          new MidiDemo().start();
       } 
      
       
       public void start() {
          init();  // initialize your midi input device
           // system dependent
          
     
          try {
             output.open(); // From midi device
             rcvr = new MyReceiver();
             MidiSystem.getTransmitter().setReceiver(rcvr);
          }
          catch (Exception e) {
             e.printStackTrace();
             System.exit(0);
          }
     
       }
     
       private class MyReceiver implements Receiver  {
          Receiver rcvr;
          public MyReceiver() {
             try {
             this.rcvr = MidiSystem.getReceiver();
             } catch (MidiUnavailableException mue) {
                mue.printStackTrace();
             }
          }
          
          @Override
          public void send(MidiMessage message, long timeStamp) {
             byte[] b = message.getMessage();
             if (b[0] != (byte)254) {
             System.out.println(b[0] & 0xFF);
             }
             rcvr.send(message, timeStamp);
          }
    
          @Override
          public void close() {
             rcvr.close();
          }
       } 
       public void init() {
       // init your midi devices here
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: Receive Data from Midi Device with Java Sound API

    Hi Jim,

    Thank you very much for the code.

    My code didn't compile until I added Receiver to the beginning of line 18, Receiver rcvr = new MyReceiver();

    Then I received the following error:

    Your line 17, is my line 22: i.e., output.open(); // From midi device

    java.lang.NullPointerException
    at MidiDemo.start(MidiDemo.java:22)
    at MidiDemo.main(MidiDemo.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main (AppMain.java:120)

    Maybe in the init() method, I should have put the getDevice.info code?

    Thanks,
    Mike

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,540
    Rep Power
    5

    Default Re: Receive Data from Midi Device with Java Sound API

    Yes. The init is where you define input and output using MidiSystem.getDeviceInfo()

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Join Date
    Mar 2014
    Posts
    5
    Rep Power
    0

    Default Re: Receive Data from Midi Device with Java Sound API

    Hi Jim,

    I had to take a break, but I just completed the init() method with :

    MidiDevice.Info[] devices;

    devices = MidiSystem.getMidiDeviceInfo();

    for (MidiDevice.Info info: devices) {
    System.out.println(" Name: " + info.toString() +
    ", Decription: " +
    info.getDescription() +
    ", Vendor: " +
    info.getVendor());
    output = MidiSystem.getMidiDevice(info);
    if (! output.isOpen()) {
    output.open();
    }

    I had to modify your code a tiny bit.

    and I'm seeing the key on and key off bytes in my terminal window.
    Baby steps. :)

    I just wanted to say thank you very much.

    By the way, is there a good book that explains the code you provided.
    it's not the java part, just the sequence to make this midi code work.
    I found all sorts of examples on line for everything else midi, except this.
    Well, except for one place, but until I got this to work, I wasn't able to make sense of the other.
    I will definitely revisit, but if you have any other suggestions, that would be cool.
    Take care,
    Mike

Similar Threads

  1. Replies: 0
    Last Post: 03-06-2012, 06:17 PM
  2. Replies: 0
    Last Post: 10-14-2011, 04:16 PM
  3. record midi sound
    By Waseem Kaleem in forum Advanced Java
    Replies: 1
    Last Post: 09-14-2011, 11:08 AM
  4. javax.sound play midi
    By Dennis in forum Advanced Java
    Replies: 6
    Last Post: 06-08-2010, 04:59 PM
  5. Trouble using midi sound package
    By isum12b in forum Eclipse
    Replies: 1
    Last Post: 04-05-2009, 10:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •