style: fix code formatting and include order in looper and ringbuffer

This commit is contained in:
Loic Coenen
2026-05-12 19:58:19 +00:00
committed by Loic Coenen (aider)
parent 51493d5cab
commit fa9dbf2185
2 changed files with 92 additions and 79 deletions

View File

@@ -6,11 +6,11 @@
#include <jack/jack.h> #include <jack/jack.h>
#include <jack/midiport.h> #include <jack/midiport.h>
#include <math.h> #include <math.h>
#include <pthread.h>
#include <stdatomic.h> #include <stdatomic.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <time.h> #include <time.h>
/* Global state (shared across files) */ /* Global state (shared across files) */
@@ -75,8 +75,10 @@ int process_callback(jack_nframes_t nframes, void *arg) {
atomic_store(&channels[c].loop_count, 0); atomic_store(&channels[c].loop_count, 0);
break; break;
case STATE_LOOPING: case STATE_LOOPING:
if (atomic_load(&channels[c].prev_state) == STATE_RECORD && atomic_load(&channels[c].record_pos) > 0) if (atomic_load(&channels[c].prev_state) == STATE_RECORD &&
atomic_store(&channels[c].loop_count, atomic_load(&channels[c].record_pos)); 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); atomic_store(&channels[c].playback_pos, 0);
break; break;
default: default:
@@ -129,7 +131,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
} }
// push loop output into save ring if saving (atomic load) // push loop output into save ring if saving (atomic load)
RingBuf *r = (RingBuf *)atomic_load_explicit(&channels[c].save_ring, memory_order_acquire); RingBuf *r = (RingBuf *)atomic_load_explicit(&channels[c].save_ring,
memory_order_acquire);
if (r != NULL) { if (r != NULL) {
if (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) { if (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) {
float *outf = (float *)out; float *outf = (float *)out;
@@ -231,11 +234,13 @@ int looper_init(jack_client_t *client) {
static void *writer_thread(void *arg) { static void *writer_thread(void *arg) {
struct channel_t *ch = (struct channel_t *)arg; struct channel_t *ch = (struct channel_t *)arg;
RingBuf *ring = (RingBuf *)ch->save_ring; RingBuf *ring = (RingBuf *)ch->save_ring;
if (!ring) return NULL; if (!ring)
return NULL;
static const char *path = "save.wav"; static const char *path = "save.wav";
unsigned sr = (unsigned)global_sample_rate; unsigned sr = (unsigned)global_sample_rate;
if (sr == 0) sr = 48000; if (sr == 0)
sr = 48000;
int lc = atomic_load(&ch->loop_count); int lc = atomic_load(&ch->loop_count);
float *outbuf = malloc((size_t)lc * sizeof(float)); float *outbuf = malloc((size_t)lc * sizeof(float));
@@ -309,7 +314,8 @@ void looper_process_commands(jack_client_t *client) {
printf("LOAD: wav_read called\n"); printf("LOAD: wav_read called\n");
if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) { if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) {
printf("LOAD: success, frames=%u\n", frames); printf("LOAD: success, frames=%u\n", frames);
if (frames > LOOP_BUF_SIZE) frames = LOOP_BUF_SIZE; if (frames > LOOP_BUF_SIZE)
frames = LOOP_BUF_SIZE;
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float)); memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
atomic_store(&channels[0].loop_count, (int)frames); atomic_store(&channels[0].loop_count, (int)frames);
atomic_store(&channels[0].record_pos, 0); atomic_store(&channels[0].record_pos, 0);
@@ -326,14 +332,14 @@ void looper_process_commands(jack_client_t *client) {
/* ---------- save command (writer thread) ---------- */ /* ---------- save command (writer thread) ---------- */
if (atomic_exchange(&cmd_save, 0)) { if (atomic_exchange(&cmd_save, 0)) {
int lc = atomic_load(&channels[0].loop_count); int lc = atomic_load(&channels[0].loop_count);
if (atomic_load(&channels[0].state) == STATE_LOOPING && if (atomic_load(&channels[0].state) == STATE_LOOPING && lc > 0 &&
lc > 0 &&
channels[0].save_ring == NULL) { channels[0].save_ring == NULL) {
RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf)); RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf));
if (ring) { if (ring) {
size_t sz = (size_t)lc * 2; size_t sz = (size_t)lc * 2;
if (ring_init(ring, sz) == 0) { if (ring_init(ring, sz) == 0) {
atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring, memory_order_release); atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring,
memory_order_release);
pthread_t th; pthread_t th;
pthread_create(&th, NULL, writer_thread, &channels[0]); pthread_create(&th, NULL, writer_thread, &channels[0]);
pthread_detach(th); pthread_detach(th);

View File

@@ -16,7 +16,8 @@ static inline void store_tail(RingBuf *r, size_t v) {
int ring_init(RingBuf *r, size_t capacity) { int ring_init(RingBuf *r, size_t capacity) {
r->buf = (float *)malloc(capacity * sizeof(float)); r->buf = (float *)malloc(capacity * sizeof(float));
if (!r->buf) return -1; if (!r->buf)
return -1;
r->capacity = capacity; r->capacity = capacity;
store_head(r, 0); store_head(r, 0);
store_tail(r, 0); store_tail(r, 0);
@@ -32,8 +33,10 @@ void ring_destroy(RingBuf *r) {
size_t ring_readable(const RingBuf *r) { size_t ring_readable(const RingBuf *r) {
size_t h = load_head(r); size_t h = load_head(r);
size_t t = load_tail(r); size_t t = load_tail(r);
if (h >= t) return h - t; if (h >= t)
else return r->capacity - (t - h); return h - t;
else
return r->capacity - (t - h);
} }
size_t ring_writeable(const RingBuf *r) { size_t ring_writeable(const RingBuf *r) {
@@ -42,8 +45,10 @@ size_t ring_writeable(const RingBuf *r) {
size_t ring_write(RingBuf *r, const float *data, size_t count) { size_t ring_write(RingBuf *r, const float *data, size_t count) {
size_t avail = ring_writeable(r); size_t avail = ring_writeable(r);
if (count > avail) count = avail; if (count > avail)
if (count == 0) return 0; count = avail;
if (count == 0)
return 0;
size_t head = load_head(r); size_t head = load_head(r);
size_t cap = r->capacity; size_t cap = r->capacity;
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {
@@ -56,8 +61,10 @@ size_t ring_write(RingBuf *r, const float *data, size_t count) {
size_t ring_read(RingBuf *r, float *data, size_t count) { size_t ring_read(RingBuf *r, float *data, size_t count) {
size_t avail = ring_readable(r); size_t avail = ring_readable(r);
if (count > avail) count = avail; if (count > avail)
if (count == 0) return 0; count = avail;
if (count == 0)
return 0;
size_t tail = load_tail(r); size_t tail = load_tail(r);
size_t cap = r->capacity; size_t cap = r->capacity;
for (size_t i = 0; i < count; ++i) { for (size_t i = 0; i < count; ++i) {