refactor: rename looper ports to ch0in/ch0out and move connection logic to client

This commit is contained in:
Loic Coenen
2026-05-31 13:05:28 +00:00
committed by Loic Coenen (aider)
parent 316320c294
commit 20176517a4
7 changed files with 31 additions and 107 deletions

View File

@@ -16,8 +16,8 @@ void init_scene(scene_t *sc) {
void channel_add(jack_client_t *client, int idx) {
char in_name[64], out_name[64];
pid_t pid = getpid();
snprintf(in_name, sizeof(in_name), "ch%din_%d", next_channel_id, (int)pid);
snprintf(out_name, sizeof(out_name), "ch%dout_%d", next_channel_id, (int)pid);
snprintf(in_name, sizeof(in_name), "ch%din", next_channel_id);
snprintf(out_name, sizeof(out_name), "ch%dout", next_channel_id);
/* Always register audio ports (needed for pass-through even for MIDI
* channels?) */
@@ -36,10 +36,10 @@ void channel_add(jack_client_t *client, int idx) {
/* If this is a MIDI channel, register MIDI ports */
if (channels[idx].type == CHANNEL_MIDI) {
char midi_in_name[64], midi_out_name[64];
snprintf(midi_in_name, sizeof(midi_in_name), "ch%dmidiin_%d",
next_channel_id, (int)pid);
snprintf(midi_out_name, sizeof(midi_out_name), "ch%dmidiout_%d",
next_channel_id, (int)pid);
snprintf(midi_in_name, sizeof(midi_in_name), "ch%dmidiin",
next_channel_id);
snprintf(midi_out_name, sizeof(midi_out_name), "ch%dmidiout",
next_channel_id);
channels[idx].midi_in = jack_port_register(
client, midi_in_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
channels[idx].midi_out = jack_port_register(

View File

@@ -590,9 +590,9 @@ int looper_init(jack_client_t *client) {
atomic_store(&channels[0].save_complete, 0);
channels[0].audio_in = jack_port_register(
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
client, "ch0in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
channels[0].audio_out = jack_port_register(
client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
client, "ch0out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!channels[0].audio_in || !channels[0].audio_out) {
fprintf(stderr, "Could not create audio ports for channel 0\n");
return -1;

View File

@@ -10,17 +10,10 @@
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <jack/jack.h>
#define FIFO_PATH "/tmp/looper_cmd"
#define LINE_MAX 256
/* Global JACK client (from looper.c) */
extern jack_client_t *global_client;
/* Stored ports for from/to */
static char fifo_from[256] = "";
static char fifo_to[256] = "";
/* Filename for the next load command (default "loop.wav") */
char load_filename[256] = "loop.wav";
@@ -105,75 +98,6 @@ static void *pipe_thread_func(void *arg) {
command_t cmd = {.type = CMD_SAVE, .channel = -1, .data = 0};
queue_push(&cmd_queue_main_fifo, cmd);
}
// --- from <port> ---
else if (strncmp(line, "from ", 5) == 0) {
fprintf(stderr, "FIFO RECEIVED from: %s\n", line + 5);
strncpy(fifo_from, line + 5, sizeof(fifo_from)-1);
fifo_from[sizeof(fifo_from)-1] = '\0';
// Immediately connect source to looper:input (independently of :to)
if (global_client) {
const char *target = "looper:input";
int ret = jack_connect(global_client, fifo_from, target);
if (ret != 0) {
fprintf(stderr, "Failed to connect %s -> %s (ret=%d), retrying...\n", fifo_from, target, ret);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 500000000};
nanosleep(&ts, NULL);
ret = jack_connect(global_client, fifo_from, target);
if (ret != 0)
fprintf(stderr, "Retry also failed %s -> %s (ret=%d)\n", fifo_from, target, ret);
}
}
}
// --- to <port> ---
else if (strncmp(line, "to ", 3) == 0) {
fprintf(stderr, "FIFO RECEIVED to: %s\n", line + 3);
strncpy(fifo_to, line + 3, sizeof(fifo_to)-1);
fifo_to[sizeof(fifo_to)-1] = '\0';
// Immediately connect looper:output to target (independently of :from)
if (global_client) {
const char *source = "looper:output";
int ret = jack_connect(global_client, source, fifo_to);
if (ret != 0) {
fprintf(stderr, "Failed to connect %s -> %s (ret=%d), retrying...\n", source, fifo_to, ret);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 500000000};
nanosleep(&ts, NULL);
ret = jack_connect(global_client, source, fifo_to);
if (ret != 0)
fprintf(stderr, "Retry also failed %s -> %s (ret=%d)\n", source, fifo_to, ret);
}
}
}
// --- connect [from] [to] ---
else if (strncmp(line, "connect", 7) == 0) {
char from[256] = "";
char to[256] = "";
// parse optional arguments: "connect from to"
char *p = line + 7;
while (*p == ' ') p++;
if (*p) {
char *space = strchr(p, ' ');
if (space) {
strncpy(from, p, space - p); from[space-p] = '\0';
strncpy(to, space+1, sizeof(to)-1);
} else {
strncpy(from, p, sizeof(from)-1);
}
}
// fallback to stored ports
if (!from[0]) strncpy(from, fifo_from, sizeof(from)-1);
if (!to[0]) strncpy(to, fifo_to, sizeof(to)-1);
if (from[0] && to[0] && global_client) {
int ret = jack_connect(global_client, from, to);
if (ret != 0) {
fprintf(stderr, "Failed to connect %s -> %s (ret=%d), retrying...\n", from, to, ret);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 500000000};
nanosleep(&ts, NULL);
ret = jack_connect(global_client, from, to);
if (ret != 0)
fprintf(stderr, "Retry also failed %s -> %s (ret=%d)\n", from, to, ret);
}
}
}
/* ignore unknown lines */
}
/* EOF all writers closed, reopen for next connection */