Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: help with Java real-time midi

  1. #1
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default help with Java real-time midi

    I want to implement a program that sends and receives individual MIDI events to/from ports on my computer. I want to do individual events (i.e., not loading a MIDI file and sequencing it). I'm very confused by the interfaces Transmitter and Receiver. Let me put it this way. Some MIDI devices are input devices (would this correspond to a Receiver?) and some are output devices (a Transmitter?). What is confusing is that the Java documentation implies that Transmitters and Receivers pair up in the same process. That's not how MIDI devices work. I would receive from a MIDI input device and do something with that, but I would not transmit back to the same device, nor would I need to transmit to get it elsewhere in the same process. It's only if I'm sending the MIDI message to a different process or hardware device that I would transmit it. So clearly the meaning of Transmitter and Receiver doesn't correspond to the physical reality as I understand it. It must do something else.

    I can't find any MIDI tutorials that explain this. Any help appreciated.

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

    Default Re: help with Java real-time midi

    Check out The Java™ Tutorials.

    Here is an example of how to play an individual note:

    i
    Java Code:
    mport javax.sound.midi.MidiDevice;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Receiver;
    import javax.sound.midi.ShortMessage;
    import javax.sound.midi.Synthesizer;
    
    public class Example {
    
       /**
        * @param args
        */
       public static void main(String[] args) throws Exception  {
         ShortMessage myMsg = new ShortMessage();
          // Start playing the note Middle C (60), 
          // moderately loud (velocity = 93).
          long timeStamp = -1;
          Synthesizer synth = MidiSystem.getSynthesizer();
          synth.open();
          Receiver       rcvr = synth.getReceiver();
          myMsg.setMessage(ShortMessage.NOTE_ON, 60, 93);
          rcvr.send(myMsg, timeStamp);
          try {
             Thread.sleep(200);
          } catch (InterruptedException e) {}
          myMsg.setMessage(ShortMessage.NOTE_OFF, 60, 0);
          rcvr.send(myMsg, timeStamp);
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {}
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: help with Java real-time midi

    But that appears to be playing a note using a MIDI synthesizer provided by a Java API. I need to send MIDI messages to a hardware MIDI port on my computer. My computer won't be playing the note; rather the physical port has a cable running to another piece of hardware.

    The link you posted leads me not to a tutorial on midi, but to the java.sound.midi documentation, which is confusing for the reasons stated in my original post. I can't find a tutorial, such as a code example, or something that explains from first principles how these Java concepts link up with the concept of hardware input and output ports.

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

    Default Re: help with Java real-time midi

    OK, I have piano with a MIDI interface. However, I had to buy an adapter which went from the MIDI In and Out ports on the piano to the USB on my LapTop. I had to use the MidiSystem.getMidiDeviceInfo in combination of MidiSystem.getMidiDevice(Info info). I used the latter to get the appropriate Receiver and Transmitter for sending and receiving messages.

    Enabling this on the actual device is device dependent as well as how the actual Midi Interface manifests itself to Java. I am not an expert on this but I have gotten it to work. I used the Tutorials I pointed to as well as the the specific hardware documentation.

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

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

    Default Re: help with Java real-time midi

    Here is a sample of how I did it. This plays a midi file on a midi capable device. You can also send messages to the Midi device (see earlier post). If you want to view the midi messages, just subclass Receiver and put a print statement in the send routine prior to forwarding the messages to the superclass. The init method just initializes the interface. It is rather brute force but it works.

    Regards,
    Jim


    Java Code:
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import javax.sound.midi.MidiDevice;
    import javax.sound.midi.MidiDevice.Info;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Receiver;
    import javax.sound.midi.Sequencer;
    
    public class Piano {
       public static String inputName  = "E-MUXMidi1X1MidiIn";
       public static String outputName = "E-MUXMidi1X1MidiOut";
       public MidiDevice    input;
       public MidiDevice    output;
       public Receiver      rcvr;
       public String  midiFile = "Your file to play.mid";
     
       public static void main(String[] args) {
          new Piano().start();
       }
     
       
       public void start() {
          init();
     
          try {
             File midi = new File(midiFile);
             InputStream ios = new BufferedInputStream(new FileInputStream(midi));
             Sequencer sqr = MidiSystem.getSequencer(false);
             sqr.setSequence(ios);
             sqr.open();
             output.open();
             rcvr = output.getReceiver();
             sqr.getTransmitter().setReceiver(rcvr);
             sqr.start();
          }
          catch (Exception e) {
             e.printStackTrace();
          }
     
       }
     
     
       public void init() {
          try {
             Info[] info = MidiSystem.getMidiDeviceInfo();
             for (Info inf : info) {
                String name = inf.getName().replace(" ", "");
                System.out.println("\"" + name + "\"");
                if (name.equals(inputName)) {
                   input = MidiSystem.getMidiDevice(inf);
                }
                if (name.equals(outputName)) {
                   output = MidiSystem.getMidiDevice(inf);
                }
             }
          }
          catch (Exception e) {
             e.printStackTrace();
          }
       }
     
     
    }
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: help with Java real-time midi

    Thanks, I think that should show me what I need to do. I enhanced your reputation.

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

    Default Re: help with Java real-time midi

    Thanks! BTW, the input and output names of the interface device are specific to my device. Just print out the info array to see what yours would be.

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

Similar Threads

  1. JAVA real-time RTSJ or C++
    By yoshi in forum Forum Lobby
    Replies: 1
    Last Post: 05-09-2012, 07:38 PM
  2. How real-time is java?
    By StateMachine in forum New To Java
    Replies: 12
    Last Post: 12-27-2011, 04:41 PM
  3. Near-Real-Time Implementation
    By stooch in forum Lucene
    Replies: 0
    Last Post: 12-08-2011, 03:25 PM
  4. Java real time plotting with visualvm
    By gianadoume in forum Java 2D
    Replies: 0
    Last Post: 02-04-2011, 11:25 PM
  5. Real Time?
    By RaustBD in forum New To Java
    Replies: 1
    Last Post: 01-01-2011, 02:14 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
  •