refactor: replace writer thread with synchronous save and fix ring buffer memory ordering

This commit is contained in:
Loic Coenen
2026-05-18 17:35:31 +00:00
committed by Loic Coenen (aider)
parent 10e47e6c0c
commit f38797fe0a
22 changed files with 291 additions and 1369 deletions

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -17,7 +17,8 @@ void channel_add(jack_client_t *client, int idx) {
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
/* Always register audio ports (needed for pass-through even for MIDI channels?) */
/* Always register audio ports (needed for pass-through even for MIDI
* channels?) */
channels[idx].audio_in = jack_port_register(
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[idx].audio_out = jack_port_register(
@@ -33,8 +34,10 @@ void channel_add(jack_client_t *client, int idx) {
/* If this is a MIDI channel, register MIDI ports */
if (channels[idx].type == CHANNEL_MIDI) {
char midi_in_name[64], midi_out_name[64];
snprintf(midi_in_name, sizeof(midi_in_name), "channel%d_midi_in", next_channel_id);
snprintf(midi_out_name, sizeof(midi_out_name), "channel%d_midi_out", next_channel_id);
snprintf(midi_in_name, sizeof(midi_in_name), "channel%d_midi_in",
next_channel_id);
snprintf(midi_out_name, sizeof(midi_out_name), "channel%d_midi_out",
next_channel_id);
channels[idx].midi_in = jack_port_register(
client, midi_in_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
channels[idx].midi_out = jack_port_register(

View File

@@ -1,117 +0,0 @@
// cppcheck-suppress missingIncludeSystem
#include "channel.h"
#include <jack/jack.h>
#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
<<<<<<< HEAD
=======
/* Helper: zero a scene and set its state to IDLE */
static void init_scene(scene_t *sc) {
memset(sc, 0, sizeof(scene_t));
atomic_store(&sc->state, STATE_IDLE);
atomic_store(&sc->prev_state, -1);
}
>>>>>>> 3-integrate-carla
void channel_add(jack_client_t *client, int idx) {
char in_name[64], out_name[64];
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
channels[idx].audio_in = jack_port_register(
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[idx].audio_out = jack_port_register(
client, out_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!channels[idx].audio_in || !channels[idx].audio_out) {
fprintf(stderr, "Failed to register ports for channel %d\n",
next_channel_id);
/* Do NOT mark channel active process loop will skip it */
atomic_store(&channels[idx].active, 0);
return;
}
atomic_store(&channels[idx].active, 1);
atomic_store(&channels[idx].state, STATE_IDLE);
channels[idx].prev_state = -1;
channels[idx].loop_count = 0;
channels[idx].record_pos = 0;
channels[idx].playback_pos = 0;
channels[idx].save_ring = NULL;
next_channel_id++;
channel_count++;
}
void channel_remove(jack_client_t *client, int idx) {
(void)client;
<<<<<<< HEAD
atomic_store(&channels[idx].active, 0);
channel_count--;
=======
struct channel_t *cur = get_channels_array();
atomic_store(&cur[idx].active, 0);
atomic_fetch_sub(&channel_count, 1);
}
void channel_add_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
if (atomic_load(&cur[idx].scene_count) >= MAX_SCENES)
return;
int ns = atomic_load(&cur[idx].scene_count);
init_scene(&cur[idx].scenes[ns]);
atomic_fetch_add(&cur[idx].scene_count, 1);
}
void channel_remove_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc <= 1)
return;
int cs = atomic_load(&cur[idx].current_scene);
/* shift remaining scenes down (atomic copy of fields) */
for (int i = cs; i < sc - 1; i++) {
atomic_store(&cur[idx].scenes[i].loop_count,
atomic_load(&cur[idx].scenes[i + 1].loop_count));
atomic_store(&cur[idx].scenes[i].record_pos,
atomic_load(&cur[idx].scenes[i + 1].record_pos));
atomic_store(&cur[idx].scenes[i].playback_pos,
atomic_load(&cur[idx].scenes[i + 1].playback_pos));
atomic_store(&cur[idx].scenes[i].state,
atomic_load(&cur[idx].scenes[i + 1].state));
atomic_store(&cur[idx].scenes[i].prev_state,
atomic_load(&cur[idx].scenes[i + 1].prev_state));
/* copy loop data (may race with RT thread; acceptable for this release) */
memcpy(cur[idx].scenes[i].loop.audio_buffer,
cur[idx].scenes[i + 1].loop.audio_buffer,
LOOP_BUF_SIZE * sizeof(float));
}
atomic_fetch_sub(&cur[idx].scene_count, 1);
int new_sc = atomic_load(&cur[idx].scene_count);
if (cs >= new_sc)
atomic_store(&cur[idx].current_scene, new_sc - 1);
}
void channel_next_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs + 1) % sc);
}
}
void channel_prev_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs - 1 + sc) % sc);
}
>>>>>>> 3-integrate-carla
}

View File

Binary file not shown.

View File

@@ -2,9 +2,9 @@
#include "looper.h"
#include "channel.h"
#include "midi.h"
#include "pipe.h"
#include "queue.h"
#include "wav.h"
#include "pipe.h"
#include <fcntl.h>
#include <jack/jack.h>
#include <jack/midiport.h>
@@ -38,13 +38,23 @@ static void looper_write_status(void) {
int state = atomic_load(&channels[ch].scenes[sc_idx].state);
const char *state_str;
switch (state) {
case STATE_IDLE: state_str = "IDLE"; break;
case STATE_RECORD: state_str = "RECORD"; break;
case STATE_LOOPING:state_str = "LOOPING";break;
case STATE_PAUSED: state_str = "PAUSED"; break;
default: state_str = "UNKNOWN";
case STATE_IDLE:
state_str = "IDLE";
break;
case STATE_RECORD:
state_str = "RECORD";
break;
case STATE_LOOPING:
state_str = "LOOPING";
break;
case STATE_PAUSED:
state_str = "PAUSED";
break;
default:
state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx, state_str);
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx,
state_str);
if (n > 0) {
int ret = write(status_fd, buf, n);
(void)ret;
@@ -69,34 +79,34 @@ atomic_int bind_channel = 0;
/* Deferred removal index (1 second grace) */
static int pending_unregister_idx = -1;
/* writer thread function and sample rate holder */
static void *writer_thread(void *arg);
/* sample rate holder */
static int global_sample_rate = 0;
/* execute a single command (called from looper_process_commands) */
static void exec_command(command_t cmd, jack_client_t *client) {
int ch = cmd.channel;
if (ch < 0) ch = 0;
if (ch < 0)
ch = 0;
switch (cmd.type) {
case CMD_CYCLE: {
int sc_idx = atomic_load(&channels[ch].current_scene);
int state = atomic_load(&channels[ch].scenes[sc_idx].state);
scene_t *sc_ptr = &channels[ch].scenes[sc_idx];
int state = atomic_load(&sc_ptr->state);
switch (state) {
case STATE_IDLE:
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_RECORD);
atomic_store(&sc_ptr->state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_LOOPING);
atomic_store(&sc_ptr->state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_PAUSED);
atomic_store(&sc_ptr->state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_LOOPING);
atomic_store(&sc_ptr->state, STATE_LOOPING);
break;
}
atomic_store(&channels[ch].scenes[sc_idx].prev_state, -1);
break;
}
case CMD_STOP:
@@ -217,7 +227,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
/* MIDI channel handling */
void *midi_in_buf = jack_port_get_buffer(channels[c].midi_in, nframes);
void *midi_out_buf = jack_port_get_buffer(channels[c].midi_out, nframes);
if (!midi_out_buf) continue;
if (!midi_out_buf)
continue;
switch (state) {
case STATE_RECORD: {
@@ -232,7 +243,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
sc->loop.midi_events[rp].timestamp = ev.time;
sc->loop.midi_events[rp].status = ev.buffer[0];
sc->loop.midi_events[rp].note = (ev.size > 1) ? ev.buffer[1] : 0;
sc->loop.midi_events[rp].velocity = (ev.size > 2) ? ev.buffer[2] : 0;
sc->loop.midi_events[rp].velocity =
(ev.size > 2) ? ev.buffer[2] : 0;
atomic_store(&sc->record_pos, rp + 1);
}
}
@@ -286,7 +298,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
jack_default_audio_sample_t *out =
(jack_default_audio_sample_t *)jack_port_get_buffer(
channels[c].audio_out, nframes);
if (!out) continue;
if (!out)
continue;
switch (state) {
case STATE_RECORD:
@@ -397,7 +410,6 @@ void jack_shutdown_cb(void *arg) {
exit(0);
}
/* ----------------------------------------------------------------
* looper initialisation
* ---------------------------------------------------------------- */
@@ -408,7 +420,8 @@ int looper_init(jack_client_t *client) {
/* create status FIFO (ignore if already exists) */
mkfifo(STATUS_FIFO, 0666);
/* open the status FIFO for reading+writing so writes work even without reader */
/* open the status FIFO for reading+writing so writes work even without reader
*/
status_fd = open(STATUS_FIFO, O_RDWR);
if (status_fd < 0) {
perror("open status FIFO");
@@ -421,7 +434,6 @@ int looper_init(jack_client_t *client) {
/* start the FIFO reader thread */
pipe_start_reader();
/* channel 0 */
channels[0].active = 1;
channels[0].type = CHANNEL_AUDIO; /* default */
@@ -453,58 +465,15 @@ int looper_init(jack_client_t *client) {
return -1;
}
/* Give JACK time to register the ports before clients connect */
{
struct timespec req = {.tv_sec = 0, .tv_nsec = 500000000};
nanosleep(&req, NULL);
}
return 0;
}
/* ----------------------------------------------------------------
* writer thread consumes the save ring and writes WAV file
* ---------------------------------------------------------------- */
static void *writer_thread(void *arg) {
struct channel_t *ch = (struct channel_t *)arg;
int sc_idx = atomic_load(&ch->current_scene);
scene_t *sc = &ch->scenes[sc_idx];
RingBuf *ring = (RingBuf *)ch->save_ring;
if (!ring)
return NULL;
static const char *path = "save.wav";
unsigned sr = (unsigned)global_sample_rate;
if (sr == 0)
sr = 48000;
int lc = atomic_load(&sc->loop_count);
float *outbuf = malloc((size_t)lc * sizeof(float));
if (!outbuf) {
ring_destroy(ring);
free(ring);
ch->save_ring = NULL;
return NULL;
}
size_t collected = 0;
size_t want = (size_t)lc;
while (collected < want) {
size_t got = ring_read(ring, outbuf + collected, want - collected);
collected += got;
if (got == 0) {
struct timespec req = {.tv_sec = 0, .tv_nsec = 10000000};
nanosleep(&req, NULL);
}
}
wav_write(path, outbuf, (unsigned)lc, sr);
free(outbuf);
/* Signal the RT thread to stop writing */
atomic_store_explicit(&ch->save_complete, 1, memory_order_release);
/* Wait for the RT thread to see the flag (one audio period) */
struct timespec req = { .tv_sec = 0, .tv_nsec = 10000000 }; /* 10ms */
nanosleep(&req, NULL);
ring_destroy(ring);
free(ring);
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
return NULL;
}
/* ----------------------------------------------------------------
* mainloop command processing
* ---------------------------------------------------------------- */
@@ -578,25 +547,31 @@ void looper_process_commands(jack_client_t *client) {
}
}
/* ---------- save command (writer thread) ---------- */
/* ---------- save command (synchronous) ---------- */
if (atomic_exchange(&cmd_save, 0)) {
int sc_idx = atomic_load(&channels[0].current_scene);
scene_t *sc = &channels[0].scenes[sc_idx];
int lc = atomic_load(&sc->loop_count);
if (atomic_load(&sc->state) == STATE_LOOPING && lc > 0 &&
channels[0].save_ring == NULL) {
RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf));
if (ring) {
size_t sz = (size_t)lc * 2;
if (ring_init(ring, sz) == 0) {
atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring,
memory_order_release);
pthread_t th;
pthread_create(&th, NULL, writer_thread, &channels[0]);
pthread_detach(th);
} else {
free(ring);
if (atomic_load(&sc->state) == STATE_LOOPING && lc > 0) {
/* Deactivate channel to prevent RT thread from reading the buffer */
int was_active = atomic_load(&channels[0].active);
if (was_active) {
atomic_store(&channels[0].active, 0);
struct timespec req = {.tv_sec = 0, .tv_nsec = 500000000}; /* 500 ms */
nanosleep(&req, NULL);
}
/* Now safe to copy the loop buffer */
float *data = malloc((size_t)lc * sizeof(float));
if (data) {
memcpy(data, sc->loop.audio_buffer, (size_t)lc * sizeof(float));
unsigned sr = (unsigned)global_sample_rate;
if (sr == 0) sr = 48000;
wav_write("save.wav", data, (unsigned)lc, sr);
free(data);
}
/* Reactivate channel */
if (was_active) {
atomic_store(&channels[0].active, 1);
}
}
}

View File

@@ -1,675 +0,0 @@
// cppcheck-suppress missingIncludeSystem
#include "looper.h"
#include "channel.h"
#include "midi.h"
<<<<<<< HEAD
#include "wav.h"
#include "ringbuffer.h"
#include "pipe.h"
#include <jack/jack.h>
#include <sys/stat.h>
=======
#include "queue.h"
>>>>>>> 3-integrate-carla
#include <fcntl.h>
#include <jack/jack.h>
#include <jack/midiport.h>
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
<<<<<<< HEAD
#include <time.h>
#include "queue.h"
#include "command.h"
/* Global command queues */
spsc_queue_t cmd_queue;
spsc_queue_t cmd_queue_main_midi;
spsc_queue_t cmd_queue_main_fifo;
=======
#include <sys/stat.h>
#include <unistd.h>
>>>>>>> 3-integrate-carla
#define STATUS_FIFO "/tmp/looper_status"
/* writer status fd */
static int status_fd = -1;
static void looper_write_status(void) {
<<<<<<< HEAD
if (status_fd < 0)
return;
char buf[256];
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
if (!atomic_load(&channels[ch].active))
continue;
int state_val = atomic_load(&channels[ch].state);
const char *state_str;
switch (state_val) {
case STATE_IDLE: state_str = "IDLE"; break;
case STATE_RECORD: state_str = "RECORD"; break;
case STATE_LOOPING: state_str = "LOOPING"; break;
case STATE_PAUSED: state_str = "PAUSED"; break;
default: state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf),
"CH=%d SC=%d STATE=%s\n",
ch, 0, state_str);
if (n > 0) {
int ret = write(status_fd, buf, n);
(void)ret;
}
}
=======
int fd = open(STATUS_FIFO, O_WRONLY | O_NONBLOCK);
if (fd < 0)
return;
struct channel_t *cur = get_channels_array();
int cap = atomic_load(&channel_capacity);
char buf[256];
for (int ch = 0; ch < cap; ch++) {
if (!atomic_load(&cur[ch].active))
continue;
int sc_idx = atomic_load(&cur[ch].current_scene);
int state = atomic_load(&cur[ch].scenes[sc_idx].state);
const char *state_str;
switch (state) {
case STATE_IDLE:
state_str = "IDLE";
break;
case STATE_RECORD:
state_str = "RECORD";
break;
case STATE_LOOPING:
state_str = "LOOPING";
break;
case STATE_PAUSED:
state_str = "PAUSED";
break;
default:
state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx,
state_str);
if (n > 0) {
int ret = write(fd, buf, n);
(void)ret;
}
}
close(fd);
>>>>>>> 3-integrate-carla
}
/* Global state (shared across files) */
struct channel_t channels[MAX_CHANNELS];
atomic_int channel_count = 0;
atomic_int channel_capacity = MAX_CHANNELS;
int next_channel_id = 1;
atomic_int cmd_add = 0;
atomic_int cmd_remove = 0;
atomic_int cmd_load = 0;
atomic_int cmd_save = 0;
jack_port_t *midi_control_port = NULL;
jack_port_t *midi_clock_port = NULL;
atomic_int control_key_active = 0;
atomic_int bind_channel = 0;
/* Deferred removal index (1 second grace) */
static int pending_unregister_idx = -1;
/* writer thread function and sample rate holder */
static void *writer_thread(void *arg);
static int global_sample_rate = 0;
/* execute a single command (called from looper_process_commands) */
static void exec_command(command_t cmd, jack_client_t *client) {
int ch = cmd.channel;
if (ch < 0) ch = 0;
switch (cmd.type) {
case CMD_CYCLE: {
int state = atomic_load(&channels[ch].state);
switch (state) {
case STATE_IDLE:
atomic_store(&channels[ch].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[ch].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[ch].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[ch].state, STATE_LOOPING);
break;
}
atomic_store(&channels[ch].prev_state, -1);
break;
}
case CMD_STOP:
atomic_store(&channels[ch].state, STATE_IDLE);
atomic_store(&channels[ch].prev_state, -1);
break;
case CMD_ADD_CHANNEL:
case CMD_ADD_MIDI_CHANNEL: {
int idx;
for (idx = 0; idx < MAX_CHANNELS; idx++)
if (!channels[idx].active)
break;
if (idx < MAX_CHANNELS)
channel_add(client, idx);
break;
}
case CMD_REMOVE_CHANNEL: {
int remove_idx = -1;
for (int idx = 1; idx < MAX_CHANNELS; idx++)
if (channels[idx].active)
remove_idx = idx;
if (remove_idx != -1) {
channel_remove(client, remove_idx);
pending_unregister_idx = remove_idx;
}
break;
}
case CMD_BIND_CHANNEL:
atomic_store(&bind_channel, cmd.data);
break;
case CMD_UNBIND:
atomic_store(&bind_channel, 0);
break;
case CMD_LOAD:
atomic_store(&cmd_load, 1);
break;
case CMD_SAVE:
atomic_store(&cmd_save, 1);
break;
case CMD_ADD_SCENE:
case CMD_REMOVE_SCENE:
case CMD_NEXT_SCENE:
case CMD_PREV_SCENE:
break;
default:
break;
}
}
/* ----------------------------------------------------------------
* process callback
* ---------------------------------------------------------------- */
int process_callback(jack_nframes_t nframes, void *arg) {
(void)arg;
if (midi_control_port) {
void *midi_ctrl_buf = jack_port_get_buffer(midi_control_port, nframes);
if (midi_ctrl_buf) {
midi_handle_events(midi_ctrl_buf, nframes);
}
}
/* process each active channel */
for (int c = 0; c < MAX_CHANNELS; c++) {
if (!atomic_load(&channels[c].active))
continue;
/* Guard against NULL ports (e.g. if port registration failed) */
if (!channels[c].audio_in || !channels[c].audio_out) {
fprintf(stderr, "WARN: channel %d has NULL audio port(s), skipping\n", c);
continue;
}
const jack_default_audio_sample_t *in =
(const jack_default_audio_sample_t *)jack_port_get_buffer(
channels[c].audio_in, nframes);
jack_default_audio_sample_t *out =
(jack_default_audio_sample_t *)jack_port_get_buffer(
channels[c].audio_out, nframes);
if (!out)
continue;
int state = atomic_load(&channels[c].state);
if (state != atomic_load(&channels[c].prev_state)) {
switch (state) {
case STATE_RECORD:
atomic_store(&channels[c].record_pos, 0);
atomic_store(&channels[c].loop_count, 0);
break;
case STATE_LOOPING:
if (atomic_load(&channels[c].prev_state) == STATE_RECORD &&
atomic_load(&channels[c].record_pos) > 0)
atomic_store(&channels[c].loop_count,
atomic_load(&channels[c].record_pos));
atomic_store(&channels[c].playback_pos, 0);
break;
default:
break;
}
}
<<<<<<< HEAD
jack_nframes_t i;
switch (state) {
case STATE_RECORD:
if (in) {
float *f_out = (float *)out;
const float *f_in = (const float *)in;
for (i = 0; i < nframes; i++) {
int rp = atomic_fetch_add(&channels[c].record_pos, 1);
if (rp < LOOP_BUF_SIZE)
channels[c].loop_buffer[rp] = f_in[i];
f_out[i] = f_in[i];
}
} else {
=======
if (active_channels[c].type == CHANNEL_MIDI) {
/* MIDI channel handling */
switch (state) {
case STATE_RECORD: {
void *midi_in_buf =
jack_port_get_buffer(active_channels[c].midi_in, nframes);
if (midi_in_buf) {
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
jack_midi_event_t ev;
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
int rp = atomic_load(&sc->record_pos);
if (rp < MAX_MIDI_EVENTS) {
sc->loop.midi_events[rp].timestamp = ev.time;
sc->loop.midi_events[rp].status = ev.buffer[0];
sc->loop.midi_events[rp].note = (ev.size > 1) ? ev.buffer[1] : 0;
sc->loop.midi_events[rp].velocity =
(ev.size > 2) ? ev.buffer[2] : 0;
atomic_store(&sc->record_pos, rp + 1);
}
}
/* forward incoming MIDI to output during record */
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
}
}
}
break;
}
case STATE_LOOPING: {
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
int cnt = atomic_load(&sc->loop_count);
if (cnt > 0) {
for (int e = 0; e < cnt; e++) {
unsigned char msg[3];
msg[0] = sc->loop.midi_events[e].status;
msg[1] = sc->loop.midi_events[e].note;
msg[2] = sc->loop.midi_events[e].velocity;
jack_midi_event_write(midi_out_buf, 0, msg, 3);
}
}
}
break;
}
case STATE_PAUSED:
/* no output */
break;
default: /* IDLE */
{
void *midi_in_buf =
jack_port_get_buffer(active_channels[c].midi_in, nframes);
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_in_buf && midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
jack_midi_event_t ev;
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
}
}
} break;
}
if (state == STATE_LOOPING) {
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
}
} else {
/* audio channel handling */
jack_nframes_t i;
switch (state) {
case STATE_RECORD:
if (in) {
float *f_out = (float *)out;
const float *f_in = (const float *)in;
for (i = 0; i < nframes; i++) {
int rp = atomic_load(&sc->record_pos);
if (rp < LOOP_BUF_SIZE) {
sc->loop.audio_buffer[rp] = f_in[i];
atomic_store(&sc->record_pos, rp + 1);
}
f_out[i] = f_in[i];
}
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
case STATE_LOOPING: {
int loop_cnt = atomic_load(&sc->loop_count);
if (loop_cnt > 0) {
float *outf = (float *)out;
int pp = atomic_load(&sc->playback_pos);
for (i = 0; i < nframes; i++) {
outf[i] = sc->loop.audio_buffer[pp];
pp = (pp + 1) % loop_cnt;
}
atomic_store(&sc->playback_pos, pp);
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
}
case STATE_PAUSED:
>>>>>>> 3-integrate-carla
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
case STATE_LOOPING:
int lc = atomic_load(&channels[c].loop_count);
if (lc > 0) {
float *outf = (float *)out;
for (i = 0; i < nframes; i++) {
int pp = atomic_load(&channels[c].playback_pos);
outf[i] = channels[c].loop_buffer[pp];
atomic_store(&channels[c].playback_pos, (pp + 1) % lc);
}
} 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;
}
// push loop output into save ring if saving (atomic load)
RingBuf *r = (RingBuf *)atomic_load_explicit(&channels[c].save_ring,
memory_order_acquire);
if (r != NULL) {
if (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) {
const float *outf = (const float *)out;
ring_write(r, outf, nframes);
}
}
atomic_store(&channels[c].prev_state, state);
}
/* MIDI clock events affect channel 0 only */
if (midi_clock_port) {
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);
jack_midi_event_t cev;
for (jack_nframes_t j = 0; j < n_clock_events; j++) {
if (jack_midi_event_get(&cev, midi_clock_buf, j) != 0)
continue;
if (cev.size >= 1) {
unsigned char msg = cev.buffer[0];
switch (msg) {
case 0xFA: {
int s = atomic_load(&channels[0].state);
if (s == STATE_IDLE)
atomic_store(&channels[0].state, STATE_RECORD);
break;
}
case 0xFC:
atomic_store(&channels[0].state, STATE_IDLE);
break;
case 0xFB: {
int s = atomic_load(&channels[0].state);
if (s == STATE_PAUSED)
atomic_store(&channels[0].state, STATE_LOOPING);
break;
}
default:
break;
}
}
}
}
}
return 0;
}
/* ----------------------------------------------------------------
* shutdown callback
* ---------------------------------------------------------------- */
void jack_shutdown_cb(void *arg) {
(void)arg;
fprintf(stderr, "JACK shutdown\n");
exit(0);
}
/* ----------------------------------------------------------------
* looper initialisation
* ---------------------------------------------------------------- */
int looper_init(jack_client_t *client) {
/* store sample rate for writer thread */
global_sample_rate = jack_get_sample_rate(client);
/* create status FIFO (ignore if already exists) */
mkfifo(STATUS_FIFO, 0666);
/* open the status FIFO for reading+writing so writes work even without reader */
status_fd = open(STATUS_FIFO, O_RDWR);
if (status_fd < 0) {
perror("open status FIFO");
}
queue_init(&cmd_queue);
queue_init(&cmd_queue_main_midi);
queue_init(&cmd_queue_main_fifo);
/* start the FIFO reader thread */
pipe_start_reader();
/* channel 0 */
channels[0].active = 1;
atomic_store(&channels[0].state, STATE_IDLE);
atomic_store(&channels[0].prev_state, -1);
channels[0].loop_count = 0;
atomic_store(&channels[0].record_pos, 0);
atomic_store(&channels[0].playback_pos, 0);
atomic_store_explicit(&channels[0].save_ring, NULL, memory_order_release);
channels[0].audio_in = jack_port_register(
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[0].audio_out = jack_port_register(
client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!channels[0].audio_in || !channels[0].audio_out) {
fprintf(stderr, "Could not create audio ports for channel 0\n");
return -1;
}
channel_count = 1;
midi_control_port = jack_port_register(
client, "control", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
midi_clock_port = jack_port_register(client, "clock", JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput, 0);
if (!midi_control_port || !midi_clock_port) {
fprintf(stderr, "Could not create MIDI ports\n");
return -1;
}
return 0;
}
/* ----------------------------------------------------------------
* writer thread consumes the save ring and writes WAV file
* ---------------------------------------------------------------- */
static void *writer_thread(void *arg) {
struct channel_t *ch = (struct channel_t *)arg;
RingBuf *ring = (RingBuf *)ch->save_ring;
if (!ring)
return NULL;
static const char *path = "save.wav";
unsigned sr = (unsigned)global_sample_rate;
if (sr == 0)
sr = 48000;
int lc = atomic_load(&ch->loop_count);
float *outbuf = malloc((size_t)lc * sizeof(float));
if (!outbuf) {
ring_destroy(ring);
free(ring);
ch->save_ring = NULL;
return NULL;
}
size_t collected = 0;
size_t want = (size_t)lc;
while (collected < want) {
size_t got = ring_read(ring, outbuf + collected, want - collected);
collected += got;
if (got == 0) {
struct timespec req = {.tv_sec = 0, .tv_nsec = 10000000};
nanosleep(&req, NULL);
}
}
wav_write(path, outbuf, (unsigned)lc, sr);
free(outbuf);
ring_destroy(ring);
free(ring);
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
return NULL;
}
/* ----------------------------------------------------------------
* mainloop command processing
* ---------------------------------------------------------------- */
void looper_process_commands(jack_client_t *client) {
/* process commands from the three queues FIRST */
command_t cmd;
while (queue_pop(&cmd_queue, &cmd))
exec_command(cmd, client);
while (queue_pop(&cmd_queue_main_midi, &cmd))
exec_command(cmd, client);
while (queue_pop(&cmd_queue_main_fifo, &cmd))
exec_command(cmd, client);
/* Unregister any ports that were marked for deferred removal.
By now the realtime thread has had at least one full cycle
to see the `active = 0` store. */
if (pending_unregister_idx != -1) {
int idx = pending_unregister_idx;
if (channels[idx].audio_in)
jack_port_unregister(client, channels[idx].audio_in);
if (channels[idx].audio_out)
jack_port_unregister(client, channels[idx].audio_out);
pending_unregister_idx = -1;
}
/* ---------- add channel ---------- */
if (atomic_exchange(&cmd_add, 0)) {
int idx;
for (idx = 0; idx < MAX_CHANNELS; idx++)
if (!channels[idx].active)
break;
if (idx < MAX_CHANNELS) {
channel_add(client, idx);
}
}
/* ---------- remove channel ---------- */
if (atomic_exchange(&cmd_remove, 0)) {
int remove_idx = -1;
for (int idx = 1; idx < MAX_CHANNELS; idx++)
if (channels[idx].active)
remove_idx = idx;
if (remove_idx != -1) {
/* Mark inactive now; ports will be unregistered next round */
channel_remove(client, remove_idx);
pending_unregister_idx = remove_idx;
}
}
/* ---------- load command ---------- */
if (atomic_exchange(&cmd_load, 0)) {
float *buf = NULL;
unsigned frames = 0;
printf("LOAD: wav_read called\n");
if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) {
printf("LOAD: success, frames=%u\n", frames);
if (frames > LOOP_BUF_SIZE)
frames = LOOP_BUF_SIZE;
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
atomic_store(&channels[0].loop_count, (int)frames);
atomic_store(&channels[0].record_pos, 0);
atomic_store(&channels[0].playback_pos, 0);
atomic_store(&channels[0].state, STATE_LOOPING);
atomic_store(&channels[0].prev_state, -1);
free(buf);
} else {
fprintf(stderr, "Failed to load loop.wav\n");
printf("LOAD: FAILED\n");
}
}
/* ---------- save command (writer thread) ---------- */
if (atomic_exchange(&cmd_save, 0)) {
int lc = atomic_load(&channels[0].loop_count);
if (atomic_load(&channels[0].state) == STATE_LOOPING && lc > 0 &&
channels[0].save_ring == NULL) {
RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf));
if (ring) {
size_t sz = (size_t)lc * 2;
if (ring_init(ring, sz) == 0) {
atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring,
memory_order_release);
pthread_t th;
pthread_create(&th, NULL, writer_thread, &channels[0]);
pthread_detach(th);
} else {
free(ring);
}
}
}
}
/* write current state to status FIFO */
looper_write_status();
}

View File

Binary file not shown.

View File

@@ -3,8 +3,8 @@
#include <jack/jack.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
(void)argc;
@@ -43,7 +43,10 @@ int main(int argc, char *argv[]) {
while (1) {
looper_process_commands(client);
{ struct timespec ts = { .tv_sec = 0, .tv_nsec = 50000000 }; nanosleep(&ts, NULL); } /* check commands every 50 ms */
{
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
nanosleep(&ts, NULL);
} /* check commands every 50 ms */
}
jack_client_close(client);

View File

Binary file not shown.

View File

@@ -57,16 +57,20 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
int cur = atomic_load(&channels[bch].scenes[sc_idx].state);
switch (cur) {
case STATE_IDLE:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_RECORD);
atomic_store(&channels[bch].scenes[sc_idx].state,
STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
atomic_store(&channels[bch].scenes[sc_idx].state,
STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_PAUSED);
atomic_store(&channels[bch].scenes[sc_idx].state,
STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
atomic_store(&channels[bch].scenes[sc_idx].state,
STATE_LOOPING);
break;
}
}

View File

@@ -1,154 +0,0 @@
// cppcheck-suppress missingIncludeSystem
#include "midi.h"
#include "channel.h"
#include <jack/jack.h>
#include <jack/midiport.h>
#include <stdatomic.h>
extern atomic_int control_key_active;
extern atomic_int cmd_add;
extern atomic_int cmd_remove;
extern atomic_int cmd_load;
extern atomic_int cmd_save;
extern atomic_int bind_channel;
void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
(void)nframes;
jack_nframes_t nevents = jack_midi_get_event_count(port_buffer);
jack_midi_event_t ev;
for (jack_nframes_t i = 0; i < nevents; i++) {
if (jack_midi_event_get(&ev, port_buffer, i) != 0)
continue;
if (ev.size < 3)
continue;
unsigned char status = ev.buffer[0];
unsigned char note = ev.buffer[1];
unsigned char vel = ev.buffer[2];
/* noteon */
if ((status & 0xf0) == 0x90 && vel > 0) {
if (note == 64) {
atomic_store(&control_key_active, 1);
} else {
int ck = atomic_load(&control_key_active);
if (ck) {
atomic_store(&control_key_active, 0);
if (note < 16) {
atomic_store(&bind_channel, note);
} else {
switch (note) {
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
case 62: /* trigger looper channel via bind_channel */
{
int bch = atomic_load(&bind_channel);
if (bch >= 0 && bch < MAX_CHANNELS) {
int cur = atomic_load(&channels[bch].state);
switch (cur) {
case STATE_IDLE:
atomic_store(&channels[bch].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[bch].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[bch].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[bch].state, STATE_LOOPING);
break;
}
}
} break;
<<<<<<< HEAD
case 63: /* unbind reset bind to channel 0 */
atomic_store(&bind_channel, 0);
break;
case 70: /* load WAV into channel 0 */
atomic_store(&cmd_load, 1);
break;
case 71: /* save WAV of channel 0 */
atomic_store(&cmd_save, 1);
break;
=======
case 63: {
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} break;
case 65: {
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} break;
case 66: {
command_t cmd = {
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 67: {
command_t cmd = {
.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 68: {
command_t cmd = {
.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 69: {
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 70: {
command_t cmd = {
.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
>>>>>>> 3-integrate-carla
default:
break;
}
}
} else {
/* direct mapping */
switch (note) {
case 1: /* toggle channel 0 */
{
int cur0 = atomic_load(&channels[0].state);
switch (cur0) {
case STATE_IDLE:
atomic_store(&channels[0].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[0].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[0].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[0].state, STATE_LOOPING);
break;
}
} break;
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
default:
break;
}
}
}
} else if ((status & 0xf0) == 0x80 ||
((status & 0xf0) == 0x90 && vel == 0)) {
atomic_store(&control_key_active, 0);
}
}
}

View File

Binary file not shown.

View File

@@ -1,111 +0,0 @@
#include "pipe.h"
#include "command.h"
#include "queue.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define FIFO_PATH "/tmp/looper_cmd"
#define LINE_MAX 256
/* forwarddeclare the global queues (defined in looper.c) */
extern spsc_queue_t cmd_queue;
extern spsc_queue_t cmd_queue_main_fifo;
static void *pipe_thread_func(void *arg) {
(void)arg;
char line[LINE_MAX];
while (1) {
FILE *fifo = fopen(FIFO_PATH, "r");
if (!fifo) {
perror("fopen fifo");
return NULL;
}
while (fgets(line, sizeof(line), fifo)) {
/* strip newline */
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[len - 1] = '\0';
if (strcmp(line, "add") == 0) {
command_t cmd = {.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "add_midi") == 0) {
<<<<<<< HEAD
command_t cmd = {.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
=======
command_t cmd = {
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
>>>>>>> 3-integrate-carla
} else if (strcmp(line, "remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strncmp(line, "record ", 7) == 0) {
int ch = atoi(line + 7);
command_t cmd = {.type = CMD_CYCLE, .channel = ch, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "stop") == 0) {
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strncmp(line, "bind ", 5) == 0) {
int ch = atoi(line + 5);
command_t cmd = {.type = CMD_BIND_CHANNEL, .channel = -1, .data = ch};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "unbind") == 0) {
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_add") == 0) {
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_next") == 0) {
command_t cmd = {.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_prev") == 0) {
command_t cmd = {.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "load") == 0) {
command_t cmd = {.type = CMD_LOAD, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "save") == 0) {
command_t cmd = {.type = CMD_SAVE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
}
/* ignore unknown lines */
}
/* EOF all writers closed, reopen for next connection */
fclose(fifo);
{
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
nanosleep(&ts, NULL);
} /* small pause before retrying */
}
return NULL; /* unreachable */
}
int pipe_start_reader(void) {
/* create FIFO if it doesn't exist */
if (mkfifo(FIFO_PATH, 0666) != 0 && errno != EEXIST) {
perror("mkfifo");
return -1;
}
pthread_t tid;
if (pthread_create(&tid, NULL, pipe_thread_func, NULL) != 0) {
perror("pthread_create");
return -1;
}
pthread_detach(tid); /* we don't need to join */
return 0;
}

View File

Binary file not shown.

View File

View File

View File

@@ -8,19 +8,18 @@ static inline size_t load_tail(const RingBuf *r) {
return atomic_load_explicit(&r->tail, memory_order_relaxed);
}
static inline void store_head(RingBuf *r, size_t v) {
atomic_store_explicit(&r->head, v, memory_order_relaxed);
atomic_store_explicit(&r->head, v, memory_order_release); // release after data written
}
static inline void store_tail(RingBuf *r, size_t v) {
atomic_store_explicit(&r->tail, v, memory_order_relaxed);
atomic_store_explicit(&r->tail, v, memory_order_release); // release after data read
}
int ring_init(RingBuf *r, size_t capacity) {
r->buf = (float *)malloc(capacity * sizeof(float));
if (!r->buf)
return -1;
if (!r->buf) return -1;
r->capacity = capacity;
store_head(r, 0);
store_tail(r, 0);
atomic_init(&r->head, 0);
atomic_init(&r->tail, 0);
return 0;
}
@@ -30,47 +29,37 @@ void ring_destroy(RingBuf *r) {
r->capacity = 0;
}
static size_t ring_readable(const RingBuf *r) {
size_t h = load_head(r);
size_t t = load_tail(r);
if (h >= t)
return h - t;
else
return r->capacity - (t - h);
}
static size_t ring_writeable(const RingBuf *r) {
return r->capacity - 1 - ring_readable(r);
}
size_t ring_write(RingBuf *r, const float *data, size_t count) {
size_t avail = ring_writeable(r);
if (count > avail)
count = avail;
if (count == 0)
return 0;
size_t head = load_head(r);
size_t tail = load_tail(r); // producer reads consumer's tail (relaxed is fine)
size_t head = load_head(r); // own head
size_t cap = r->capacity;
size_t used = (head >= tail) ? (head - tail) : (cap - (tail - head));
size_t avail = cap - 1 - used;
if (count > avail) count = avail;
if (count == 0) return 0;
size_t pos = head;
for (size_t i = 0; i < count; ++i) {
r->buf[head] = data[i];
head = (head + 1) % cap;
r->buf[pos] = data[i];
pos = (pos + 1) % cap;
}
store_head(r, head);
store_head(r, pos); // release makes data visible to consumer
return count;
}
size_t ring_read(RingBuf *r, float *data, size_t count) {
size_t avail = ring_readable(r);
if (count > avail)
count = avail;
if (count == 0)
return 0;
size_t tail = load_tail(r);
size_t head = atomic_load_explicit(&r->head, memory_order_acquire); // acquire see producer's writes
size_t tail = load_tail(r); // own tail
size_t cap = r->capacity;
size_t used = (head >= tail) ? (head - tail) : (cap - (tail - head));
if (count > used) count = used;
if (count == 0) return 0;
size_t pos = tail;
for (size_t i = 0; i < count; ++i) {
data[i] = r->buf[tail];
tail = (tail + 1) % cap;
data[i] = r->buf[pos];
pos = (pos + 1) % cap;
}
store_tail(r, tail);
store_tail(r, pos); // release makes consumer's tail visible to producer
return count;
}

View File

Binary file not shown.

View File

@@ -1,14 +1,15 @@
#include "wav.h"
#include "channel.h"
#include <sndfile.h>
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int wav_read(const char *path, float **buffer, unsigned *frames) {
SF_INFO info;
info.format = 0;
SNDFILE *sf = sf_open(path, SFM_READ, &info);
if (!sf) return -1;
if (!sf)
return -1;
/* We need mono 16-bit PCM; refuse anything else */
if (info.channels != 1 || info.samplerate <= 0) {
@@ -16,9 +17,14 @@ int wav_read(const char *path, float **buffer, unsigned *frames) {
return -1;
}
unsigned total = (info.frames > (sf_count_t)LOOP_BUF_SIZE) ? LOOP_BUF_SIZE : (unsigned)info.frames;
float *buf = (float*)malloc(total * sizeof(float));
if (!buf) { sf_close(sf); return -1; }
unsigned total = (info.frames > (sf_count_t)LOOP_BUF_SIZE)
? LOOP_BUF_SIZE
: (unsigned)info.frames;
float *buf = (float *)malloc(total * sizeof(float));
if (!buf) {
sf_close(sf);
return -1;
}
sf_count_t nread = sf_readf_float(sf, buf, total);
sf_close(sf);
@@ -27,13 +33,15 @@ int wav_read(const char *path, float **buffer, unsigned *frames) {
return 0;
}
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) {
int wav_write(const char *path, const float *data, unsigned frames,
unsigned sample_rate) {
SF_INFO info;
info.samplerate = sample_rate;
info.channels = 1;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SNDFILE *sf = sf_open(path, SFM_WRITE, &info);
if (!sf) return -1;
if (!sf)
return -1;
sf_writef_float(sf, data, frames);
sf_close(sf);

View File

Binary file not shown.

View File

@@ -11,6 +11,7 @@
#include <jack/midiport.h>
#include <math.h>
#include <time.h>
#include <sndfile.h>
/* static variables for passthrough test */
static jack_port_t *passthrough_output_port = NULL;
@@ -333,20 +334,12 @@ static int test_looper_looping(void) {
return 1;
}
/* first noteon: 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) */
/* connect audio and activate immediately */
int sr = jack_get_sample_rate(client);
continuous_sine = 0; /* disable continuous tone */
beep_remaining = (int)(0.1f * sr); /* 0.1 second beep */
continuous_sine = 0;
beep_remaining = (int)(3.0f * sr); /* 3 sec beep covers entire recording */
bursts = 0;
prev_above = 0;
passthrough_output_port = audio_out;
passthrough_input_port = audio_in;
passthrough_phase = 0.0f;
@@ -355,7 +348,6 @@ static int test_looper_looping(void) {
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);
@@ -363,19 +355,23 @@ static int test_looper_looping(void) {
return 1;
}
safe_usleep(150000); /* let beep start */
/* ensure beep is fully captured */
safe_usleep(800000); /* 0.8s after start of beep */
/* first noteon: 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(3000000); /* 3s to capture beep */
/* second noteon: RECORD -> LOOPING */
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);
/* wait for several loop repetitions */
safe_usleep(8000000); /* 8 seconds listen */
jack_deactivate(client);
jack_client_close(client);
@@ -504,7 +500,7 @@ static int test_control_key_modifier(void) {
/* 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 */
beep_remaining = (int)(0.5f * sr); /* 0.5 sec beep (was 0.1) */
bursts = 0;
prev_above = 0;
passthrough_output_port = audio_out;
@@ -517,26 +513,30 @@ static int test_control_key_modifier(void) {
passthrough_done = 0;
jack_set_process_callback(client, passthrough_process, NULL);
if (jack_activate(client)) {
fprintf(stderr, " FAIL: cannot activate test client\n");
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 */
safe_usleep(500000); /* allow beep to start */
/* second note 64 + note 62 to move RECORD → LOOPING */
if (send_jack_note_on("looper:control", 64, 127) != 0) {
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: control key resend\n");
return 1;
}
safe_usleep(200000);
safe_usleep(500000);
if (send_jack_note_on("looper:control", 62, 127) != 0) {
jack_deactivate(client);
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);
safe_usleep(5000000); /* listen for 5 seconds */
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM);
@@ -619,7 +619,7 @@ static int test_bind_channel(void) {
/* Wait and detect bursts as before */
int sr = jack_get_sample_rate(client);
continuous_sine = 0;
beep_remaining = (int)(0.1f * sr);
beep_remaining = (int)(0.5f * sr); /* 0.5 sec beep (was 0.1) */
bursts = 0;
prev_above = 0;
passthrough_output_port = audio_out;
@@ -632,26 +632,30 @@ static int test_bind_channel(void) {
passthrough_done = 0;
jack_set_process_callback(client, passthrough_process, NULL);
if (jack_activate(client)) {
fprintf(stderr, " FAIL: cannot activate test client\n");
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 */
safe_usleep(500000); /* allow beep to start */
/* second note 64 + note 62 to move RECORD → LOOPING */
if (send_jack_note_on("looper:control", 64, 127) != 0) {
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: control key for loop\n");
return 1;
}
safe_usleep(200000);
safe_usleep(500000);
if (send_jack_note_on("looper:control", 62, 127) != 0) {
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: toggle for loop\n");
return 1;
}
safe_usleep(2000000);
safe_usleep(5000000); /* listen for 5 seconds */
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM);
@@ -749,7 +753,7 @@ static int test_bind_unbind(void) {
/* Wait for beep and loop */
int sr = jack_get_sample_rate(client);
continuous_sine = 0;
beep_remaining = (int)(0.1f * sr);
beep_remaining = (int)(0.5f * sr); /* 0.5 sec beep (was 0.1) */
bursts = 0;
prev_above = 0;
passthrough_output_port = audio_out;
@@ -766,7 +770,7 @@ static int test_bind_unbind(void) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(200000); /* allow beep */
safe_usleep(1000000); /* allow recording to start before we set beep */
/* second control+62 -> loop */
if (send_jack_note_on("looper:control", 64, 127) != 0) {
jack_client_close(client);
@@ -774,7 +778,7 @@ static int test_bind_unbind(void) {
fprintf(stderr, " FAIL: control key for loop\n");
return 1;
}
safe_usleep(200000);
safe_usleep(2000000); /* give time to record beep */
if (send_jack_note_on("looper:control", 62, 127) != 0) {
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
@@ -882,38 +886,17 @@ static int test_remove_channel(void) {
* Helper: generate a simple 440 Hz WAV file for load tests
* ------------------------------------------------------------ */
static int generate_test_wav(const char *path, unsigned sample_rate, unsigned duration_frames) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) return -1;
unsigned data_bytes = duration_frames * 2;
unsigned file_size = 44 + data_bytes;
unsigned char header[44];
memset(header, 0, 44);
memcpy(header, "RIFF", 4);
unsigned chunk_size = file_size - 8;
header[4] = chunk_size & 0xff; header[5] = (chunk_size>>8)&0xff;
header[6] = (chunk_size>>16)&0xff; header[7] = (chunk_size>>24)&0xff;
memcpy(header+8, "WAVE", 4);
memcpy(header+12, "fmt ", 4);
header[16]=16; header[17]=0; header[18]=0; header[19]=0;
header[20]=1; header[21]=0; /* PCM */
header[22]=1; header[23]=0; /* mono */
header[24]= sample_rate & 0xff; header[25]=(sample_rate>>8)&0xff;
header[26]=(sample_rate>>16)&0xff; header[27]=(sample_rate>>24)&0xff;
unsigned br = sample_rate * 2;
header[28]= br & 0xff; header[29]=(br>>8)&0xff;
header[30]=(br>>16)&0xff; header[31]=(br>>24)&0xff;
header[32]=2; header[33]=0;
header[34]=16; header[35]=0;
memcpy(header+36, "data", 4);
header[40]= data_bytes & 0xff; header[41]=(data_bytes>>8)&0xff;
header[42]=(data_bytes>>16)&0xff; header[43]=(data_bytes>>24)&0xff;
if (write(fd, header, 44) != 44) { close(fd); return -1; }
SF_INFO info;
info.samplerate = sample_rate;
info.channels = 1;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SNDFILE *sf = sf_open(path, SFM_WRITE, &info);
if (!sf) return -1;
for (unsigned i = 0; i < duration_frames; i++) {
float sample = sinf(2.0f * (float)M_PI * 440.0f * i / sample_rate);
int16_t s = (int16_t)(sample * 32767);
if (write(fd, &s, 2) != 2) { close(fd); return -1; }
sf_writef_float(sf, &sample, 1);
}
close(fd);
sf_close(sf);
return 0;
}
@@ -1062,15 +1045,17 @@ static int test_wav_save(void) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(800000);
/* stop recording (cycle again) */
safe_usleep(3000000); /* record for 3s (ensure enough beep) */
/* Send second record command to transition RECORD → LOOPING */
if (send_fifo_command("record 0") != 0) {
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(500000);
safe_usleep(1000000); /* give time for state change and loop_count to be set */
/* save */
if (send_fifo_command("save") != 0) {
jack_deactivate(client);
@@ -1078,16 +1063,28 @@ static int test_wav_save(void) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(2000000);
/* check save.wav */
safe_usleep(8000000); /* wait for synchronous save to complete (8s) */
/* check save.wav with retries */
int saved = 0;
for (int retry = 0; retry < 5; retry++) {
int fd = open("save.wav", O_RDONLY);
if (fd < 0) {
if (fd >= 0) {
saved = 1;
close(fd);
break;
}
safe_usleep(1000000);
}
if (!saved) {
jack_deactivate(client);
jack_client_close(client);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: save.wav not created\n");
return 1;
}
/* verify header */
int fd = open("save.wav", O_RDONLY);
unsigned char hdr[44];
if (read(fd, hdr, 44) != 44) {
close(fd); unlink("save.wav");