Hi elfh,
When I originally designed the library it was supposed to also support raw MIDI, but I clearly forgot to fully implement that! (it was partly there, but ....)
I have updated the library and added support for 2 types of raw MIDI. I will provide some examples below of how to do what you want with raw midi. I have also update the javadocs, and there is a new code example provided to illustrate these new features.
So go check out the
updated javadocs (look at Papplet.rawMidi() and PApplet.midiMessage()),
re-download the examples and
re-download the libraryI'm not exactly sure what you mean by clock information, so I'll assume that you want to receive "timing ticks" aka "timing clock" for the purpose of my examples
So if you wanted to get the song position pointer or the timing clock inside your PApplet (your sketch) here are your two options:
1. Use rawMidi(byte[] data) Code:
import themidibus.*; //Import the library
void setup() {
size(300,300);
background(0);
}
void draw() {
//do something interesting
}
void rawMidi(byte[] data) {
if(data[0] == (byte)0xF2) {
//0xF2 is the status byte for SONG_POSITION_POINTER
println("Pointer LSB: "+(int)(data[1] & 0xFF));
println("Pointer MSB: "+(int)(data[2] & 0xFF));
} else if(data[0] == (byte)0xF8) {
//0xF8 is the status byte for TIMING_CLOCK
println("There was a timing tick");
}
}
2. Use midiMessage(MidiMessage message) Code:
import themidibus.*; //Import the library
import javax.sound.midi.MidiMessage; //This is important because in this case we will use the MidiMessage class
void setup() {
size(300,300);
background(0);
}
void draw() {
//do something interesting
}
void midiMessage(MidiMessage message) {
if(message.getStatus() == 0xF2) {
//0xF2 is the status byte for SONG_POSITION_POINTER
byte[] data = message.getMessage();
println("Pointer LSB: "+(int)(data[1] & 0xFF));
println("Pointer MSB: "+(int)(data[2] & 0xFF));
} else if(message.getStatus() == 0xF8) {
//0xF8 is the status byte for TIMING_CLOCK
println("There was a timing tick");
}
}
A Quick WarningBe careful manipulating bytes, because java only supports signed bytes but MIDI uses unsigned bytes
For comparisons do one of the following:
(in the example assume data[0] contains an unsigned byte from MIDI, stored in Java's signed byte type)
Code:... if(data[0] == (byte)0x90) { ...
This converts the int 0x90 to signed byte and you can safely compare the two signed bytes
Code:... if((int)(data[0] & 0xFF) == 0x90) { ...
This correctly converts the signed byte data[0] to int
as if it was an unsigned byteor if you want to be weird
Code:... if(data[0] == (int)(byte)0x90) { ...
Yeah I won't try and explain that one...
Finally if you just want to convert the unsigned byte to int so it can be displayed on the screen use:
Code: ... println((int)(data[0] & 0xFF)); ...
as seen earlier ...
Doing it with interfacesIt would also be possible, if you wanted to, to do the same kind of thing by writing a new class which implements either the
RawMidiListener interface or the
StandardMidiListener interface. These interface are included in the library. Any object which is an instance of such a class could then be added to your MidiBus object using
addMidiListener(MidiListener listener). Whenever a new MIDI message is received your listener would be notified in raw form or in MidiMessage form and you could process this information in the same way as above.
I can provide example of this if you're interested, just let me know.
I hope that this is helpful. Give me your feedback/requests/complaints