style: fix code formatting and include order in looper and ringbuffer
This commit is contained in:
committed by
Loic Coenen (aider)
parent
51493d5cab
commit
fa9dbf2185
84
src/looper.c
84
src/looper.c
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user