feat: implement loop recording and playback with integration test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
88
src/main.c
88
src/main.c
@@ -6,6 +6,9 @@
|
|||||||
#include <jack/jack.h>
|
#include <jack/jack.h>
|
||||||
#include <jack/midiport.h>
|
#include <jack/midiport.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define LOOP_BUF_SIZE (5 * 48000) /* 5 seconds at 48 kHz, mono */
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
STATE_IDLE,
|
STATE_IDLE,
|
||||||
@@ -16,6 +19,13 @@ typedef enum {
|
|||||||
|
|
||||||
static atomic_int current_state = STATE_IDLE;
|
static atomic_int current_state = STATE_IDLE;
|
||||||
|
|
||||||
|
/* loop buffer and playback state */
|
||||||
|
static float loop_buffer[LOOP_BUF_SIZE];
|
||||||
|
static int loop_count = 0; /* number of recorded samples */
|
||||||
|
static int record_pos = 0; /* next write index while recording */
|
||||||
|
static int playback_pos = 0; /* next read index while looping */
|
||||||
|
static int prev_state = -1; /* for detecting state changes */
|
||||||
|
|
||||||
static jack_port_t *input_port;
|
static jack_port_t *input_port;
|
||||||
static jack_port_t *output_port;
|
static jack_port_t *output_port;
|
||||||
static jack_port_t *midi_control_port;
|
static jack_port_t *midi_control_port;
|
||||||
@@ -29,21 +39,29 @@ static int process(jack_nframes_t nframes, void *arg)
|
|||||||
jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer(input_port, nframes);
|
jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer(input_port, nframes);
|
||||||
jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer(output_port, nframes);
|
jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer(output_port, nframes);
|
||||||
|
|
||||||
if (out) {
|
/* ----- state change detection ----- */
|
||||||
if (in) {
|
int state = atomic_load(¤t_state);
|
||||||
memcpy(out, in, sizeof(jack_default_audio_sample_t) * nframes);
|
if (state != prev_state) {
|
||||||
} else {
|
if (state == STATE_RECORD) {
|
||||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
record_pos = 0;
|
||||||
|
loop_count = 0;
|
||||||
|
} else if (state == STATE_LOOPING) {
|
||||||
|
if (record_pos > 0) {
|
||||||
|
loop_count = record_pos; /* what we recorded */
|
||||||
}
|
}
|
||||||
|
playback_pos = 0; /* restart from beginning */
|
||||||
|
}
|
||||||
|
prev_state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ----- handle MIDI control port (state transitions) ----- */
|
||||||
void *midi_ctrl_buf = jack_port_get_buffer(midi_control_port, nframes);
|
void *midi_ctrl_buf = jack_port_get_buffer(midi_control_port, nframes);
|
||||||
if (midi_ctrl_buf) {
|
if (midi_ctrl_buf) {
|
||||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_ctrl_buf);
|
jack_nframes_t nevents = jack_midi_get_event_count(midi_ctrl_buf);
|
||||||
jack_midi_event_t ev;
|
jack_midi_event_t ev;
|
||||||
for (jack_nframes_t i = 0; i < nevents; i++) {
|
for (jack_nframes_t i = 0; i < nevents; i++) {
|
||||||
if (jack_midi_event_get(&ev, midi_ctrl_buf, i) == 0) {
|
if (jack_midi_event_get(&ev, midi_ctrl_buf, i) == 0) {
|
||||||
// note on with note number 1
|
/* note on with note number 1 */
|
||||||
if ((ev.size >= 3) && ((ev.buffer[0] & 0xf0) == 0x90)) {
|
if ((ev.size >= 3) && ((ev.buffer[0] & 0xf0) == 0x90)) {
|
||||||
unsigned char note = ev.buffer[1];
|
unsigned char note = ev.buffer[1];
|
||||||
if (note == 1) {
|
if (note == 1) {
|
||||||
@@ -68,7 +86,54 @@ static int process(jack_nframes_t nframes, void *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process MIDI clock messages for transport control (start/stop)
|
/* ----- audio output based on current state ----- */
|
||||||
|
if (!out) return 0; /* cannot happen, but safe */
|
||||||
|
|
||||||
|
jack_nframes_t i;
|
||||||
|
|
||||||
|
switch (state) {
|
||||||
|
case STATE_RECORD:
|
||||||
|
if (in) {
|
||||||
|
const float *inf = (const float *)in;
|
||||||
|
float *outf = (float *)out;
|
||||||
|
for (i = 0; i < nframes; i++) {
|
||||||
|
if (record_pos < LOOP_BUF_SIZE) {
|
||||||
|
loop_buffer[record_pos] = inf[i];
|
||||||
|
record_pos++;
|
||||||
|
}
|
||||||
|
outf[i] = inf[i]; /* monitor input */
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_LOOPING:
|
||||||
|
if (loop_count > 0) {
|
||||||
|
float *outf = (float *)out;
|
||||||
|
for (i = 0; i < nframes; i++) {
|
||||||
|
outf[i] = loop_buffer[playback_pos];
|
||||||
|
playback_pos = (playback_pos + 1) % loop_count;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_PAUSED:
|
||||||
|
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: /* IDLE */
|
||||||
|
if (in) {
|
||||||
|
memcpy(out, in, sizeof(jack_default_audio_sample_t) * nframes);
|
||||||
|
} else {
|
||||||
|
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----- MIDI clock events (unchanged) ----- */
|
||||||
void *midi_clock_buf = jack_port_get_buffer(midi_clock_port, nframes);
|
void *midi_clock_buf = jack_port_get_buffer(midi_clock_port, nframes);
|
||||||
if (midi_clock_buf) {
|
if (midi_clock_buf) {
|
||||||
jack_nframes_t n_clock_events = jack_midi_get_event_count(midi_clock_buf);
|
jack_nframes_t n_clock_events = jack_midi_get_event_count(midi_clock_buf);
|
||||||
@@ -77,15 +142,14 @@ static int process(jack_nframes_t nframes, void *arg)
|
|||||||
if (jack_midi_event_get(&cev, midi_clock_buf, j) == 0) {
|
if (jack_midi_event_get(&cev, midi_clock_buf, j) == 0) {
|
||||||
if (cev.size >= 1) {
|
if (cev.size >= 1) {
|
||||||
unsigned char msg = cev.buffer[0];
|
unsigned char msg = cev.buffer[0];
|
||||||
// real-time messages: Start (0xFA), Stop (0xFC), Continue (0xFB)
|
if (msg == 0xFA) {
|
||||||
if (msg == 0xFA) { // Start transport -> begin recording if idle
|
|
||||||
int s = atomic_load(¤t_state);
|
int s = atomic_load(¤t_state);
|
||||||
if (s == STATE_IDLE) {
|
if (s == STATE_IDLE) {
|
||||||
atomic_store(¤t_state, STATE_RECORD);
|
atomic_store(¤t_state, STATE_RECORD);
|
||||||
}
|
}
|
||||||
} else if (msg == 0xFC) { // Stop transport -> return to IDLE
|
} else if (msg == 0xFC) {
|
||||||
atomic_store(¤t_state, STATE_IDLE);
|
atomic_store(¤t_state, STATE_IDLE);
|
||||||
} else if (msg == 0xFB) { // Continue transport -> resume looping (if paused)
|
} else if (msg == 0xFB) {
|
||||||
int s = atomic_load(¤t_state);
|
int s = atomic_load(¤t_state);
|
||||||
if (s == STATE_PAUSED) {
|
if (s == STATE_PAUSED) {
|
||||||
atomic_store(¤t_state, STATE_LOOPING);
|
atomic_store(¤t_state, STATE_LOOPING);
|
||||||
@@ -166,6 +230,8 @@ int main(int argc, char *argv[])
|
|||||||
/* allow SIGUSR1 to report state and exit */
|
/* allow SIGUSR1 to report state and exit */
|
||||||
signal(SIGUSR1, sigusr1_handler);
|
signal(SIGUSR1, sigusr1_handler);
|
||||||
|
|
||||||
|
prev_state = -1; /* initialise change detection */
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,11 @@ static long passthrough_total_samples = 0;
|
|||||||
static double passthrough_sum_sq = 0.0;
|
static double passthrough_sum_sq = 0.0;
|
||||||
static volatile int passthrough_done = 0;
|
static volatile int passthrough_done = 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) {
|
static int passthrough_process(jack_nframes_t nframes, void *arg) {
|
||||||
(void)arg;
|
(void)arg;
|
||||||
jack_default_audio_sample_t *out =
|
jack_default_audio_sample_t *out =
|
||||||
@@ -30,10 +35,30 @@ static int passthrough_process(jack_nframes_t nframes, void *arg) {
|
|||||||
float *outf = out;
|
float *outf = out;
|
||||||
const float *inf = in;
|
const float *inf = in;
|
||||||
for (jack_nframes_t i = 0; i < nframes; i++) {
|
for (jack_nframes_t i = 0; i < nframes; i++) {
|
||||||
float val = sinf(passthrough_phase);
|
/* generate beep while beep_remaining > 0 */
|
||||||
outf[i] = val;
|
extern int beep_remaining; /* defined in test_looper_looping */
|
||||||
|
float out_val;
|
||||||
|
if (beep_remaining > 0) {
|
||||||
|
out_val = sinf(passthrough_phase);
|
||||||
passthrough_phase += 2.0f * (float)M_PI * passthrough_freq / passthrough_sample_rate;
|
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 (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) */
|
||||||
|
extern int bursts;
|
||||||
|
extern int prev_above;
|
||||||
|
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_sum_sq += (double)inf[i] * (double)inf[i];
|
||||||
passthrough_total_samples++;
|
passthrough_total_samples++;
|
||||||
}
|
}
|
||||||
@@ -305,6 +330,146 @@ static void test_looping_not_implemented(void) {
|
|||||||
printf(" SUCCESS: nothing was measured (looping feature not implemented)\n");
|
printf(" SUCCESS: nothing was measured (looping feature not implemented)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* state variables used by the process callback */
|
||||||
|
static volatile int beep_remaining = 0;
|
||||||
|
static volatile int bursts = 0;
|
||||||
|
static volatile int prev_above = 0;
|
||||||
|
|
||||||
|
/* 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.
|
* Helper: run all MIDI‑based state transition tests.
|
||||||
* Requires jack_midi_send; if missing these tests are skipped.
|
* Requires jack_midi_send; if missing these tests are skipped.
|
||||||
@@ -339,9 +504,12 @@ int main(void) {
|
|||||||
/* 3. Audio pass‑through test – must work for basic connectivity */
|
/* 3. Audio pass‑through test – must work for basic connectivity */
|
||||||
test_audio_pass_through();
|
test_audio_pass_through();
|
||||||
|
|
||||||
/* 4. Test that looping feature is missing (expected) */
|
/* 4. Test that looping feature is now implemented */
|
||||||
test_looping_not_implemented();
|
if (test_looper_looping() != 0) {
|
||||||
|
fprintf(stderr, " FAILED\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
printf("All tests completed successfully (missing features noted).\n");
|
printf("All tests completed successfully.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user