Results 1 to 7 of 7
Thread: help with Java real-time midi
- 03-11-2013, 09:36 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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.
- 03-11-2013, 09:57 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 696
- Rep Power
- 1
Re: help with Java real-time midi
Check out The Java™ Tutorials.
Here is an example of how to play an individual note:
iRegards,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) {} } }
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-11-2013, 10:06 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
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.
- 03-11-2013, 11:16 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 696
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-12-2013, 12:14 AM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 696
- Rep Power
- 1
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
YAT -- Yet Another Typo
- 03-12-2013, 12:58 AM #6
Member
- Join Date
- Mar 2013
- Posts
- 6
- Rep Power
- 0
Re: help with Java real-time midi
Thanks, I think that should show me what I need to do. I enhanced your reputation.
- 03-12-2013, 01:31 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 696
- Rep Power
- 1
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,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
JAVA real-time RTSJ or C++
By yoshi in forum Forum LobbyReplies: 1Last Post: 05-09-2012, 07:38 PM -
How real-time is java?
By StateMachine in forum New To JavaReplies: 12Last Post: 12-27-2011, 04:41 PM -
Near-Real-Time Implementation
By stooch in forum LuceneReplies: 0Last Post: 12-08-2011, 03:25 PM -
Java real time plotting with visualvm
By gianadoume in forum Java 2DReplies: 0Last Post: 02-04-2011, 11:25 PM -
Real Time?
By RaustBD in forum New To JavaReplies: 1Last Post: 01-01-2011, 02:14 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks