Reputation: 39
What I wish to do is write a simple program where whenever I press keys on my external midi controller, an internal receiver accepts the note on messages and somehow allows them to printed as byte information. Ive set up a receiver which is set to my external hardware midi controller's transmitter. I'm not sure how to access the midi information from the receiver after it has arrived there. In terms of research which I have done on the subject, Ive read oracles midi over view and looked at various examples on StackOverFlow. The Oracle article explains connecting the receiver and the transmitter and how to send this info to a synth, but does not cover how to access the Midi Short Messages from the receiver itself without a synth or a sequencer. The StackOverFlow examples have relevant info but I haven't seen anything which explains how to get to extract info from the receiver itself. If someone where to write some example code, and write a detailed explanation, I think it would be a great service to beginners like myself, because it would show how to make the first step in writing code that is compatible with midi i.e. How to receive the midi data in Java and then the first steps in being able to use it. Here is some sample code of what I am already trying.
package receivemidiattempt;
import javax.sound.midi.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ReceiveMidiAttempt {
public static void main(String[] args) {
// My internal receiver which I set up with my own MidiReceiver class which implements
// Receiver
Receiver r = new MidiReceiver ();
//My Transmitter
Transmitter trans = null;
try {
//Setting up transmitter with the external midi controller
trans= MidiSystem.getTransmitter();
}
catch(MidiUnavailableException e)
{
System.out.println("No Midi port Detected");
}
//contecting the transmitter to my internal receiver
trans.setReceiver(r);
}
}
Ive got the transmitter and the receiver connected, but am not sure what to do from here to extract the midi info from the receiver so I can print it. The receiver interface only has two methods, close and send, where close closes the receiver and Send sends a message to the receiver itself. Hoping I can find some way to take the midi info arriving at the receiver and store it in a byte variable called midiInfo which i can then print.
Alright Ive messed around with Java for the past hour and Ive almost figured it out. I think what I'm confused about is the way methods work in a class when an object of that class has been instantiated. For example just by putting println(msg) in the method Send() from the class I made which implements receiver, without invoking the method, every time I press a key on my controller a strange message gets printed which looks like a@787fd and changes every time. If i use msg.getLength i can see that every time I press a key the number 3 is printed. This makes sense considering that a Short message has three bytes The status byte, the pitch byte and the velocity byte. What I still can't figure out is how to get java to print the actual bytes. Almost there though!
Upvotes: 0
Views: 475
Reputation: 20834
The easiest way to do this would be to use JFugue:
// You'll need some try/catches around this block. This is traditional Java Midi code.
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
MidiDevice device = MidiSystem.getMidiDevice(infos[0]); // You'll have to get the right device for your MIDI controller.
// Here comes the JFugue code
MusicTransmitterToParserListener m = new MusicTransmitterToParserListener(device);
m.addParserListener(new ParserListenerAdapter() {
@Override public void onNotePressed(Note note) {
System.out.println(note + " pressed");
}
@Override public void onNoteReleased(Note note) {
System.out.println(note + " released");
}
} );
// Choose either this option:
m.startListening();
...do stuff...
m.stopListening();
// Or choose this option:
m.listenForMillis(5000); // Listen for 5000 milliseconds (5 seconds)
Upvotes: 2