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/midiport.h>
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
/* 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);

View File

@@ -2,68 +2,75 @@
#include <stdlib.h>
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;
}