495 lines
16 KiB
C
495 lines
16 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 <math.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;
|
||
|
||
/* 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);
|
||
jack_default_audio_sample_t *in =
|
||
(jack_default_audio_sample_t *)jack_port_get_buffer(passthrough_input_port, nframes);
|
||
if (!out || !in) return 0;
|
||
float *outf = out;
|
||
const float *inf = in;
|
||
for (jack_nframes_t i = 0; i < nframes; i++) {
|
||
/* generate beep while beep_remaining > 0 */
|
||
float out_val;
|
||
if (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;
|
||
beep_remaining--;
|
||
} else {
|
||
out_val = 0.0f;
|
||
}
|
||
outf[i] = out_val;
|
||
|
||
/* detect bursts on the input (looper output) */
|
||
float sample = inf[i];
|
||
int above = (fabsf(sample) > 0.05f);
|
||
if (above && !prev_above) {
|
||
bursts++;
|
||
}
|
||
prev_above = above;
|
||
|
||
passthrough_sum_sq += (double)inf[i] * (double)inf[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;
|
||
}
|
||
|
||
/*
|
||
* Send a hex MIDI message to the given port.
|
||
*/
|
||
static int send_midi(const char *port, const char *msg) {
|
||
char cmd[512];
|
||
snprintf(cmd, sizeof(cmd),
|
||
"jack_midi_send -c looper:%s -m '%s' 2>/dev/null",
|
||
port, msg);
|
||
int ret = system(cmd);
|
||
if (ret != 0) {
|
||
fprintf(stderr, "jack_midi_send failed: %s\n", cmd);
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/*
|
||
* Ask the looper to report its current state and exit.
|
||
* Returns the state (0..3) or -1 on failure.
|
||
*/
|
||
static int request_state_and_exit(pid_t pid) {
|
||
kill(pid, SIGUSR1);
|
||
int status;
|
||
if (waitpid(pid, &status, 0) != pid) {
|
||
perror("waitpid");
|
||
return -1;
|
||
}
|
||
if (WIFEXITED(status)) {
|
||
int code = WEXITSTATUS(status);
|
||
/* looper returns state+1, so state = code-1 */
|
||
int state = code - 1;
|
||
if (state >= STATE_IDLE && state <= STATE_PAUSED) {
|
||
return state;
|
||
}
|
||
fprintf(stderr, "Unexpected exit code %d (expected 1..4)\n", code);
|
||
return -1;
|
||
}
|
||
fprintf(stderr, "Looper terminated by signal %d\n", WTERMSIG(status));
|
||
return -1;
|
||
}
|
||
|
||
/*
|
||
* Perform a single transition test: start looper, send @p midi_msg
|
||
* (may be NULL for idle‑only test), then verify state equals @p expected_state.
|
||
* Exits the whole program on failure.
|
||
*/
|
||
static void test_transition(const char *label,
|
||
const char *midi_port,
|
||
const char *midi_msg,
|
||
int expected_state)
|
||
{
|
||
printf("Test: %s (expect state %d)\n", label, expected_state);
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) {
|
||
fprintf(stderr, "FAIL: could not start looper\n");
|
||
exit(1);
|
||
}
|
||
if (midi_msg) {
|
||
send_midi(midi_port, midi_msg);
|
||
sleep(WAIT_SECONDS);
|
||
}
|
||
int got = request_state_and_exit(pid);
|
||
if (got == expected_state) {
|
||
printf(" PASS\n");
|
||
} else {
|
||
fprintf(stderr, " FAIL: got %d, expected %d\n", got, expected_state);
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* Test MIDI clock Start (0xFA) while idle.
|
||
*/
|
||
static void test_clock_start(void) {
|
||
test_transition("clock start -> record", "clock", "FA", STATE_RECORD);
|
||
}
|
||
|
||
/*
|
||
* Test MIDI clock Stop (0xFC) after first entering RECORD via control note.
|
||
*/
|
||
static void test_clock_stop(void) {
|
||
printf("Test: clock stop after record (expect idle)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) exit(1);
|
||
/* IDLE -> RECORD */
|
||
send_midi("control", "90 01 7f");
|
||
sleep(WAIT_SECONDS);
|
||
/* clock stop -> IDLE */
|
||
send_midi("clock", "FC");
|
||
sleep(WAIT_SECONDS);
|
||
int got = request_state_and_exit(pid);
|
||
if (got == STATE_IDLE) {
|
||
printf(" PASS\n");
|
||
} else {
|
||
fprintf(stderr, " FAIL: got %d, expected %d\n", got, STATE_IDLE);
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
/*
|
||
* Test MIDI clock Continue (0xFB) from PAUSED -> LOOPING.
|
||
*/
|
||
static void test_clock_continue(void) {
|
||
printf("Test: clock continue from paused (expect looping)\n");
|
||
pid_t pid = start_looper();
|
||
if (pid < 0) exit(1);
|
||
/* IDLE -> RECORD */
|
||
send_midi("control", "90 01 7f");
|
||
sleep(WAIT_SECONDS);
|
||
/* RECORD -> LOOPING */
|
||
send_midi("control", "90 01 7f");
|
||
sleep(WAIT_SECONDS);
|
||
/* LOOPING -> PAUSED */
|
||
send_midi("control", "90 01 7f");
|
||
sleep(WAIT_SECONDS);
|
||
/* clock continue -> LOOPING */
|
||
send_midi("clock", "FB");
|
||
sleep(WAIT_SECONDS);
|
||
int got = request_state_and_exit(pid);
|
||
if (got == STATE_LOOPING) {
|
||
printf(" PASS\n");
|
||
} else {
|
||
fprintf(stderr, " FAIL: got %d, expected %d\n", got, STATE_LOOPING);
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
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;
|
||
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;
|
||
}
|
||
usleep(2200000); /* 2.2 seconds */
|
||
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: open a transient JACK client, send a MIDI note‑on, close */
|
||
static int send_jack_note_on(const char *target_port, unsigned char note, unsigned char velocity) {
|
||
jack_client_t *trig;
|
||
jack_status_t st;
|
||
trig = jack_client_open("test_midi_trig", JackNoStartServer, &st);
|
||
if (!trig) return -1;
|
||
jack_port_t *port = jack_port_register(trig, "out",
|
||
JACK_DEFAULT_MIDI_TYPE,
|
||
JackPortIsOutput, 0);
|
||
if (!port) { jack_client_close(trig); return -1; }
|
||
if (jack_activate(trig)) { jack_client_close(trig); return -1; }
|
||
char src[64];
|
||
snprintf(src, sizeof(src), "test_midi_trig:out");
|
||
if (jack_connect(trig, src, target_port)) {
|
||
jack_client_close(trig);
|
||
return -1;
|
||
}
|
||
usleep(200000);
|
||
jack_nframes_t now = jack_frame_time(trig);
|
||
jack_midi_data_t data[3] = { 0x90, note, velocity };
|
||
jack_midi_event_write(port, now, data, 3);
|
||
usleep(100000);
|
||
jack_deactivate(trig);
|
||
jack_client_close(trig);
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
* Full loop recording test:
|
||
* 1. start looper
|
||
* 2. open test client (audio + MIDI)
|
||
* 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;
|
||
|
||
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;
|
||
}
|
||
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;
|
||
}
|
||
usleep(200000); /* allow state to change */
|
||
|
||
int sr = jack_get_sample_rate(client);
|
||
beep_remaining = (int)(0.1f * sr); /* 0.1 second beep */
|
||
bursts = 0;
|
||
prev_above = 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;
|
||
}
|
||
|
||
/* override the global passthrough callbacks to generate beep and detect bursts */
|
||
/* We'll embed detection inside a helper that we set via static pointer. */
|
||
/* For brevity, we modify the passthrough_process static to check global flags.
|
||
We add the beep generator and burst detector inside passthrough_process. */
|
||
/* The existing passthrough_process already writes a sine to its output port,
|
||
but does not use beep_remaining. We'll replace it with a version that
|
||
respects beep_remaining and also counts bursts on the input (looper output). */
|
||
/* We'll directly overwrite the static function pointer? Instead, we'll
|
||
modify the function definition later. For now we trust that the
|
||
existing passthrough_process will be adapted (see code change below). */
|
||
|
||
usleep(150000); /* let beep start */
|
||
|
||
/* after beep finishes, give it a moment then send note‑on to stop recording */
|
||
usleep(500000);
|
||
beep_remaining = 0;
|
||
|
||
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 (3 seconds) */
|
||
usleep(3000000);
|
||
|
||
jack_deactivate(client);
|
||
jack_client_close(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;
|
||
}
|
||
|
||
/*
|
||
* Helper: run all MIDI‑based state transition tests.
|
||
* Requires jack_midi_send; if missing these tests are skipped.
|
||
*/
|
||
static int run_midi_tests(void) {
|
||
if (system("which jack_midi_send >/dev/null 2>&1") != 0) {
|
||
printf("SKIP: MIDI state tests – jack_midi_send not installed\n");
|
||
return 0; /* not a failure, just skip */
|
||
}
|
||
|
||
test_transition("IDLE -> RECORD", "control", "90 01 7f", STATE_RECORD);
|
||
test_transition("RECORD -> LOOPING", "control", "90 01 7f", STATE_LOOPING);
|
||
test_transition("LOOPING -> PAUSED", "control", "90 01 7f", STATE_PAUSED);
|
||
test_transition("PAUSED -> LOOPING", "control", "90 01 7f", STATE_LOOPING);
|
||
|
||
test_clock_start();
|
||
test_clock_stop();
|
||
test_clock_continue();
|
||
|
||
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();
|
||
|
||
/* 4. Test that looping feature is now implemented */
|
||
if (test_looper_looping() != 0) {
|
||
fprintf(stderr, " FAILED\n");
|
||
return 1;
|
||
}
|
||
|
||
printf("All tests completed successfully.\n");
|
||
return 0;
|
||
}
|