/* compile with -lasound */ #include #include #include #include #define INPUT_DEVICE "/dev/input/by-path/platform-i8042-serio-0-event-kbd" #define CHANNEL 0 #define NOTE_A 48 #define NOTE_B 52 #define CONTROLLER_X 92 int main(void) { struct input_event ie; int fd, port, err; snd_seq_t *seq; snd_seq_event_t ev; fd = open(INPUT_DEVICE, O_RDONLY); if (fd == -1) { perror(INPUT_DEVICE); return 1; } err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_OUTPUT, 0); if (err < 0) { fprintf(stderr, "cannot open sequencer: %s\n", snd_strerror(err)); return 1; } snd_seq_set_client_name(seq, "Input/MIDI converter"); port = snd_seq_create_simple_port(seq, "Port 1", SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_SOFTWARE); if (port < 0) { fprintf(stderr, "cannot create port: %s\n", snd_strerror(port)); return 1; } snd_seq_ev_clear(&ev); snd_seq_ev_set_source(&ev, port); snd_seq_ev_set_subs(&ev); snd_seq_ev_set_direct(&ev); for (;;) { if (read(fd, &ie, sizeof(ie)) != sizeof(ie)) break; ev.type = SND_SEQ_EVENT_NONE; switch (ie.type) { case EV_KEY: switch (ie.code) { case KEY_A: if (ie.value) snd_seq_ev_set_noteon(&ev, CHANNEL, NOTE_A, 127); else snd_seq_ev_set_noteoff(&ev, CHANNEL, NOTE_A, 64); break; case KEY_B: if (ie.value) snd_seq_ev_set_noteon(&ev, CHANNEL, NOTE_B, 127); else snd_seq_ev_set_noteoff(&ev, CHANNEL, NOTE_B, 64); break; } break; case EV_ABS: switch (ie.code) { case ABS_X: /* value 0..127 */ snd_seq_ev_set_controller(&ev, CHANNEL, CONTROLLER_X, ie.value); break; } break; } if (ev.type != SND_SEQ_EVENT_NONE) snd_seq_event_output_direct(seq, &ev); } snd_seq_close(seq); close(fd); return 0; }