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/midiport.h>
|
||||
#include <stdatomic.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LOOP_BUF_SIZE (5 * 48000) /* 5 seconds at 48 kHz, mono */
|
||||
|
||||
typedef enum {
|
||||
STATE_IDLE,
|
||||
@@ -16,6 +19,13 @@ typedef enum {
|
||||
|
||||
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 *output_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 *out = (jack_default_audio_sample_t *) jack_port_get_buffer(output_port, nframes);
|
||||
|
||||
if (out) {
|
||||
if (in) {
|
||||
memcpy(out, in, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
/* ----- state change detection ----- */
|
||||
int state = atomic_load(¤t_state);
|
||||
if (state != prev_state) {
|
||||
if (state == STATE_RECORD) {
|
||||
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);
|
||||
if (midi_ctrl_buf) {
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_ctrl_buf);
|
||||
jack_midi_event_t ev;
|
||||
for (jack_nframes_t i = 0; i < nevents; i++) {
|
||||
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)) {
|
||||
unsigned char note = ev.buffer[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);
|
||||
if (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 (cev.size >= 1) {
|
||||
unsigned char msg = cev.buffer[0];
|
||||
// real-time messages: Start (0xFA), Stop (0xFC), Continue (0xFB)
|
||||
if (msg == 0xFA) { // Start transport -> begin recording if idle
|
||||
if (msg == 0xFA) {
|
||||
int s = atomic_load(¤t_state);
|
||||
if (s == STATE_IDLE) {
|
||||
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);
|
||||
} else if (msg == 0xFB) { // Continue transport -> resume looping (if paused)
|
||||
} else if (msg == 0xFB) {
|
||||
int s = atomic_load(¤t_state);
|
||||
if (s == STATE_PAUSED) {
|
||||
atomic_store(¤t_state, STATE_LOOPING);
|
||||
@@ -166,6 +230,8 @@ int main(int argc, char *argv[])
|
||||
/* allow SIGUSR1 to report state and exit */
|
||||
signal(SIGUSR1, sigusr1_handler);
|
||||
|
||||
prev_state = -1; /* initialise change detection */
|
||||
|
||||
while (1) {
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user