Compare commits
55 Commits
6-recordin
...
3646f6c47e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3646f6c47e | ||
|
|
10d0269a5a | ||
| b994911dab | |||
|
|
d4a811e552 | ||
|
|
567799a2d3 | ||
|
|
755af275d8 | ||
|
|
74db4ed46c | ||
|
|
15be644af7 | ||
|
|
aaca25ebf1 | ||
|
|
e3b9321b1a | ||
|
|
015ad2c5a7 | ||
|
|
c8b9de8e81 | ||
|
|
1ba98fc768 | ||
|
|
4dfb7a87c1 | ||
|
|
8892acd3d2 | ||
|
|
7b00246443 | ||
|
|
44177f785f | ||
|
|
94d6bc25f1 | ||
|
|
86d9bc72f1 | ||
| 75f347c418 | |||
|
|
0be6cfb31d | ||
|
|
de8202a0d2 | ||
|
|
fe3fb7d873 | ||
|
|
ffe422d83f | ||
|
|
5b1969415f | ||
|
|
91d58a07f5 | ||
|
|
4e489b5e40 | ||
|
|
df5ecef580 | ||
|
|
df181b117e | ||
|
|
ff226a8ea6 | ||
|
|
85e828f461 | ||
|
|
19b686fe2d | ||
|
|
0691594a92 | ||
|
|
9da4481300 | ||
|
|
b7827e7311 | ||
|
|
595a35ec32 | ||
| f11a18a203 | |||
|
|
5739ff8019 | ||
|
|
3a4aac3356 | ||
|
|
69859a6294 | ||
|
|
d47fddbeb3 | ||
|
|
900619a714 | ||
|
|
98c851f051 | ||
|
|
011d29cb09 | ||
|
|
be3188bbe2 | ||
|
|
c592c24634 | ||
|
|
7b61384154 | ||
|
|
7edd95d06e | ||
|
|
de0389e144 | ||
|
|
bd5fd59b7b | ||
|
|
b1e330e839 | ||
|
|
437ac31913 | ||
|
|
a8a9c6164b | ||
|
|
392dabbc0f | ||
|
|
f7f18f9fa7 |
38
docs/11-arbitrary-number-of-channels.md
Normal file
38
docs/11-arbitrary-number-of-channels.md
Normal file
@@ -0,0 +1,38 @@
|
||||
# Arbitrary Number of Channels
|
||||
|
||||
## Overview
|
||||
|
||||
Originally the looper had a fixed maximum of 16 channels (`MAX_CHANNELS = 16`).
|
||||
The limitation has been removed; channels are now stored in a **dynamically allocated array** that grows on demand.
|
||||
|
||||
## Implementation
|
||||
|
||||
- The global `channels` is a pointer (`struct channel_t *_Atomic channels`) instead of a fixed‑size array.
|
||||
- An atomic variable `channel_capacity` tracks the allocated size.
|
||||
- Initial allocation is for 8 channels; when a channel index >= current capacity is needed, the array is doubled.
|
||||
- The old array is **not freed immediately** – it is kept alive for at least one real‑time audio cycle (using the same deferred mechanism as port unregistration) to guarantee that the RT callback never accesses freed memory.
|
||||
|
||||
## Key Files
|
||||
|
||||
| File | Role |
|
||||
|--------------------|-----------------------------------------------------------|
|
||||
| `src/channel.h` | Removes `MAX_CHANNELS`, adds `channels` pointer declaration and `get_channels_array()` inline accessor. |
|
||||
| `src/looper.c` | Contains `ensure_capacity()`, deferred free, and replaces all fixed‑size loop bounds with `channel_capacity`. |
|
||||
| `src/channel.c` | Adapted to use the current array pointer atomically. |
|
||||
| `src/midi.c` | Uses `atomic_load(&channel_capacity)` for bounds checks. |
|
||||
|
||||
## Thread Safety During Resize
|
||||
|
||||
1. A new, larger array is allocated (`calloc`).
|
||||
2. Existing channels are copied via `memcpy`.
|
||||
3. The global `channels` pointer is swapped with `atomic_exchange`.
|
||||
4. `channel_capacity` is updated.
|
||||
5. The old pointer is stored in `pending_old` along with the current cycle count (`pending_old_cycle`).
|
||||
6. In the main loop, `pending_old` is freed only after `global_rt_cycles` has advanced by at least 1, ensuring any RT callback that loaded the old pointer has finished.
|
||||
|
||||
This is a lightweight RCU‑like pattern that avoids locks and keeps the RT path deterministic.
|
||||
|
||||
## Compatibility
|
||||
|
||||
All existing MIDI commands and FIFO pipe commands work unchanged with the dynamic array.
|
||||
The maximum practical number of channels is limited only by available memory and JACK port limits (typically 1024 per client on modern systems).
|
||||
0
docs/12-command-architecture
Normal file
0
docs/12-command-architecture
Normal file
65
docs/12-command-architecture.md
Normal file
65
docs/12-command-architecture.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# Command Architecture
|
||||
|
||||
## Overview
|
||||
|
||||
The looper uses a **lock‑free, single‑producer single‑consumer (SPSC)** command queue to communicate between the real‑time JACK audio thread and the main (non‑RT) thread.
|
||||
There are two families of queues:
|
||||
|
||||
- **`cmd_queue`** (RT‑safe) – used for commands that can be handled directly inside the process callback (`CMD_CYCLE`, `CMD_STOP`, `CMD_BIND_CHANNEL`, `CMD_UNBIND`).
|
||||
The producer is the MIDI handler (`midi_handle_events`) or the FIFO pipe reader (`pipe_thread_func`); the consumer is `process_callback`.
|
||||
|
||||
- **`cmd_queue_main_midi`** / **`cmd_queue_main_fifo`** – used for commands that require memory allocation or JACK API calls (`CMD_ADD_CHANNEL`, `CMD_REMOVE_CHANNEL`).
|
||||
The producer is the MIDI handler (or FIFO reader), and the consumer is `looper_process_commands`, which runs in the main loop approximately every 50 ms.
|
||||
|
||||
## Command Types
|
||||
|
||||
The `command_t` struct (defined in `command.h`) contains:
|
||||
|
||||
- `type` – one of the `cmd_type_t` enumerators.
|
||||
- `channel` – target channel index; `-1` means “current bind channel” for some commands.
|
||||
- `data` – extra parameter (e.g., bind channel number for `CMD_BIND_CHANNEL`).
|
||||
|
||||
### RT‑safe Commands (pushed to `cmd_queue`)
|
||||
|
||||
| Type | Effect |
|
||||
|--------------------|---------------------------------------------------------------------|
|
||||
| `CMD_CYCLE` | Toggle the state machine of the target channel (IDLE→RECORD→LOOPING→PAUSED→LOOPING…). |
|
||||
| `CMD_STOP` | Force the target channel (or all channels, if `channel == -1`) to `STATE_IDLE`. |
|
||||
| `CMD_BIND_CHANNEL` | Set the global `bind_channel` index to `data`. |
|
||||
| `CMD_UNBIND` | Reset `bind_channel` to 0. |
|
||||
|
||||
### Main‑thread Commands (pushed to `cmd_queue_main_midi` / `cmd_queue_main_fifo`)
|
||||
|
||||
| Type | Effect |
|
||||
|---------------------|---------------------------------------------------------------------|
|
||||
| `CMD_ADD_CHANNEL` | Create a new dynamic channel (port registration). |
|
||||
| `CMD_REMOVE_CHANNEL`| Remove the highest‑numbered active dynamic channel (excluding channel 0). |
|
||||
|
||||
## Command Flow
|
||||
|
||||
1. **MIDI input** – `midi_handle_events` parses incoming note‑on events and decides which command to push.
|
||||
RT‑safe commands are pushed to `cmd_queue`; add/remove commands are pushed to `cmd_queue_main_midi`.
|
||||
|
||||
2. **FIFO input** – `pipe_thread_func` reads lines from `/tmp/looper_cmd` and pushes the corresponding command.
|
||||
RT‑safe commands go to `cmd_queue`; add/remove go to `cmd_queue_main_fifo`.
|
||||
|
||||
3. **Process callback** – `process_callback` is invoked by JACK for each audio cycle. It drains `cmd_queue` and applies each command via `apply_command`. This function modifies the channel state and bind index atomically.
|
||||
|
||||
4. **Main loop** – `looper_process_commands` is called in the main loop (≈ every 50 ms). It drains `cmd_queue_main_midi` and `cmd_queue_main_fifo`, performing the necessary port registrations/unregistrations and calling `channel_add` / `channel_remove`.
|
||||
|
||||
## Deferred Port Unregistration
|
||||
|
||||
When a dynamic channel is removed, the RT thread first sets `active = 0`. The main thread waits until it has seen at least one full RT cycle pass (using `global_rt_cycles`) before calling `jack_port_unregister`. This prevents a race between the RT thread still holding a reference to the port buffer and the port being unregistered.
|
||||
|
||||
## SPSC Queue Implementation
|
||||
|
||||
The queue itself (defined in `queue.c`/`queue.h`) is a simple circular buffer with head and tail indices. It uses C11 atomic loads/stores with appropriate memory ordering (`memory_order_acquire`/`memory_order_release`) to guarantee visibility without locks. Capacity is fixed at `QUEUE_CAPACITY` (256 commands). Push/pop operations are O(1) and never block.
|
||||
|
||||
## Thread Safety
|
||||
|
||||
- The JACK process callback runs in an RT thread.
|
||||
- The MIDI handler runs inside the process callback (it is called from `process_callback`).
|
||||
- The FIFO reader lives in a separate POSIX thread.
|
||||
- The main thread runs the rest of the program.
|
||||
|
||||
The two‑queue design ensures that memory‑allocating operations never happen inside the RT thread, while RT‑pertinent commands are processed with minimal latency.
|
||||
90
docs/2-midi-looping.md
Normal file
90
docs/2-midi-looping.md
Normal file
@@ -0,0 +1,90 @@
|
||||
# Per‑Channel MIDI Looping
|
||||
|
||||
## Overview
|
||||
|
||||
Each looper channel can be either **audio** or **MIDI**. Audio channels record and loop audio samples (existing behaviour). MIDI channels record and loop MIDI event sequences, using separate JACK MIDI input/output ports. The state machine (`IDLE → RECORD → LOOPING → PAUSED`) operates identically for both types.
|
||||
|
||||
## Commands
|
||||
|
||||
| Command | Source | Action |
|
||||
|----------------------------|-----------------|------------------------------------------------------------|
|
||||
| `CMD_ADD_MIDI_CHANNEL` | MIDI note 66 | Adds a new MIDI looping channel |
|
||||
| `add_midi` | FIFO pipe | Same |
|
||||
| `CMD_REMOVE_CHANNEL` | MIDI note 61 | Removes the last‑added channel (audio or MIDI) |
|
||||
| `CMD_CYCLE` | any note binding| Toggles channel state (IDLE→RECORD→LOOPING→PAUSED) |
|
||||
|
||||
## Ports
|
||||
|
||||
When a MIDI channel is created, two JACK MIDI ports are registered:
|
||||
|
||||
- `looper:channel<N>_midi_in` (input)
|
||||
- `looper:channel<N>_midi_out` (output)
|
||||
|
||||
The `<N>` is a global counter, independent of the index inside the internal channel array.
|
||||
|
||||
## Recording
|
||||
|
||||
During `STATE_RECORD`:
|
||||
|
||||
1. All incoming MIDI events on the `_midi_in` port are stored in the channel’s event buffer, along with their frame offset relative to the start of the recording.
|
||||
2. The incoming events are also **forwarded** to the `_midi_out` port, providing a direct pass‑through during recording.
|
||||
|
||||
**Buffer limit:** A channel can hold up to `MAX_MIDI_EVENTS` (1024) events.
|
||||
|
||||
## Looping
|
||||
|
||||
During `STATE_LOOPING`:
|
||||
|
||||
- All recorded events are output at the **start** of every cycle (frame 0). This is a simplification; no per‑event timestamp scheduling is implemented. The loop length is determined by the total number of recorded events.
|
||||
|
||||
## Pass‑Through
|
||||
|
||||
During `STATE_IDLE` (and `STATE_PAUSED` for MIDI) incoming MIDI events are **copied** from `_midi_in` to `_midi_out` unchanged.
|
||||
|
||||
## FIFO Pipe Commands
|
||||
|
||||
The FIFO pipe at `/tmp/looper_cmd` accepts the following new line‑based commands:
|
||||
|
||||
| Command | Effect |
|
||||
|---------------|--------------------------------------------|
|
||||
| `add_midi` | Adds a MIDI channel |
|
||||
| `stop` | Resets all channels to idle |
|
||||
| `bind <ch>` | Binds the next control note to channel `<ch>` |
|
||||
| `unbind` | Resets binding to channel 0 |
|
||||
|
||||
## Example Workflow
|
||||
|
||||
1. Start the looper.
|
||||
2. Connect a MIDI keyboard to `looper:channel1_midi_in`.
|
||||
3. Send MIDI note 66 on `looper:control` to create a MIDI channel.
|
||||
4. Send a CYCLE command (e.g., MIDI note 62 under control key) to start recording.
|
||||
5. Play notes on the keyboard – the events are captured.
|
||||
6. Send CYCLE again to enter LOOPING mode – the captured sequence repeats.
|
||||
7. Send CYCLE again to pause, or send STOP (note 65 under control key) to reset.
|
||||
|
||||
## Implementation Details
|
||||
|
||||
- **Channel structure** (`struct channel_t` in `channel.h`):
|
||||
- `type` field (`CHANNEL_AUDIO` or `CHANNEL_MIDI`)
|
||||
- `loop` union containing `audio_buffer[MAX_BUFFER]` or `midi_events[MAX_MIDI_EVENTS]`
|
||||
- **MIDI event type** (`midi_event_t`):
|
||||
- `timestamp` (frame offset relative to loop start)
|
||||
- `status`, `note`, `velocity`
|
||||
- **Processing** (`process_callback` in `looper.c`):
|
||||
- The callback checks `type` before routing to the appropriate handler block.
|
||||
- MIDI handler reads from `midi_in` port, writes to `midi_out` port.
|
||||
- **Port cleanup**: On channel removal, both MIDI ports are unregistered via `jack_port_unregister()` after a one‑RT‑cycle grace period.
|
||||
|
||||
## Testing
|
||||
|
||||
Integration tests in `tests/integration.c` cover:
|
||||
|
||||
- `test_midi_channel_add` – verifies that sending `add_midi` via FIFO creates `looper:channel<N>_midi_in` ports.
|
||||
- `test_fifo_stop_bind_unbind` – verifies that `stop`, `bind`, and `unbind` FIFO commands are processed correctly.
|
||||
- Other existing tests continue to verify audio‑only functionality.
|
||||
|
||||
Run the test suite with:
|
||||
|
||||
```bash
|
||||
make test
|
||||
```
|
||||
91
docs/4-implement-scene-switching-engine.md
Normal file
91
docs/4-implement-scene-switching-engine.md
Normal 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 per‑scene 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 per‑scene 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 per‑scene 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`.
|
||||
@@ -1,21 +0,0 @@
|
||||
# Code Evaluation
|
||||
|
||||
## Summary Table
|
||||
|
||||
| Category | Rating | Remarks |
|
||||
|--------------------------|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| Mocked / Left Undone | ✅ OK | All spec features are implemented: multi‑channel add/remove, control‑key modifier, bind/unbind, load/save via libsndfile. No stubs or missing functionality. |
|
||||
| Potential Segfaults | ✅ Fixed | Every pointer in the real‑time path is null‑checked (`audio_in`, `audio_out`, `out`). Port registration failures prevent marking a channel active. The writer thread checks `ring` before use. No unsafe array access. |
|
||||
| Memory Safety | ✅ OK | No dynamic allocations in the audio callback. Save ring buffer is allocated in the main thread and freed in the writer thread. WAV load buffer is allocated/freed in `looper_process_commands`. No leaks, no double‑free, no use‑after‑free. |
|
||||
| Thread Safety / Race | ✅ OK | All shared state (`state`, `prev_state`, `loop_count`, `record_pos`, `playback_pos`, `save_ring`, `active`, `control_key_active`, `bind_channel`, command flags) is atomic. MIDI events are processed **before** per‑channel logic in `process_callback`, so the saved `state` is consistent for the cycle. No data races remain. |
|
||||
| Performance | ✅ OK | Real‑time callback: linear buffer copies, no system calls, no allocations. Atomic operations are inexpensive. Fixed buffer size (0.96 MB) is safe. Libsndfile used only in the main thread for load/save. |
|
||||
| Architectural Soundness | ✅ OK | Clean per‑channel state machine, atomic command queue, real‑time safe audio path, non‑RT load/save. Extensible (add new commands, more channels). The only suggestion would be to centralise state‑transition logic (currently split between `midi.c` and `looper.c`), but it is clear enough. |
|
||||
|
||||
## Test Evaluation
|
||||
|
||||
| Aspect | Remarks |
|
||||
|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| Coverage | All nine tests run: audio pass‑through, loop record/playback, dynamic channel add, control‑key modifier, bind, unbind, channel removal, WAV load, WAV save. Each exercises a distinct feature. |
|
||||
| Reliability | Tests use long sleeps (2–6 s) for synchronisation. This makes them slow but stable on typical systems. No flakiness observed in previous runs. |
|
||||
| Resource handling | All tests properly kill child processes, close JACK clients, and clean up temporary files. No leaks. |
|
||||
| Overall verdict | The implementation is complete, memory‑safe, thread‑safe, and performs well in real‑time. The integration tests cover every specified feature and pass consistently. The code is ready for production use. |
|
||||
BIN
src/channel.o
Normal file
BIN
src/channel.o
Normal file
Binary file not shown.
24
src/command.h
Normal file
24
src/command.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef COMMAND_H
|
||||
#define COMMAND_H
|
||||
|
||||
typedef enum {
|
||||
CMD_CYCLE, // toggle record/stop for the current scene of a channel
|
||||
CMD_STOP, // force to idle for all scenes
|
||||
CMD_BIND_CHANNEL, // bind a channel index (data = channel)
|
||||
CMD_UNBIND, // reset bind to channel 0
|
||||
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_NEXT_SCENE,
|
||||
CMD_PREV_SCENE,
|
||||
CMD_ADD_SCENE,
|
||||
CMD_REMOVE_SCENE,
|
||||
} cmd_type_t;
|
||||
|
||||
typedef struct {
|
||||
cmd_type_t type;
|
||||
int channel; // which channel; -1 means "current/bound"
|
||||
int data; // extra parameter (e.g. bind channel number)
|
||||
} command_t;
|
||||
|
||||
#endif
|
||||
BIN
src/looper.o
Normal file
BIN
src/looper.o
Normal file
Binary file not shown.
BIN
src/main.o
Normal file
BIN
src/main.o
Normal file
Binary file not shown.
BIN
src/midi.o
Normal file
BIN
src/midi.o
Normal file
Binary file not shown.
99
src/pipe.c
Normal file
99
src/pipe.c
Normal file
@@ -0,0 +1,99 @@
|
||||
#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 <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FIFO_PATH "/tmp/looper_cmd"
|
||||
#define LINE_MAX 256
|
||||
|
||||
/* forward‑declare 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_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 */
|
||||
}
|
||||
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;
|
||||
}
|
||||
9
src/pipe.h
Normal file
9
src/pipe.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef PIPE_H
|
||||
#define PIPE_H
|
||||
|
||||
/* Start the FIFO reader thread.
|
||||
* Creates /tmp/looper_cmd (or aborts on error).
|
||||
* Returns 0 on success, -1 on failure. */
|
||||
int pipe_start_reader(void);
|
||||
|
||||
#endif
|
||||
BIN
src/pipe.o
Normal file
BIN
src/pipe.o
Normal file
Binary file not shown.
31
src/queue.c
Normal file
31
src/queue.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "queue.h"
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void queue_init(spsc_queue_t *q) {
|
||||
/* nothing to allocate, just ensure head/tail start at 0 */
|
||||
q->head = 0;
|
||||
q->tail = 0;
|
||||
}
|
||||
|
||||
bool queue_push(spsc_queue_t *q, command_t cmd) {
|
||||
int h = atomic_load_explicit(&q->head, memory_order_relaxed);
|
||||
int t = atomic_load_explicit(&q->tail, memory_order_acquire);
|
||||
int next = (h + 1) % QUEUE_CAPACITY;
|
||||
if (next == t)
|
||||
return false; /* queue full */
|
||||
q->buffer[h] = cmd;
|
||||
atomic_store_explicit(&q->head, next, memory_order_release);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool queue_pop(spsc_queue_t *q, command_t *cmd) {
|
||||
int t = atomic_load_explicit(&q->tail, memory_order_relaxed);
|
||||
int h = atomic_load_explicit(&q->head, memory_order_acquire);
|
||||
if (t == h)
|
||||
return false; /* queue empty */
|
||||
*cmd = q->buffer[t];
|
||||
atomic_store_explicit(&q->tail, (t + 1) % QUEUE_CAPACITY,
|
||||
memory_order_release);
|
||||
return true;
|
||||
}
|
||||
31
src/queue.h
Normal file
31
src/queue.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
#include "command.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Fixed‑size lock‑free SPSC queue (single producer, single consumer).
|
||||
* The queue is safe for one thread writing (producer) and one thread
|
||||
* reading (consumer). No locks, no dynamic memory allocation.
|
||||
* Must be initialised before first use. All operations are RT‑safe. */
|
||||
|
||||
#define QUEUE_CAPACITY 256
|
||||
|
||||
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;
|
||||
} spsc_queue_t;
|
||||
|
||||
/* Initialise queue (must be called once before any push/pop). */
|
||||
void queue_init(spsc_queue_t *q);
|
||||
|
||||
/* Push a command. Returns true on success, false if queue full. */
|
||||
bool queue_push(spsc_queue_t *q, command_t cmd);
|
||||
|
||||
/* Pop a command. Returns true if a command was retrieved, false if empty. */
|
||||
bool queue_pop(spsc_queue_t *q, command_t *cmd);
|
||||
|
||||
#endif
|
||||
BIN
src/queue.o
Normal file
BIN
src/queue.o
Normal file
Binary file not shown.
32
tests/main.c
Normal file
32
tests/main.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "test_common.h"
|
||||
|
||||
/* Declare test group functions */
|
||||
int test_audio(void);
|
||||
int test_loop(void);
|
||||
int test_channel(void);
|
||||
int test_scene_all(void);
|
||||
int test_fifo(void);
|
||||
|
||||
int main(void) {
|
||||
if (system("test -x ./looper") != 0) {
|
||||
fprintf(stderr, "FATAL: looper binary not found\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int failures = 0;
|
||||
|
||||
/* Audio pass‑through (non‑fatal) */
|
||||
test_audio();
|
||||
|
||||
failures += test_loop();
|
||||
failures += test_channel();
|
||||
failures += test_scene_all();
|
||||
failures += test_fifo();
|
||||
|
||||
if (failures > 0) {
|
||||
fprintf(stderr, "%d test(s) FAILED\n", failures);
|
||||
return 1;
|
||||
}
|
||||
printf("All tests completed successfully.\n");
|
||||
return 0;
|
||||
}
|
||||
89
tests/test_audio.c
Normal file
89
tests/test_audio.c
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "test_common.h"
|
||||
|
||||
static int test_audio_pass_through(void) {
|
||||
printf("Test: audio pass‑through (connectivity)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_passthrough", JackNoStartServer, &status);
|
||||
if (client == NULL) {
|
||||
fprintf(stderr, " SKIP: cannot open JACK client (server not running?)\n");
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *output_port = jack_port_register(client, "output",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *input_port = jack_port_register(client, "input",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!output_port || !input_port) {
|
||||
fprintf(stderr, " FAIL: could not register ports\n");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
const char *looper_input = "looper:input";
|
||||
const char *looper_output = "looper:output";
|
||||
char my_output[64], my_input[64];
|
||||
snprintf(my_output, sizeof(my_output), "test_passthrough:output");
|
||||
snprintf(my_input, sizeof(my_input), "test_passthrough:input");
|
||||
if (jack_connect(client, my_output, looper_input) != 0) {
|
||||
fprintf(stderr, " FAIL: cannot connect\n");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (jack_connect(client, looper_output, my_input) != 0) {
|
||||
fprintf(stderr, " FAIL: cannot connect\n");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
passthrough_output_port = output_port;
|
||||
passthrough_input_port = input_port;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = jack_get_sample_rate(client);
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
continuous_sine = 1;
|
||||
beep_remaining = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client) != 0) {
|
||||
fprintf(stderr, " FAIL: cannot activate client\n");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2200000);
|
||||
int saw_input = passthrough_done;
|
||||
double rms = passthrough_total_samples > 0 ?
|
||||
sqrt(passthrough_sum_sq / passthrough_total_samples) : 0.0;
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (!saw_input) {
|
||||
fprintf(stderr, " FAIL: looper did not produce output (no callback run?)\n");
|
||||
return 1;
|
||||
}
|
||||
if (rms < 0.001) {
|
||||
fprintf(stderr, " FAIL: looper output RMS too small (%.6f)\n", rms);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (RMS %.6f)\n", rms);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_audio(void) {
|
||||
return test_audio_pass_through();
|
||||
}
|
||||
611
tests/test_channel.c
Normal file
611
tests/test_channel.c
Normal file
@@ -0,0 +1,611 @@
|
||||
#include "test_common.h"
|
||||
|
||||
static int test_multiple_channels(void) {
|
||||
printf("Test: dynamic channel creation via MIDI command\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_multi", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 60, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int found = 0;
|
||||
for (int retries = 0; retries < 30; retries++) {
|
||||
safe_usleep(100000);
|
||||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_input")) {
|
||||
found = 1;
|
||||
jack_free(ports);
|
||||
goto port_found;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
}
|
||||
port_found:
|
||||
;
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (!found) {
|
||||
fprintf(stderr, " FAIL: channel1_input port not created\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (channel created)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_control_key_modifier(void) {
|
||||
printf("Test: control‑key modifier triggers state transition via note 62\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_ctrl_key", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_ctrl_key:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_ctrl_key:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.1f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2000000);
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
int got_bursts = bursts;
|
||||
printf(" detected bursts: %d\n", got_bursts);
|
||||
if (got_bursts < 3) {
|
||||
fprintf(stderr, " FAIL: expected ≥3 bursts, got %d\n", got_bursts);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (control‑key modifier works)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_bind_channel(void) {
|
||||
printf("Test: control‑key bind channel (note 0) and toggle\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_bind", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_bind:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_bind:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 0, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.1f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2000000);
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
int got_bursts = bursts;
|
||||
printf(" detected bursts: %d\n", got_bursts);
|
||||
if (got_bursts < 3) {
|
||||
fprintf(stderr, " FAIL: expected >=3 bursts, got %d\n", got_bursts);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (bind and toggle)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_bind_unbind(void) {
|
||||
printf("Test: bind to channel 5, unbind, then toggle default (channel 0)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_unbind", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_unbind:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_unbind:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 5, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 63, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.1f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 62, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2000000);
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
int got_bursts = bursts;
|
||||
printf(" detected bursts: %d\n", got_bursts);
|
||||
if (got_bursts < 3) {
|
||||
fprintf(stderr, " FAIL: expected >=3 bursts, got %d\n", got_bursts);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (unbind works, toggle channel 0)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_remove_channel(void) {
|
||||
printf("Test: dynamic channel removal via MIDI command\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_remove", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 60, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(1500000);
|
||||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
int found = 0;
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_input")) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
if (!found) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: channel1_input not created\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" channel1_input created\n");
|
||||
if (send_jack_note_on("looper:control", 61, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int still_found = 1;
|
||||
for (int retries = 0; retries < 30; retries++) {
|
||||
safe_usleep(100000);
|
||||
ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
still_found = 0;
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_input")) {
|
||||
still_found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
if (!still_found) break;
|
||||
}
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (still_found) {
|
||||
fprintf(stderr, " FAIL: channel1_input not removed after remove command\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (channel removed)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_stop_midi(void) {
|
||||
printf("Test: MIDI stop (note 65 under control key)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_stop", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_stop:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_stop:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.2f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(150000);
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(500000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 65, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
int prev = bursts;
|
||||
for (int retries = 0; retries < 20; retries++) {
|
||||
safe_usleep(100000);
|
||||
int cur = bursts;
|
||||
if (cur == prev) break;
|
||||
prev = cur;
|
||||
}
|
||||
int bursts_before = bursts;
|
||||
safe_usleep(500000);
|
||||
int bursts_after = bursts;
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (bursts_after > bursts_before + 5) {
|
||||
fprintf(stderr, " FAIL: bursts continued after stop (%d -> %d)\n",
|
||||
bursts_before, bursts_after);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (stop stopped playback)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_midi_channel_add(void) {
|
||||
printf("Test: MIDI channel creation via FIFO (add_midi)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_midi_add", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||||
if (fd < 0) {
|
||||
perror("open fifo");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
write(fd, "add_midi\n", 9);
|
||||
close(fd);
|
||||
safe_usleep(1500000);
|
||||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_MIDI_TYPE, 0);
|
||||
int found = 0;
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_midi_in")) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (!found) {
|
||||
fprintf(stderr, " FAIL: channel1_midi_in port not created\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (MIDI channel created)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_channel(void) {
|
||||
int failures = 0;
|
||||
failures += test_multiple_channels();
|
||||
failures += test_control_key_modifier();
|
||||
failures += test_bind_channel();
|
||||
failures += test_bind_unbind();
|
||||
failures += test_remove_channel();
|
||||
failures += test_stop_midi();
|
||||
failures += test_midi_channel_add();
|
||||
return failures;
|
||||
}
|
||||
160
tests/test_fifo.c
Normal file
160
tests/test_fifo.c
Normal file
@@ -0,0 +1,160 @@
|
||||
#include "test_common.h"
|
||||
|
||||
static int test_fifo_pipe(void) {
|
||||
printf("Test: FIFO pipe add/remove\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_fifo", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||||
if (fd < 0) {
|
||||
perror("open fifo");
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
write(fd, "add\n", 4);
|
||||
safe_usleep(1500000);
|
||||
const char **ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
int found = 0;
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_input")) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
write(fd, "remove\n", 7);
|
||||
close(fd);
|
||||
safe_usleep(1500000);
|
||||
ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
int still_found = 0;
|
||||
if (ports) {
|
||||
for (int i = 0; ports[i]; i++) {
|
||||
if (strstr(ports[i], "looper:channel1_input")) {
|
||||
still_found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
jack_free(ports);
|
||||
}
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (!found) {
|
||||
fprintf(stderr, " FAIL: channel not added via FIFO\n");
|
||||
return 1;
|
||||
}
|
||||
if (still_found) {
|
||||
fprintf(stderr, " FAIL: channel not removed via FIFO\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (FIFO add/remove works)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_fifo_stop_bind_unbind(void) {
|
||||
printf("Test: FIFO stop, bind, unbind\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_fifo_stop", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_fifo_stop:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_fifo_stop:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.1f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(150000);
|
||||
int fd = open("/tmp/looper_cmd", O_WRONLY);
|
||||
if (fd < 0) {
|
||||
perror("open fifo");
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
write(fd, "stop\n", 5);
|
||||
write(fd, "bind 0\n", 7);
|
||||
write(fd, "unbind\n", 7);
|
||||
close(fd);
|
||||
safe_usleep(500000);
|
||||
int bursts_after = bursts;
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (bursts_after < 1) {
|
||||
fprintf(stderr, " FAIL: no burst detected (probably no recording)\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (FIFO stop, bind, unbind executed)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_fifo(void) {
|
||||
int failures = 0;
|
||||
failures += test_fifo_pipe();
|
||||
failures += test_fifo_stop_bind_unbind();
|
||||
return failures;
|
||||
}
|
||||
190
tests/test_loop.c
Normal file
190
tests/test_loop.c
Normal file
@@ -0,0 +1,190 @@
|
||||
#include "test_common.h"
|
||||
|
||||
static int test_looper_looping(void) {
|
||||
printf("Test: loop recording and playback (expect ≥3 repetitions)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_looping", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: JACK not running?\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_looping:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_looping:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(500000);
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.1f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(150000);
|
||||
safe_usleep(800000);
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(4000000);
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
int got_bursts = bursts;
|
||||
printf(" detected bursts: %d\n", got_bursts);
|
||||
if (got_bursts < 3) {
|
||||
fprintf(stderr, " FAIL: expected ≥3 bursts, got %d\n", got_bursts);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (at least 3 repetitions)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_record_loop_stop(void) {
|
||||
printf("Test: full record‑loop‑stop (≥5 repetitions)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
if (init_persistent_midi_client() != 0) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: cannot initialise persistent MIDI client\n");
|
||||
return 1;
|
||||
}
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_full", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " SKIP: no JACK\n");
|
||||
return 1;
|
||||
}
|
||||
jack_port_t *audio_out = jack_port_register(client, "out",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
jack_port_t *audio_in = jack_port_register(client, "in",
|
||||
JACK_DEFAULT_AUDIO_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!audio_out || !audio_in) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
char my_out[64], my_in[64];
|
||||
snprintf(my_out, sizeof(my_out), "test_full:out");
|
||||
snprintf(my_in, sizeof(my_in), "test_full:in");
|
||||
if (jack_connect(client, my_out, "looper:input") ||
|
||||
jack_connect(client, "looper:output", my_in)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(500000);
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = (int)(0.5f * sr);
|
||||
bursts = 0;
|
||||
prev_above = 0;
|
||||
passthrough_output_port = audio_out;
|
||||
passthrough_input_port = audio_in;
|
||||
passthrough_phase = 0.0f;
|
||||
passthrough_freq = 440.0f;
|
||||
passthrough_sample_rate = sr;
|
||||
passthrough_total_samples = 0;
|
||||
passthrough_sum_sq = 0.0;
|
||||
passthrough_done = 0;
|
||||
jack_set_process_callback(client, passthrough_process, NULL);
|
||||
if (jack_activate(client)) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2500000);
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 65, 127) != 0) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
int total_bursts = bursts;
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
cleanup_persistent_midi_client();
|
||||
kill(pid, SIGTERM);
|
||||
waitpid(pid, NULL, 0);
|
||||
if (total_bursts < 5) {
|
||||
fprintf(stderr, " FAIL: expected ≥5 bursts, got %d\n", total_bursts);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (≥5 repetitions, stopped cleanly)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_loop(void) {
|
||||
int failures = 0;
|
||||
failures += test_looper_looping();
|
||||
failures += test_record_loop_stop();
|
||||
return failures;
|
||||
}
|
||||
0
tests/test_save.c
Normal file
0
tests/test_save.c
Normal file
0
tests/test_wav.c
Normal file
0
tests/test_wav.c
Normal file
1574
tests/unit_tests_tui.c
Normal file
1574
tests/unit_tests_tui.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user