feat: add script module for note-to-command mapping with FIFO support

This commit is contained in:
Loic Coenen
2026-05-18 21:12:29 +00:00
parent 16a800209f
commit 61e97dc529
9 changed files with 660 additions and 17 deletions

View File

@@ -565,7 +565,8 @@ void looper_process_commands(jack_client_t *client) {
if (data) {
memcpy(data, sc->loop.audio_buffer, (size_t)lc * sizeof(float));
unsigned sr = (unsigned)global_sample_rate;
if (sr == 0) sr = 48000;
if (sr == 0)
sr = 48000;
wav_write("save.wav", data, (unsigned)lc, sr);
free(data);
}

View File

@@ -8,15 +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_release); // release after data written
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_release); // release after data read
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;
atomic_init(&r->head, 0);
atomic_init(&r->tail, 0);
@@ -30,13 +33,16 @@ void ring_destroy(RingBuf *r) {
}
size_t ring_write(RingBuf *r, const float *data, size_t count) {
size_t tail = load_tail(r); // producer reads consumer's tail (relaxed is fine)
size_t head = load_head(r); // own head
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;
if (count > avail)
count = avail;
if (count == 0)
return 0;
size_t pos = head;
for (size_t i = 0; i < count; ++i) {
@@ -48,12 +54,15 @@ 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 head = atomic_load_explicit(&r->head, memory_order_acquire); // acquire see producer's writes
size_t tail = load_tail(r); // own tail
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;
if (count > used)
count = used;
if (count == 0)
return 0;
size_t pos = tail;
for (size_t i = 0; i < count; ++i) {