Archive for February, 2011

h1

MIDI on linux should always be this easy!

2011-02-14

#include <stdio.h>
typedef unsigned char u8;

u8 state = 0;
u8 vel = 0;
u8 note = 0;

void read_midi(u8 c)
{
    if( c & 0x80 )
    {
        state = c >> 4;
        return;
    }
    switch( state )
    {
        case 0x8:
        case 0x9:
            note = c;
            state = 0x91;
            break;
        case 0x91:
            vel = c;
            state = 0;
            printf("%x %x\n",note,vel);
            break;
        case 0xA:
        case 0xB:
        case 0xC:
        case 0xD:
            break;
    }
}

int main(int args, char **argv)
{
    FILE *midi_in;
    u8 c;
    midi_in = fopen("/dev/midi", "r");
    while( 1 != 2 )
    {
        c = fgetc(midi_in);
        read_midi(c);

    }
    return 0;
}

That’s how easy it should be to write a MIDI inspection program. And it is, if you’re running linux with the OSS drivers installed. I can’t speak for my other installations that run the 2.6.x kernel, but this Alix box running voyager doesn’t seem to have any trouble exporting a /dev/midi character device to the filesystem, enabling programmers easy access at the data generated by my Keystation, which is hooked up via USB.

Follow

Get every new post delivered to your Inbox.