Compare commits
19 Commits
1-multicha
...
f96d7d290d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f96d7d290d | ||
|
|
2d254c0503 | ||
|
|
4339fda529 | ||
|
|
04b59999c8 | ||
|
|
df1f4fa6bd | ||
|
|
7e5362259b | ||
|
|
b10d218749 | ||
|
|
cc50577444 | ||
|
|
346c15d1c3 | ||
|
|
7deea9266b | ||
|
|
7d842163a2 | ||
|
|
54fa307360 | ||
|
|
5430795510 | ||
|
|
5a2414b4c3 | ||
|
|
6b490ed739 | ||
|
|
72839a9e5f | ||
|
|
d6336970bf | ||
|
|
8c061f93cd | ||
| 2b4531f3f3 |
6
makefile
6
makefile
@@ -1,8 +1,8 @@
|
||||
CC ?= gcc
|
||||
CFLAGS ?= -Wall -Wextra -g -Isrc
|
||||
LDFLAGS ?= -ljack -lm
|
||||
LDFLAGS ?= -ljack -lm -lpthread
|
||||
|
||||
SRC = src/main.c src/looper.c src/channel.c src/midi.c
|
||||
SRC = src/main.c src/looper.c src/channel.c src/midi.c src/ringbuffer.c src/wav.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
looper: $(OBJ)
|
||||
@@ -12,7 +12,7 @@ src/%.o: src/%.c
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
integration: looper tests/integration.c
|
||||
$(CC) $(CFLAGS) -o integration_test tests/integration.c -ljack -lm
|
||||
$(CC) $(CFLAGS) -o integration_test tests/integration.c -ljack -lm -lpthread
|
||||
./integration_test
|
||||
|
||||
test: integration
|
||||
|
||||
@@ -28,6 +28,7 @@ void channel_add(jack_client_t *client, int idx) {
|
||||
channels[idx].loop_count = 0;
|
||||
channels[idx].record_pos = 0;
|
||||
channels[idx].playback_pos = 0;
|
||||
channels[idx].save_ring = NULL;
|
||||
|
||||
next_channel_id++;
|
||||
channel_count++;
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#define LOOP_BUF_SIZE (5 * 48000)
|
||||
#define MAX_CHANNELS 16
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
typedef enum {
|
||||
STATE_IDLE,
|
||||
STATE_RECORD,
|
||||
@@ -19,12 +21,14 @@ struct channel_t {
|
||||
atomic_int state;
|
||||
int prev_state;
|
||||
float loop_buffer[LOOP_BUF_SIZE];
|
||||
int loop_count;
|
||||
atomic_int loop_count;
|
||||
int record_pos;
|
||||
int playback_pos;
|
||||
atomic_int active;
|
||||
jack_port_t *audio_in;
|
||||
jack_port_t *audio_out;
|
||||
|
||||
_Atomic RingBuf *save_ring;
|
||||
};
|
||||
|
||||
/* Globals declared in looper.c */
|
||||
@@ -33,6 +37,8 @@ extern atomic_int channel_count;
|
||||
extern int next_channel_id;
|
||||
extern atomic_int cmd_add;
|
||||
extern atomic_int cmd_remove;
|
||||
extern atomic_int cmd_load;
|
||||
extern atomic_int cmd_save;
|
||||
|
||||
void channel_add(jack_client_t *client, int idx);
|
||||
void channel_remove(jack_client_t *client, int idx);
|
||||
|
||||
111
src/looper.c
111
src/looper.c
@@ -2,6 +2,7 @@
|
||||
#include "looper.h"
|
||||
#include "channel.h"
|
||||
#include "midi.h"
|
||||
#include "wav.h"
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <math.h>
|
||||
@@ -9,6 +10,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
/* Global state (shared across files) */
|
||||
struct channel_t channels[MAX_CHANNELS];
|
||||
@@ -16,6 +19,8 @@ atomic_int channel_count = 0;
|
||||
int next_channel_id = 1;
|
||||
atomic_int cmd_add = 0;
|
||||
atomic_int cmd_remove = 0;
|
||||
atomic_int cmd_load = 0;
|
||||
atomic_int cmd_save = 0;
|
||||
jack_port_t *midi_control_port = NULL;
|
||||
jack_port_t *midi_clock_port = NULL;
|
||||
atomic_int control_key_active = 0;
|
||||
@@ -24,6 +29,10 @@ atomic_int bind_channel = 0;
|
||||
/* Deferred removal index (1 second grace) */
|
||||
static int pending_unregister_idx = -1;
|
||||
|
||||
/* writer thread function and sample rate holder */
|
||||
static void *writer_thread(void *arg);
|
||||
static int global_sample_rate = 0;
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* process callback
|
||||
* ---------------------------------------------------------------- */
|
||||
@@ -63,11 +72,11 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
channels[c].record_pos = 0;
|
||||
channels[c].loop_count = 0;
|
||||
atomic_store(&channels[c].loop_count, 0);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
if (channels[c].record_pos > 0)
|
||||
channels[c].loop_count = channels[c].record_pos;
|
||||
if (channels[c].prev_state == STATE_RECORD && channels[c].record_pos > 0)
|
||||
atomic_store(&channels[c].loop_count, channels[c].record_pos);
|
||||
channels[c].playback_pos = 0;
|
||||
break;
|
||||
default:
|
||||
@@ -93,12 +102,13 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
break;
|
||||
|
||||
case STATE_LOOPING:
|
||||
if (channels[c].loop_count > 0) {
|
||||
int lc = atomic_load(&channels[c].loop_count);
|
||||
if (lc > 0) {
|
||||
float *outf = (float *)out;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
outf[i] = channels[c].loop_buffer[channels[c].playback_pos];
|
||||
channels[c].playback_pos =
|
||||
(channels[c].playback_pos + 1) % channels[c].loop_count;
|
||||
(channels[c].playback_pos + 1) % lc;
|
||||
}
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
@@ -118,6 +128,15 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
// push loop output into save ring if saving (atomic load)
|
||||
RingBuf *r = (RingBuf *)atomic_load_explicit(&channels[c].save_ring, memory_order_acquire);
|
||||
if (r != NULL) {
|
||||
if (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) {
|
||||
float *outf = (float *)out;
|
||||
ring_write(r, outf, nframes);
|
||||
}
|
||||
}
|
||||
|
||||
channels[c].prev_state = state;
|
||||
}
|
||||
|
||||
@@ -172,6 +191,9 @@ void jack_shutdown_cb(void *arg) {
|
||||
* looper initialisation
|
||||
* ---------------------------------------------------------------- */
|
||||
int looper_init(jack_client_t *client) {
|
||||
/* store sample rate for writer thread */
|
||||
global_sample_rate = jack_get_sample_rate(client);
|
||||
|
||||
/* channel 0 */
|
||||
channels[0].active = 1;
|
||||
atomic_store(&channels[0].state, STATE_IDLE);
|
||||
@@ -179,6 +201,7 @@ int looper_init(jack_client_t *client) {
|
||||
channels[0].loop_count = 0;
|
||||
channels[0].record_pos = 0;
|
||||
channels[0].playback_pos = 0;
|
||||
atomic_store_explicit(&channels[0].save_ring, NULL, memory_order_release);
|
||||
|
||||
channels[0].audio_in = jack_port_register(
|
||||
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
@@ -202,6 +225,45 @@ int looper_init(jack_client_t *client) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* writer thread – consumes the save ring and writes WAV file
|
||||
* ---------------------------------------------------------------- */
|
||||
static void *writer_thread(void *arg) {
|
||||
struct channel_t *ch = (struct channel_t *)arg;
|
||||
RingBuf *ring = (RingBuf *)ch->save_ring;
|
||||
if (!ring) return NULL;
|
||||
|
||||
static const char *path = "save.wav";
|
||||
unsigned sr = (unsigned)global_sample_rate;
|
||||
if (sr == 0) sr = 48000;
|
||||
|
||||
int lc = atomic_load(&ch->loop_count);
|
||||
float *outbuf = malloc((size_t)lc * sizeof(float));
|
||||
if (!outbuf) {
|
||||
ring_destroy(ring);
|
||||
free(ring);
|
||||
ch->save_ring = NULL;
|
||||
return NULL;
|
||||
}
|
||||
size_t collected = 0;
|
||||
size_t want = (size_t)lc;
|
||||
while (collected < want) {
|
||||
size_t got = ring_read(ring, outbuf + collected, want - collected);
|
||||
collected += got;
|
||||
if (got == 0) {
|
||||
struct timespec req = { .tv_sec = 0, .tv_nsec = 10000000 };
|
||||
nanosleep(&req, NULL);
|
||||
}
|
||||
}
|
||||
wav_write(path, outbuf, (unsigned)lc, sr);
|
||||
free(outbuf);
|
||||
|
||||
ring_destroy(ring);
|
||||
free(ring);
|
||||
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* main‑loop command processing
|
||||
* ---------------------------------------------------------------- */
|
||||
@@ -239,4 +301,43 @@ void looper_process_commands(jack_client_t *client) {
|
||||
pending_unregister_idx = remove_idx;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- load command ---------- */
|
||||
if (atomic_exchange(&cmd_load, 0)) {
|
||||
float *buf = NULL;
|
||||
unsigned frames = 0;
|
||||
if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) {
|
||||
if (frames > LOOP_BUF_SIZE) frames = LOOP_BUF_SIZE;
|
||||
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
|
||||
atomic_store(&channels[0].loop_count, (int)frames);
|
||||
channels[0].record_pos = 0;
|
||||
channels[0].playback_pos = 0;
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
channels[0].prev_state = -1;
|
||||
free(buf);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to load loop.wav\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- save command (writer thread) ---------- */
|
||||
if (atomic_exchange(&cmd_save, 0)) {
|
||||
int lc = atomic_load(&channels[0].loop_count);
|
||||
if (atomic_load(&channels[0].state) == STATE_LOOPING &&
|
||||
lc > 0 &&
|
||||
channels[0].save_ring == NULL) {
|
||||
RingBuf *ring = (RingBuf*)malloc(sizeof(RingBuf));
|
||||
if (ring) {
|
||||
size_t sz = (size_t)lc * 2;
|
||||
if (ring_init(ring, sz) == 0) {
|
||||
atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring, memory_order_release);
|
||||
pthread_t th;
|
||||
pthread_create(&th, NULL, writer_thread, &channels[0]);
|
||||
pthread_detach(th);
|
||||
} else {
|
||||
free(ring);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
extern atomic_int control_key_active;
|
||||
extern atomic_int cmd_add;
|
||||
extern atomic_int cmd_remove;
|
||||
extern atomic_int cmd_load;
|
||||
extern atomic_int cmd_save;
|
||||
extern atomic_int bind_channel;
|
||||
|
||||
void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
|
||||
@@ -67,6 +69,12 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
|
||||
case 63: /* unbind – reset bind to channel 0 */
|
||||
atomic_store(&bind_channel, 0);
|
||||
break;
|
||||
case 70: /* load WAV into channel 0 */
|
||||
atomic_store(&cmd_load, 1);
|
||||
break;
|
||||
case 71: /* save WAV of channel 0 */
|
||||
atomic_store(&cmd_save, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
69
src/ringbuffer.c
Normal file
69
src/ringbuffer.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "ringbuffer.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static inline size_t load_head(const RingBuf *r) {
|
||||
return atomic_load_explicit(&r->head, memory_order_relaxed);
|
||||
}
|
||||
static inline size_t load_tail(const RingBuf *r) {
|
||||
return atomic_load_explicit(&r->tail, memory_order_relaxed);
|
||||
}
|
||||
static inline void store_head(RingBuf *r, size_t v) {
|
||||
atomic_store_explicit(&r->head, v, memory_order_relaxed);
|
||||
}
|
||||
static inline void store_tail(RingBuf *r, size_t v) {
|
||||
atomic_store_explicit(&r->tail, v, memory_order_relaxed);
|
||||
}
|
||||
|
||||
int ring_init(RingBuf *r, size_t capacity) {
|
||||
r->buf = (float*)malloc(capacity * sizeof(float));
|
||||
if (!r->buf) return -1;
|
||||
r->capacity = capacity;
|
||||
store_head(r, 0);
|
||||
store_tail(r, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ring_destroy(RingBuf *r) {
|
||||
free(r->buf);
|
||||
r->buf = NULL;
|
||||
r->capacity = 0;
|
||||
}
|
||||
|
||||
size_t ring_readable(const RingBuf *r) {
|
||||
size_t h = load_head(r);
|
||||
size_t t = load_tail(r);
|
||||
if (h >= t) return h - t;
|
||||
else return r->capacity - (t - h);
|
||||
}
|
||||
|
||||
size_t ring_writeable(const RingBuf *r) {
|
||||
return r->capacity - 1 - ring_readable(r);
|
||||
}
|
||||
|
||||
size_t ring_write(RingBuf *r, const float *data, size_t count) {
|
||||
size_t avail = ring_writeable(r);
|
||||
if (count > avail) count = avail;
|
||||
if (count == 0) return 0;
|
||||
size_t head = load_head(r);
|
||||
size_t cap = r->capacity;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
r->buf[head] = data[i];
|
||||
head = (head + 1) % cap;
|
||||
}
|
||||
store_head(r, head);
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t ring_read(RingBuf *r, float *data, size_t count) {
|
||||
size_t avail = ring_readable(r);
|
||||
if (count > avail) count = avail;
|
||||
if (count == 0) return 0;
|
||||
size_t tail = load_tail(r);
|
||||
size_t cap = r->capacity;
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
data[i] = r->buf[tail];
|
||||
tail = (tail + 1) % cap;
|
||||
}
|
||||
store_tail(r, tail);
|
||||
return count;
|
||||
}
|
||||
21
src/ringbuffer.h
Normal file
21
src/ringbuffer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef RINGBUFFER_H
|
||||
#define RINGBUFFER_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
typedef struct {
|
||||
atomic_size_t head;
|
||||
atomic_size_t tail;
|
||||
size_t capacity;
|
||||
float *buf;
|
||||
} RingBuf;
|
||||
|
||||
int ring_init(RingBuf *r, size_t capacity);
|
||||
void ring_destroy(RingBuf *r);
|
||||
size_t ring_readable(const RingBuf *r);
|
||||
size_t ring_writeable(const RingBuf *r);
|
||||
size_t ring_write(RingBuf *r, const float *data, size_t count);
|
||||
size_t ring_read(RingBuf *r, float *data, size_t count);
|
||||
|
||||
#endif
|
||||
113
src/wav.c
Normal file
113
src/wav.c
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "wav.h"
|
||||
#include "channel.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static inline int read_uint16(int fd, uint16_t *v) {
|
||||
return read(fd, v, sizeof(uint16_t)) == sizeof(uint16_t) ? 0 : -1;
|
||||
}
|
||||
static inline int read_uint32(int fd, uint32_t *v) {
|
||||
return read(fd, v, sizeof(uint32_t)) == sizeof(uint32_t) ? 0 : -1;
|
||||
}
|
||||
|
||||
int wav_read(const char *path, float **buffer, unsigned *frames) {
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd < 0) return -1;
|
||||
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
|
||||
char riff[4];
|
||||
if (read(fd, riff, 4) != 4 || memcmp(riff, "RIFF", 4) != 0) { close(fd); return -1; }
|
||||
uint32_t chunk_size;
|
||||
if (read_uint32(fd, &chunk_size) != 0) { close(fd); return -1; }
|
||||
char wave[4];
|
||||
if (read(fd, wave, 4) != 4 || memcmp(wave, "WAVE", 4) != 0) { close(fd); return -1; }
|
||||
uint32_t fmt_size = 0;
|
||||
uint16_t audio_format = 0;
|
||||
uint16_t num_channels = 0;
|
||||
uint32_t sample_rate = 0;
|
||||
uint16_t bits_per_sample = 0;
|
||||
while (1) {
|
||||
char sub_id[4];
|
||||
if (read(fd, sub_id, 4) != 4) { close(fd); return -1; }
|
||||
if (read_uint32(fd, &fmt_size) != 0) { close(fd); return -1; }
|
||||
if (memcmp(sub_id, "fmt ", 4) == 0) {
|
||||
if (read_uint16(fd, &audio_format) != 0) { close(fd); return -1; }
|
||||
if (read_uint16(fd, &num_channels) != 0) { close(fd); return -1; }
|
||||
if (read_uint32(fd, &sample_rate) != 0) { close(fd); return -1; }
|
||||
if (read_uint16(fd, &bits_per_sample) != 0){ close(fd); return -1; }
|
||||
if (fmt_size > 16) lseek(fd, fmt_size - 16, SEEK_CUR);
|
||||
continue;
|
||||
}
|
||||
if (memcmp(sub_id, "data", 4) == 0) break;
|
||||
lseek(fd, fmt_size, SEEK_CUR);
|
||||
}
|
||||
if (audio_format != 1 || num_channels != 1 || bits_per_sample != 16) {
|
||||
close(fd); return -1;
|
||||
}
|
||||
unsigned max_frames = LOOP_BUF_SIZE;
|
||||
unsigned total_frames = 0;
|
||||
float *buf = (float*)malloc(max_frames * sizeof(float));
|
||||
if (!buf) { close(fd); return -1; }
|
||||
while (total_frames < max_frames) {
|
||||
int16_t sample;
|
||||
ssize_t n = read(fd, &sample, 2);
|
||||
if (n < 2) break;
|
||||
buf[total_frames++] = sample / 32768.0f;
|
||||
}
|
||||
close(fd);
|
||||
*buffer = buf;
|
||||
*frames = total_frames;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate) {
|
||||
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) return -1;
|
||||
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
|
||||
unsigned data_bytes = frames * 2;
|
||||
unsigned file_size = 44 + data_bytes;
|
||||
unsigned char header[44];
|
||||
memset(header, 0, 44);
|
||||
memcpy(header, "RIFF", 4);
|
||||
header[4] = (unsigned char)( file_size & 0xff);
|
||||
header[5] = (unsigned char)((file_size>>8) & 0xff);
|
||||
header[6] = (unsigned char)((file_size>>16) & 0xff);
|
||||
header[7] = (unsigned char)((file_size>>24) & 0xff);
|
||||
memcpy(header+8, "WAVE", 4);
|
||||
memcpy(header+12, "fmt ", 4);
|
||||
header[16]=16; header[17]=0; header[18]=0; header[19]=0;
|
||||
header[20]=1; header[21]=0;
|
||||
header[22]=1; header[23]=0;
|
||||
unsigned sr = sample_rate;
|
||||
header[24] = (unsigned char)( sr & 0xff);
|
||||
header[25] = (unsigned char)((sr>>8) & 0xff);
|
||||
header[26] = (unsigned char)((sr>>16)& 0xff);
|
||||
header[27] = (unsigned char)((sr>>24)& 0xff);
|
||||
unsigned br = sr * 2;
|
||||
header[28] = (unsigned char)( br & 0xff);
|
||||
header[29] = (unsigned char)((br>>8) & 0xff);
|
||||
header[30] = (unsigned char)((br>>16)& 0xff);
|
||||
header[31] = (unsigned char)((br>>24)& 0xff);
|
||||
header[32]=2; header[33]=0;
|
||||
header[34]=16; header[35]=0;
|
||||
memcpy(header+36, "data", 4);
|
||||
header[40] = (unsigned char)( data_bytes & 0xff);
|
||||
header[41] = (unsigned char)((data_bytes>>8) & 0xff);
|
||||
header[42] = (unsigned char)((data_bytes>>16)& 0xff);
|
||||
header[43] = (unsigned char)((data_bytes>>24)& 0xff);
|
||||
ssize_t written = write(fd, header, 44);
|
||||
if (written != 44) { close(fd); return -1; }
|
||||
for (unsigned i = 0; i < frames; ++i) {
|
||||
float s = data[i];
|
||||
if (s < -1.0f) s = -1.0f;
|
||||
if (s > 1.0f) s = 1.0f;
|
||||
int16_t sample = (int16_t)(s * 32767);
|
||||
written = write(fd, &sample, 2);
|
||||
if (written != 2) { close(fd); return -1; }
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
9
src/wav.h
Normal file
9
src/wav.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef WAV_H
|
||||
#define WAV_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int wav_read(const char *path, float **buffer, unsigned *frames);
|
||||
int wav_write(const char *path, const float *data, unsigned frames, unsigned sample_rate);
|
||||
|
||||
#endif
|
||||
@@ -56,6 +56,34 @@ static int midi_inject_process(jack_nframes_t nframes, void *arg) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Helper: initialise the persistent MIDI client (open and connect) */
|
||||
static int midi_inject_init(const char *target_port) {
|
||||
if (midi_inject_client) return 0; /* already initialised */
|
||||
jack_status_t st;
|
||||
midi_inject_client = jack_client_open("midi_inject_persistent", JackNoStartServer, &st);
|
||||
if (!midi_inject_client) return -1;
|
||||
midi_inject_port = jack_port_register(midi_inject_client, "out",
|
||||
JACK_DEFAULT_MIDI_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
if (!midi_inject_port) return -1;
|
||||
jack_set_process_callback(midi_inject_client, midi_inject_process, NULL);
|
||||
if (jack_activate(midi_inject_client) != 0) return -1;
|
||||
char src[64];
|
||||
snprintf(src, sizeof(src), "midi_inject_persistent:out");
|
||||
if (jack_connect(midi_inject_client, src, target_port) != 0) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Helper: close the persistent MIDI client */
|
||||
static void midi_inject_close(void) {
|
||||
if (midi_inject_client) {
|
||||
jack_deactivate(midi_inject_client);
|
||||
jack_client_close(midi_inject_client);
|
||||
midi_inject_client = NULL;
|
||||
midi_inject_port = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* The test code uses this callback in two ways:
|
||||
- For the audio passthrough test (existing function) it still works.
|
||||
- For the loop test we need a version that respects the static variables
|
||||
@@ -143,6 +171,8 @@ static int test_audio_pass_through(void) {
|
||||
printf("Test: audio pass‑through (connectivity)\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_passthrough", JackNoStartServer, &status);
|
||||
@@ -225,50 +255,35 @@ static int test_audio_pass_through(void) {
|
||||
|
||||
|
||||
/* Helper: open a transient JACK client, send a MIDI note‑on, close */
|
||||
static jack_client_t *midi_persistent_client = NULL;
|
||||
static jack_port_t *midi_persistent_port = NULL;
|
||||
|
||||
static int send_jack_note_on(const char *target_port, unsigned char note, unsigned char velocity) {
|
||||
/* initialise client on first call (per‑test) */
|
||||
if (midi_inject_init(target_port) != 0) return -1;
|
||||
|
||||
midi_inject_note = note;
|
||||
midi_inject_velocity = velocity;
|
||||
midi_inject_pending = 1;
|
||||
|
||||
jack_status_t st;
|
||||
midi_inject_client = jack_client_open("test_midi_inject", JackNoStartServer, &st);
|
||||
if (!midi_inject_client) return -1;
|
||||
|
||||
midi_inject_port = jack_port_register(midi_inject_client, "out",
|
||||
JACK_DEFAULT_MIDI_TYPE,
|
||||
JackPortIsOutput, 0);
|
||||
if (!midi_inject_port) {
|
||||
jack_client_close(midi_inject_client);
|
||||
midi_inject_client = NULL;
|
||||
return -1;
|
||||
}
|
||||
char src[64];
|
||||
snprintf(src, sizeof(src), "test_midi_inject:out");
|
||||
if (jack_connect(midi_inject_client, src, target_port) != 0) {
|
||||
jack_client_close(midi_inject_client);
|
||||
midi_inject_client = NULL;
|
||||
midi_inject_port = NULL;
|
||||
return -1;
|
||||
}
|
||||
midi_inject_pending = 1; /* signal before activation */
|
||||
jack_set_process_callback(midi_inject_client, midi_inject_process, NULL);
|
||||
if (jack_activate(midi_inject_client) != 0) {
|
||||
jack_client_close(midi_inject_client);
|
||||
midi_inject_client = NULL;
|
||||
midi_inject_port = NULL;
|
||||
return -1;
|
||||
}
|
||||
/* wait for the process callback to clear the flag (event delivered) */
|
||||
for (int attempts = 0; attempts < 50; attempts++) { /* ~50 * 10ms = 500ms */
|
||||
/* wait for delivery (process callback clears the flag) */
|
||||
for (int attempts = 0; attempts < 100; attempts++) {
|
||||
safe_usleep(10000);
|
||||
if (!midi_inject_pending) break;
|
||||
}
|
||||
jack_deactivate(midi_inject_client);
|
||||
jack_client_close(midi_inject_client);
|
||||
midi_inject_client = NULL;
|
||||
midi_inject_port = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* must be called after all tests */
|
||||
static void close_persistent_midi(void) {
|
||||
if (midi_persistent_client) {
|
||||
jack_deactivate(midi_persistent_client);
|
||||
jack_client_close(midi_persistent_client);
|
||||
midi_persistent_client = NULL;
|
||||
midi_persistent_port = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Full loop recording test:
|
||||
* 1. start looper
|
||||
@@ -284,6 +299,9 @@ static int test_looper_looping(void) {
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_looping", JackNoStartServer, &status);
|
||||
@@ -381,6 +399,9 @@ static int test_multiple_channels(void) {
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_multi", JackNoStartServer, &status);
|
||||
@@ -428,6 +449,8 @@ 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;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_ctrl_key", JackNoStartServer, &status);
|
||||
@@ -528,6 +551,8 @@ 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;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_bind", JackNoStartServer, &status);
|
||||
@@ -641,6 +666,8 @@ 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;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_unbind", JackNoStartServer, &status);
|
||||
@@ -769,6 +796,8 @@ 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;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_remove", JackNoStartServer, &status);
|
||||
@@ -811,7 +840,7 @@ static int test_remove_channel(void) {
|
||||
fprintf(stderr, " FAIL: send note 61 failed\n");
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(1500000);
|
||||
safe_usleep(3000000);
|
||||
/* verify channel1_input has disappeared */
|
||||
ports = jack_get_ports(client, NULL, JACK_DEFAULT_AUDIO_TYPE, 0);
|
||||
int still_found = 0;
|
||||
@@ -835,6 +864,254 @@ static int test_remove_channel(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------
|
||||
* Helper: generate a simple 440 Hz WAV file for load tests
|
||||
* ------------------------------------------------------------ */
|
||||
static int generate_test_wav(const char *path, unsigned sample_rate, unsigned duration_frames) {
|
||||
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (fd < 0) return -1;
|
||||
unsigned data_bytes = duration_frames * 2;
|
||||
unsigned file_size = 44 + data_bytes;
|
||||
unsigned char header[44];
|
||||
memset(header, 0, 44);
|
||||
memcpy(header, "RIFF", 4);
|
||||
header[4] = file_size & 0xff; header[5] = (file_size>>8)&0xff;
|
||||
header[6] = (file_size>>16)&0xff; header[7] = (file_size>>24)&0xff;
|
||||
memcpy(header+8, "WAVE", 4);
|
||||
memcpy(header+12, "fmt ", 4);
|
||||
header[16]=16; header[17]=0; header[18]=0; header[19]=0;
|
||||
header[20]=1; header[21]=0; /* PCM */
|
||||
header[22]=1; header[23]=0; /* mono */
|
||||
header[24]= sample_rate & 0xff; header[25]=(sample_rate>>8)&0xff;
|
||||
header[26]=(sample_rate>>16)&0xff; header[27]=(sample_rate>>24)&0xff;
|
||||
unsigned br = sample_rate * 2;
|
||||
header[28]= br & 0xff; header[29]=(br>>8)&0xff;
|
||||
header[30]=(br>>16)&0xff; header[31]=(br>>24)&0xff;
|
||||
header[32]=2; header[33]=0;
|
||||
header[34]=16; header[35]=0;
|
||||
memcpy(header+36, "data", 4);
|
||||
header[40]= data_bytes & 0xff; header[41]=(data_bytes>>8)&0xff;
|
||||
header[42]=(data_bytes>>16)&0xff; header[43]=(data_bytes>>24)&0xff;
|
||||
if (write(fd, header, 44) != 44) { close(fd); return -1; }
|
||||
for (unsigned i = 0; i < duration_frames; i++) {
|
||||
float sample = sinf(2.0f * (float)M_PI * 440.0f * i / sample_rate);
|
||||
int16_t s = (int16_t)(sample * 32767);
|
||||
if (write(fd, &s, 2) != 2) { close(fd); return -1; }
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------
|
||||
* Test: load WAV file (note 70 under control key)
|
||||
* ------------------------------------------------------------ */
|
||||
static int test_wav_load(void) {
|
||||
printf("Test: load WAV file into channel 0 and detect playback\n");
|
||||
if (generate_test_wav("loop.wav", 48000, 48000) != 0) {
|
||||
fprintf(stderr, " FAIL: could not create test WAV\n");
|
||||
return 1;
|
||||
}
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) { unlink("loop.wav"); return 1; }
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_wav_load", JackNoStartServer, &status);
|
||||
if (!client) {
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav");
|
||||
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);
|
||||
unlink("loop.wav");
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (jack_connect(client, "test_wav_load:out", "looper:input") ||
|
||||
jack_connect(client, "looper:output", "test_wav_load:in")) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav");
|
||||
return 1;
|
||||
}
|
||||
/* set up passthrough callback before sending load command */
|
||||
int sr = jack_get_sample_rate(client);
|
||||
continuous_sine = 0;
|
||||
beep_remaining = 0;
|
||||
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_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav");
|
||||
return 1;
|
||||
}
|
||||
/* send control key + note 70 to trigger load */
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav"); return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 70, 127) != 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav"); return 1;
|
||||
}
|
||||
/* wait for the loop to be fully loaded and playing */
|
||||
safe_usleep(3000000);
|
||||
/* continue listening for the rest of the time */
|
||||
safe_usleep(6000000); /* total 9 seconds after activation */
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
unlink("loop.wav");
|
||||
int got_bursts = bursts;
|
||||
double rms = passthrough_total_samples > 0 ?
|
||||
sqrt(passthrough_sum_sq / passthrough_total_samples) : 0.0;
|
||||
printf(" detected bursts: %d, RMS: %.6f\n", got_bursts, rms);
|
||||
if (got_bursts < 3) {
|
||||
fprintf(stderr, " FAIL: expected ≥3 bursts from loaded loop, got %d, RMS=%.6f\n", got_bursts, rms);
|
||||
return 1;
|
||||
}
|
||||
printf(" PASS (loaded loop plays)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------
|
||||
* Test: save WAV file (note 71 under control key)
|
||||
* ------------------------------------------------------------ */
|
||||
static int test_wav_save(void) {
|
||||
printf("Test: save WAV file from loop\n");
|
||||
pid_t pid = start_looper();
|
||||
if (pid < 0) return 1;
|
||||
/* ensure fresh MIDI connection for this test */
|
||||
midi_inject_close();
|
||||
jack_client_t *client;
|
||||
jack_status_t status;
|
||||
client = jack_client_open("test_wav_save", 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);
|
||||
if (jack_connect(client, "test_wav_save:out", "looper:input") ||
|
||||
jack_connect(client, "looper:output", "test_wav_save:in")) {
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
/* record a beep: send note 1 (toggle channel 0) */
|
||||
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);
|
||||
/* start generating a beep */
|
||||
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_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(800000);
|
||||
/* toggle again to stop recording and start looping */
|
||||
if (send_jack_note_on("looper:control", 1, 127) != 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(500000);
|
||||
/* send control key + note 71 to save */
|
||||
if (send_jack_note_on("looper:control", 64, 127) != 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(200000);
|
||||
if (send_jack_note_on("looper:control", 71, 127) != 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
return 1;
|
||||
}
|
||||
safe_usleep(2000000);
|
||||
/* check save.wav exists and has data */
|
||||
int fd = open("save.wav", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: save.wav not created\n");
|
||||
return 1;
|
||||
}
|
||||
unsigned char hdr[44];
|
||||
if (read(fd, hdr, 44) != 44) {
|
||||
close(fd); unlink("save.wav");
|
||||
jack_deactivate(client); jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: short header\n");
|
||||
return 1;
|
||||
}
|
||||
unsigned data_bytes = hdr[40] | (hdr[41]<<8) | (hdr[42]<<16) | (hdr[43]<<24);
|
||||
close(fd);
|
||||
if (data_bytes == 0) {
|
||||
unlink("save.wav");
|
||||
jack_deactivate(client); jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
fprintf(stderr, " FAIL: empty save.wav\n");
|
||||
return 1;
|
||||
}
|
||||
printf(" save.wav data size: %u bytes\n", data_bytes);
|
||||
unlink("save.wav");
|
||||
jack_deactivate(client);
|
||||
jack_client_close(client);
|
||||
kill(pid, SIGTERM); waitpid(pid, NULL, 0);
|
||||
printf(" PASS (save.wav created)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
/* 1. binary must exist */
|
||||
@@ -886,6 +1163,20 @@ int main(void) {
|
||||
failures++;
|
||||
}
|
||||
|
||||
/* 10. Test WAV load */
|
||||
if (test_wav_load() != 0) {
|
||||
fprintf(stderr, " FAILED\n");
|
||||
failures++;
|
||||
}
|
||||
|
||||
/* 11. Test WAV save */
|
||||
if (test_wav_save() != 0) {
|
||||
fprintf(stderr, " FAILED\n");
|
||||
failures++;
|
||||
}
|
||||
|
||||
close_persistent_midi();
|
||||
|
||||
if (failures > 0) {
|
||||
fprintf(stderr, "%d test(s) FAILED\n", failures);
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user