1666 lines
54 KiB
C
1666 lines
54 KiB
C
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <unistd.h>
|
||
#include <signal.h>
|
||
#include <sys/wait.h>
|
||
#include <sys/types.h>
|
||
#include <string.h>
|
||
#include <stdarg.h>
|
||
#include <fcntl.h>
|
||
#include <jack/jack.h>
|
||
#include <jack/midiport.h>
|
||
#include <math.h>
|
||
#include <time.h>
|
||
|
||
/* static variables for passthrough test */
|
||
static jack_port_t *passthrough_output_port = NULL;
|
||
static jack_port_t *passthrough_input_port = NULL;
|
||
static float passthrough_phase = 0.0f;
|
||
static float passthrough_freq = 440.0f;
|
||
static int passthrough_sample_rate = 0;
|
||
static long passthrough_total_samples = 0;
|
||
static double passthrough_sum_sq = 0.0;
|
||
static volatile int passthrough_done = 0;
|
||
static volatile int beep_remaining = 0;
|
||
static volatile int bursts = 0;
|
||
static volatile int prev_above = 0;
|
||
static int continuous_sine = 0;
|
||
|
||
/* variables for MIDI injection (used by send_jack_note_on) */
|
||
static volatile int midi_inject_pending = 0;
|
||
static jack_port_t *midi_inject_port = NULL;
|
||
static jack_client_t *midi_inject_client = NULL;
|
||
static unsigned char midi_inject_note = 0;
|
||
static unsigned char midi_inject_velocity = 0;
|
||
|
||
/* Persistent MIDI injection client – avoids race conditions of transient clients */
|
||
static jack_client_t *persistent_midi_client = NULL;
|
||
static jack_port_t *persistent_midi_port = NULL;
|
||
|
||
static void safe_usleep(unsigned int usec) {
|
||
struct timespec ts;
|
||
ts.tv_sec = usec / 1000000;
|
||
ts.tv_nsec = (usec % 1000000) * 1000L;
|
||
nanosleep(&ts, NULL);
|
||
}
|
||
|
||
static int midi_inject_process(jack_nframes_t nframes, void *arg) {
|
||
(void)arg;
|
||
if (!midi_inject_port) return 0;
|
||
void *port_buf = jack_port_get_buffer(midi_inject_port, nframes);
|
||
if (!port_buf) return 0;
|
||
jack_midi_clear_buffer(port_buf);
|
||
if (!midi_inject_pending) return 0;
|
||
jack_midi_data_t *buf = jack_midi_event_reserve(port_buf, 0, 3);
|
||
if (!buf) return 0;
|
||
buf[0] = 0x90;
|
||
buf[1] = midi_inject_note;
|
||
buf[2] = midi_inject_velocity;
|
||
midi_inject_pending = 0;
|
||
return 0;
|
||
}
|
||
|
||
/* Initialise the persistent MIDI client (must be called once before any send) */
|
||
static int init_persistent_midi_client(void) {
|
||
if (persistent_midi_client) return 0; /* already initialised */
|
||
jack_status_t st;
|
||
persistent_midi_client = jack_client_open("test_midi_persistent", JackNoStartServer, &st);
|
||
if (!persistent_midi_client) return -1;
|
||
persistent_midi_port = jack_port_register(persistent_midi_client, "out",
|
||
JACK_DEFAULT_MIDI_TYPE,
|
||
JackPortIsOutput, 0);
|
||
if (!persistent_midi_port) {
|
||
jack_client_close(persistent_midi_client);
|
||
persistent_midi_client = NULL;
|
||
return -1;
|
||
}
|
||
jack_set_process_callback(persistent_midi_client, midi_inject_process, NULL);
|
||
if (jack_activate(persistent_midi_client) != 0) {
|
||
jack_client_close(persistent_midi_client);
|
||
persistent_midi_client = NULL;
|
||
return -1;
|
||
}
|
||
/* Connect to looper control port */
|
||
if (jack_connect(persistent_midi_client, "test_midi_persistent:out", "looper:control") != 0) {
|
||
jack_deactivate(persistent_midi_client);
|
||
jack_client_close(persistent_midi_client);
|
||
persistent_midi_client = NULL;
|
||
return -1;
|
||
}
|
||
/* Use the persistent port for injection */
|
||
midi_inject_port = persistent_midi_port;
|
||
midi_inject_client = persistent_midi_client;
|
||
return 0;
|
||
}
|
||
|
||
/* Clean up the persistent MIDI client at the end */
|
||
static void cleanup_persistent_midi_client(void) {
|
||
if (persistent_midi_client) {
|
||
jack_deactivate(persistent_midi_client);
|
||
jack_client_close(persistent_midi_client);
|
||
persistent_midi_client = NULL;
|
||
persistent_midi_port = NULL;
|
||
midi_inject_port = NULL;
|
||
midi_inject_client = NULL;
|
||
}
|
||
}
|
||
|
||
/* The test code uses this callback in two ways:
|
||
- For the audio passthrough test (existing function) it still works.
|
||
- For the loop test we need a version that respects the static variables
|
||
beep_remaining and bursts (declared in test_looper_looping).
|
||
We change the existing function to also handle those globals. */
|
||
static int passthrough_process(jack_nframes_t nframes, void *arg) {
|
||
(void)arg;
|
||
jack_default_audio_sample_t *out =
|
||
(jack_default_audio_sample_t *)jack_port_get_buffer(passthrough_output_port, nframes);
|
||
const jack_default_audio_sample_t *in =
|
||
(const jack_default_audio_sample_t *)jack_port_get_buffer(passthrough_input_port, nframes);
|
||
if (!out || !in) return 0;
|
||
float *f_out = (float *)out;
|
||
const float *f_in = (const float *)in;
|
||
for (jack_nframes_t i = 0; i < nframes; i++) {
|
||
/* generate beep while beep_remaining > 0 or continuous sine */
|
||
float out_val;
|
||
if (continuous_sine || beep_remaining > 0) {
|
||
out_val = sinf(passthrough_phase);
|
||
passthrough_phase += 2.0f * (float)M_PI * passthrough_freq / passthrough_sample_rate;
|
||
if (passthrough_phase > 2.0f * M_PI)
|
||
passthrough_phase -= 2.0f * M_PI;
|
||
if (beep_remaining > 0) beep_remaining--;
|
||
} else {
|
||
out_val = 0.0f;
|
||
}
|
||
f_out[i] = out_val;
|
||
|
||
/* detect bursts on the input (looper output) */
|
||
float sample = f_in[i];
|
||
int above = (fabsf(sample) > 0.05f);
|
||
if (above && !prev_above) {
|
||
bursts++;
|
||
}
|
||
prev_above = above;
|
||
|
||
passthrough_sum_sq += (double)f_in[i] * (double)f_in[i];
|
||
passthrough_total_samples++;
|
||
}
|
||
if (passthrough_total_samples >= passthrough_sample_rate * 2) {
|
||
passthrough_done = 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
* Integration test for the JACK looper.
|
||
*
|
||
* Uses SIGUSR1 to request the looper to report its current state and exit.
|
||
* Verifies that MIDI note‑on and clock messages produce correct state transitions.
|
||
*/
|
||
|
||
#define STATE_IDLE 0
|
||
#define STATE_RECORD 1
|
||
#define STATE_LOOPING 2
|
||
#define STATE_PAUSED 3
|
||
|
||
#define WAIT_SECONDS 1
|
||
|
||
/*
|
||
* Start a fresh instance of the looper, wait for JACK ports to appear,
|
||
* return its PID, or -1 on failure.
|
||
*/
|
||
static pid_t start_looper(void) {
|
||
pid_t pid = fork();
|
||
if (pid < 0) {
|
||
perror("fork");
|
||
return -1;
|
||
}
|
||
if (pid == 0) {
|
||
/* child: suppress stderr messages */
|
||
close(2);
|
||
open("/dev/null", O_WRONLY);
|
||
execl("./looper", "looper", NULL);
|
||
perror("execl");
|
||
_exit(1);
|
||
}
|
||
printf("Started looper (pid %d)\n", (int)pid);
|
||
sleep(3); /* wait for JACK ports to register */
|
||
return pid;
|
||
}
|
||
|
||
|
||
static int test_audio_pass_through(void) {
|
||
printf("Test: audio pass‑through (connectivity)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_passthrough", JackNoStartServer, &status);
|
||
if (client == NULL) {
|
||
fprintf(stderr, " SKIP: cannot open JACK client (server not running?)\n");
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
jack_port_t *output_port = jack_port_register(client, "output",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *input_port = jack_port_register(client, "input",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!output_port || !input_port) {
|
||
fprintf(stderr, " FAIL: could not register ports\n");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
const char *looper_input = "looper:input";
|
||
const char *looper_output = "looper:output";
|
||
char my_output[64], my_input[64];
|
||
snprintf(my_output, sizeof(my_output), "test_passthrough:output");
|
||
snprintf(my_input, sizeof(my_input), "test_passthrough:input");
|
||
if (jack_connect(client, my_output, looper_input) != 0) {
|
||
fprintf(stderr, " FAIL: cannot connect test_passthrough:output -> looper:input\n");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
if (jack_connect(client, looper_output, my_input) != 0) {
|
||
fprintf(stderr, " FAIL: cannot connect looper:output -> test_passthrough:input\n");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
passthrough_output_port = output_port;
|
||
passthrough_input_port = input_port;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = jack_get_sample_rate(client);
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
continuous_sine = 1; /* enable continuous tone for this test */
|
||
beep_remaining = 0; /* not needed */
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client) != 0) {
|
||
fprintf(stderr, " FAIL: cannot activate client\n");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(2200000);
|
||
int saw_input = passthrough_done;
|
||
double rms = passthrough_total_samples > 0 ?
|
||
sqrt(passthrough_sum_sq / passthrough_total_samples) : 0.0;
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
if (!saw_input) {
|
||
fprintf(stderr, " FAIL: looper did not produce output (no callback run?)\n");
|
||
return 1;
|
||
}
|
||
if (rms < 0.001) {
|
||
fprintf(stderr, " FAIL: looper output RMS too small (%.6f)\n", rms);
|
||
return 1;
|
||
}
|
||
printf(" PASS (RMS %.6f)\n", rms);
|
||
return 0;
|
||
}
|
||
|
||
|
||
/* Helper: send a MIDI note‑on using the persistent client */
|
||
static int send_jack_note_on(const char *target_port, unsigned char note, unsigned char velocity) {
|
||
(void)target_port; /* connection is already made to looper:control */
|
||
/* Persistent client must be initialised by the calling test */
|
||
if (!persistent_midi_client) return -1;
|
||
midi_inject_note = note;
|
||
midi_inject_velocity = velocity;
|
||
midi_inject_pending = 1;
|
||
/* wait for the process callback to clear the flag (event delivered) */
|
||
for (int attempts = 0; attempts < 50; attempts++) { /* ~500ms */
|
||
safe_usleep(10000);
|
||
if (!midi_inject_pending) break;
|
||
}
|
||
return (midi_inject_pending == 0) ? 0 : -1;
|
||
}
|
||
|
||
/*
|
||
* Full loop recording test:
|
||
* 1. start looper
|
||
* 2. open JACK test client (audio)
|
||
* 3. send note‑on to move IDLE->RECORD
|
||
* 4. generate a short 440 Hz beep (~0.1 s) while recording
|
||
* 5. send note‑on to move RECORD->LOOPING
|
||
* 6. monitor looper output for the beep being repeated (≥3 times)
|
||
*/
|
||
static int test_looper_looping(void) {
|
||
printf("Test: loop recording and playback (expect ≥3 repetitions)\n");
|
||
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
/* Create persistent MIDI client for this looper instance */
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_looping", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: JACK not running?\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000); /* wait for ports to appear */
|
||
/* connect test:out -> looper:input, looper:output -> test:in */
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_looping:out");
|
||
snprintf(my_in, sizeof(my_in), "test_looping:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
|
||
/* first note‑on: IDLE -> RECORD */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(500000); /* allow state to change (500ms) */
|
||
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0; /* disable continuous tone */
|
||
beep_remaining = (int)(0.1f * sr); /* 0.1 second beep */
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
|
||
safe_usleep(150000); /* let beep start */
|
||
|
||
/* ensure beep is fully captured */
|
||
safe_usleep(800000); /* 0.8s after start of beep */
|
||
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
|
||
/* wait enough time for several loops (4 seconds to be safe) */
|
||
safe_usleep(4000000);
|
||
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
|
||
int got_bursts = bursts;
|
||
printf(" detected bursts: %d\n", got_bursts);
|
||
if (got_bursts < 3) {
|
||
fprintf(stderr, " FAIL: expected ≥3 bursts, got %d\n", got_bursts);
|
||
return 1;
|
||
}
|
||
printf(" PASS (at least 3 repetitions)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test multiple channels */
|
||
static int test_multiple_channels(void) {
|
||
printf("Test: dynamic channel creation via MIDI command\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_multi", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
|
||
if (send_jack_note_on("looper:control", 60, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 60 failed\n");
|
||
return 1;
|
||
}
|
||
/* Poll until the port appears (up to 3 seconds) */
|
||
int found = 0;
|
||
for (int retries = 0; retries < 30; retries++) {
|
||
safe_usleep(100000);
|
||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_input")) {
|
||
found = 1;
|
||
jack_free(ports);
|
||
goto port_found;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
}
|
||
port_found:
|
||
;
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
|
||
if (!found) {
|
||
fprintf(stderr, " FAIL: channel1_input port not created after add command\n");
|
||
return 1;
|
||
}
|
||
printf(" PASS (channel created)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test control‑key modifier (note 64 + note 62) */
|
||
static int test_control_key_modifier(void) {
|
||
printf("Test: control‑key modifier triggers state transition via note 62\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_ctrl_key", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
/* connect same as in test_looper_looping but no beep generation */
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_ctrl_key:out");
|
||
snprintf(my_in, sizeof(my_in), "test_ctrl_key:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
/* First send note 64 (control key) */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 64 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
/* Now send note 62 (toggle channel 0) */
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 62 failed\n");
|
||
return 1;
|
||
}
|
||
/* Wait for looper to enter RECORD and detect audio */
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.1f * sr); /* 0.1 second beep */
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000); /* allow beep */
|
||
/* send note 62 again under control key to move RECORD->LOOPING */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key re‑send\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 62 for loop\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(2000000);
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
int got_bursts = bursts;
|
||
printf(" detected bursts: %d\n", got_bursts);
|
||
if (got_bursts < 3) {
|
||
fprintf(stderr, " FAIL: expected ≥3 bursts, got %d\n", got_bursts);
|
||
return 1;
|
||
}
|
||
printf(" PASS (control‑key modifier works)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test bind channel */
|
||
static int test_bind_channel(void) {
|
||
printf("Test: control‑key bind channel (note 0) and toggle\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_bind", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_bind:out");
|
||
snprintf(my_in, sizeof(my_in), "test_bind:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
/* Send control key + note 0 to bind to channel 0 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send control key failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 0, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send bind note 0 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
/* Now toggle using control+note62 – should toggle channel 0 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send control key again failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send toggle note 62 failed\n");
|
||
return 1;
|
||
}
|
||
/* Wait and detect bursts as before */
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.1f * sr);
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000); /* allow beep */
|
||
/* send control+note62 again to move RECORD->LOOPING */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key for loop\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: toggle for loop\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(2000000);
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
int got_bursts = bursts;
|
||
printf(" detected bursts: %d\n", got_bursts);
|
||
if (got_bursts < 3) {
|
||
fprintf(stderr, " FAIL: expected >=3 bursts, got %d\n", got_bursts);
|
||
return 1;
|
||
}
|
||
printf(" PASS (bind and toggle)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test unbind */
|
||
static int test_bind_unbind(void) {
|
||
printf("Test: bind to channel 5, unbind, then toggle default (channel 0)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_unbind", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_unbind:out");
|
||
snprintf(my_in, sizeof(my_in), "test_unbind:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
/* Bind to channel 5 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send control key failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 5, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: bind to 5 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
/* Unbind (reset to 0) */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key for unbind\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 63, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send unbind note 63 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
/* Now toggle with control+62 – should affect channel 0 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key for toggle\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: toggle note 62\n");
|
||
return 1;
|
||
}
|
||
/* Wait for beep and loop */
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.1f * sr);
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000); /* allow beep */
|
||
/* second control+62 -> loop */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key for loop\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: toggle for loop\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(2000000);
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
int got_bursts = bursts;
|
||
printf(" detected bursts: %d\n", got_bursts);
|
||
if (got_bursts < 3) {
|
||
fprintf(stderr, " FAIL: expected >=3 bursts, got %d\n", got_bursts);
|
||
return 1;
|
||
}
|
||
printf(" PASS (unbind works, toggle channel 0)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test remove channel */
|
||
static int test_remove_channel(void) {
|
||
printf("Test: dynamic channel removal via MIDI command\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_remove", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
/* add channel */
|
||
if (send_jack_note_on("looper:control", 60, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 60 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(1500000);
|
||
/* verify channel1_input exists */
|
||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||
int found = 0;
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_input")) {
|
||
found = 1;
|
||
break;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
if (!found) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: channel1_input not created\n");
|
||
return 1;
|
||
}
|
||
printf(" channel1_input created\n");
|
||
/* remove channel */
|
||
if (send_jack_note_on("looper:control", 61, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 61 failed\n");
|
||
return 1;
|
||
}
|
||
/* Poll until the port disappears (up to 3 seconds) */
|
||
int still_found = 1;
|
||
for (int retries = 0; retries < 30; retries++) {
|
||
safe_usleep(100000);
|
||
ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||
still_found = 0;
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_input")) {
|
||
still_found = 1;
|
||
break;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
if (!still_found) break;
|
||
}
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
if (still_found) {
|
||
fprintf(stderr, " FAIL: channel1_input not removed after remove command\n");
|
||
return 1;
|
||
}
|
||
printf(" PASS (channel removed)\n");
|
||
return 0;
|
||
}
|
||
|
||
|
||
/* test FIFO stop, bind, unbind */
|
||
static int test_fifo_stop_bind_unbind(void) {
|
||
printf("Test: FIFO stop, bind, unbind\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_fifo_stop", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_fifo_stop:out");
|
||
snprintf(my_in, sizeof(my_in), "test_fifo_stop:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
|
||
/* start recording via note1 */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.1f * sr);
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(150000);
|
||
|
||
/* now send stop, bind, unbind via FIFO */
|
||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||
if (fd < 0) {
|
||
perror("open fifo");
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
write(fd, "stop\n", 5);
|
||
write(fd, "bind 0\n", 7);
|
||
write(fd, "unbind\n", 7);
|
||
close(fd);
|
||
safe_usleep(500000);
|
||
int bursts_after = bursts;
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
if (bursts_after < 1) {
|
||
fprintf(stderr, " FAIL: no burst detected (probably no recording)\n");
|
||
return 1;
|
||
}
|
||
printf(" PASS (FIFO stop, bind, unbind executed)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test MIDI channel creation via FIFO */
|
||
static int test_midi_channel_add(void) {
|
||
printf("Test: MIDI channel creation via FIFO (add_midi)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_midi_add", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
|
||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||
if (fd < 0) {
|
||
perror("open fifo");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
write(fd, "add_midi\n", 9);
|
||
close(fd);
|
||
safe_usleep(1500000); /* allow main loop to process */
|
||
|
||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_MIDI_TYPE, 0);
|
||
int found = 0;
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_midi_in")) {
|
||
found = 1;
|
||
break;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
|
||
if (!found) {
|
||
fprintf(stderr, " FAIL: channel1_midi_in port not created\n");
|
||
return 1;
|
||
}
|
||
printf(" PASS (MIDI channel created)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test FIFO pipe */
|
||
static int test_fifo_pipe(void) {
|
||
printf("Test: FIFO pipe add/remove\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_fifo", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
|
||
/* write "add\n" to the FIFO */
|
||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||
if (fd < 0) {
|
||
perror("open fifo");
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
write(fd, "add\n", 4);
|
||
/* Keep fd open; do NOT close yet */
|
||
safe_usleep(1500000); /* give main loop time to process */
|
||
|
||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||
int found = 0;
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_input")) {
|
||
found = 1;
|
||
break;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
|
||
/* Write "remove\n" to the FIFO, same fd */
|
||
write(fd, "remove\n", 7);
|
||
close(fd);
|
||
|
||
safe_usleep(1500000);
|
||
|
||
ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||
int still_found = 0;
|
||
if (ports) {
|
||
for (int i = 0; ports[i]; i++) {
|
||
if (strstr(ports[i], "looper:channel1_input")) {
|
||
still_found = 1;
|
||
break;
|
||
}
|
||
}
|
||
jack_free(ports);
|
||
}
|
||
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
|
||
if (!found) {
|
||
fprintf(stderr, " FAIL: channel not added via FIFO\n");
|
||
return 1;
|
||
}
|
||
if (still_found) {
|
||
fprintf(stderr, " FAIL: channel not removed via FIFO\n");
|
||
return 1;
|
||
}
|
||
printf(" PASS (FIFO add/remove works)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* Scene tests */
|
||
|
||
/* Helper to write a command to the looper FIFO */
|
||
static int write_fifo(const char *cmd) {
|
||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||
if (fd < 0) return 0;
|
||
int len = strlen(cmd);
|
||
int written = write(fd, cmd, len);
|
||
close(fd);
|
||
return written == len;
|
||
}
|
||
|
||
static int test_scene_add_remove(void) {
|
||
printf("Test: scene add/remove via FIFO\n");
|
||
fflush(stdout);
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
printf(" sending scene_add...\n");
|
||
fflush(stdout);
|
||
if (!write_fifo("scene_add\n")) {
|
||
kill(pid, SIGTERM);
|
||
for (int tries = 0; tries < 20; tries++) {
|
||
int wstatus;
|
||
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
|
||
if (ret == pid) break;
|
||
if (ret < 0) break;
|
||
safe_usleep(100000);
|
||
}
|
||
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot write to FIFO\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(50000); /* allow processing */
|
||
|
||
printf(" sending scene_next...\n");
|
||
fflush(stdout);
|
||
if (!write_fifo("scene_next\n")) {
|
||
kill(pid, SIGTERM);
|
||
for (int tries = 0; tries < 20; tries++) {
|
||
int wstatus;
|
||
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
|
||
if (ret == pid) break;
|
||
if (ret < 0) break;
|
||
safe_usleep(100000);
|
||
}
|
||
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
printf(" sending scene_remove...\n");
|
||
fflush(stdout);
|
||
if (!write_fifo("scene_remove\n")) {
|
||
kill(pid, SIGTERM);
|
||
for (int tries = 0; tries < 20; tries++) {
|
||
int wstatus;
|
||
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
|
||
if (ret == pid) break;
|
||
if (ret < 0) break;
|
||
safe_usleep(100000);
|
||
}
|
||
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
/* kill with timeout */
|
||
kill(pid, SIGTERM);
|
||
for (int tries = 0; tries < 20; tries++) {
|
||
int wstatus;
|
||
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
|
||
if (ret == pid) {
|
||
printf(" PASS (scene add/remove, looper exited)\n");
|
||
fflush(stdout);
|
||
return 0;
|
||
}
|
||
if (ret < 0) {
|
||
perror("waitpid");
|
||
break;
|
||
}
|
||
safe_usleep(100000); /* 100ms */
|
||
}
|
||
kill(pid, SIGKILL);
|
||
waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: looper did not exit in time\n");
|
||
return 1;
|
||
}
|
||
|
||
static int test_scene_next_prev_midi(void) {
|
||
printf("Test: scene next/prev via MIDI control key\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
|
||
/* First add a scene so we have >1 scenes */
|
||
if (!write_fifo("scene_add\n")) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot write to FIFO\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(100000);
|
||
|
||
/* Send control key note 64 to arm control */
|
||
if (send_jack_note_on("looper:control", 64, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 64\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
/* Send note 67 (next scene) */
|
||
if (send_jack_note_on("looper:control", 67, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 67\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
/* Send note 68 (prev scene) */
|
||
if (send_jack_note_on("looper:control", 68, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 68\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
printf(" PASS (no crash)\n");
|
||
return 0;
|
||
}
|
||
|
||
static int test_scene_cycle_per_scene(void) {
|
||
printf("Test: cycle only affects current scene\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
/* Add a second scene */
|
||
if (!write_fifo("scene_add\n")) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(100000);
|
||
|
||
/* Switch to scene 0, record a short loop */
|
||
write_fifo("bind 0\n");
|
||
write_fifo("record 0\n");
|
||
safe_usleep(200000); /* let some audio pass through */
|
||
write_fifo("stop\n"); /* stops and sets to looping on scene 0 */
|
||
safe_usleep(50000);
|
||
|
||
/* Now switch to scene 1 */
|
||
write_fifo("scene_next\n");
|
||
safe_usleep(50000);
|
||
|
||
/* Verify that scene 1 is idle and not looping (no crash) */
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
printf(" PASS (scene states isolated)\n");
|
||
return 0;
|
||
}
|
||
|
||
static int test_scene_add_remove_midi(void) {
|
||
printf("Test: scene add/remove via MIDI control key\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
|
||
/* Arm control */
|
||
if (send_jack_note_on("looper:control", 64, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send control key\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(50000);
|
||
|
||
/* Add scene: note 69 */
|
||
if (send_jack_note_on("looper:control", 69, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 69\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(100000);
|
||
|
||
/* Remove scene: note 70 */
|
||
if (send_jack_note_on("looper:control", 70, 100) != 0) {
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note 70\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(100000);
|
||
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
printf(" PASS (no crash)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* test stop via MIDI (control key + note 65) */
|
||
static int test_stop_midi(void) {
|
||
printf("Test: MIDI stop (note 65 under control key)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_stop", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_stop:out");
|
||
snprintf(my_in, sizeof(my_in), "test_stop:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
/* start recording: send note 1 */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note1 failed\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.2f * sr); /* 0.2s beep while recording */
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(150000);
|
||
/* loop: send note 1 again */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: loop note1\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(500000);
|
||
/* stop: control key then note 65 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 65, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: stop note 65\n");
|
||
return 1;
|
||
}
|
||
/* Poll until bursts stop increasing (or up to 2 seconds) */
|
||
int prev = bursts;
|
||
for (int retries = 0; retries < 20; retries++) {
|
||
safe_usleep(100000);
|
||
int cur = bursts;
|
||
if (cur == prev) break;
|
||
prev = cur;
|
||
}
|
||
int bursts_before = bursts;
|
||
safe_usleep(500000);
|
||
int bursts_after = bursts;
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
if (bursts_after > bursts_before + 5) {
|
||
fprintf(stderr, " FAIL: bursts continued after stop (%d -> %d)\n",
|
||
bursts_before, bursts_after);
|
||
return 1;
|
||
}
|
||
printf(" PASS (stop stopped playback)\n");
|
||
return 0;
|
||
}
|
||
|
||
/* full flow: record 1s, loop 5 times, stop, verify at least 5 bursts */
|
||
static int test_record_loop_stop(void) {
|
||
printf("Test: full record‑loop‑stop (≥5 repetitions)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) return 1;
|
||
if (init_persistent_midi_client() != 0) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||
return 1;
|
||
}
|
||
jack_client_t *client;
|
||
jack_status_t status;
|
||
client = jack_client_open("test_full", JackNoStartServer, &status);
|
||
if (!client) {
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " SKIP: no JACK\n");
|
||
return 1;
|
||
}
|
||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsOutput, 0);
|
||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||
JACK_DEFAULT_AUDIO_TYPE,
|
||
JackPortIsInput, 0);
|
||
if (!audio_out || !audio_in) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
char my_out[64], my_in[64];
|
||
snprintf(my_out, sizeof(my_out), "test_full:out");
|
||
snprintf(my_in, sizeof(my_in), "test_full:in");
|
||
if (jack_connect(client, my_out, "looper:input") ||
|
||
jack_connect(client, "looper:output", my_in)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
/* start recording */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: send note1\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(500000);
|
||
/* generate a 0.5 s beep while recording */
|
||
int sr = jack_get_sample_rate(client);
|
||
continuous_sine = 0;
|
||
beep_remaining = (int)(0.5f * sr);
|
||
bursts = 0;
|
||
prev_above = 0;
|
||
passthrough_output_port = audio_out;
|
||
passthrough_input_port = audio_in;
|
||
passthrough_phase = 0.0f;
|
||
passthrough_freq = 440.0f;
|
||
passthrough_sample_rate = sr;
|
||
passthrough_total_samples = 0;
|
||
passthrough_sum_sq = 0.0;
|
||
passthrough_done = 0;
|
||
jack_set_process_callback(client, passthrough_process, NULL);
|
||
if (jack_activate(client)) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
/* end recording -> loop */
|
||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: loop note1\n");
|
||
return 1;
|
||
}
|
||
/* wait for about 5 loops (assuming 0.5s recorded -> ~2.5s loop) */
|
||
safe_usleep(2500000);
|
||
/* stop via control+65 */
|
||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: control key\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
if (send_jack_note_on("looper:control", 65, 127) != 0) {
|
||
jack_client_close(client);
|
||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||
fprintf(stderr, " FAIL: stop note 65\n");
|
||
return 1;
|
||
}
|
||
safe_usleep(200000);
|
||
int total_bursts = bursts;
|
||
jack_deactivate(client);
|
||
jack_client_close(client);
|
||
cleanup_persistent_midi_client();
|
||
kill(pid, SIGTERM);
|
||
waitpid(pid, NULL, 0);
|
||
if (total_bursts < 5) {
|
||
fprintf(stderr, " FAIL: expected ≥5 bursts, got %d\n", total_bursts);
|
||
return 1;
|
||
}
|
||
printf(" PASS (≥5 repetitions, stopped cleanly)\n");
|
||
return 0;
|
||
}
|
||
|
||
int main(void) {
|
||
/* 1. binary must exist */
|
||
if (system("test -x ./looper") != 0) {
|
||
fprintf(stderr, "FATAL: looper binary not found\n");
|
||
return 1;
|
||
}
|
||
|
||
/* 2. MIDI transition tests (skipped – no external tools) */
|
||
|
||
/* 3. Audio pass‑through test – must work for basic connectivity */
|
||
test_audio_pass_through();
|
||
|
||
int failures = 0;
|
||
|
||
/* 4. Test that looping feature is now implemented */
|
||
if (test_looper_looping() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 5. Test multiple dynamic channels */
|
||
if (test_multiple_channels() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 6. Test control‑key modifier */
|
||
if (test_control_key_modifier() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 7. Test bind channel */
|
||
if (test_bind_channel() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 8. Test unbind */
|
||
if (test_bind_unbind() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 9. Test channel removal */
|
||
if (test_remove_channel() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 10. Test FIFO pipe */
|
||
if (test_fifo_pipe() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* Scene tests */
|
||
if (test_scene_add_remove() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
if (test_scene_next_prev_midi() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
if (test_scene_cycle_per_scene() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
if (test_scene_add_remove_midi() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 11. Test MIDI stop */
|
||
if (test_stop_midi() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 12. Test full record‑loop‑stop flow */
|
||
if (test_record_loop_stop() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 13. Test FIFO stop/bind/unbind */
|
||
if (test_fifo_stop_bind_unbind() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
/* 14. Test MIDI channel creation */
|
||
if (test_midi_channel_add() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
failures++;
|
||
}
|
||
|
||
if (failures > 0) {
|
||
fprintf(stderr, "%d test(s) FAILED\n", failures);
|
||
return 1;
|
||
}
|
||
printf("All tests completed successfully.\n");
|
||
return 0;
|
||
}
|