Wednesday, June 04, 2008

radiodrum



I just got some code running on Linux that reads the output of the Radiodrum alternative music controller. It reads data right from the MIDI port, /dev/midi1 and outputs the values to stdout.


#include <sys/soundcard.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define MIDI_DEVICE "/dev/midi1"

int main(void) {
unsigned char message[4];

int byte1, byte2, byte3;
float s1x, s1y, s1z;
float s2x, s2y, s2z;

int s1whack;
int s2whack;

// first open the sequencer device for reading.
int seqfd = open(MIDI_DEVICE, O_RDONLY);
if (seqfd == -1) {
printf("Error: cannot open %s\n", MIDI_DEVICE);
exit(1);
}

// now just wait around for MIDI bytes to arrive and print them to screen.
while (1) {
read(seqfd, &message, sizeof(message));

byte1 = message[0];
byte2 = message[1];
byte3 = message[2];

s1whack = 0;
s2whack = 0;

if ((byte1 == 160)&&(byte2 == 1)) {
s1whack = 1;
}
if ((byte1 == 160)&&(byte2 == 8)) {
s1x = byte3 / 128.0;
}
if ((byte1 == 160)&&(byte2 == 9)) {
s1y = byte3 / 128.0;
}
if ((byte1 == 160)&&(byte2 == 10)) {
s1z = (byte3-14.0) / 128.0;
}
if ((byte1 == 160)&&(byte2 == 2)) {
s2whack = 1;
}
if ((byte1 == 160)&&(byte2 == 11)) {
s2x = byte3 / 128.0;
}
if ((byte1 == 160)&&(byte2 == 12)) {
s2y = byte3 / 128.0;
}
if ((byte1 == 160)&&(byte2 == 13)) {
s2z = (byte3 -14) / 128.0;
}

printf("%f %f %f\t%f %f %f\n",s1x,s1y,s1z,s2x,s2y,s2z);

}
}