Skip to content

Instantly share code, notes, and snippets.

@yingshaoxo
Created March 20, 2024 06:55
Show Gist options
  • Save yingshaoxo/24883f3a0495e6305a64b1902a9fb517 to your computer and use it in GitHub Desktop.
Save yingshaoxo/24883f3a0495e6305a64b1902a9fb517 to your computer and use it in GitHub Desktop.
Linux MIDI input driver, protocol 1.0
// Linux MIDI input driver, protocol 1.0, there is no need to upgrade to protocol 2.0
// Author: yingshaoxo
// Run it in root shell
// gcc main.c -o main.run
// ./main.run
#include <sys/soundcard.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MIDI_DEVICE "/dev/midi1"
//#define MIDI_DEVICE "/dev/dmmidi1"
//#define MIDI_DEVICE "/dev/snd/midiC1D0"
int main(void) {
unsigned char inpacket[4];
// 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, &inpacket, sizeof(inpacket));
if (inpacket[0] == 144) {
printf("Pressed: ");
printf("%d\n", inpacket[1]);
} else {
printf("Released: ");
printf("%d\n\n", inpacket[1]);
}
//printf("strongth: %d", inpacket[2]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment