feat: auto-reconnect midi
Clang Lint CI / lint-no-video (push) Successful in 59s
Clang Build CI / run-no-video (push) Successful in 59s
Clang Build CI / run-video (push) Successful in 59s
Clang Build CI / build-release (push) Successful in 1m31s
Clang Lint CI / lint-video (push) Successful in 1m7s

This commit is contained in:
2026-05-14 15:28:38 +02:00
parent adc520bc8b
commit 28b87d316a
8 changed files with 100 additions and 48 deletions
+30 -8
View File
@@ -13,14 +13,17 @@ void midi_open(MidiDevice *device, const char *name) {
snd_rawmidi_open(&device->input, &device->output, name, SND_RAWMIDI_NONBLOCK);
device->error = device->input == NULL || device->output == NULL;
device->connected = device->input != NULL && device->output != NULL;
log_info("(%s) MIDI open", name);
if (device->connected) {
log_info("(%s) MIDI open", name);
} else {
log_warn("(%s) MIDI open failed", name);
}
}
void midi_write(const MidiDevice *device, unsigned char code,
unsigned char value) {
if (device->error) {
void midi_write(MidiDevice device, unsigned char code, unsigned char value) {
if (!device.connected) {
return;
}
@@ -30,7 +33,15 @@ void midi_write(const MidiDevice *device, unsigned char code,
buffer[1] = code;
buffer[2] = value;
snd_rawmidi_write(device->output, buffer, 3);
snd_rawmidi_write(device.output, buffer, 3);
}
void midi_close(MidiDevice *device) {
if (device->connected) {
snd_rawmidi_close(device->input);
snd_rawmidi_close(device->output);
device->connected = false;
}
}
void *midi_background_listen(void *args) {
@@ -39,17 +50,28 @@ void *midi_background_listen(void *args) {
Context *context = process_args->context;
int bytes_read;
snd_rawmidi_info_t *info;
unsigned char buffer[3];
log_info("(%s) background acquisition started", device->name);
while (!context->stop) {
snd_rawmidi_info_malloc(&info);
while (!context->stop && snd_rawmidi_info(device->output, info) == 0) {
bytes_read = snd_rawmidi_read(device->input, buffer, 3);
if (bytes_read == 3) {
process_args->event_callback(buffer[1], buffer[2]);
}
}
log_info("(%s) background acquisition stopped by main thread", device->name);
snd_rawmidi_info_free(info);
if (context->stop) {
log_info("(%s) background acquisition stopped by main thread",
device->name);
} else {
log_info("(%s) background acquisition stopped after error", device->name);
midi_close(device);
}
pthread_exit(NULL);
}