30 Commits

Author SHA1 Message Date
Loic Coenen
10e47e6c0c Merge branch '3-integrate-carla' 2026-05-17 19:39:54 +00:00
Loic Coenen
6c19429fba Merge branch '8-add-tui' - tests not passing 2026-05-17 19:02:03 +00:00
Loic Coenen
3646f6c47e Merge branch '6-recording-wav-file' 2026-05-17 16:59:56 +00:00
Loic Coenen
bb648d471b fix: resolve cppcheck warnings for const pointer and static functions
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:58:20 +00:00
Loic Coenen
fa9dbf2185 style: fix code formatting and include order in looper and ringbuffer 2026-05-12 19:58:19 +00:00
Loic Coenen
51493d5cab docs: add WAV load/save documentation and update evaluation table
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:35:21 +00:00
Loic Coenen
ce2dd7be76 fix: make channel state variables atomic to eliminate data races
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:32:10 +00:00
Loic Coenen
87d5e658c5 fix: restore all integration tests in main()
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:18:20 +00:00
Loic Coenen
525516fe03 refactor: replace manual WAV I/O with libsndfile
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:15:12 +00:00
Loic Coenen
3e52142f62 feat: replace manual WAV parsing with libsndfile
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:14:35 +00:00
Loic Coenen
a92b5c51e1 fix: skip remaining fmt chunk bytes correctly in wav_read
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:09:58 +00:00
Loic Coenen
bb3dfa8b2a fix: correct RIFF chunk size in test WAV header
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:07:09 +00:00
Loic Coenen
3721c0c9e1 refactor: disable all tests except failing WAV load/save
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:04:36 +00:00
Loic Coenen
c041645019 fix: increase sleep duration in WAV load test to ensure control key processing
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:03:22 +00:00
Loic Coenen
6344eaed47 fix: add debug output and increase delay in WAV load test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 19:02:59 +00:00
Loic Coenen
f96d7d290d fix: ensure fresh MIDI connection before each integration test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:49:12 +00:00
Loic Coenen
2d254c0503 fix: ensure fresh MIDI connection before each integration test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:39:48 +00:00
Loic Coenen
4339fda529 fix: keep persistent MIDI client across notes in integration tests
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:37:15 +00:00
Loic Coenen
04b59999c8 fix: make loop_count atomic and increase remove channel delay
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:28:54 +00:00
Loic Coenen
df1f4fa6bd fix: only set loop_count from record_pos when transitioning from record state
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:22:55 +00:00
Loic Coenen
7e5362259b refactor: extract JACK MIDI client reconnection logic
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:19:35 +00:00
Loic Coenen
b10d218749 fix: reconnect MIDI client before each test to avoid stale connections
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:19:06 +00:00
Loic Coenen
cc50577444 fix: cast atomic pointer loads/stores and remove duplicate free in writer_thread
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-12 18:01:57 +00:00
Loic Coenen
346c15d1c3 fix: use persistent MIDI client and fix save_ring race condition
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 22:14:33 +00:00
Loic Coenen
7deea9266b fix: reorder passthrough setup before load command in WAV load test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 21:49:35 +00:00
Loic Coenen
7d842163a2 fix: increase listen duration and add RMS logging in WAV load test
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 21:39:49 +00:00
Loic Coenen
54fa307360 fix: increase sleep durations in WAV load test to prevent false failure
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 21:31:29 +00:00
Loic Coenen
5430795510 feat: push loop output into save ring during playback
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 21:16:02 +00:00
Loic Coenen
5a2414b4c3 feat: add WAV load/save and ring buffer implementation
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 21:15:12 +00:00
Loic Coenen
6b490ed739 feat: add WAV file loading, saving, and dedicated I/O threads
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-11 20:58:00 +00:00
31 changed files with 2032 additions and 1320 deletions

View File

@@ -0,0 +1,45 @@
# Sampling and Recording (WAV Load/Save)
The looper supports loading a WAV file into channel 0 and saving the current loop of channel 0 as a WAV file. Both operations use the **libsndfile** library, ensuring correct handling of RIFF headers, chunk sizes, and sample format conversion.
## Load Command
- **MIDI note 70** with the control key (note 64) triggers loading.
- The file `loop.wav` (located in the working directory) is read by `wav_read()` in `src/wav.c`.
- The function calls `sf_open(path, SFM_READ, &info)`.
- It accepts only mono PCM WAV files. If the file is not mono or has an invalid sample rate, it returns `-1`.
- The number of frames read is capped at `LOOP_BUF_SIZE` (5 seconds at 48 kHz).
- The data is stored in `channels[0].loop_buffer` and `channels[0].loop_count` is set atomically.
- The state of channel 0 is set to `STATE_LOOPING` and `prev_state` is set to `-1` to trigger the loop start in the next audio cycle.
## Save Command
- **MIDI note 71** with the control key (note 64) triggers saving.
- The looper must currently be in `STATE_LOOPING` and have a nonzero `loop_count`.
- A ring buffer (`RingBuf`) is allocated with capacity `2 × loop_count` samples.
- The pointer to the ring buffer is published via `atomic_store_explicit` on `channels[0].save_ring`.
- In each audio callback cycle, if the channel is looping and a save ring exists, the audio output data is written into the ring buffer.
- A dedicated **writer thread** (`writer_thread`) is launched (detached) to consume the ring buffer.
- The writer thread reads `loop_count` samples from the ring buffer, sleeping 10ms between empty reads.
- Once all samples are collected, it writes them to `save.wav` using `sf_writef_float()`.
- After writing, the ring buffer is destroyed and freed, and the save ring pointer is set to `NULL`.
## Dependencies
- **libsndfile** must be installed (development headers). Add `-lsndfile` to your linker flags (already present in the provided `makefile`).
## Implementation Files
- `src/wav.c` contains `wav_read()` and `wav_write()` based on libsndfile.
- `src/looper.c` contains the load/save command handling in `looper_process_commands()` and the writer thread function.
- `src/channel.h` defines `save_ring` as `_Atomic RingBuf *`.
## Testing
- The integration test `test_wav_load` creates a short 440Hz WAV file, loads it via MIDI, and checks for ≥3 bursts of audio output.
- The integration test `test_wav_save` records a beep, loops it, issues the save command, and verifies the resulting WAV file has nonzero data size.
## Notes
- The save operation is asynchronous: the writer thread runs in the background while the audio callback continues to fill the ring buffer. The test waits 2s for the file to be written before checking.
- The load operation is synchronous: the callback sleeps 1s after the MIDI command to give the main loop time to process it.

BIN
engine/integration_test Executable file
View File

Binary file not shown.

BIN
engine/looper Executable file
View File

Binary file not shown.

View File

@@ -1,8 +1,8 @@
CC ?= gcc
CFLAGS ?= -Wall -Wextra -g -Isrc
LDFLAGS ?= -ljack -lm
LDFLAGS ?= -ljack -lm -lsndfile -lpthread
SRC = src/main.c src/looper.c src/channel.c src/midi.c src/queue.c src/pipe.c
SRC = src/main.c src/looper.c src/channel.c src/midi.c src/queue.c src/pipe.c src/ringbuffer.c src/wav.c
OBJ = $(SRC:.c=.o)
looper: $(OBJ)
@@ -12,10 +12,10 @@ src/%.o: src/%.c
$(CC) $(CFLAGS) -c -o $@ $<
integration: looper tests/integration.c
$(CC) $(CFLAGS) -o integration_test tests/integration.c -ljack -lm
$(CC) $(CFLAGS) -o integration_test tests/integration.c -ljack -lm -lsndfile -lpthread
test_status_fifo: looper tests/test_status_fifo.c
$(CC) $(CFLAGS) -o test_status_fifo tests/test_status_fifo.c -ljack -lm
$(CC) $(CFLAGS) -o test_status_fifo tests/test_status_fifo.c -ljack -lm -lsndfile -lpthread
test: integration test_status_fifo
./test_status_fifo

View File

@@ -6,131 +6,123 @@
#include <string.h>
/* Helper: zero a scene and set its state to IDLE */
static void init_scene(scene_t *sc) {
void init_scene(scene_t *sc) {
memset(sc, 0, sizeof(scene_t));
atomic_store(&sc->state, STATE_IDLE);
atomic_store(&sc->prev_state, -1);
}
void channel_add(jack_client_t *client, int idx) {
struct channel_t *cur = get_channels_array();
char in_name[64], out_name[64];
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
cur[idx].audio_in = jack_port_register(
/* Always register audio ports (needed for pass-through even for MIDI channels?) */
channels[idx].audio_in = jack_port_register(
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
cur[idx].audio_out = jack_port_register(
channels[idx].audio_out = jack_port_register(
client, out_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!cur[idx].audio_in || !cur[idx].audio_out) {
if (!channels[idx].audio_in || !channels[idx].audio_out) {
fprintf(stderr, "Failed to register ports for channel %d\n",
next_channel_id);
atomic_store(&cur[idx].active, 0);
/* Do NOT mark channel active process loop will skip it */
atomic_store(&channels[idx].active, 0);
return;
}
atomic_store(&cur[idx].active, 1);
cur[idx].type = CHANNEL_AUDIO;
atomic_store(&cur[idx].scene_count, 1);
atomic_store(&cur[idx].current_scene, 0);
init_scene(&cur[idx].scenes[0]);
next_channel_id++;
atomic_fetch_add(&channel_count, 1);
}
void channel_add_midi(jack_client_t *client, int idx) {
struct channel_t *cur = get_channels_array();
char in_name[64], out_name[64];
snprintf(in_name, sizeof(in_name), "channel%d_midi_in", next_channel_id);
snprintf(out_name, sizeof(out_name), "channel%d_midi_out", next_channel_id);
cur[idx].midi_in = jack_port_register(client, in_name, JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput, 0);
cur[idx].midi_out = jack_port_register(
client, out_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (!cur[idx].midi_in || !cur[idx].midi_out) {
fprintf(stderr, "Failed to register MIDI ports for channel %d\n",
next_channel_id);
atomic_store(&cur[idx].active, 0);
return;
/* If this is a MIDI channel, register MIDI ports */
if (channels[idx].type == CHANNEL_MIDI) {
char midi_in_name[64], midi_out_name[64];
snprintf(midi_in_name, sizeof(midi_in_name), "channel%d_midi_in", next_channel_id);
snprintf(midi_out_name, sizeof(midi_out_name), "channel%d_midi_out", next_channel_id);
channels[idx].midi_in = jack_port_register(
client, midi_in_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
channels[idx].midi_out = jack_port_register(
client, midi_out_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
if (!channels[idx].midi_in || !channels[idx].midi_out) {
fprintf(stderr, "Failed to register MIDI ports for channel %d\n",
next_channel_id);
atomic_store(&channels[idx].active, 0);
jack_port_unregister(client, channels[idx].audio_in);
jack_port_unregister(client, channels[idx].audio_out);
return;
}
} else {
channels[idx].midi_in = NULL;
channels[idx].midi_out = NULL;
}
atomic_store(&cur[idx].active, 1);
cur[idx].type = CHANNEL_MIDI;
atomic_store(&cur[idx].scene_count, 1);
atomic_store(&cur[idx].current_scene, 0);
init_scene(&cur[idx].scenes[0]);
atomic_store(&channels[idx].active, 1);
/* Initialise first scene */
channels[idx].scene_count = 1;
channels[idx].current_scene = 0;
init_scene(&channels[idx].scenes[0]);
channels[idx].save_ring = NULL;
atomic_store(&channels[idx].save_complete, 0);
next_channel_id++;
atomic_fetch_add(&channel_count, 1);
channel_count++;
}
void channel_remove(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
atomic_store(&cur[idx].active, 0);
atomic_store(&channels[idx].active, 0);
atomic_fetch_sub(&channel_count, 1);
}
void channel_add_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
if (atomic_load(&cur[idx].scene_count) >= MAX_SCENES)
if (atomic_load(&channels[idx].scene_count) >= MAX_SCENES)
return;
int ns = atomic_load(&cur[idx].scene_count);
init_scene(&cur[idx].scenes[ns]);
atomic_fetch_add(&cur[idx].scene_count, 1);
int ns = atomic_load(&channels[idx].scene_count);
init_scene(&channels[idx].scenes[ns]);
atomic_fetch_add(&channels[idx].scene_count, 1);
}
void channel_remove_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
int sc = atomic_load(&channels[idx].scene_count);
if (sc <= 1)
return;
int cs = atomic_load(&cur[idx].current_scene);
int cs = atomic_load(&channels[idx].current_scene);
/* shift remaining scenes down (atomic copy of fields) */
for (int i = cs; i < sc - 1; i++) {
atomic_store(&cur[idx].scenes[i].loop_count,
atomic_load(&cur[idx].scenes[i + 1].loop_count));
atomic_store(&cur[idx].scenes[i].record_pos,
atomic_load(&cur[idx].scenes[i + 1].record_pos));
atomic_store(&cur[idx].scenes[i].playback_pos,
atomic_load(&cur[idx].scenes[i + 1].playback_pos));
atomic_store(&cur[idx].scenes[i].state,
atomic_load(&cur[idx].scenes[i + 1].state));
atomic_store(&cur[idx].scenes[i].prev_state,
atomic_load(&cur[idx].scenes[i + 1].prev_state));
atomic_store(&channels[idx].scenes[i].loop_count,
atomic_load(&channels[idx].scenes[i + 1].loop_count));
atomic_store(&channels[idx].scenes[i].record_pos,
atomic_load(&channels[idx].scenes[i + 1].record_pos));
atomic_store(&channels[idx].scenes[i].playback_pos,
atomic_load(&channels[idx].scenes[i + 1].playback_pos));
atomic_store(&channels[idx].scenes[i].state,
atomic_load(&channels[idx].scenes[i + 1].state));
atomic_store(&channels[idx].scenes[i].prev_state,
atomic_load(&channels[idx].scenes[i + 1].prev_state));
/* copy loop data (may race with RT thread; acceptable for this release) */
memcpy(cur[idx].scenes[i].loop.audio_buffer,
cur[idx].scenes[i + 1].loop.audio_buffer,
memcpy(channels[idx].scenes[i].loop.audio_buffer,
channels[idx].scenes[i + 1].loop.audio_buffer,
LOOP_BUF_SIZE * sizeof(float));
}
atomic_fetch_sub(&cur[idx].scene_count, 1);
int new_sc = atomic_load(&cur[idx].scene_count);
atomic_fetch_sub(&channels[idx].scene_count, 1);
int new_sc = atomic_load(&channels[idx].scene_count);
if (cs >= new_sc)
atomic_store(&cur[idx].current_scene, new_sc - 1);
atomic_store(&channels[idx].current_scene, new_sc - 1);
}
void channel_next_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
int sc = atomic_load(&channels[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs + 1) % sc);
int cs = atomic_load(&channels[idx].current_scene);
atomic_store(&channels[idx].current_scene, (cs + 1) % sc);
}
}
void channel_prev_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
int sc = atomic_load(&channels[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs - 1 + sc) % sc);
int cs = atomic_load(&channels[idx].current_scene);
atomic_store(&channels[idx].current_scene, (cs - 1 + sc) % sc);
}
}

117
engine/src/channel.c~ Normal file
View File

@@ -0,0 +1,117 @@
// cppcheck-suppress missingIncludeSystem
#include "channel.h"
#include <jack/jack.h>
#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
<<<<<<< HEAD
=======
/* Helper: zero a scene and set its state to IDLE */
static void init_scene(scene_t *sc) {
memset(sc, 0, sizeof(scene_t));
atomic_store(&sc->state, STATE_IDLE);
atomic_store(&sc->prev_state, -1);
}
>>>>>>> 3-integrate-carla
void channel_add(jack_client_t *client, int idx) {
char in_name[64], out_name[64];
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
channels[idx].audio_in = jack_port_register(
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[idx].audio_out = jack_port_register(
client, out_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!channels[idx].audio_in || !channels[idx].audio_out) {
fprintf(stderr, "Failed to register ports for channel %d\n",
next_channel_id);
/* Do NOT mark channel active process loop will skip it */
atomic_store(&channels[idx].active, 0);
return;
}
atomic_store(&channels[idx].active, 1);
atomic_store(&channels[idx].state, STATE_IDLE);
channels[idx].prev_state = -1;
channels[idx].loop_count = 0;
channels[idx].record_pos = 0;
channels[idx].playback_pos = 0;
channels[idx].save_ring = NULL;
next_channel_id++;
channel_count++;
}
void channel_remove(jack_client_t *client, int idx) {
(void)client;
<<<<<<< HEAD
atomic_store(&channels[idx].active, 0);
channel_count--;
=======
struct channel_t *cur = get_channels_array();
atomic_store(&cur[idx].active, 0);
atomic_fetch_sub(&channel_count, 1);
}
void channel_add_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
if (atomic_load(&cur[idx].scene_count) >= MAX_SCENES)
return;
int ns = atomic_load(&cur[idx].scene_count);
init_scene(&cur[idx].scenes[ns]);
atomic_fetch_add(&cur[idx].scene_count, 1);
}
void channel_remove_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc <= 1)
return;
int cs = atomic_load(&cur[idx].current_scene);
/* shift remaining scenes down (atomic copy of fields) */
for (int i = cs; i < sc - 1; i++) {
atomic_store(&cur[idx].scenes[i].loop_count,
atomic_load(&cur[idx].scenes[i + 1].loop_count));
atomic_store(&cur[idx].scenes[i].record_pos,
atomic_load(&cur[idx].scenes[i + 1].record_pos));
atomic_store(&cur[idx].scenes[i].playback_pos,
atomic_load(&cur[idx].scenes[i + 1].playback_pos));
atomic_store(&cur[idx].scenes[i].state,
atomic_load(&cur[idx].scenes[i + 1].state));
atomic_store(&cur[idx].scenes[i].prev_state,
atomic_load(&cur[idx].scenes[i + 1].prev_state));
/* copy loop data (may race with RT thread; acceptable for this release) */
memcpy(cur[idx].scenes[i].loop.audio_buffer,
cur[idx].scenes[i + 1].loop.audio_buffer,
LOOP_BUF_SIZE * sizeof(float));
}
atomic_fetch_sub(&cur[idx].scene_count, 1);
int new_sc = atomic_load(&cur[idx].scene_count);
if (cs >= new_sc)
atomic_store(&cur[idx].current_scene, new_sc - 1);
}
void channel_next_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs + 1) % sc);
}
}
void channel_prev_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
int sc = atomic_load(&cur[idx].scene_count);
if (sc > 1) {
int cs = atomic_load(&cur[idx].current_scene);
atomic_store(&cur[idx].current_scene, (cs - 1 + sc) % sc);
}
>>>>>>> 3-integrate-carla
}

View File

@@ -5,23 +5,14 @@
#include <jack/jack.h>
#include <stdatomic.h>
#define MAX_SCENES 4
#define LOOP_BUF_SIZE (5 * 48000)
#define MAX_MIDI_EVENTS 1024
#define MAX_CHANNELS 16
#define MAX_SCENES 16
#include "ringbuffer.h"
typedef enum {
CHANNEL_AUDIO,
CHANNEL_MIDI
} channel_type_t;
typedef struct {
jack_nframes_t timestamp; /* frame offset relative to loop start */
unsigned char status;
unsigned char note;
unsigned char velocity;
} midi_event_t;
typedef enum { CHANNEL_AUDIO, CHANNEL_MIDI } channel_type_t;
typedef enum {
STATE_IDLE,
@@ -30,46 +21,58 @@ typedef enum {
STATE_PAUSED
} looper_state;
/* Structure for a recorded or playing MIDI event */
typedef struct {
union {
float audio_buffer[LOOP_BUF_SIZE];
midi_event_t midi_events[MAX_MIDI_EVENTS];
} loop;
atomic_int loop_count;
atomic_int record_pos;
atomic_int playback_pos;
atomic_int state;
atomic_int prev_state;
jack_nframes_t timestamp;
unsigned char status;
unsigned char note;
unsigned char velocity;
} midi_event_t;
/* Loop data for a scene */
typedef struct {
float audio_buffer[LOOP_BUF_SIZE];
midi_event_t midi_events[MAX_MIDI_EVENTS];
} loop_data_t;
/* A single scene within a channel */
typedef struct {
atomic_int state;
atomic_int prev_state;
atomic_int loop_count;
atomic_int record_pos;
atomic_int playback_pos;
loop_data_t loop;
} scene_t;
struct channel_t {
channel_type_t type;
channel_type_t type; /* AUDIO or MIDI */
atomic_int active;
jack_port_t *audio_in;
jack_port_t *audio_out;
jack_port_t *midi_in;
jack_port_t *midi_in; /* NULL for audio channels */
jack_port_t *midi_out;
int scene_count; /* number of scenes (max MAX_SCENES) */
int current_scene; /* index of currently active scene */
scene_t scenes[MAX_SCENES];
atomic_int scene_count;
atomic_int current_scene;
_Atomic RingBuf *save_ring;
atomic_int save_complete; /* 1 when writer is done; RT thread must stop writing */
};
/* Globals declared in looper.c */
extern struct channel_t *_Atomic channels;
extern atomic_int channel_capacity;
extern struct channel_t channels[MAX_CHANNELS];
extern atomic_int channel_count;
extern atomic_int channel_capacity;
extern int next_channel_id;
extern atomic_int cmd_add;
extern atomic_int cmd_remove;
extern atomic_int cmd_load;
extern atomic_int cmd_save;
/* Safe accessor for the realtime thread (returns a snapshot of the current pointer) */
static inline struct channel_t *get_channels_array(void) {
return atomic_load(&channels);
}
void init_scene(scene_t *sc);
void channel_add(jack_client_t *client, int idx);
void channel_remove(jack_client_t *client, int idx);
void channel_add_midi(jack_client_t *client, int idx);
/* Scene management (called from main loop) */
void channel_add_scene(jack_client_t *client, int idx);
void channel_remove_scene(jack_client_t *client, int idx);
void channel_next_scene(jack_client_t *client, int idx);

BIN
engine/src/channel.o Normal file
View File

Binary file not shown.

View File

@@ -9,6 +9,8 @@ typedef enum {
CMD_ADD_CHANNEL, // add a new dynamic channel
CMD_REMOVE_CHANNEL, // remove last dynamic channel
CMD_ADD_MIDI_CHANNEL, // add a new dynamic MIDI channel
CMD_LOAD, // load WAV file into channel 0
CMD_SAVE, // save loop as WAV file
CMD_NEXT_SCENE,
CMD_PREV_SCENE,
CMD_ADD_SCENE,

View File

File diff suppressed because it is too large Load Diff

675
engine/src/looper.c~ Normal file
View File

@@ -0,0 +1,675 @@
// cppcheck-suppress missingIncludeSystem
#include "looper.h"
#include "channel.h"
#include "midi.h"
<<<<<<< HEAD
#include "wav.h"
#include "ringbuffer.h"
#include "pipe.h"
#include <jack/jack.h>
#include <sys/stat.h>
=======
#include "queue.h"
>>>>>>> 3-integrate-carla
#include <fcntl.h>
#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>
<<<<<<< HEAD
#include <time.h>
#include "queue.h"
#include "command.h"
/* Global command queues */
spsc_queue_t cmd_queue;
spsc_queue_t cmd_queue_main_midi;
spsc_queue_t cmd_queue_main_fifo;
=======
#include <sys/stat.h>
#include <unistd.h>
>>>>>>> 3-integrate-carla
#define STATUS_FIFO "/tmp/looper_status"
/* writer status fd */
static int status_fd = -1;
static void looper_write_status(void) {
<<<<<<< HEAD
if (status_fd < 0)
return;
char buf[256];
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
if (!atomic_load(&channels[ch].active))
continue;
int state_val = atomic_load(&channels[ch].state);
const char *state_str;
switch (state_val) {
case STATE_IDLE: state_str = "IDLE"; break;
case STATE_RECORD: state_str = "RECORD"; break;
case STATE_LOOPING: state_str = "LOOPING"; break;
case STATE_PAUSED: state_str = "PAUSED"; break;
default: state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf),
"CH=%d SC=%d STATE=%s\n",
ch, 0, state_str);
if (n > 0) {
int ret = write(status_fd, buf, n);
(void)ret;
}
}
=======
int fd = open(STATUS_FIFO, O_WRONLY | O_NONBLOCK);
if (fd < 0)
return;
struct channel_t *cur = get_channels_array();
int cap = atomic_load(&channel_capacity);
char buf[256];
for (int ch = 0; ch < cap; ch++) {
if (!atomic_load(&cur[ch].active))
continue;
int sc_idx = atomic_load(&cur[ch].current_scene);
int state = atomic_load(&cur[ch].scenes[sc_idx].state);
const char *state_str;
switch (state) {
case STATE_IDLE:
state_str = "IDLE";
break;
case STATE_RECORD:
state_str = "RECORD";
break;
case STATE_LOOPING:
state_str = "LOOPING";
break;
case STATE_PAUSED:
state_str = "PAUSED";
break;
default:
state_str = "UNKNOWN";
}
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx,
state_str);
if (n > 0) {
int ret = write(fd, buf, n);
(void)ret;
}
}
close(fd);
>>>>>>> 3-integrate-carla
}
/* Global state (shared across files) */
struct channel_t channels[MAX_CHANNELS];
atomic_int channel_count = 0;
atomic_int channel_capacity = MAX_CHANNELS;
int next_channel_id = 1;
atomic_int cmd_add = 0;
atomic_int cmd_remove = 0;
atomic_int cmd_load = 0;
atomic_int cmd_save = 0;
jack_port_t *midi_control_port = NULL;
jack_port_t *midi_clock_port = NULL;
atomic_int control_key_active = 0;
atomic_int bind_channel = 0;
/* Deferred removal index (1 second grace) */
static int pending_unregister_idx = -1;
/* writer thread function and sample rate holder */
static void *writer_thread(void *arg);
static int global_sample_rate = 0;
/* execute a single command (called from looper_process_commands) */
static void exec_command(command_t cmd, jack_client_t *client) {
int ch = cmd.channel;
if (ch < 0) ch = 0;
switch (cmd.type) {
case CMD_CYCLE: {
int state = atomic_load(&channels[ch].state);
switch (state) {
case STATE_IDLE:
atomic_store(&channels[ch].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[ch].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[ch].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[ch].state, STATE_LOOPING);
break;
}
atomic_store(&channels[ch].prev_state, -1);
break;
}
case CMD_STOP:
atomic_store(&channels[ch].state, STATE_IDLE);
atomic_store(&channels[ch].prev_state, -1);
break;
case CMD_ADD_CHANNEL:
case CMD_ADD_MIDI_CHANNEL: {
int idx;
for (idx = 0; idx < MAX_CHANNELS; idx++)
if (!channels[idx].active)
break;
if (idx < MAX_CHANNELS)
channel_add(client, idx);
break;
}
case CMD_REMOVE_CHANNEL: {
int remove_idx = -1;
for (int idx = 1; idx < MAX_CHANNELS; idx++)
if (channels[idx].active)
remove_idx = idx;
if (remove_idx != -1) {
channel_remove(client, remove_idx);
pending_unregister_idx = remove_idx;
}
break;
}
case CMD_BIND_CHANNEL:
atomic_store(&bind_channel, cmd.data);
break;
case CMD_UNBIND:
atomic_store(&bind_channel, 0);
break;
case CMD_LOAD:
atomic_store(&cmd_load, 1);
break;
case CMD_SAVE:
atomic_store(&cmd_save, 1);
break;
case CMD_ADD_SCENE:
case CMD_REMOVE_SCENE:
case CMD_NEXT_SCENE:
case CMD_PREV_SCENE:
break;
default:
break;
}
}
/* ----------------------------------------------------------------
* process callback
* ---------------------------------------------------------------- */
int process_callback(jack_nframes_t nframes, void *arg) {
(void)arg;
if (midi_control_port) {
void *midi_ctrl_buf = jack_port_get_buffer(midi_control_port, nframes);
if (midi_ctrl_buf) {
midi_handle_events(midi_ctrl_buf, nframes);
}
}
/* process each active channel */
for (int c = 0; c < MAX_CHANNELS; c++) {
if (!atomic_load(&channels[c].active))
continue;
/* Guard against NULL ports (e.g. if port registration failed) */
if (!channels[c].audio_in || !channels[c].audio_out) {
fprintf(stderr, "WARN: channel %d has NULL audio port(s), skipping\n", c);
continue;
}
const jack_default_audio_sample_t *in =
(const jack_default_audio_sample_t *)jack_port_get_buffer(
channels[c].audio_in, nframes);
jack_default_audio_sample_t *out =
(jack_default_audio_sample_t *)jack_port_get_buffer(
channels[c].audio_out, nframes);
if (!out)
continue;
int state = atomic_load(&channels[c].state);
if (state != atomic_load(&channels[c].prev_state)) {
switch (state) {
case STATE_RECORD:
atomic_store(&channels[c].record_pos, 0);
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));
atomic_store(&channels[c].playback_pos, 0);
break;
default:
break;
}
}
<<<<<<< HEAD
jack_nframes_t i;
switch (state) {
case STATE_RECORD:
if (in) {
float *f_out = (float *)out;
const float *f_in = (const float *)in;
for (i = 0; i < nframes; i++) {
int rp = atomic_fetch_add(&channels[c].record_pos, 1);
if (rp < LOOP_BUF_SIZE)
channels[c].loop_buffer[rp] = f_in[i];
f_out[i] = f_in[i];
}
} else {
=======
if (active_channels[c].type == CHANNEL_MIDI) {
/* MIDI channel handling */
switch (state) {
case STATE_RECORD: {
void *midi_in_buf =
jack_port_get_buffer(active_channels[c].midi_in, nframes);
if (midi_in_buf) {
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
jack_midi_event_t ev;
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
int rp = atomic_load(&sc->record_pos);
if (rp < MAX_MIDI_EVENTS) {
sc->loop.midi_events[rp].timestamp = ev.time;
sc->loop.midi_events[rp].status = ev.buffer[0];
sc->loop.midi_events[rp].note = (ev.size > 1) ? ev.buffer[1] : 0;
sc->loop.midi_events[rp].velocity =
(ev.size > 2) ? ev.buffer[2] : 0;
atomic_store(&sc->record_pos, rp + 1);
}
}
/* forward incoming MIDI to output during record */
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
}
}
}
break;
}
case STATE_LOOPING: {
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
int cnt = atomic_load(&sc->loop_count);
if (cnt > 0) {
for (int e = 0; e < cnt; e++) {
unsigned char msg[3];
msg[0] = sc->loop.midi_events[e].status;
msg[1] = sc->loop.midi_events[e].note;
msg[2] = sc->loop.midi_events[e].velocity;
jack_midi_event_write(midi_out_buf, 0, msg, 3);
}
}
}
break;
}
case STATE_PAUSED:
/* no output */
break;
default: /* IDLE */
{
void *midi_in_buf =
jack_port_get_buffer(active_channels[c].midi_in, nframes);
void *midi_out_buf =
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_in_buf && midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
jack_midi_event_t ev;
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
}
}
} break;
}
if (state == STATE_LOOPING) {
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
}
} else {
/* audio channel handling */
jack_nframes_t i;
switch (state) {
case STATE_RECORD:
if (in) {
float *f_out = (float *)out;
const float *f_in = (const float *)in;
for (i = 0; i < nframes; i++) {
int rp = atomic_load(&sc->record_pos);
if (rp < LOOP_BUF_SIZE) {
sc->loop.audio_buffer[rp] = f_in[i];
atomic_store(&sc->record_pos, rp + 1);
}
f_out[i] = f_in[i];
}
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
case STATE_LOOPING: {
int loop_cnt = atomic_load(&sc->loop_count);
if (loop_cnt > 0) {
float *outf = (float *)out;
int pp = atomic_load(&sc->playback_pos);
for (i = 0; i < nframes; i++) {
outf[i] = sc->loop.audio_buffer[pp];
pp = (pp + 1) % loop_cnt;
}
atomic_store(&sc->playback_pos, pp);
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
}
case STATE_PAUSED:
>>>>>>> 3-integrate-carla
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
case STATE_LOOPING:
int lc = atomic_load(&channels[c].loop_count);
if (lc > 0) {
float *outf = (float *)out;
for (i = 0; i < nframes; i++) {
int pp = atomic_load(&channels[c].playback_pos);
outf[i] = channels[c].loop_buffer[pp];
atomic_store(&channels[c].playback_pos, (pp + 1) % lc);
}
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
case STATE_PAUSED:
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
break;
default: /* IDLE */
if (in) {
memcpy(out, in, sizeof(jack_default_audio_sample_t) * nframes);
} else {
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
}
break;
}
// push loop output into save ring if saving (atomic load)
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) {
const float *outf = (const float *)out;
ring_write(r, outf, nframes);
}
}
atomic_store(&channels[c].prev_state, state);
}
/* MIDI clock events affect channel 0 only */
if (midi_clock_port) {
void *midi_clock_buf = jack_port_get_buffer(midi_clock_port, nframes);
if (midi_clock_buf) {
jack_nframes_t n_clock_events = jack_midi_get_event_count(midi_clock_buf);
jack_midi_event_t cev;
for (jack_nframes_t j = 0; j < n_clock_events; j++) {
if (jack_midi_event_get(&cev, midi_clock_buf, j) != 0)
continue;
if (cev.size >= 1) {
unsigned char msg = cev.buffer[0];
switch (msg) {
case 0xFA: {
int s = atomic_load(&channels[0].state);
if (s == STATE_IDLE)
atomic_store(&channels[0].state, STATE_RECORD);
break;
}
case 0xFC:
atomic_store(&channels[0].state, STATE_IDLE);
break;
case 0xFB: {
int s = atomic_load(&channels[0].state);
if (s == STATE_PAUSED)
atomic_store(&channels[0].state, STATE_LOOPING);
break;
}
default:
break;
}
}
}
}
}
return 0;
}
/* ----------------------------------------------------------------
* shutdown callback
* ---------------------------------------------------------------- */
void jack_shutdown_cb(void *arg) {
(void)arg;
fprintf(stderr, "JACK shutdown\n");
exit(0);
}
/* ----------------------------------------------------------------
* looper initialisation
* ---------------------------------------------------------------- */
int looper_init(jack_client_t *client) {
/* store sample rate for writer thread */
global_sample_rate = jack_get_sample_rate(client);
/* create status FIFO (ignore if already exists) */
mkfifo(STATUS_FIFO, 0666);
/* open the status FIFO for reading+writing so writes work even without reader */
status_fd = open(STATUS_FIFO, O_RDWR);
if (status_fd < 0) {
perror("open status FIFO");
}
queue_init(&cmd_queue);
queue_init(&cmd_queue_main_midi);
queue_init(&cmd_queue_main_fifo);
/* start the FIFO reader thread */
pipe_start_reader();
/* channel 0 */
channels[0].active = 1;
atomic_store(&channels[0].state, STATE_IDLE);
atomic_store(&channels[0].prev_state, -1);
channels[0].loop_count = 0;
atomic_store(&channels[0].record_pos, 0);
atomic_store(&channels[0].playback_pos, 0);
atomic_store_explicit(&channels[0].save_ring, NULL, memory_order_release);
channels[0].audio_in = jack_port_register(
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[0].audio_out = jack_port_register(
client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!channels[0].audio_in || !channels[0].audio_out) {
fprintf(stderr, "Could not create audio ports for channel 0\n");
return -1;
}
channel_count = 1;
midi_control_port = jack_port_register(
client, "control", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
midi_clock_port = jack_port_register(client, "clock", JACK_DEFAULT_MIDI_TYPE,
JackPortIsInput, 0);
if (!midi_control_port || !midi_clock_port) {
fprintf(stderr, "Could not create MIDI ports\n");
return -1;
}
return 0;
}
/* ----------------------------------------------------------------
* 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;
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);
ring_destroy(ring);
free(ring);
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
return NULL;
}
/* ----------------------------------------------------------------
* mainloop command processing
* ---------------------------------------------------------------- */
void looper_process_commands(jack_client_t *client) {
/* process commands from the three queues FIRST */
command_t cmd;
while (queue_pop(&cmd_queue, &cmd))
exec_command(cmd, client);
while (queue_pop(&cmd_queue_main_midi, &cmd))
exec_command(cmd, client);
while (queue_pop(&cmd_queue_main_fifo, &cmd))
exec_command(cmd, client);
/* Unregister any ports that were marked for deferred removal.
By now the realtime thread has had at least one full cycle
to see the `active = 0` store. */
if (pending_unregister_idx != -1) {
int idx = pending_unregister_idx;
if (channels[idx].audio_in)
jack_port_unregister(client, channels[idx].audio_in);
if (channels[idx].audio_out)
jack_port_unregister(client, channels[idx].audio_out);
pending_unregister_idx = -1;
}
/* ---------- add channel ---------- */
if (atomic_exchange(&cmd_add, 0)) {
int idx;
for (idx = 0; idx < MAX_CHANNELS; idx++)
if (!channels[idx].active)
break;
if (idx < MAX_CHANNELS) {
channel_add(client, idx);
}
}
/* ---------- remove channel ---------- */
if (atomic_exchange(&cmd_remove, 0)) {
int remove_idx = -1;
for (int idx = 1; idx < MAX_CHANNELS; idx++)
if (channels[idx].active)
remove_idx = idx;
if (remove_idx != -1) {
/* Mark inactive now; ports will be unregistered next round */
channel_remove(client, remove_idx);
pending_unregister_idx = remove_idx;
}
}
/* ---------- load command ---------- */
if (atomic_exchange(&cmd_load, 0)) {
float *buf = NULL;
unsigned frames = 0;
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;
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
atomic_store(&channels[0].loop_count, (int)frames);
atomic_store(&channels[0].record_pos, 0);
atomic_store(&channels[0].playback_pos, 0);
atomic_store(&channels[0].state, STATE_LOOPING);
atomic_store(&channels[0].prev_state, -1);
free(buf);
} else {
fprintf(stderr, "Failed to load loop.wav\n");
printf("LOAD: FAILED\n");
}
}
/* ---------- 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 &&
channels[0].save_ring == NULL) {
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);
pthread_t th;
pthread_create(&th, NULL, writer_thread, &channels[0]);
pthread_detach(th);
} else {
free(ring);
}
}
}
}
/* write current state to status FIFO */
looper_write_status();
}

BIN
engine/src/looper.o Normal file
View File

Binary file not shown.

View File

@@ -1,11 +1,10 @@
// cppcheck-suppress missingIncludeSystem
#include "looper.h"
#include "pipe.h"
#include <jack/jack.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <time.h>
int main(int argc, char *argv[]) {
(void)argc;
@@ -34,12 +33,6 @@ int main(int argc, char *argv[]) {
return 1;
}
if (pipe_start_reader() != 0) {
fprintf(stderr, "pipe reader initialisation failed\n");
jack_client_close(client);
return 1;
}
if (jack_activate(client)) {
fprintf(stderr, "Cannot activate client\n");
jack_client_close(client);
@@ -50,10 +43,7 @@ int main(int argc, char *argv[]) {
while (1) {
looper_process_commands(client);
{
struct timespec ts = {.tv_sec = 0, .tv_nsec = 1000000};
nanosleep(&ts, NULL);
} /* check commands every 1 ms */
{ struct timespec ts = { .tv_sec = 0, .tv_nsec = 50000000 }; nanosleep(&ts, NULL); } /* check commands every 50 ms */
}
jack_client_close(client);

BIN
engine/src/main.o Normal file
View File

Binary file not shown.

View File

@@ -1,16 +1,20 @@
// cppcheck-suppress missingIncludeSystem
#include "midi.h"
#include "channel.h"
#include "command.h"
#include "queue.h"
#include <jack/jack.h>
#include <jack/midiport.h>
#include <stdatomic.h>
extern atomic_int control_key_active;
extern atomic_int bind_channel;
/* queues declared in looper.c */
extern spsc_queue_t cmd_queue;
extern spsc_queue_t cmd_queue_main_midi;
extern atomic_int control_key_active;
extern atomic_int cmd_add;
extern atomic_int cmd_remove;
extern atomic_int cmd_load;
extern atomic_int cmd_save;
extern atomic_int bind_channel;
void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
(void)nframes;
@@ -35,27 +39,36 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
int ck = atomic_load(&control_key_active);
if (ck) {
atomic_store(&control_key_active, 0);
if (note < 16 && note < atomic_load(&channel_capacity)) {
command_t cmd = {
.type = CMD_BIND_CHANNEL, .channel = -1, .data = note};
queue_push(&cmd_queue, cmd);
if (note < 16) {
atomic_store(&bind_channel, note);
} else {
switch (note) {
case 60: {
command_t cmd = {
.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 61: {
command_t cmd = {
.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 62: {
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
case 62: /* trigger looper channel via bind_channel */
{
int bch = atomic_load(&bind_channel);
if (bch >= 0 && bch < atomic_load(&channel_capacity)) {
command_t cmd = {.type = CMD_CYCLE, .channel = bch, .data = 0};
queue_push(&cmd_queue, cmd);
if (bch >= 0 && bch < MAX_CHANNELS) {
int sc_idx = atomic_load(&channels[bch].current_scene);
int cur = atomic_load(&channels[bch].scenes[sc_idx].state);
switch (cur) {
case STATE_IDLE:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
break;
}
}
} break;
case 63: {
@@ -97,19 +110,31 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
} else {
/* direct mapping */
switch (note) {
case 1: {
command_t cmd = {.type = CMD_CYCLE, .channel = 0, .data = 0};
queue_push(&cmd_queue, cmd);
} break;
case 60: {
command_t cmd = {.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 61: {
command_t cmd = {
.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
case 1: /* toggle channel 0 */
{
int sc0 = atomic_load(&channels[0].current_scene);
int cur0 = atomic_load(&channels[0].scenes[sc0].state);
switch (cur0) {
case STATE_IDLE:
atomic_store(&channels[0].scenes[sc0].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[0].scenes[sc0].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[0].scenes[sc0].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[0].scenes[sc0].state, STATE_LOOPING);
break;
}
} break;
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
default:
break;
}

154
engine/src/midi.c~ Normal file
View File

@@ -0,0 +1,154 @@
// cppcheck-suppress missingIncludeSystem
#include "midi.h"
#include "channel.h"
#include <jack/jack.h>
#include <jack/midiport.h>
#include <stdatomic.h>
extern atomic_int control_key_active;
extern atomic_int cmd_add;
extern atomic_int cmd_remove;
extern atomic_int cmd_load;
extern atomic_int cmd_save;
extern atomic_int bind_channel;
void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
(void)nframes;
jack_nframes_t nevents = jack_midi_get_event_count(port_buffer);
jack_midi_event_t ev;
for (jack_nframes_t i = 0; i < nevents; i++) {
if (jack_midi_event_get(&ev, port_buffer, i) != 0)
continue;
if (ev.size < 3)
continue;
unsigned char status = ev.buffer[0];
unsigned char note = ev.buffer[1];
unsigned char vel = ev.buffer[2];
/* noteon */
if ((status & 0xf0) == 0x90 && vel > 0) {
if (note == 64) {
atomic_store(&control_key_active, 1);
} else {
int ck = atomic_load(&control_key_active);
if (ck) {
atomic_store(&control_key_active, 0);
if (note < 16) {
atomic_store(&bind_channel, note);
} else {
switch (note) {
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
case 62: /* trigger looper channel via bind_channel */
{
int bch = atomic_load(&bind_channel);
if (bch >= 0 && bch < MAX_CHANNELS) {
int cur = atomic_load(&channels[bch].state);
switch (cur) {
case STATE_IDLE:
atomic_store(&channels[bch].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[bch].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[bch].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[bch].state, STATE_LOOPING);
break;
}
}
} break;
<<<<<<< HEAD
case 63: /* unbind reset bind to channel 0 */
atomic_store(&bind_channel, 0);
break;
case 70: /* load WAV into channel 0 */
atomic_store(&cmd_load, 1);
break;
case 71: /* save WAV of channel 0 */
atomic_store(&cmd_save, 1);
break;
=======
case 63: {
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} break;
case 65: {
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} break;
case 66: {
command_t cmd = {
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 67: {
command_t cmd = {
.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 68: {
command_t cmd = {
.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 69: {
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
case 70: {
command_t cmd = {
.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_midi, cmd);
} break;
>>>>>>> 3-integrate-carla
default:
break;
}
}
} else {
/* direct mapping */
switch (note) {
case 1: /* toggle channel 0 */
{
int cur0 = atomic_load(&channels[0].state);
switch (cur0) {
case STATE_IDLE:
atomic_store(&channels[0].state, STATE_RECORD);
break;
case STATE_RECORD:
atomic_store(&channels[0].state, STATE_LOOPING);
break;
case STATE_LOOPING:
atomic_store(&channels[0].state, STATE_PAUSED);
break;
case STATE_PAUSED:
atomic_store(&channels[0].state, STATE_LOOPING);
break;
}
} break;
case 60:
atomic_store(&cmd_add, 1);
break;
case 61:
atomic_store(&cmd_remove, 1);
break;
default:
break;
}
}
}
} else if ((status & 0xf0) == 0x80 ||
((status & 0xf0) == 0x90 && vel == 0)) {
atomic_store(&control_key_active, 0);
}
}
}

BIN
engine/src/midi.o Normal file
View File

Binary file not shown.

View File

@@ -37,14 +37,14 @@ static void *pipe_thread_func(void *arg) {
if (strcmp(line, "add") == 0) {
command_t cmd = {.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "add_midi") == 0) {
command_t cmd = {
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
} else if (strcmp(line, "remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strncmp(line, "record ", 7) == 0) {
int ch = atoi(line + 7);
command_t cmd = {.type = CMD_CYCLE, .channel = ch, .data = 0};
@@ -61,16 +61,22 @@ static void *pipe_thread_func(void *arg) {
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_add") == 0) {
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_next") == 0) {
command_t cmd = {.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_prev") == 0) {
command_t cmd = {.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "load") == 0) {
command_t cmd = {.type = CMD_LOAD, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "save") == 0) {
command_t cmd = {.type = CMD_SAVE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
}
/* ignore unknown lines */
}

111
engine/src/pipe.c~ Normal file
View File

@@ -0,0 +1,111 @@
#include "pipe.h"
#include "command.h"
#include "queue.h"
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#define FIFO_PATH "/tmp/looper_cmd"
#define LINE_MAX 256
/* forwarddeclare the global queues (defined in looper.c) */
extern spsc_queue_t cmd_queue;
extern spsc_queue_t cmd_queue_main_fifo;
static void *pipe_thread_func(void *arg) {
(void)arg;
char line[LINE_MAX];
while (1) {
FILE *fifo = fopen(FIFO_PATH, "r");
if (!fifo) {
perror("fopen fifo");
return NULL;
}
while (fgets(line, sizeof(line), fifo)) {
/* strip newline */
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[len - 1] = '\0';
if (strcmp(line, "add") == 0) {
command_t cmd = {.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "add_midi") == 0) {
<<<<<<< HEAD
command_t cmd = {.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
=======
command_t cmd = {
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
>>>>>>> 3-integrate-carla
} else if (strcmp(line, "remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strncmp(line, "record ", 7) == 0) {
int ch = atoi(line + 7);
command_t cmd = {.type = CMD_CYCLE, .channel = ch, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "stop") == 0) {
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strncmp(line, "bind ", 5) == 0) {
int ch = atoi(line + 5);
command_t cmd = {.type = CMD_BIND_CHANNEL, .channel = -1, .data = ch};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "unbind") == 0) {
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_add") == 0) {
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_remove") == 0) {
command_t cmd = {.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_next") == 0) {
command_t cmd = {.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "scene_prev") == 0) {
command_t cmd = {.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "load") == 0) {
command_t cmd = {.type = CMD_LOAD, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
} else if (strcmp(line, "save") == 0) {
command_t cmd = {.type = CMD_SAVE, .channel = -1, .data = 0};
queue_push(&cmd_queue, cmd);
}
/* ignore unknown lines */
}
/* EOF all writers closed, reopen for next connection */
fclose(fifo);
{
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
nanosleep(&ts, NULL);
} /* small pause before retrying */
}
return NULL; /* unreachable */
}
int pipe_start_reader(void) {
/* create FIFO if it doesn't exist */
if (mkfifo(FIFO_PATH, 0666) != 0 && errno != EEXIST) {
perror("mkfifo");
return -1;
}
pthread_t tid;
if (pthread_create(&tid, NULL, pipe_thread_func, NULL) != 0) {
perror("pthread_create");
return -1;
}
pthread_detach(tid); /* we don't need to join */
return 0;
}

BIN
engine/src/pipe.o Normal file
View File

Binary file not shown.

View File

@@ -1,6 +1,7 @@
#ifndef QUEUE_H
#define QUEUE_H
#include <stdatomic.h>
#include "command.h"
#include <stdbool.h>
@@ -15,8 +16,8 @@ typedef struct {
command_t buffer[QUEUE_CAPACITY];
/* head: index where next element will be written (producer only)
* tail: index of next element to read (consumer only) */
int head;
int tail;
atomic_int head;
atomic_int tail;
} spsc_queue_t;
/* Initialise queue (must be called once before any push/pop). */

BIN
engine/src/queue.o Normal file
View File

Binary file not shown.

76
engine/src/ringbuffer.c Normal file
View File

@@ -0,0 +1,76 @@
#include "ringbuffer.h"
#include <stdlib.h>
static inline size_t load_head(const RingBuf *r) {
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);
}
static inline void store_head(RingBuf *r, size_t v) {
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);
}
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;
}
void ring_destroy(RingBuf *r) {
free(r->buf);
r->buf = NULL;
r->capacity = 0;
}
static 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);
}
static size_t ring_writeable(const RingBuf *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 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;
}

19
engine/src/ringbuffer.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include <stddef.h>
#include <stdatomic.h>
typedef struct {
atomic_size_t head;
atomic_size_t tail;
size_t capacity;
float *buf;
} RingBuf;
int ring_init(RingBuf *r, size_t capacity);
void ring_destroy(RingBuf *r);
size_t ring_write(RingBuf *r, const float *data, size_t count);
size_t ring_read(RingBuf *r, float *data, size_t count);
#endif

BIN
engine/src/ringbuffer.o Normal file
View File

Binary file not shown.

41
engine/src/wav.c Normal file
View File

@@ -0,0 +1,41 @@
#include "wav.h"
#include "channel.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int wav_read(const char *path, float **buffer, unsigned *frames) {
SF_INFO info;
info.format = 0;
SNDFILE *sf = sf_open(path, SFM_READ, &info);
if (!sf) return -1;
/* We need mono 16-bit PCM; refuse anything else */
if (info.channels != 1 || info.samplerate <= 0) {
sf_close(sf);
return -1;
}
unsigned total = (info.frames > (sf_count_t)LOOP_BUF_SIZE) ? LOOP_BUF_SIZE : (unsigned)info.frames;
float *buf = (float*)malloc(total * sizeof(float));
if (!buf) { sf_close(sf); return -1; }
sf_count_t nread = sf_readf_float(sf, buf, total);
sf_close(sf);
*buffer = buf;
*frames = (unsigned)nread;
return 0;
}
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) {
SF_INFO info;
info.samplerate = sample_rate;
info.channels = 1;
info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
SNDFILE *sf = sf_open(path, SFM_WRITE, &info);
if (!sf) return -1;
sf_writef_float(sf, data, frames);
sf_close(sf);
return 0;
}

9
engine/src/wav.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef WAV_H
#define WAV_H
#include <stddef.h>
int wav_read(const char *path, float **buffer, unsigned *frames);
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate);
#endif

BIN
engine/src/wav.o Normal file
View File

Binary file not shown.

BIN
engine/test_status_fifo Executable file
View File

Binary file not shown.

View File

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,4 @@
# Toplevel Makefile delegates build/clean/test to subdirectories
# Top-level Makefile delegates build/clean/test to subdirectories
SUBDIRS = engine client