From fa9dbf21859d16861801a4fcc0dc16ffdfc0c73e Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Tue, 12 May 2026 19:58:19 +0000 Subject: [PATCH] style: fix code formatting and include order in looper and ringbuffer --- src/looper.c | 84 ++++++++++++++++++++++++---------------------- src/ringbuffer.c | 87 ++++++++++++++++++++++++++---------------------- 2 files changed, 92 insertions(+), 79 deletions(-) diff --git a/src/looper.c b/src/looper.c index c80ca27..a817d74 100644 --- a/src/looper.c +++ b/src/looper.c @@ -6,11 +6,11 @@ #include #include #include +#include #include #include #include #include -#include #include /* 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); 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)); + 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: @@ -129,11 +131,12 @@ int process_callback(jack_nframes_t nframes, void *arg) { } // 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 (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) { - float *outf = (float *)out; - ring_write(r, outf, nframes); + float *outf = (float *)out; + ring_write(r, outf, nframes); } } @@ -229,39 +232,41 @@ int looper_init(jack_client_t *client) { * 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; + 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); + 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); - atomic_store_explicit(&ch->save_ring, NULL, memory_order_release); + 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; } /* ---------------------------------------------------------------- @@ -309,7 +314,8 @@ void looper_process_commands(jack_client_t *client) { 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; + 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); @@ -326,14 +332,14 @@ void looper_process_commands(jack_client_t *client) { /* ---------- 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 && + if (atomic_load(&channels[0].state) == STATE_LOOPING && lc > 0 && channels[0].save_ring == NULL) { - RingBuf *ring = (RingBuf*)malloc(sizeof(RingBuf)); + 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); + 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); diff --git a/src/ringbuffer.c b/src/ringbuffer.c index 6543388..0f0682e 100644 --- a/src/ringbuffer.c +++ b/src/ringbuffer.c @@ -2,68 +2,75 @@ #include static inline size_t load_head(const RingBuf *r) { - return atomic_load_explicit(&r->head, memory_order_relaxed); + return atomic_load_explicit(&r->head, memory_order_relaxed); } static inline size_t load_tail(const RingBuf *r) { - return atomic_load_explicit(&r->tail, memory_order_relaxed); + 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_relaxed); } 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_relaxed); } int ring_init(RingBuf *r, size_t capacity) { - r->buf = (float*)malloc(capacity * sizeof(float)); - if (!r->buf) return -1; - r->capacity = capacity; - store_head(r, 0); - store_tail(r, 0); - return 0; + r->buf = (float *)malloc(capacity * sizeof(float)); + if (!r->buf) + return -1; + r->capacity = capacity; + store_head(r, 0); + store_tail(r, 0); + return 0; } void ring_destroy(RingBuf *r) { - free(r->buf); - r->buf = NULL; - r->capacity = 0; + free(r->buf); + r->buf = NULL; + r->capacity = 0; } 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); + size_t h = load_head(r); + size_t t = load_tail(r); + if (h >= t) + return h - t; + else + return r->capacity - (t - h); } size_t ring_writeable(const RingBuf *r) { - return r->capacity - 1 - ring_readable(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 cap = r->capacity; - for (size_t i = 0; i < count; ++i) { - r->buf[head] = data[i]; - head = (head + 1) % cap; - } - store_head(r, head); - return 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 cap = r->capacity; + for (size_t i = 0; i < count; ++i) { + r->buf[head] = data[i]; + head = (head + 1) % cap; + } + store_head(r, head); + 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 cap = r->capacity; - for (size_t i = 0; i < count; ++i) { - data[i] = r->buf[tail]; - tail = (tail + 1) % cap; - } - store_tail(r, tail); - return 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 cap = r->capacity; + for (size_t i = 0; i < count; ++i) { + data[i] = r->buf[tail]; + tail = (tail + 1) % cap; + } + store_tail(r, tail); + return count; }