fix: use persistent MIDI client and fix save_ring race condition

Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-11 22:14:33 +00:00
parent 7deea9266b
commit 346c15d1c3
3 changed files with 59 additions and 43 deletions

View File

@@ -225,50 +225,61 @@ static int test_audio_pass_through(void) {
/* Helper: open a transient JACK client, send a MIDI noteon, close */
static jack_client_t *midi_persistent_client = NULL;
static jack_port_t *midi_persistent_port = NULL;
static int send_jack_note_on(const char *target_port, unsigned char note, unsigned char velocity) {
/* create persistent client on first call */
if (!midi_persistent_client) {
jack_status_t st;
midi_persistent_client = jack_client_open("midi_inject_persistent", JackNoStartServer, &st);
if (!midi_persistent_client) return -1;
midi_persistent_port = jack_port_register(midi_persistent_client, "out",
JACK_DEFAULT_MIDI_TYPE,
JackPortIsOutput, 0);
if (!midi_persistent_port) {
jack_client_close(midi_persistent_client);
midi_persistent_client = NULL;
return -1;
}
char src[64];
snprintf(src, sizeof(src), "midi_inject_persistent:out");
if (jack_connect(midi_persistent_client, src, target_port) != 0) {
jack_client_close(midi_persistent_client);
midi_persistent_client = NULL;
midi_persistent_port = NULL;
return -1;
}
jack_set_process_callback(midi_persistent_client, midi_inject_process, NULL);
if (jack_activate(midi_persistent_client) != 0) {
jack_client_close(midi_persistent_client);
midi_persistent_client = NULL;
midi_persistent_port = NULL;
return -1;
}
}
midi_inject_note = note;
midi_inject_velocity = velocity;
midi_inject_pending = 1;
jack_status_t st;
midi_inject_client = jack_client_open("test_midi_inject", JackNoStartServer, &st);
if (!midi_inject_client) return -1;
midi_inject_port = jack_port_register(midi_inject_client, "out",
JACK_DEFAULT_MIDI_TYPE,
JackPortIsOutput, 0);
if (!midi_inject_port) {
jack_client_close(midi_inject_client);
midi_inject_client = NULL;
return -1;
}
char src[64];
snprintf(src, sizeof(src), "test_midi_inject:out");
if (jack_connect(midi_inject_client, src, target_port) != 0) {
jack_client_close(midi_inject_client);
midi_inject_client = NULL;
midi_inject_port = NULL;
return -1;
}
midi_inject_pending = 1; /* signal before activation */
jack_set_process_callback(midi_inject_client, midi_inject_process, NULL);
if (jack_activate(midi_inject_client) != 0) {
jack_client_close(midi_inject_client);
midi_inject_client = NULL;
midi_inject_port = NULL;
return -1;
}
/* wait for the process callback to clear the flag (event delivered) */
for (int attempts = 0; attempts < 50; attempts++) { /* ~50 * 10ms = 500ms */
/* wait for delivery (process callback clears the flag) */
for (int attempts = 0; attempts < 100; attempts++) {
safe_usleep(10000);
if (!midi_inject_pending) break;
}
jack_deactivate(midi_inject_client);
jack_client_close(midi_inject_client);
midi_inject_client = NULL;
midi_inject_port = NULL;
return 0;
}
/* must be called after all tests */
static void close_persistent_midi(void) {
if (midi_persistent_client) {
jack_deactivate(midi_persistent_client);
jack_client_close(midi_persistent_client);
midi_persistent_client = NULL;
midi_persistent_port = NULL;
}
}
/*
* Full loop recording test:
* 1. start looper
@@ -946,9 +957,9 @@ static int test_wav_load(void) {
unlink("loop.wav"); return 1;
}
/* wait for the loop to be fully loaded and playing */
safe_usleep(2000000);
safe_usleep(3000000);
/* continue listening for the rest of the time */
safe_usleep(4000000); /* total 6 seconds after activation */
safe_usleep(6000000); /* total 9 seconds after activation */
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
@@ -1142,6 +1153,8 @@ int main(void) {
failures++;
}
close_persistent_midi();
if (failures > 0) {
fprintf(stderr, "%d test(s) FAILED\n", failures);
return 1;