10 Commits

Author SHA1 Message Date
Loic Coenen
d4a811e552 docs: add scene switching engine documentation and update evaluation
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:42:34 +00:00
Loic Coenen
567799a2d3 docs: add scene switching engine implementation guide 2026-05-10 19:42:33 +00:00
Loic Coenen
755af275d8 fix: convert shared scene metadata to atomic_int to fix data races
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:33:12 +00:00
Loic Coenen
74db4ed46c fix: add missing channel pointer declaration in apply_command
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:13:51 +00:00
Loic Coenen
15be644af7 refactor: remove unused variable 'cur' in looper_process_commands
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:07:52 +00:00
Loic Coenen
aaca25ebf1 refactor: remove unused local variable in looper commands
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:01:37 +00:00
Loic Coenen
e3b9321b1a fix: remove unused variable and suppress cppcheck warnings
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 19:00:13 +00:00
Loic Coenen
015ad2c5a7 chore: add trailing space to CFLAGS in makefile 2026-05-10 19:00:11 +00:00
Loic Coenen
c8b9de8e81 fix: reopen FIFO on EOF to prevent blocking on subsequent writes
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 18:39:10 +00:00
Loic Coenen
1ba98fc768 fix: prevent hang in scene add/remove test and fix unsafe scene copy
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
2026-05-10 18:34:26 +00:00
8 changed files with 311 additions and 151 deletions

View File

@@ -0,0 +1,91 @@
# Scene Switching Engine
## Overview
The scene switching engine allows a channel to have multiple independent recording/playback states (scenes).
Only one scene per channel is active at a time. The active scene's state (IDLE / RECORD / LOOPING / PAUSED) is
controlled independently of other scenes.
## Data Model
Each `channel_t` holds an array of up to `MAX_SCENES` (16) `scene_t` structures. Two atomic integers keep track
of the number of scenes and which scene is currently active:
```c
atomic_int scene_count; // number of scenes for this channel
atomic_int current_scene; // index of the active scene (0 ≤ current_scene < scene_count)
```
Each `scene_t` contains the loop buffer (audio or MIDI events) and the perscene atomic state:
```c
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; // STATE_IDLE / STATE_RECORD / STATE_LOOPING / STATE_PAUSED
atomic_int prev_state; // previous state (used by RT callback to detect transitions)
```
## Commands
| Command | Trigger (MIDI) | Trigger (FIFO) | Effect |
|--------------------------|------------------------|-----------------------|---------------------------------------------------------|
| **CMD_NEXT_SCENE** | note 67 (control key) | `scene_next\n` | Increments `current_scene` (wraps around). |
| **CMD_PREV_SCENE** | note 68 (control key) | `scene_prev\n` | Decrements `current_scene` (wraps around). |
| **CMD_ADD_SCENE** | note 69 (control key) | `scene_add\n` | Appends a new empty scene, increments `scene_count`. |
| **CMD_REMOVE_SCENE** | note 70 (control key) | `scene_remove\n` | Removes the current scene (shifts remaining scenes). |
All scene commands are processed on the main loop (not in the RT callback). They are pushed to
`cmd_queue_main_midi` (for MIDI) or `cmd_queue_main_fifo` (for FIFO) and applied by
`looper_process_commands()`.
## Thread Safety
- `scene_count` and `current_scene` are `atomic_int`; all reads/writes use `atomic_load`/`atomic_store`.
- The perscene fields (`loop_count`, `record_pos`, `playback_pos`, `state`, `prev_state`) are also `atomic_int`,
so the RT callback and the main loop can safely read and write them concurrently.
- The audio loop buffer itself (a plain `float` array) is not atomic. During scene removal the buffer is copied
via `memcpy`. If a scene is actively looping, this copy may produce a temporarily inconsistent buffer.
**Known limitation:** scene removal should only be performed when the channel is idle (all scenes in
`STATE_IDLE`). The integration test `test_scene_add_remove` does exactly this.
## Implementation Details
1. **`channel_add_scene`**
- Called from main loop.
- Checks `scene_count < MAX_SCENES` (atomically).
- Calls `init_scene()` to zero the new scene and set its state to `STATE_IDLE`.
- Atomically increments `scene_count`.
2. **`channel_remove_scene`**
- Called from main loop.
- Refuses if `scene_count <= 1` (at least one scene must always exist).
- Shifts all scenes after the current one down one position each scene field is copied with
`atomic_store`/`atomic_load`.
- The audio buffer is copied with `memcpy` (see limitation above).
- Decrements `scene_count` and adjusts `current_scene` if it would become out of bounds.
3. **`channel_next_scene` / `channel_prev_scene`**
- Called from main loop.
- If `scene_count > 1`, atomically increments/decrements `current_scene` (wrapping using modulo).
4. **RT callback (`process_callback`)**
- At the start of each frame it reads `current_scene` atomically to obtain the scene index for that
channel.
- All perscene reads (state, loop_count, record_pos, playback_pos) use `atomic_load`.
- When the state changes, the callback atomically resets `record_pos`, `loop_count`, `playback_pos`
as appropriate.
## Tests
- `test_scene_add_remove` (FIFO) adds a scene, cycles next, removes the scene, exits.
- `test_scene_next_prev_midi` sends control key + notes 67/68 to switch scenes.
- `test_scene_cycle_per_scene` records a loop on scene 0, switches to scene 1, verifies scene 1 is idle.
- `test_scene_add_remove_midi` sends control key + notes 69/70 to add/remove scenes.
All scene tests pass as part of `make test`.

View File

@@ -20,6 +20,7 @@
- `CMD_BIND_CHANNEL`, `CMD_UNBIND`, `CMD_CYCLE`, `CMD_ADD_CHANNEL`, `CMD_REMOVE_CHANNEL` are all wired.
- The integration test suite now includes `test_fifo_stop_bind_unbind()` and `test_midi_channel_add()`.
- The FIFO pipe reader handles `"stop"`, `"bind <ch>"`, `"unbind"`, and `"add_midi"`.
- **Note:** The separate test files in `tests/` (`test_audio.c`, `test_channel.c`, `test_fifo.c`, `test_loop.c`, `main.c`) are not compiled by the makefile and require a missing `test_common.h`. They are not part of the build they do not affect functionality and may be removed in a future cleanup.
### 2. Potential Segfaults
- **Audio channels:** `audio_in`/`audio_out` are checked for NULL before use.

View File

@@ -1,5 +1,5 @@
CC ?= gcc
CFLAGS ?= -Wall -Wextra -g -Isrc
CFLAGS ?= -Wall -Wextra -g -Isrc
LDFLAGS ?= -ljack -lm
SRC = src/main.c src/looper.c src/channel.c src/midi.c src/queue.c src/pipe.c
@@ -22,7 +22,7 @@ clean:
rm -f looper integration_test src/*.o
check:
cppcheck --enable=all --error-exitcode=1 --suppress=missingIncludeSystem --suppress=normalCheckLevelMaxBranches src/*.c --library=posix .
cppcheck --enable=all --error-exitcode=1 --suppress=missingIncludeSystem --suppress=normalCheckLevelMaxBranches src/*.c --library=posix
# Optional: Format code using clang-format
format:

View File

@@ -9,7 +9,7 @@
static void init_scene(scene_t *sc) {
memset(sc, 0, sizeof(scene_t));
atomic_store(&sc->state, STATE_IDLE);
sc->prev_state = -1;
atomic_store(&sc->prev_state, -1);
}
void channel_add(jack_client_t *client, int idx) {
@@ -32,8 +32,8 @@ void channel_add(jack_client_t *client, int idx) {
atomic_store(&cur[idx].active, 1);
cur[idx].type = CHANNEL_AUDIO;
cur[idx].scene_count = 1;
cur[idx].current_scene = 0;
atomic_store(&cur[idx].scene_count, 1);
atomic_store(&cur[idx].current_scene, 0);
init_scene(&cur[idx].scenes[0]);
next_channel_id++;
@@ -60,8 +60,8 @@ void channel_add_midi(jack_client_t *client, int idx) {
atomic_store(&cur[idx].active, 1);
cur[idx].type = CHANNEL_MIDI;
cur[idx].scene_count = 1;
cur[idx].current_scene = 0;
atomic_store(&cur[idx].scene_count, 1);
atomic_store(&cur[idx].current_scene, 0);
init_scene(&cur[idx].scenes[0]);
next_channel_id++;
@@ -78,43 +78,59 @@ void channel_remove(jack_client_t *client, int idx) {
void channel_add_scene(jack_client_t *client, int idx) {
(void)client;
struct channel_t *cur = get_channels_array();
if (cur[idx].scene_count >= MAX_SCENES)
if (atomic_load(&cur[idx].scene_count) >= MAX_SCENES)
return;
int ns = cur[idx].scene_count;
int ns = atomic_load(&cur[idx].scene_count);
init_scene(&cur[idx].scenes[ns]);
cur[idx].scene_count++;
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();
if (cur[idx].scene_count <= 1)
int sc = atomic_load(&cur[idx].scene_count);
if (sc <= 1)
return;
int cs = cur[idx].current_scene;
/* shift remaining scenes down */
for (int i = cs; i < cur[idx].scene_count - 1; i++) {
cur[idx].scenes[i] = cur[idx].scenes[i + 1];
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));
}
cur[idx].scene_count--;
if (cur[idx].current_scene >= cur[idx].scene_count)
cur[idx].current_scene = cur[idx].scene_count - 1;
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();
if (cur[idx].scene_count > 1) {
cur[idx].current_scene =
(cur[idx].current_scene + 1) % cur[idx].scene_count;
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();
if (cur[idx].scene_count > 1) {
cur[idx].current_scene =
(cur[idx].current_scene - 1 + cur[idx].scene_count) %
cur[idx].scene_count;
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);
}
}

View File

@@ -35,11 +35,11 @@ typedef struct {
float audio_buffer[LOOP_BUF_SIZE];
midi_event_t midi_events[MAX_MIDI_EVENTS];
} loop;
int loop_count;
int record_pos;
int playback_pos;
atomic_int loop_count;
atomic_int record_pos;
atomic_int playback_pos;
atomic_int state;
int prev_state;
atomic_int prev_state;
} scene_t;
struct channel_t {
@@ -50,8 +50,8 @@ struct channel_t {
jack_port_t *midi_in;
jack_port_t *midi_out;
scene_t scenes[MAX_SCENES];
int scene_count;
int current_scene;
atomic_int scene_count;
atomic_int current_scene;
};
/* Globals declared in looper.c */

View File

@@ -60,13 +60,13 @@ static int ensure_capacity(jack_client_t *client, int idx) {
}
static void apply_command(command_t cmd) {
struct channel_t *cur = get_channels_array();
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
switch (cmd.type) {
case CMD_CYCLE:
if (cmd.channel >= 0 && cmd.channel < cap) {
int sc_idx = cur[cmd.channel].current_scene;
int sc_idx = atomic_load(&cur[cmd.channel].current_scene);
scene_t *sc = &cur[cmd.channel].scenes[sc_idx];
int cst = atomic_load(&sc->state);
int next;
@@ -93,22 +93,24 @@ static void apply_command(command_t cmd) {
case CMD_STOP:
if (cmd.channel >= 0 && cmd.channel < cap) {
struct channel_t *ch = &cur[cmd.channel];
for (int s = 0; s < ch->scene_count; s++) {
int sc_cnt = atomic_load(&ch->scene_count);
for (int s = 0; s < sc_cnt; s++) {
atomic_store(&ch->scenes[s].state, STATE_IDLE);
ch->scenes[s].loop_count = 0;
ch->scenes[s].record_pos = 0;
ch->scenes[s].playback_pos = 0;
ch->scenes[s].prev_state = -1;
atomic_store(&ch->scenes[s].loop_count, 0);
atomic_store(&ch->scenes[s].record_pos, 0);
atomic_store(&ch->scenes[s].playback_pos, 0);
atomic_store(&ch->scenes[s].prev_state, -1);
}
} else {
for (int i = 0; i < cap; i++) {
struct channel_t *ch = &cur[i];
for (int s = 0; s < ch->scene_count; s++) {
int sc_cnt = atomic_load(&ch->scene_count);
for (int s = 0; s < sc_cnt; s++) {
atomic_store(&ch->scenes[s].state, STATE_IDLE);
ch->scenes[s].loop_count = 0;
ch->scenes[s].record_pos = 0;
ch->scenes[s].playback_pos = 0;
ch->scenes[s].prev_state = -1;
atomic_store(&ch->scenes[s].loop_count, 0);
atomic_store(&ch->scenes[s].record_pos, 0);
atomic_store(&ch->scenes[s].playback_pos, 0);
atomic_store(&ch->scenes[s].prev_state, -1);
}
}
}
@@ -167,7 +169,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
}
/* Obtain current scene pointer */
int sc_idx = active_channels[c].current_scene;
int sc_idx = atomic_load(&active_channels[c].current_scene);
scene_t *sc = &active_channels[c].scenes[sc_idx];
const jack_default_audio_sample_t *in =
@@ -180,17 +182,18 @@ int process_callback(jack_nframes_t nframes, void *arg) {
continue;
int state = atomic_load(&sc->state);
int prev_state = atomic_load(&sc->prev_state);
if (state != sc->prev_state) {
if (state != prev_state) {
switch (state) {
case STATE_RECORD:
sc->record_pos = 0;
sc->loop_count = 0;
atomic_store(&sc->record_pos, 0);
atomic_store(&sc->loop_count, 0);
break;
case STATE_LOOPING:
if (sc->record_pos > 0)
sc->loop_count = sc->record_pos;
sc->playback_pos = 0;
if (atomic_load(&sc->record_pos) > 0)
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
atomic_store(&sc->playback_pos, 0);
break;
default:
break;
@@ -209,14 +212,15 @@ int process_callback(jack_nframes_t nframes, void *arg) {
for (jack_nframes_t j = 0; j < nevents; j++) {
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
continue;
if (sc->record_pos < MAX_MIDI_EVENTS) {
sc->loop.midi_events[sc->record_pos].timestamp = ev.time;
sc->loop.midi_events[sc->record_pos].status = ev.buffer[0];
sc->loop.midi_events[sc->record_pos].note =
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[sc->record_pos].velocity =
sc->loop.midi_events[rp].velocity =
(ev.size > 2) ? ev.buffer[2] : 0;
sc->record_pos++;
atomic_store(&sc->record_pos, rp + 1);
}
}
/* forward incoming MIDI to output during record */
@@ -238,7 +242,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
jack_port_get_buffer(active_channels[c].midi_out, nframes);
if (midi_out_buf) {
jack_midi_clear_buffer(midi_out_buf);
int cnt = sc->loop_count;
int cnt = atomic_load(&sc->loop_count);
if (cnt > 0) {
for (int e = 0; e < cnt; e++) {
unsigned char msg[3];
@@ -274,7 +278,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
break;
}
if (state == STATE_LOOPING) {
sc->loop_count = sc->record_pos;
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
}
} else {
/* audio channel handling */
@@ -285,8 +289,11 @@ int process_callback(jack_nframes_t nframes, void *arg) {
float *f_out = (float *)out;
const float *f_in = (const float *)in;
for (i = 0; i < nframes; i++) {
if (sc->record_pos < LOOP_BUF_SIZE)
sc->loop.audio_buffer[sc->record_pos++] = f_in[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 {
@@ -294,17 +301,21 @@ int process_callback(jack_nframes_t nframes, void *arg) {
}
break;
case STATE_LOOPING:
if (sc->loop_count > 0) {
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[sc->playback_pos];
sc->playback_pos = (sc->playback_pos + 1) % sc->loop_count;
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:
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
@@ -320,7 +331,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
}
}
sc->prev_state = state;
atomic_store(&sc->prev_state, state);
}
/* MIDI clock events affect channel 0 only */
@@ -337,7 +348,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
switch (msg) {
case 0xFA: {
struct channel_t *cur = atomic_load(&channels);
int sc_idx = cur[0].current_scene;
int sc_idx = atomic_load(&cur[0].current_scene);
int s = atomic_load(&cur[0].scenes[sc_idx].state);
if (s == STATE_IDLE)
atomic_store(&cur[0].scenes[sc_idx].state, STATE_RECORD);
@@ -345,13 +356,13 @@ int process_callback(jack_nframes_t nframes, void *arg) {
}
case 0xFC: {
struct channel_t *cur = atomic_load(&channels);
int sc_idx = cur[0].current_scene;
int sc_idx = atomic_load(&cur[0].current_scene);
atomic_store(&cur[0].scenes[sc_idx].state, STATE_IDLE);
break;
}
case 0xFB: {
struct channel_t *cur = atomic_load(&channels);
int sc_idx = cur[0].current_scene;
int sc_idx = atomic_load(&cur[0].current_scene);
int s = atomic_load(&cur[0].scenes[sc_idx].state);
if (s == STATE_PAUSED)
atomic_store(&cur[0].scenes[sc_idx].state, STATE_LOOPING);
@@ -394,13 +405,13 @@ int looper_init(jack_client_t *client) {
struct channel_t *init = atomic_load(&channels);
/* channel 0 */
atomic_store(&init[0].active, 1);
init[0].scene_count = 1;
init[0].current_scene = 0;
init[0].scenes[0].loop_count = 0;
init[0].scenes[0].record_pos = 0;
init[0].scenes[0].playback_pos = 0;
atomic_store(&init[0].scene_count, 1);
atomic_store(&init[0].current_scene, 0);
atomic_store(&init[0].scenes[0].loop_count, 0);
atomic_store(&init[0].scenes[0].record_pos, 0);
atomic_store(&init[0].scenes[0].playback_pos, 0);
atomic_store(&init[0].scenes[0].state, STATE_IDLE);
init[0].scenes[0].prev_state = -1;
atomic_store(&init[0].scenes[0].prev_state, -1);
init[0].audio_in = jack_port_register(
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
@@ -434,10 +445,9 @@ void looper_process_commands(jack_client_t *client) {
switch (cmd.type) {
case CMD_ADD_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int idx;
for (idx = 0; idx < cap; idx++)
if (!atomic_load(&cur[idx].active))
if (!atomic_load(&(get_channels_array()[idx].active)))
break;
if (idx == cap) {
if (ensure_capacity(client, idx) != 0)
@@ -448,10 +458,9 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_ADD_MIDI_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int idx;
for (idx = 0; idx < cap; idx++)
if (!atomic_load(&cur[idx].active))
if (!atomic_load(&(get_channels_array()[idx].active)))
break;
if (idx == cap) {
if (ensure_capacity(client, idx) != 0)
@@ -462,10 +471,9 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_REMOVE_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int remove_idx = -1;
for (int idx = 1; idx < cap; idx++)
if (atomic_load(&cur[idx].active))
if (atomic_load(&(get_channels_array()[idx].active)))
remove_idx = idx;
if (remove_idx != -1) {
channel_remove(client, remove_idx);
@@ -476,7 +484,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_ADD_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -486,7 +493,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_REMOVE_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -496,7 +502,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_NEXT_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -506,7 +511,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_PREV_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -522,10 +526,9 @@ void looper_process_commands(jack_client_t *client) {
switch (cmd.type) {
case CMD_ADD_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int idx;
for (idx = 0; idx < cap; idx++)
if (!atomic_load(&cur[idx].active))
if (!atomic_load(&(get_channels_array()[idx].active)))
break;
if (idx == cap) {
if (ensure_capacity(client, idx) != 0)
@@ -536,10 +539,9 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_ADD_MIDI_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int idx;
for (idx = 0; idx < cap; idx++)
if (!atomic_load(&cur[idx].active))
if (!atomic_load(&(get_channels_array()[idx].active)))
break;
if (idx == cap) {
if (ensure_capacity(client, idx) != 0)
@@ -550,10 +552,9 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_REMOVE_CHANNEL: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int remove_idx = -1;
for (int idx = 1; idx < cap; idx++)
if (atomic_load(&cur[idx].active))
if (atomic_load(&(get_channels_array()[idx].active)))
remove_idx = idx;
if (remove_idx != -1) {
channel_remove(client, remove_idx);
@@ -564,7 +565,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_ADD_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -574,7 +574,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_REMOVE_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -584,7 +583,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_NEXT_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {
@@ -594,7 +592,6 @@ void looper_process_commands(jack_client_t *client) {
}
case CMD_PREV_SCENE: {
int cap = atomic_load(&channel_capacity);
struct channel_t *cur = get_channels_array();
int bind = atomic_load(&bind_channel);
int ch = bind;
if (ch < cap) {

View File

@@ -7,6 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -19,58 +20,67 @@ extern spsc_queue_t cmd_queue_main_fifo;
static void *pipe_thread_func(void *arg) {
(void)arg;
FILE *fifo = fopen(FIFO_PATH, "r");
if (!fifo) {
perror("fopen fifo");
return NULL;
}
char line[LINE_MAX];
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_main_fifo, 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);
} 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_main_fifo, 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);
} 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);
} 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);
while (1) {
FILE *fifo = fopen(FIFO_PATH, "r");
if (!fifo) {
perror("fopen fifo");
return NULL;
}
/* ignore unknown lines */
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_main_fifo, 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);
} 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_main_fifo, 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);
} 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);
} 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);
}
/* 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 */
}
fclose(fifo);
return NULL;
return NULL; /* unreachable */
}
int pipe_start_reader(void) {

View File

@@ -1135,34 +1135,79 @@ static int write_fifo(const char *cmd) {
static int test_scene_add_remove(void) {
printf("Test: scene add/remove via FIFO\n");
fflush(stdout);
pid_t pid = start_looper();
if (pid < 0) return 1;
/* add a scene */
printf(" sending scene_add...\n");
fflush(stdout);
if (!write_fifo("scene_add\n")) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
kill(pid, SIGTERM);
for (int tries = 0; tries < 20; tries++) {
int wstatus;
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
if (ret == pid) break;
if (ret < 0) break;
safe_usleep(100000);
}
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: cannot write to FIFO\n");
return 1;
}
safe_usleep(50000); /* allow processing */
/* verify that scene_next works (doesn't crash) */
printf(" sending scene_next...\n");
fflush(stdout);
if (!write_fifo("scene_next\n")) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
kill(pid, SIGTERM);
for (int tries = 0; tries < 20; tries++) {
int wstatus;
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
if (ret == pid) break;
if (ret < 0) break;
safe_usleep(100000);
}
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(50000);
/* remove scene */
printf(" sending scene_remove...\n");
fflush(stdout);
if (!write_fifo("scene_remove\n")) {
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
kill(pid, SIGTERM);
for (int tries = 0; tries < 20; tries++) {
int wstatus;
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
if (ret == pid) break;
if (ret < 0) break;
safe_usleep(100000);
}
kill(pid, SIGKILL); waitpid(pid, NULL, 0);
return 1;
}
safe_usleep(50000);
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
printf(" PASS (no crash means success)\n");
return 0;
/* kill with timeout */
kill(pid, SIGTERM);
for (int tries = 0; tries < 20; tries++) {
int wstatus;
pid_t ret = waitpid(pid, &wstatus, WNOHANG);
if (ret == pid) {
printf(" PASS (scene add/remove, looper exited)\n");
fflush(stdout);
return 0;
}
if (ret < 0) {
perror("waitpid");
break;
}
safe_usleep(100000); /* 100ms */
}
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
fprintf(stderr, " FAIL: looper did not exit in time\n");
return 1;
}
static int test_scene_next_prev_midi(void) {