fix: use atomic loads for clip state and read position in JACK callback
Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
17
engine.c
17
engine.c
@@ -6,6 +6,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
|
||||||
static int process_callback(jack_nframes_t nframes, void *arg) {
|
static int process_callback(jack_nframes_t nframes, void *arg) {
|
||||||
Engine *engine = (Engine *)arg;
|
Engine *engine = (Engine *)arg;
|
||||||
@@ -67,7 +68,8 @@ static int process_callback(jack_nframes_t nframes, void *arg) {
|
|||||||
// The actual recording should be done via an action.
|
// The actual recording should be done via an action.
|
||||||
// For now, we just dispatch the trigger and let the reducer handle it.
|
// For now, we just dispatch the trigger and let the reducer handle it.
|
||||||
|
|
||||||
uint8_t out_velocity = clip_state_to_velocity(state->clips[note % MAX_CLIPS].state);
|
ClipState note_state = (ClipState)atomic_load(&state->clips[note % MAX_CLIPS].state);
|
||||||
|
uint8_t out_velocity = clip_state_to_velocity(note_state);
|
||||||
uint8_t out_msg[3] = {0x90 | channel, note, out_velocity};
|
uint8_t out_msg[3] = {0x90 | channel, note, out_velocity};
|
||||||
jack_midi_event_write(midi_out_buf, midi_event.time, out_msg, 3);
|
jack_midi_event_write(midi_out_buf, midi_event.time, out_msg, 3);
|
||||||
} else {
|
} else {
|
||||||
@@ -101,7 +103,8 @@ static int process_callback(jack_nframes_t nframes, void *arg) {
|
|||||||
int clip_idx = g * GRID_ROWS * GRID_COLS + s * GRID_COLS + ch;
|
int clip_idx = g * GRID_ROWS * GRID_COLS + s * GRID_COLS + ch;
|
||||||
MidiClip *mclip = &state->midi_clips[clip_idx];
|
MidiClip *mclip = &state->midi_clips[clip_idx];
|
||||||
|
|
||||||
if (mclip->state == CLIP_LOOPING && mclip->event_count > 0 && mclip->events != NULL) {
|
ClipState mclip_state = (ClipState)atomic_load(&mclip->state);
|
||||||
|
if (mclip_state == CLIP_LOOPING && mclip->event_count > 0 && mclip->events != NULL) {
|
||||||
if (mclip->read_index < mclip->event_count) {
|
if (mclip->read_index < mclip->event_count) {
|
||||||
int idx = mclip->read_index;
|
int idx = mclip->read_index;
|
||||||
uint8_t msg[3] = {
|
uint8_t msg[3] = {
|
||||||
@@ -143,16 +146,18 @@ static int process_callback(jack_nframes_t nframes, void *arg) {
|
|||||||
int clip_idx = g * GRID_ROWS * GRID_COLS + s * GRID_COLS + ch;
|
int clip_idx = g * GRID_ROWS * GRID_COLS + s * GRID_COLS + ch;
|
||||||
Clip *clip = &state->clips[clip_idx];
|
Clip *clip = &state->clips[clip_idx];
|
||||||
|
|
||||||
if (clip->state == CLIP_RECORDING) {
|
ClipState clip_state = (ClipState)atomic_load(&clip->state);
|
||||||
|
if (clip_state == CLIP_RECORDING) {
|
||||||
// Write to lock-free ring buffer instead of clip buffer directly
|
// Write to lock-free ring buffer instead of clip buffer directly
|
||||||
size_t wp = atomic_load(&state->record_write_pos[ch]);
|
size_t wp = atomic_load(&state->record_write_pos[ch]);
|
||||||
state->record_buffer[ch][wp % MAX_BUFFER_SIZE] = audio_in[ch][i];
|
state->record_buffer[ch][wp % MAX_BUFFER_SIZE] = audio_in[ch][i];
|
||||||
atomic_store(&state->record_write_pos[ch], wp + 1);
|
atomic_store(&state->record_write_pos[ch], wp + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clip->state == CLIP_LOOPING && clip->buffer_size > 0 && clip->buffer != NULL) {
|
if (clip_state == CLIP_LOOPING && clip->buffer_size > 0 && clip->buffer != NULL) {
|
||||||
rack_in[i] += clip->buffer[clip->read_position];
|
size_t rp = atomic_load(&clip->read_position);
|
||||||
clip->read_position = (clip->read_position + 1) % clip->buffer_size;
|
rack_in[i] += clip->buffer[rp];
|
||||||
|
atomic_store(&clip->read_position, (rp + 1) % clip->buffer_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user