Merge branch '3-integrate-carla'
This commit is contained in:
BIN
engine/integration_test
Executable file
BIN
engine/integration_test
Executable file
Binary file not shown.
BIN
engine/looper
Executable file
BIN
engine/looper
Executable file
Binary file not shown.
@@ -5,11 +5,19 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Helper: zero a scene and set its state to IDLE */
|
||||
void init_scene(scene_t *sc) {
|
||||
memset(sc, 0, sizeof(scene_t));
|
||||
atomic_store(&sc->state, STATE_IDLE);
|
||||
atomic_store(&sc->prev_state, -1);
|
||||
}
|
||||
|
||||
void channel_add(jack_client_t *client, int idx) {
|
||||
char in_name[64], out_name[64];
|
||||
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
|
||||
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
|
||||
|
||||
/* Always register audio ports (needed for pass-through even for MIDI channels?) */
|
||||
channels[idx].audio_in = jack_port_register(
|
||||
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
channels[idx].audio_out = jack_port_register(
|
||||
@@ -22,13 +30,36 @@ void channel_add(jack_client_t *client, int idx) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* If this is a MIDI channel, register MIDI ports */
|
||||
if (channels[idx].type == CHANNEL_MIDI) {
|
||||
char midi_in_name[64], midi_out_name[64];
|
||||
snprintf(midi_in_name, sizeof(midi_in_name), "channel%d_midi_in", next_channel_id);
|
||||
snprintf(midi_out_name, sizeof(midi_out_name), "channel%d_midi_out", next_channel_id);
|
||||
channels[idx].midi_in = jack_port_register(
|
||||
client, midi_in_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
|
||||
channels[idx].midi_out = jack_port_register(
|
||||
client, midi_out_name, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
|
||||
if (!channels[idx].midi_in || !channels[idx].midi_out) {
|
||||
fprintf(stderr, "Failed to register MIDI ports for channel %d\n",
|
||||
next_channel_id);
|
||||
atomic_store(&channels[idx].active, 0);
|
||||
jack_port_unregister(client, channels[idx].audio_in);
|
||||
jack_port_unregister(client, channels[idx].audio_out);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
channels[idx].midi_in = NULL;
|
||||
channels[idx].midi_out = NULL;
|
||||
}
|
||||
|
||||
atomic_store(&channels[idx].active, 1);
|
||||
atomic_store(&channels[idx].state, STATE_IDLE);
|
||||
channels[idx].prev_state = -1;
|
||||
channels[idx].loop_count = 0;
|
||||
channels[idx].record_pos = 0;
|
||||
channels[idx].playback_pos = 0;
|
||||
/* Initialise first scene */
|
||||
channels[idx].scene_count = 1;
|
||||
channels[idx].current_scene = 0;
|
||||
init_scene(&channels[idx].scenes[0]);
|
||||
|
||||
channels[idx].save_ring = NULL;
|
||||
atomic_store(&channels[idx].save_complete, 0);
|
||||
|
||||
next_channel_id++;
|
||||
channel_count++;
|
||||
@@ -37,5 +68,61 @@ void channel_add(jack_client_t *client, int idx) {
|
||||
void channel_remove(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
atomic_store(&channels[idx].active, 0);
|
||||
channel_count--;
|
||||
atomic_fetch_sub(&channel_count, 1);
|
||||
}
|
||||
|
||||
void channel_add_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
if (atomic_load(&channels[idx].scene_count) >= MAX_SCENES)
|
||||
return;
|
||||
int ns = atomic_load(&channels[idx].scene_count);
|
||||
init_scene(&channels[idx].scenes[ns]);
|
||||
atomic_fetch_add(&channels[idx].scene_count, 1);
|
||||
}
|
||||
|
||||
void channel_remove_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
int sc = atomic_load(&channels[idx].scene_count);
|
||||
if (sc <= 1)
|
||||
return;
|
||||
int cs = atomic_load(&channels[idx].current_scene);
|
||||
/* shift remaining scenes down (atomic copy of fields) */
|
||||
for (int i = cs; i < sc - 1; i++) {
|
||||
atomic_store(&channels[idx].scenes[i].loop_count,
|
||||
atomic_load(&channels[idx].scenes[i + 1].loop_count));
|
||||
atomic_store(&channels[idx].scenes[i].record_pos,
|
||||
atomic_load(&channels[idx].scenes[i + 1].record_pos));
|
||||
atomic_store(&channels[idx].scenes[i].playback_pos,
|
||||
atomic_load(&channels[idx].scenes[i + 1].playback_pos));
|
||||
atomic_store(&channels[idx].scenes[i].state,
|
||||
atomic_load(&channels[idx].scenes[i + 1].state));
|
||||
atomic_store(&channels[idx].scenes[i].prev_state,
|
||||
atomic_load(&channels[idx].scenes[i + 1].prev_state));
|
||||
/* copy loop data (may race with RT thread; acceptable for this release) */
|
||||
memcpy(channels[idx].scenes[i].loop.audio_buffer,
|
||||
channels[idx].scenes[i + 1].loop.audio_buffer,
|
||||
LOOP_BUF_SIZE * sizeof(float));
|
||||
}
|
||||
atomic_fetch_sub(&channels[idx].scene_count, 1);
|
||||
int new_sc = atomic_load(&channels[idx].scene_count);
|
||||
if (cs >= new_sc)
|
||||
atomic_store(&channels[idx].current_scene, new_sc - 1);
|
||||
}
|
||||
|
||||
void channel_next_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
int sc = atomic_load(&channels[idx].scene_count);
|
||||
if (sc > 1) {
|
||||
int cs = atomic_load(&channels[idx].current_scene);
|
||||
atomic_store(&channels[idx].current_scene, (cs + 1) % sc);
|
||||
}
|
||||
}
|
||||
|
||||
void channel_prev_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
int sc = atomic_load(&channels[idx].scene_count);
|
||||
if (sc > 1) {
|
||||
int cs = atomic_load(&channels[idx].current_scene);
|
||||
atomic_store(&channels[idx].current_scene, (cs - 1 + sc) % sc);
|
||||
}
|
||||
}
|
||||
|
||||
117
engine/src/channel.c~
Normal file
117
engine/src/channel.c~
Normal file
@@ -0,0 +1,117 @@
|
||||
// cppcheck-suppress missingIncludeSystem
|
||||
#include "channel.h"
|
||||
#include <jack/jack.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
/* Helper: zero a scene and set its state to IDLE */
|
||||
static void init_scene(scene_t *sc) {
|
||||
memset(sc, 0, sizeof(scene_t));
|
||||
atomic_store(&sc->state, STATE_IDLE);
|
||||
atomic_store(&sc->prev_state, -1);
|
||||
}
|
||||
|
||||
>>>>>>> 3-integrate-carla
|
||||
void channel_add(jack_client_t *client, int idx) {
|
||||
char in_name[64], out_name[64];
|
||||
snprintf(in_name, sizeof(in_name), "channel%d_input", next_channel_id);
|
||||
snprintf(out_name, sizeof(out_name), "channel%d_output", next_channel_id);
|
||||
|
||||
channels[idx].audio_in = jack_port_register(
|
||||
client, in_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
channels[idx].audio_out = jack_port_register(
|
||||
client, out_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
|
||||
if (!channels[idx].audio_in || !channels[idx].audio_out) {
|
||||
fprintf(stderr, "Failed to register ports for channel %d\n",
|
||||
next_channel_id);
|
||||
/* Do NOT mark channel active – process loop will skip it */
|
||||
atomic_store(&channels[idx].active, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
atomic_store(&channels[idx].active, 1);
|
||||
atomic_store(&channels[idx].state, STATE_IDLE);
|
||||
channels[idx].prev_state = -1;
|
||||
channels[idx].loop_count = 0;
|
||||
channels[idx].record_pos = 0;
|
||||
channels[idx].playback_pos = 0;
|
||||
channels[idx].save_ring = NULL;
|
||||
|
||||
next_channel_id++;
|
||||
channel_count++;
|
||||
}
|
||||
|
||||
void channel_remove(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
<<<<<<< HEAD
|
||||
atomic_store(&channels[idx].active, 0);
|
||||
channel_count--;
|
||||
=======
|
||||
struct channel_t *cur = get_channels_array();
|
||||
atomic_store(&cur[idx].active, 0);
|
||||
atomic_fetch_sub(&channel_count, 1);
|
||||
}
|
||||
|
||||
void channel_add_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
struct channel_t *cur = get_channels_array();
|
||||
if (atomic_load(&cur[idx].scene_count) >= MAX_SCENES)
|
||||
return;
|
||||
int ns = atomic_load(&cur[idx].scene_count);
|
||||
init_scene(&cur[idx].scenes[ns]);
|
||||
atomic_fetch_add(&cur[idx].scene_count, 1);
|
||||
}
|
||||
|
||||
void channel_remove_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int sc = atomic_load(&cur[idx].scene_count);
|
||||
if (sc <= 1)
|
||||
return;
|
||||
int cs = atomic_load(&cur[idx].current_scene);
|
||||
/* shift remaining scenes down (atomic copy of fields) */
|
||||
for (int i = cs; i < sc - 1; i++) {
|
||||
atomic_store(&cur[idx].scenes[i].loop_count,
|
||||
atomic_load(&cur[idx].scenes[i + 1].loop_count));
|
||||
atomic_store(&cur[idx].scenes[i].record_pos,
|
||||
atomic_load(&cur[idx].scenes[i + 1].record_pos));
|
||||
atomic_store(&cur[idx].scenes[i].playback_pos,
|
||||
atomic_load(&cur[idx].scenes[i + 1].playback_pos));
|
||||
atomic_store(&cur[idx].scenes[i].state,
|
||||
atomic_load(&cur[idx].scenes[i + 1].state));
|
||||
atomic_store(&cur[idx].scenes[i].prev_state,
|
||||
atomic_load(&cur[idx].scenes[i + 1].prev_state));
|
||||
/* copy loop data (may race with RT thread; acceptable for this release) */
|
||||
memcpy(cur[idx].scenes[i].loop.audio_buffer,
|
||||
cur[idx].scenes[i + 1].loop.audio_buffer,
|
||||
LOOP_BUF_SIZE * sizeof(float));
|
||||
}
|
||||
atomic_fetch_sub(&cur[idx].scene_count, 1);
|
||||
int new_sc = atomic_load(&cur[idx].scene_count);
|
||||
if (cs >= new_sc)
|
||||
atomic_store(&cur[idx].current_scene, new_sc - 1);
|
||||
}
|
||||
|
||||
void channel_next_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int sc = atomic_load(&cur[idx].scene_count);
|
||||
if (sc > 1) {
|
||||
int cs = atomic_load(&cur[idx].current_scene);
|
||||
atomic_store(&cur[idx].current_scene, (cs + 1) % sc);
|
||||
}
|
||||
}
|
||||
|
||||
void channel_prev_scene(jack_client_t *client, int idx) {
|
||||
(void)client;
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int sc = atomic_load(&cur[idx].scene_count);
|
||||
if (sc > 1) {
|
||||
int cs = atomic_load(&cur[idx].current_scene);
|
||||
atomic_store(&cur[idx].current_scene, (cs - 1 + sc) % sc);
|
||||
}
|
||||
>>>>>>> 3-integrate-carla
|
||||
}
|
||||
@@ -5,11 +5,15 @@
|
||||
#include <jack/jack.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#define MAX_SCENES 4
|
||||
#define LOOP_BUF_SIZE (5 * 48000)
|
||||
#define MAX_MIDI_EVENTS 1024
|
||||
#define MAX_CHANNELS 16
|
||||
|
||||
#include "ringbuffer.h"
|
||||
|
||||
typedef enum { CHANNEL_AUDIO, CHANNEL_MIDI } channel_type_t;
|
||||
|
||||
typedef enum {
|
||||
STATE_IDLE,
|
||||
STATE_RECORD,
|
||||
@@ -17,18 +21,43 @@ typedef enum {
|
||||
STATE_PAUSED
|
||||
} looper_state;
|
||||
|
||||
/* Structure for a recorded or playing MIDI event */
|
||||
typedef struct {
|
||||
jack_nframes_t timestamp;
|
||||
unsigned char status;
|
||||
unsigned char note;
|
||||
unsigned char velocity;
|
||||
} midi_event_t;
|
||||
|
||||
/* Loop data for a scene */
|
||||
typedef struct {
|
||||
float audio_buffer[LOOP_BUF_SIZE];
|
||||
midi_event_t midi_events[MAX_MIDI_EVENTS];
|
||||
} loop_data_t;
|
||||
|
||||
/* A single scene within a channel */
|
||||
typedef struct {
|
||||
atomic_int state;
|
||||
atomic_int prev_state;
|
||||
atomic_int loop_count;
|
||||
atomic_int record_pos;
|
||||
atomic_int playback_pos;
|
||||
loop_data_t loop;
|
||||
} scene_t;
|
||||
|
||||
struct channel_t {
|
||||
atomic_int state;
|
||||
atomic_int prev_state;
|
||||
float loop_buffer[LOOP_BUF_SIZE];
|
||||
atomic_int loop_count;
|
||||
atomic_int record_pos;
|
||||
atomic_int playback_pos;
|
||||
channel_type_t type; /* AUDIO or MIDI */
|
||||
atomic_int active;
|
||||
jack_port_t *audio_in;
|
||||
jack_port_t *audio_out;
|
||||
jack_port_t *midi_in; /* NULL for audio channels */
|
||||
jack_port_t *midi_out;
|
||||
int scene_count; /* number of scenes (max MAX_SCENES) */
|
||||
int current_scene; /* index of currently active scene */
|
||||
scene_t scenes[MAX_SCENES];
|
||||
|
||||
_Atomic RingBuf *save_ring;
|
||||
atomic_int save_complete; /* 1 when writer is done; RT thread must stop writing */
|
||||
};
|
||||
|
||||
/* Globals declared in looper.c */
|
||||
@@ -41,7 +70,12 @@ extern atomic_int cmd_remove;
|
||||
extern atomic_int cmd_load;
|
||||
extern atomic_int cmd_save;
|
||||
|
||||
void init_scene(scene_t *sc);
|
||||
void channel_add(jack_client_t *client, int idx);
|
||||
void channel_remove(jack_client_t *client, int idx);
|
||||
void channel_add_scene(jack_client_t *client, int idx);
|
||||
void channel_remove_scene(jack_client_t *client, int idx);
|
||||
void channel_next_scene(jack_client_t *client, int idx);
|
||||
void channel_prev_scene(jack_client_t *client, int idx);
|
||||
|
||||
#endif
|
||||
|
||||
BIN
engine/src/channel.o
Normal file
BIN
engine/src/channel.o
Normal file
Binary file not shown.
@@ -2,13 +2,11 @@
|
||||
#include "looper.h"
|
||||
#include "channel.h"
|
||||
#include "midi.h"
|
||||
#include "queue.h"
|
||||
#include "wav.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "pipe.h"
|
||||
#include <jack/jack.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
@@ -16,11 +14,10 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "queue.h"
|
||||
#include "command.h"
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Global command queues */
|
||||
/* Global command queues (used by midi.c and pipe.c) */
|
||||
spsc_queue_t cmd_queue;
|
||||
spsc_queue_t cmd_queue_main_midi;
|
||||
spsc_queue_t cmd_queue_main_fifo;
|
||||
@@ -31,29 +28,28 @@ spsc_queue_t cmd_queue_main_fifo;
|
||||
static int status_fd = -1;
|
||||
|
||||
static void looper_write_status(void) {
|
||||
if (status_fd < 0)
|
||||
return;
|
||||
char buf[256];
|
||||
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
||||
if (!atomic_load(&channels[ch].active))
|
||||
continue;
|
||||
int state_val = atomic_load(&channels[ch].state);
|
||||
const char *state_str;
|
||||
switch (state_val) {
|
||||
case STATE_IDLE: state_str = "IDLE"; break;
|
||||
case STATE_RECORD: state_str = "RECORD"; break;
|
||||
case STATE_LOOPING: state_str = "LOOPING"; break;
|
||||
case STATE_PAUSED: state_str = "PAUSED"; break;
|
||||
default: state_str = "UNKNOWN";
|
||||
}
|
||||
int n = snprintf(buf, sizeof(buf),
|
||||
"CH=%d SC=%d STATE=%s\n",
|
||||
ch, 0, state_str);
|
||||
if (n > 0) {
|
||||
int ret = write(status_fd, buf, n);
|
||||
(void)ret;
|
||||
}
|
||||
if (status_fd < 0)
|
||||
return;
|
||||
char buf[256];
|
||||
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
||||
if (!atomic_load(&channels[ch].active))
|
||||
continue;
|
||||
int sc_idx = atomic_load(&channels[ch].current_scene);
|
||||
int state = atomic_load(&channels[ch].scenes[sc_idx].state);
|
||||
const char *state_str;
|
||||
switch (state) {
|
||||
case STATE_IDLE: state_str = "IDLE"; break;
|
||||
case STATE_RECORD: state_str = "RECORD"; break;
|
||||
case STATE_LOOPING:state_str = "LOOPING";break;
|
||||
case STATE_PAUSED: state_str = "PAUSED"; break;
|
||||
default: state_str = "UNKNOWN";
|
||||
}
|
||||
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx, state_str);
|
||||
if (n > 0) {
|
||||
int ret = write(status_fd, buf, n);
|
||||
(void)ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Global state (shared across files) */
|
||||
@@ -84,27 +80,30 @@ static void exec_command(command_t cmd, jack_client_t *client) {
|
||||
|
||||
switch (cmd.type) {
|
||||
case CMD_CYCLE: {
|
||||
int state = atomic_load(&channels[ch].state);
|
||||
int sc_idx = atomic_load(&channels[ch].current_scene);
|
||||
int state = atomic_load(&channels[ch].scenes[sc_idx].state);
|
||||
switch (state) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[ch].state, STATE_RECORD);
|
||||
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[ch].state, STATE_LOOPING);
|
||||
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[ch].state, STATE_PAUSED);
|
||||
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[ch].state, STATE_LOOPING);
|
||||
atomic_store(&channels[ch].scenes[sc_idx].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
atomic_store(&channels[ch].prev_state, -1);
|
||||
atomic_store(&channels[ch].scenes[sc_idx].prev_state, -1);
|
||||
break;
|
||||
}
|
||||
case CMD_STOP:
|
||||
atomic_store(&channels[ch].state, STATE_IDLE);
|
||||
atomic_store(&channels[ch].prev_state, -1);
|
||||
for (int s = 0; s < atomic_load(&channels[ch].scene_count); s++) {
|
||||
atomic_store(&channels[ch].scenes[s].state, STATE_IDLE);
|
||||
atomic_store(&channels[ch].scenes[s].prev_state, -1);
|
||||
}
|
||||
break;
|
||||
|
||||
case CMD_ADD_CHANNEL:
|
||||
@@ -147,9 +146,19 @@ static void exec_command(command_t cmd, jack_client_t *client) {
|
||||
break;
|
||||
|
||||
case CMD_ADD_SCENE:
|
||||
channel_add_scene(client, ch);
|
||||
break;
|
||||
|
||||
case CMD_REMOVE_SCENE:
|
||||
channel_remove_scene(client, ch);
|
||||
break;
|
||||
|
||||
case CMD_NEXT_SCENE:
|
||||
channel_next_scene(client, ch);
|
||||
break;
|
||||
|
||||
case CMD_PREV_SCENE:
|
||||
channel_prev_scene(client, ch);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -181,45 +190,113 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const jack_default_audio_sample_t *in =
|
||||
(const jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_in, nframes);
|
||||
jack_default_audio_sample_t *out =
|
||||
(jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_out, nframes);
|
||||
if (!out)
|
||||
continue;
|
||||
/* For each channel, use the current scene */
|
||||
int sc_idx = atomic_load(&channels[c].current_scene);
|
||||
scene_t *sc = &channels[c].scenes[sc_idx];
|
||||
int state = atomic_load(&sc->state);
|
||||
int prev = atomic_load(&sc->prev_state);
|
||||
|
||||
int state = atomic_load(&channels[c].state);
|
||||
|
||||
if (state != atomic_load(&channels[c].prev_state)) {
|
||||
if (state != prev) {
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[c].record_pos, 0);
|
||||
atomic_store(&channels[c].loop_count, 0);
|
||||
atomic_store(&sc->record_pos, 0);
|
||||
atomic_store(&sc->loop_count, 0);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
if (atomic_load(&channels[c].prev_state) == STATE_RECORD &&
|
||||
atomic_load(&channels[c].record_pos) > 0)
|
||||
atomic_store(&channels[c].loop_count,
|
||||
atomic_load(&channels[c].record_pos));
|
||||
atomic_store(&channels[c].playback_pos, 0);
|
||||
if (prev == STATE_RECORD && atomic_load(&sc->record_pos) > 0)
|
||||
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
|
||||
atomic_store(&sc->playback_pos, 0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
jack_nframes_t i;
|
||||
/* Handle MIDI channels separately */
|
||||
if (channels[c].type == CHANNEL_MIDI) {
|
||||
/* MIDI channel handling */
|
||||
void *midi_in_buf = jack_port_get_buffer(channels[c].midi_in, nframes);
|
||||
void *midi_out_buf = jack_port_get_buffer(channels[c].midi_out, nframes);
|
||||
if (!midi_out_buf) continue;
|
||||
|
||||
switch (state) {
|
||||
case STATE_RECORD: {
|
||||
if (midi_in_buf) {
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
|
||||
jack_midi_event_t ev;
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
int rp = atomic_load(&sc->record_pos);
|
||||
if (rp < MAX_MIDI_EVENTS) {
|
||||
sc->loop.midi_events[rp].timestamp = ev.time;
|
||||
sc->loop.midi_events[rp].status = ev.buffer[0];
|
||||
sc->loop.midi_events[rp].note = (ev.size > 1) ? ev.buffer[1] : 0;
|
||||
sc->loop.midi_events[rp].velocity = (ev.size > 2) ? ev.buffer[2] : 0;
|
||||
atomic_store(&sc->record_pos, rp + 1);
|
||||
}
|
||||
}
|
||||
/* forward incoming MIDI to output during record */
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_LOOPING: {
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
int cnt = atomic_load(&sc->loop_count);
|
||||
if (cnt > 0) {
|
||||
for (int e = 0; e < cnt; e++) {
|
||||
unsigned char msg[3];
|
||||
msg[0] = sc->loop.midi_events[e].status;
|
||||
msg[1] = sc->loop.midi_events[e].note;
|
||||
msg[2] = sc->loop.midi_events[e].velocity;
|
||||
jack_midi_event_write(midi_out_buf, 0, msg, 3);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_PAUSED:
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
break;
|
||||
default: /* IDLE */
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
if (midi_in_buf) {
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
|
||||
jack_midi_event_t ev;
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Audio channel handling */
|
||||
const jack_default_audio_sample_t *in =
|
||||
(const jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_in, nframes);
|
||||
jack_default_audio_sample_t *out =
|
||||
(jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_out, nframes);
|
||||
if (!out) continue;
|
||||
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
if (in) {
|
||||
float *f_out = (float *)out;
|
||||
const float *f_in = (const float *)in;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
int rp = atomic_fetch_add(&channels[c].record_pos, 1);
|
||||
for (jack_nframes_t i = 0; i < nframes; i++) {
|
||||
int rp = atomic_fetch_add(&sc->record_pos, 1);
|
||||
if (rp < LOOP_BUF_SIZE)
|
||||
channels[c].loop_buffer[rp] = f_in[i];
|
||||
sc->loop.audio_buffer[rp] = f_in[i];
|
||||
f_out[i] = f_in[i];
|
||||
}
|
||||
} else {
|
||||
@@ -227,19 +304,21 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_LOOPING:
|
||||
int lc = atomic_load(&channels[c].loop_count);
|
||||
if (lc > 0) {
|
||||
case STATE_LOOPING: {
|
||||
int loop_cnt = atomic_load(&sc->loop_count);
|
||||
if (loop_cnt > 0) {
|
||||
float *outf = (float *)out;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
int pp = atomic_load(&channels[c].playback_pos);
|
||||
outf[i] = channels[c].loop_buffer[pp];
|
||||
atomic_store(&channels[c].playback_pos, (pp + 1) % lc);
|
||||
int pp = atomic_load(&sc->playback_pos);
|
||||
for (jack_nframes_t i = 0; i < nframes; i++) {
|
||||
outf[i] = sc->loop.audio_buffer[pp];
|
||||
pp = (pp + 1) % loop_cnt;
|
||||
}
|
||||
atomic_store(&sc->playback_pos, pp);
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case STATE_PAUSED:
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
@@ -254,20 +333,20 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
break;
|
||||
}
|
||||
|
||||
// push loop output into save ring if saving (atomic load)
|
||||
/* 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) {
|
||||
if (r != NULL && !atomic_load(&channels[c].save_complete)) {
|
||||
if (state == STATE_LOOPING && atomic_load(&sc->loop_count) > 0) {
|
||||
const float *outf = (const float *)out;
|
||||
ring_write(r, outf, nframes);
|
||||
}
|
||||
}
|
||||
|
||||
atomic_store(&channels[c].prev_state, state);
|
||||
atomic_store(&sc->prev_state, state);
|
||||
}
|
||||
|
||||
/* MIDI clock events – affect channel 0 only */
|
||||
/* MIDI clock events – affect current scene of channel 0 */
|
||||
if (midi_clock_port) {
|
||||
void *midi_clock_buf = jack_port_get_buffer(midi_clock_port, nframes);
|
||||
if (midi_clock_buf) {
|
||||
@@ -280,18 +359,22 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
unsigned char msg = cev.buffer[0];
|
||||
switch (msg) {
|
||||
case 0xFA: {
|
||||
int s = atomic_load(&channels[0].state);
|
||||
int sc0 = atomic_load(&channels[0].current_scene);
|
||||
int s = atomic_load(&channels[0].scenes[sc0].state);
|
||||
if (s == STATE_IDLE)
|
||||
atomic_store(&channels[0].state, STATE_RECORD);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_RECORD);
|
||||
break;
|
||||
}
|
||||
case 0xFC:
|
||||
atomic_store(&channels[0].state, STATE_IDLE);
|
||||
case 0xFC: {
|
||||
int sc0 = atomic_load(&channels[0].current_scene);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_IDLE);
|
||||
break;
|
||||
}
|
||||
case 0xFB: {
|
||||
int s = atomic_load(&channels[0].state);
|
||||
int sc0 = atomic_load(&channels[0].current_scene);
|
||||
int s = atomic_load(&channels[0].scenes[sc0].state);
|
||||
if (s == STATE_PAUSED)
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -341,12 +424,15 @@ int looper_init(jack_client_t *client) {
|
||||
|
||||
/* channel 0 */
|
||||
channels[0].active = 1;
|
||||
atomic_store(&channels[0].state, STATE_IDLE);
|
||||
atomic_store(&channels[0].prev_state, -1);
|
||||
channels[0].loop_count = 0;
|
||||
atomic_store(&channels[0].record_pos, 0);
|
||||
atomic_store(&channels[0].playback_pos, 0);
|
||||
channels[0].type = CHANNEL_AUDIO; /* default */
|
||||
channels[0].current_scene = 0;
|
||||
channels[0].scene_count = 1;
|
||||
init_scene(&channels[0].scenes[0]); /* sets state IDLE, prev_state -1 */
|
||||
atomic_store(&channels[0].scenes[0].loop_count, 0);
|
||||
atomic_store(&channels[0].scenes[0].record_pos, 0);
|
||||
atomic_store(&channels[0].scenes[0].playback_pos, 0);
|
||||
atomic_store_explicit(&channels[0].save_ring, NULL, memory_order_release);
|
||||
atomic_store(&channels[0].save_complete, 0);
|
||||
|
||||
channels[0].audio_in = jack_port_register(
|
||||
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
@@ -375,6 +461,8 @@ int looper_init(jack_client_t *client) {
|
||||
* ---------------------------------------------------------------- */
|
||||
static void *writer_thread(void *arg) {
|
||||
struct channel_t *ch = (struct channel_t *)arg;
|
||||
int sc_idx = atomic_load(&ch->current_scene);
|
||||
scene_t *sc = &ch->scenes[sc_idx];
|
||||
RingBuf *ring = (RingBuf *)ch->save_ring;
|
||||
if (!ring)
|
||||
return NULL;
|
||||
@@ -384,7 +472,7 @@ static void *writer_thread(void *arg) {
|
||||
if (sr == 0)
|
||||
sr = 48000;
|
||||
|
||||
int lc = atomic_load(&ch->loop_count);
|
||||
int lc = atomic_load(&sc->loop_count);
|
||||
float *outbuf = malloc((size_t)lc * sizeof(float));
|
||||
if (!outbuf) {
|
||||
ring_destroy(ring);
|
||||
@@ -405,6 +493,12 @@ static void *writer_thread(void *arg) {
|
||||
wav_write(path, outbuf, (unsigned)lc, sr);
|
||||
free(outbuf);
|
||||
|
||||
/* Signal the RT thread to stop writing */
|
||||
atomic_store_explicit(&ch->save_complete, 1, memory_order_release);
|
||||
/* Wait for the RT thread to see the flag (one audio period) */
|
||||
struct timespec req = { .tv_sec = 0, .tv_nsec = 10000000 }; /* 10ms */
|
||||
nanosleep(&req, NULL);
|
||||
|
||||
ring_destroy(ring);
|
||||
free(ring);
|
||||
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
|
||||
@@ -467,14 +561,16 @@ void looper_process_commands(jack_client_t *client) {
|
||||
printf("LOAD: wav_read called\n");
|
||||
if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) {
|
||||
printf("LOAD: success, frames=%u\n", frames);
|
||||
int sc_idx = atomic_load(&channels[0].current_scene);
|
||||
scene_t *sc = &channels[0].scenes[sc_idx];
|
||||
if (frames > LOOP_BUF_SIZE)
|
||||
frames = LOOP_BUF_SIZE;
|
||||
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
|
||||
atomic_store(&channels[0].loop_count, (int)frames);
|
||||
atomic_store(&channels[0].record_pos, 0);
|
||||
atomic_store(&channels[0].playback_pos, 0);
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
atomic_store(&channels[0].prev_state, -1);
|
||||
memcpy(sc->loop.audio_buffer, buf, frames * sizeof(float));
|
||||
atomic_store(&sc->loop_count, (int)frames);
|
||||
atomic_store(&sc->record_pos, 0);
|
||||
atomic_store(&sc->playback_pos, 0);
|
||||
atomic_store(&sc->state, STATE_LOOPING);
|
||||
atomic_store(&sc->prev_state, -1);
|
||||
free(buf);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to load loop.wav\n");
|
||||
@@ -484,8 +580,10 @@ void looper_process_commands(jack_client_t *client) {
|
||||
|
||||
/* ---------- 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 &&
|
||||
int sc_idx = atomic_load(&channels[0].current_scene);
|
||||
scene_t *sc = &channels[0].scenes[sc_idx];
|
||||
int lc = atomic_load(&sc->loop_count);
|
||||
if (atomic_load(&sc->state) == STATE_LOOPING && lc > 0 &&
|
||||
channels[0].save_ring == NULL) {
|
||||
RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf));
|
||||
if (ring) {
|
||||
|
||||
675
engine/src/looper.c~
Normal file
675
engine/src/looper.c~
Normal file
@@ -0,0 +1,675 @@
|
||||
// cppcheck-suppress missingIncludeSystem
|
||||
#include "looper.h"
|
||||
#include "channel.h"
|
||||
#include "midi.h"
|
||||
<<<<<<< HEAD
|
||||
#include "wav.h"
|
||||
#include "ringbuffer.h"
|
||||
#include "pipe.h"
|
||||
#include <jack/jack.h>
|
||||
#include <sys/stat.h>
|
||||
=======
|
||||
#include "queue.h"
|
||||
>>>>>>> 3-integrate-carla
|
||||
#include <fcntl.h>
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
<<<<<<< HEAD
|
||||
#include <time.h>
|
||||
#include "queue.h"
|
||||
#include "command.h"
|
||||
|
||||
/* Global command queues */
|
||||
spsc_queue_t cmd_queue;
|
||||
spsc_queue_t cmd_queue_main_midi;
|
||||
spsc_queue_t cmd_queue_main_fifo;
|
||||
=======
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
>>>>>>> 3-integrate-carla
|
||||
|
||||
#define STATUS_FIFO "/tmp/looper_status"
|
||||
|
||||
/* writer status fd */
|
||||
static int status_fd = -1;
|
||||
|
||||
static void looper_write_status(void) {
|
||||
<<<<<<< HEAD
|
||||
if (status_fd < 0)
|
||||
return;
|
||||
char buf[256];
|
||||
for (int ch = 0; ch < MAX_CHANNELS; ch++) {
|
||||
if (!atomic_load(&channels[ch].active))
|
||||
continue;
|
||||
int state_val = atomic_load(&channels[ch].state);
|
||||
const char *state_str;
|
||||
switch (state_val) {
|
||||
case STATE_IDLE: state_str = "IDLE"; break;
|
||||
case STATE_RECORD: state_str = "RECORD"; break;
|
||||
case STATE_LOOPING: state_str = "LOOPING"; break;
|
||||
case STATE_PAUSED: state_str = "PAUSED"; break;
|
||||
default: state_str = "UNKNOWN";
|
||||
}
|
||||
int n = snprintf(buf, sizeof(buf),
|
||||
"CH=%d SC=%d STATE=%s\n",
|
||||
ch, 0, state_str);
|
||||
if (n > 0) {
|
||||
int ret = write(status_fd, buf, n);
|
||||
(void)ret;
|
||||
}
|
||||
}
|
||||
=======
|
||||
int fd = open(STATUS_FIFO, O_WRONLY | O_NONBLOCK);
|
||||
if (fd < 0)
|
||||
return;
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
char buf[256];
|
||||
for (int ch = 0; ch < cap; ch++) {
|
||||
if (!atomic_load(&cur[ch].active))
|
||||
continue;
|
||||
int sc_idx = atomic_load(&cur[ch].current_scene);
|
||||
int state = atomic_load(&cur[ch].scenes[sc_idx].state);
|
||||
const char *state_str;
|
||||
switch (state) {
|
||||
case STATE_IDLE:
|
||||
state_str = "IDLE";
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
state_str = "RECORD";
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
state_str = "LOOPING";
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
state_str = "PAUSED";
|
||||
break;
|
||||
default:
|
||||
state_str = "UNKNOWN";
|
||||
}
|
||||
int n = snprintf(buf, sizeof(buf), "CH=%d SC=%d STATE=%s\n", ch, sc_idx,
|
||||
state_str);
|
||||
if (n > 0) {
|
||||
int ret = write(fd, buf, n);
|
||||
(void)ret;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
>>>>>>> 3-integrate-carla
|
||||
}
|
||||
|
||||
/* Global state (shared across files) */
|
||||
struct channel_t channels[MAX_CHANNELS];
|
||||
atomic_int channel_count = 0;
|
||||
atomic_int channel_capacity = MAX_CHANNELS;
|
||||
int next_channel_id = 1;
|
||||
atomic_int cmd_add = 0;
|
||||
atomic_int cmd_remove = 0;
|
||||
atomic_int cmd_load = 0;
|
||||
atomic_int cmd_save = 0;
|
||||
jack_port_t *midi_control_port = NULL;
|
||||
jack_port_t *midi_clock_port = NULL;
|
||||
atomic_int control_key_active = 0;
|
||||
atomic_int bind_channel = 0;
|
||||
|
||||
/* Deferred removal index (1 second grace) */
|
||||
static int pending_unregister_idx = -1;
|
||||
|
||||
/* writer thread function and sample rate holder */
|
||||
static void *writer_thread(void *arg);
|
||||
static int global_sample_rate = 0;
|
||||
|
||||
/* execute a single command (called from looper_process_commands) */
|
||||
static void exec_command(command_t cmd, jack_client_t *client) {
|
||||
int ch = cmd.channel;
|
||||
if (ch < 0) ch = 0;
|
||||
|
||||
switch (cmd.type) {
|
||||
case CMD_CYCLE: {
|
||||
int state = atomic_load(&channels[ch].state);
|
||||
switch (state) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[ch].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[ch].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[ch].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[ch].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
atomic_store(&channels[ch].prev_state, -1);
|
||||
break;
|
||||
}
|
||||
case CMD_STOP:
|
||||
atomic_store(&channels[ch].state, STATE_IDLE);
|
||||
atomic_store(&channels[ch].prev_state, -1);
|
||||
break;
|
||||
|
||||
case CMD_ADD_CHANNEL:
|
||||
case CMD_ADD_MIDI_CHANNEL: {
|
||||
int idx;
|
||||
for (idx = 0; idx < MAX_CHANNELS; idx++)
|
||||
if (!channels[idx].active)
|
||||
break;
|
||||
if (idx < MAX_CHANNELS)
|
||||
channel_add(client, idx);
|
||||
break;
|
||||
}
|
||||
|
||||
case CMD_REMOVE_CHANNEL: {
|
||||
int remove_idx = -1;
|
||||
for (int idx = 1; idx < MAX_CHANNELS; idx++)
|
||||
if (channels[idx].active)
|
||||
remove_idx = idx;
|
||||
if (remove_idx != -1) {
|
||||
channel_remove(client, remove_idx);
|
||||
pending_unregister_idx = remove_idx;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CMD_BIND_CHANNEL:
|
||||
atomic_store(&bind_channel, cmd.data);
|
||||
break;
|
||||
|
||||
case CMD_UNBIND:
|
||||
atomic_store(&bind_channel, 0);
|
||||
break;
|
||||
|
||||
case CMD_LOAD:
|
||||
atomic_store(&cmd_load, 1);
|
||||
break;
|
||||
|
||||
case CMD_SAVE:
|
||||
atomic_store(&cmd_save, 1);
|
||||
break;
|
||||
|
||||
case CMD_ADD_SCENE:
|
||||
case CMD_REMOVE_SCENE:
|
||||
case CMD_NEXT_SCENE:
|
||||
case CMD_PREV_SCENE:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* process callback
|
||||
* ---------------------------------------------------------------- */
|
||||
int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
(void)arg;
|
||||
|
||||
if (midi_control_port) {
|
||||
void *midi_ctrl_buf = jack_port_get_buffer(midi_control_port, nframes);
|
||||
if (midi_ctrl_buf) {
|
||||
midi_handle_events(midi_ctrl_buf, nframes);
|
||||
}
|
||||
}
|
||||
|
||||
/* process each active channel */
|
||||
for (int c = 0; c < MAX_CHANNELS; c++) {
|
||||
if (!atomic_load(&channels[c].active))
|
||||
continue;
|
||||
|
||||
/* Guard against NULL ports (e.g. if port registration failed) */
|
||||
if (!channels[c].audio_in || !channels[c].audio_out) {
|
||||
fprintf(stderr, "WARN: channel %d has NULL audio port(s), skipping\n", c);
|
||||
continue;
|
||||
}
|
||||
|
||||
const jack_default_audio_sample_t *in =
|
||||
(const jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_in, nframes);
|
||||
jack_default_audio_sample_t *out =
|
||||
(jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
channels[c].audio_out, nframes);
|
||||
if (!out)
|
||||
continue;
|
||||
|
||||
int state = atomic_load(&channels[c].state);
|
||||
|
||||
if (state != atomic_load(&channels[c].prev_state)) {
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[c].record_pos, 0);
|
||||
atomic_store(&channels[c].loop_count, 0);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
if (atomic_load(&channels[c].prev_state) == STATE_RECORD &&
|
||||
atomic_load(&channels[c].record_pos) > 0)
|
||||
atomic_store(&channels[c].loop_count,
|
||||
atomic_load(&channels[c].record_pos));
|
||||
atomic_store(&channels[c].playback_pos, 0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
jack_nframes_t i;
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
if (in) {
|
||||
float *f_out = (float *)out;
|
||||
const float *f_in = (const float *)in;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
int rp = atomic_fetch_add(&channels[c].record_pos, 1);
|
||||
if (rp < LOOP_BUF_SIZE)
|
||||
channels[c].loop_buffer[rp] = f_in[i];
|
||||
f_out[i] = f_in[i];
|
||||
}
|
||||
} else {
|
||||
=======
|
||||
if (active_channels[c].type == CHANNEL_MIDI) {
|
||||
/* MIDI channel handling */
|
||||
switch (state) {
|
||||
case STATE_RECORD: {
|
||||
void *midi_in_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_in, nframes);
|
||||
if (midi_in_buf) {
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
|
||||
jack_midi_event_t ev;
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
int rp = atomic_load(&sc->record_pos);
|
||||
if (rp < MAX_MIDI_EVENTS) {
|
||||
sc->loop.midi_events[rp].timestamp = ev.time;
|
||||
sc->loop.midi_events[rp].status = ev.buffer[0];
|
||||
sc->loop.midi_events[rp].note = (ev.size > 1) ? ev.buffer[1] : 0;
|
||||
sc->loop.midi_events[rp].velocity =
|
||||
(ev.size > 2) ? ev.buffer[2] : 0;
|
||||
atomic_store(&sc->record_pos, rp + 1);
|
||||
}
|
||||
}
|
||||
/* forward incoming MIDI to output during record */
|
||||
void *midi_out_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_out, nframes);
|
||||
if (midi_out_buf) {
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_LOOPING: {
|
||||
void *midi_out_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_out, nframes);
|
||||
if (midi_out_buf) {
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
int cnt = atomic_load(&sc->loop_count);
|
||||
if (cnt > 0) {
|
||||
for (int e = 0; e < cnt; e++) {
|
||||
unsigned char msg[3];
|
||||
msg[0] = sc->loop.midi_events[e].status;
|
||||
msg[1] = sc->loop.midi_events[e].note;
|
||||
msg[2] = sc->loop.midi_events[e].velocity;
|
||||
jack_midi_event_write(midi_out_buf, 0, msg, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case STATE_PAUSED:
|
||||
/* no output */
|
||||
break;
|
||||
default: /* IDLE */
|
||||
{
|
||||
void *midi_in_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_in, nframes);
|
||||
void *midi_out_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_out, nframes);
|
||||
if (midi_in_buf && midi_out_buf) {
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(midi_in_buf);
|
||||
jack_midi_event_t ev;
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
jack_midi_event_write(midi_out_buf, ev.time, ev.buffer, ev.size);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
}
|
||||
if (state == STATE_LOOPING) {
|
||||
atomic_store(&sc->loop_count, atomic_load(&sc->record_pos));
|
||||
}
|
||||
} else {
|
||||
/* audio channel handling */
|
||||
jack_nframes_t i;
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
if (in) {
|
||||
float *f_out = (float *)out;
|
||||
const float *f_in = (const float *)in;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
int rp = atomic_load(&sc->record_pos);
|
||||
if (rp < LOOP_BUF_SIZE) {
|
||||
sc->loop.audio_buffer[rp] = f_in[i];
|
||||
atomic_store(&sc->record_pos, rp + 1);
|
||||
}
|
||||
f_out[i] = f_in[i];
|
||||
}
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_LOOPING: {
|
||||
int loop_cnt = atomic_load(&sc->loop_count);
|
||||
if (loop_cnt > 0) {
|
||||
float *outf = (float *)out;
|
||||
int pp = atomic_load(&sc->playback_pos);
|
||||
for (i = 0; i < nframes; i++) {
|
||||
outf[i] = sc->loop.audio_buffer[pp];
|
||||
pp = (pp + 1) % loop_cnt;
|
||||
}
|
||||
atomic_store(&sc->playback_pos, pp);
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case STATE_PAUSED:
|
||||
>>>>>>> 3-integrate-carla
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_LOOPING:
|
||||
int lc = atomic_load(&channels[c].loop_count);
|
||||
if (lc > 0) {
|
||||
float *outf = (float *)out;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
int pp = atomic_load(&channels[c].playback_pos);
|
||||
outf[i] = channels[c].loop_buffer[pp];
|
||||
atomic_store(&channels[c].playback_pos, (pp + 1) % lc);
|
||||
}
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
|
||||
case STATE_PAUSED:
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
break;
|
||||
|
||||
default: /* IDLE */
|
||||
if (in) {
|
||||
memcpy(out, in, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// push loop output into save ring if saving (atomic load)
|
||||
RingBuf *r = (RingBuf *)atomic_load_explicit(&channels[c].save_ring,
|
||||
memory_order_acquire);
|
||||
if (r != NULL) {
|
||||
if (state == STATE_LOOPING && atomic_load(&channels[c].loop_count) > 0) {
|
||||
const float *outf = (const float *)out;
|
||||
ring_write(r, outf, nframes);
|
||||
}
|
||||
}
|
||||
|
||||
atomic_store(&channels[c].prev_state, state);
|
||||
}
|
||||
|
||||
/* MIDI clock events – affect channel 0 only */
|
||||
if (midi_clock_port) {
|
||||
void *midi_clock_buf = jack_port_get_buffer(midi_clock_port, nframes);
|
||||
if (midi_clock_buf) {
|
||||
jack_nframes_t n_clock_events = jack_midi_get_event_count(midi_clock_buf);
|
||||
jack_midi_event_t cev;
|
||||
for (jack_nframes_t j = 0; j < n_clock_events; j++) {
|
||||
if (jack_midi_event_get(&cev, midi_clock_buf, j) != 0)
|
||||
continue;
|
||||
if (cev.size >= 1) {
|
||||
unsigned char msg = cev.buffer[0];
|
||||
switch (msg) {
|
||||
case 0xFA: {
|
||||
int s = atomic_load(&channels[0].state);
|
||||
if (s == STATE_IDLE)
|
||||
atomic_store(&channels[0].state, STATE_RECORD);
|
||||
break;
|
||||
}
|
||||
case 0xFC:
|
||||
atomic_store(&channels[0].state, STATE_IDLE);
|
||||
break;
|
||||
case 0xFB: {
|
||||
int s = atomic_load(&channels[0].state);
|
||||
if (s == STATE_PAUSED)
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* shutdown callback
|
||||
* ---------------------------------------------------------------- */
|
||||
void jack_shutdown_cb(void *arg) {
|
||||
(void)arg;
|
||||
fprintf(stderr, "JACK shutdown\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* looper initialisation
|
||||
* ---------------------------------------------------------------- */
|
||||
int looper_init(jack_client_t *client) {
|
||||
/* store sample rate for writer thread */
|
||||
global_sample_rate = jack_get_sample_rate(client);
|
||||
|
||||
/* create status FIFO (ignore if already exists) */
|
||||
mkfifo(STATUS_FIFO, 0666);
|
||||
|
||||
/* open the status FIFO for reading+writing so writes work even without reader */
|
||||
status_fd = open(STATUS_FIFO, O_RDWR);
|
||||
if (status_fd < 0) {
|
||||
perror("open status FIFO");
|
||||
}
|
||||
|
||||
queue_init(&cmd_queue);
|
||||
queue_init(&cmd_queue_main_midi);
|
||||
queue_init(&cmd_queue_main_fifo);
|
||||
|
||||
/* start the FIFO reader thread */
|
||||
pipe_start_reader();
|
||||
|
||||
|
||||
/* channel 0 */
|
||||
channels[0].active = 1;
|
||||
atomic_store(&channels[0].state, STATE_IDLE);
|
||||
atomic_store(&channels[0].prev_state, -1);
|
||||
channels[0].loop_count = 0;
|
||||
atomic_store(&channels[0].record_pos, 0);
|
||||
atomic_store(&channels[0].playback_pos, 0);
|
||||
atomic_store_explicit(&channels[0].save_ring, NULL, memory_order_release);
|
||||
|
||||
channels[0].audio_in = jack_port_register(
|
||||
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
channels[0].audio_out = jack_port_register(
|
||||
client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
|
||||
if (!channels[0].audio_in || !channels[0].audio_out) {
|
||||
fprintf(stderr, "Could not create audio ports for channel 0\n");
|
||||
return -1;
|
||||
}
|
||||
channel_count = 1;
|
||||
|
||||
midi_control_port = jack_port_register(
|
||||
client, "control", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
|
||||
midi_clock_port = jack_port_register(client, "clock", JACK_DEFAULT_MIDI_TYPE,
|
||||
JackPortIsInput, 0);
|
||||
if (!midi_control_port || !midi_clock_port) {
|
||||
fprintf(stderr, "Could not create MIDI ports\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* writer thread – consumes the save ring and writes WAV file
|
||||
* ---------------------------------------------------------------- */
|
||||
static void *writer_thread(void *arg) {
|
||||
struct channel_t *ch = (struct channel_t *)arg;
|
||||
RingBuf *ring = (RingBuf *)ch->save_ring;
|
||||
if (!ring)
|
||||
return NULL;
|
||||
|
||||
static const char *path = "save.wav";
|
||||
unsigned sr = (unsigned)global_sample_rate;
|
||||
if (sr == 0)
|
||||
sr = 48000;
|
||||
|
||||
int lc = atomic_load(&ch->loop_count);
|
||||
float *outbuf = malloc((size_t)lc * sizeof(float));
|
||||
if (!outbuf) {
|
||||
ring_destroy(ring);
|
||||
free(ring);
|
||||
ch->save_ring = NULL;
|
||||
return NULL;
|
||||
}
|
||||
size_t collected = 0;
|
||||
size_t want = (size_t)lc;
|
||||
while (collected < want) {
|
||||
size_t got = ring_read(ring, outbuf + collected, want - collected);
|
||||
collected += got;
|
||||
if (got == 0) {
|
||||
struct timespec req = {.tv_sec = 0, .tv_nsec = 10000000};
|
||||
nanosleep(&req, NULL);
|
||||
}
|
||||
}
|
||||
wav_write(path, outbuf, (unsigned)lc, sr);
|
||||
free(outbuf);
|
||||
|
||||
ring_destroy(ring);
|
||||
free(ring);
|
||||
atomic_store_explicit(&ch->save_ring, NULL, memory_order_release);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------
|
||||
* main‑loop command processing
|
||||
* ---------------------------------------------------------------- */
|
||||
void looper_process_commands(jack_client_t *client) {
|
||||
/* process commands from the three queues FIRST */
|
||||
command_t cmd;
|
||||
while (queue_pop(&cmd_queue, &cmd))
|
||||
exec_command(cmd, client);
|
||||
while (queue_pop(&cmd_queue_main_midi, &cmd))
|
||||
exec_command(cmd, client);
|
||||
while (queue_pop(&cmd_queue_main_fifo, &cmd))
|
||||
exec_command(cmd, client);
|
||||
|
||||
/* Unregister any ports that were marked for deferred removal.
|
||||
By now the real‑time thread has had at least one full cycle
|
||||
to see the `active = 0` store. */
|
||||
if (pending_unregister_idx != -1) {
|
||||
int idx = pending_unregister_idx;
|
||||
if (channels[idx].audio_in)
|
||||
jack_port_unregister(client, channels[idx].audio_in);
|
||||
if (channels[idx].audio_out)
|
||||
jack_port_unregister(client, channels[idx].audio_out);
|
||||
pending_unregister_idx = -1;
|
||||
}
|
||||
|
||||
/* ---------- add channel ---------- */
|
||||
if (atomic_exchange(&cmd_add, 0)) {
|
||||
int idx;
|
||||
for (idx = 0; idx < MAX_CHANNELS; idx++)
|
||||
if (!channels[idx].active)
|
||||
break;
|
||||
if (idx < MAX_CHANNELS) {
|
||||
channel_add(client, idx);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- remove channel ---------- */
|
||||
if (atomic_exchange(&cmd_remove, 0)) {
|
||||
int remove_idx = -1;
|
||||
for (int idx = 1; idx < MAX_CHANNELS; idx++)
|
||||
if (channels[idx].active)
|
||||
remove_idx = idx;
|
||||
if (remove_idx != -1) {
|
||||
/* Mark inactive now; ports will be unregistered next round */
|
||||
channel_remove(client, remove_idx);
|
||||
pending_unregister_idx = remove_idx;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- load command ---------- */
|
||||
if (atomic_exchange(&cmd_load, 0)) {
|
||||
float *buf = NULL;
|
||||
unsigned frames = 0;
|
||||
printf("LOAD: wav_read called\n");
|
||||
if (wav_read("loop.wav", &buf, &frames) == 0 && frames > 0) {
|
||||
printf("LOAD: success, frames=%u\n", frames);
|
||||
if (frames > LOOP_BUF_SIZE)
|
||||
frames = LOOP_BUF_SIZE;
|
||||
memcpy(channels[0].loop_buffer, buf, frames * sizeof(float));
|
||||
atomic_store(&channels[0].loop_count, (int)frames);
|
||||
atomic_store(&channels[0].record_pos, 0);
|
||||
atomic_store(&channels[0].playback_pos, 0);
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
atomic_store(&channels[0].prev_state, -1);
|
||||
free(buf);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to load loop.wav\n");
|
||||
printf("LOAD: FAILED\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- save command (writer thread) ---------- */
|
||||
if (atomic_exchange(&cmd_save, 0)) {
|
||||
int lc = atomic_load(&channels[0].loop_count);
|
||||
if (atomic_load(&channels[0].state) == STATE_LOOPING && lc > 0 &&
|
||||
channels[0].save_ring == NULL) {
|
||||
RingBuf *ring = (RingBuf *)malloc(sizeof(RingBuf));
|
||||
if (ring) {
|
||||
size_t sz = (size_t)lc * 2;
|
||||
if (ring_init(ring, sz) == 0) {
|
||||
atomic_store_explicit(&channels[0].save_ring, (_Atomic RingBuf *)ring,
|
||||
memory_order_release);
|
||||
pthread_t th;
|
||||
pthread_create(&th, NULL, writer_thread, &channels[0]);
|
||||
pthread_detach(th);
|
||||
} else {
|
||||
free(ring);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* write current state to status FIFO */
|
||||
looper_write_status();
|
||||
}
|
||||
BIN
engine/src/looper.o
Normal file
BIN
engine/src/looper.o
Normal file
Binary file not shown.
BIN
engine/src/main.o
Normal file
BIN
engine/src/main.o
Normal file
Binary file not shown.
@@ -1,10 +1,14 @@
|
||||
// cppcheck-suppress missingIncludeSystem
|
||||
#include "midi.h"
|
||||
#include "channel.h"
|
||||
#include "queue.h"
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
/* queues declared in looper.c */
|
||||
extern spsc_queue_t cmd_queue;
|
||||
extern spsc_queue_t cmd_queue_main_midi;
|
||||
extern atomic_int control_key_active;
|
||||
extern atomic_int cmd_add;
|
||||
extern atomic_int cmd_remove;
|
||||
@@ -49,32 +53,56 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
|
||||
{
|
||||
int bch = atomic_load(&bind_channel);
|
||||
if (bch >= 0 && bch < MAX_CHANNELS) {
|
||||
int cur = atomic_load(&channels[bch].state);
|
||||
int sc_idx = atomic_load(&channels[bch].current_scene);
|
||||
int cur = atomic_load(&channels[bch].scenes[sc_idx].state);
|
||||
switch (cur) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[bch].state, STATE_RECORD);
|
||||
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[bch].state, STATE_LOOPING);
|
||||
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[bch].state, STATE_PAUSED);
|
||||
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[bch].state, STATE_LOOPING);
|
||||
atomic_store(&channels[bch].scenes[sc_idx].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
case 63: /* unbind – reset bind to channel 0 */
|
||||
atomic_store(&bind_channel, 0);
|
||||
break;
|
||||
case 70: /* load WAV into channel 0 */
|
||||
atomic_store(&cmd_load, 1);
|
||||
break;
|
||||
case 71: /* save WAV of channel 0 */
|
||||
atomic_store(&cmd_save, 1);
|
||||
break;
|
||||
case 63: {
|
||||
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} break;
|
||||
case 65: {
|
||||
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} break;
|
||||
case 66: {
|
||||
command_t cmd = {
|
||||
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 67: {
|
||||
command_t cmd = {
|
||||
.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 68: {
|
||||
command_t cmd = {
|
||||
.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 69: {
|
||||
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 70: {
|
||||
command_t cmd = {
|
||||
.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -84,19 +112,20 @@ void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
|
||||
switch (note) {
|
||||
case 1: /* toggle channel 0 */
|
||||
{
|
||||
int cur0 = atomic_load(&channels[0].state);
|
||||
int sc0 = atomic_load(&channels[0].current_scene);
|
||||
int cur0 = atomic_load(&channels[0].scenes[sc0].state);
|
||||
switch (cur0) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[0].state, STATE_RECORD);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[0].state, STATE_PAUSED);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
atomic_store(&channels[0].scenes[sc0].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
154
engine/src/midi.c~
Normal file
154
engine/src/midi.c~
Normal file
@@ -0,0 +1,154 @@
|
||||
// cppcheck-suppress missingIncludeSystem
|
||||
#include "midi.h"
|
||||
#include "channel.h"
|
||||
#include <jack/jack.h>
|
||||
#include <jack/midiport.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
extern atomic_int control_key_active;
|
||||
extern atomic_int cmd_add;
|
||||
extern atomic_int cmd_remove;
|
||||
extern atomic_int cmd_load;
|
||||
extern atomic_int cmd_save;
|
||||
extern atomic_int bind_channel;
|
||||
|
||||
void midi_handle_events(void *port_buffer, jack_nframes_t nframes) {
|
||||
(void)nframes;
|
||||
jack_nframes_t nevents = jack_midi_get_event_count(port_buffer);
|
||||
jack_midi_event_t ev;
|
||||
|
||||
for (jack_nframes_t i = 0; i < nevents; i++) {
|
||||
if (jack_midi_event_get(&ev, port_buffer, i) != 0)
|
||||
continue;
|
||||
if (ev.size < 3)
|
||||
continue;
|
||||
|
||||
unsigned char status = ev.buffer[0];
|
||||
unsigned char note = ev.buffer[1];
|
||||
unsigned char vel = ev.buffer[2];
|
||||
|
||||
/* note‑on */
|
||||
if ((status & 0xf0) == 0x90 && vel > 0) {
|
||||
if (note == 64) {
|
||||
atomic_store(&control_key_active, 1);
|
||||
} else {
|
||||
int ck = atomic_load(&control_key_active);
|
||||
if (ck) {
|
||||
atomic_store(&control_key_active, 0);
|
||||
if (note < 16) {
|
||||
atomic_store(&bind_channel, note);
|
||||
} else {
|
||||
switch (note) {
|
||||
case 60:
|
||||
atomic_store(&cmd_add, 1);
|
||||
break;
|
||||
case 61:
|
||||
atomic_store(&cmd_remove, 1);
|
||||
break;
|
||||
case 62: /* trigger looper – channel via bind_channel */
|
||||
{
|
||||
int bch = atomic_load(&bind_channel);
|
||||
if (bch >= 0 && bch < MAX_CHANNELS) {
|
||||
int cur = atomic_load(&channels[bch].state);
|
||||
switch (cur) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[bch].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[bch].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[bch].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[bch].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
<<<<<<< HEAD
|
||||
case 63: /* unbind – reset bind to channel 0 */
|
||||
atomic_store(&bind_channel, 0);
|
||||
break;
|
||||
case 70: /* load WAV into channel 0 */
|
||||
atomic_store(&cmd_load, 1);
|
||||
break;
|
||||
case 71: /* save WAV of channel 0 */
|
||||
atomic_store(&cmd_save, 1);
|
||||
break;
|
||||
=======
|
||||
case 63: {
|
||||
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} break;
|
||||
case 65: {
|
||||
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} break;
|
||||
case 66: {
|
||||
command_t cmd = {
|
||||
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 67: {
|
||||
command_t cmd = {
|
||||
.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 68: {
|
||||
command_t cmd = {
|
||||
.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 69: {
|
||||
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
case 70: {
|
||||
command_t cmd = {
|
||||
.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_midi, cmd);
|
||||
} break;
|
||||
>>>>>>> 3-integrate-carla
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* direct mapping */
|
||||
switch (note) {
|
||||
case 1: /* toggle channel 0 */
|
||||
{
|
||||
int cur0 = atomic_load(&channels[0].state);
|
||||
switch (cur0) {
|
||||
case STATE_IDLE:
|
||||
atomic_store(&channels[0].state, STATE_RECORD);
|
||||
break;
|
||||
case STATE_RECORD:
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
atomic_store(&channels[0].state, STATE_PAUSED);
|
||||
break;
|
||||
case STATE_PAUSED:
|
||||
atomic_store(&channels[0].state, STATE_LOOPING);
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
case 60:
|
||||
atomic_store(&cmd_add, 1);
|
||||
break;
|
||||
case 61:
|
||||
atomic_store(&cmd_remove, 1);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if ((status & 0xf0) == 0x80 ||
|
||||
((status & 0xf0) == 0x90 && vel == 0)) {
|
||||
atomic_store(&control_key_active, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
engine/src/midi.o
Normal file
BIN
engine/src/midi.o
Normal file
Binary file not shown.
@@ -7,8 +7,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FIFO_PATH "/tmp/looper_cmd"
|
||||
@@ -39,8 +39,9 @@ static void *pipe_thread_func(void *arg) {
|
||||
command_t cmd = {.type = CMD_ADD_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "add_midi") == 0) {
|
||||
command_t cmd = {.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
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, cmd);
|
||||
|
||||
111
engine/src/pipe.c~
Normal file
111
engine/src/pipe.c~
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "pipe.h"
|
||||
#include "command.h"
|
||||
#include "queue.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FIFO_PATH "/tmp/looper_cmd"
|
||||
#define LINE_MAX 256
|
||||
|
||||
/* 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, cmd);
|
||||
} else if (strcmp(line, "add_midi") == 0) {
|
||||
<<<<<<< HEAD
|
||||
command_t cmd = {.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
=======
|
||||
command_t cmd = {
|
||||
.type = CMD_ADD_MIDI_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue_main_fifo, cmd);
|
||||
>>>>>>> 3-integrate-carla
|
||||
} else if (strcmp(line, "remove") == 0) {
|
||||
command_t cmd = {.type = CMD_REMOVE_CHANNEL, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strncmp(line, "record ", 7) == 0) {
|
||||
int ch = atoi(line + 7);
|
||||
command_t cmd = {.type = CMD_CYCLE, .channel = ch, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "stop") == 0) {
|
||||
command_t cmd = {.type = CMD_STOP, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strncmp(line, "bind ", 5) == 0) {
|
||||
int ch = atoi(line + 5);
|
||||
command_t cmd = {.type = CMD_BIND_CHANNEL, .channel = -1, .data = ch};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "unbind") == 0) {
|
||||
command_t cmd = {.type = CMD_UNBIND, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "scene_add") == 0) {
|
||||
command_t cmd = {.type = CMD_ADD_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "scene_remove") == 0) {
|
||||
command_t cmd = {.type = CMD_REMOVE_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "scene_next") == 0) {
|
||||
command_t cmd = {.type = CMD_NEXT_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "scene_prev") == 0) {
|
||||
command_t cmd = {.type = CMD_PREV_SCENE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "load") == 0) {
|
||||
command_t cmd = {.type = CMD_LOAD, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
} else if (strcmp(line, "save") == 0) {
|
||||
command_t cmd = {.type = CMD_SAVE, .channel = -1, .data = 0};
|
||||
queue_push(&cmd_queue, cmd);
|
||||
}
|
||||
/* ignore unknown lines */
|
||||
}
|
||||
/* EOF – all writers closed, reopen for next connection */
|
||||
fclose(fifo);
|
||||
{
|
||||
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
|
||||
nanosleep(&ts, NULL);
|
||||
} /* small pause before retrying */
|
||||
}
|
||||
return NULL; /* unreachable */
|
||||
}
|
||||
|
||||
int pipe_start_reader(void) {
|
||||
/* create FIFO if it doesn't exist */
|
||||
if (mkfifo(FIFO_PATH, 0666) != 0 && errno != EEXIST) {
|
||||
perror("mkfifo");
|
||||
return -1;
|
||||
}
|
||||
pthread_t tid;
|
||||
if (pthread_create(&tid, NULL, pipe_thread_func, NULL) != 0) {
|
||||
perror("pthread_create");
|
||||
return -1;
|
||||
}
|
||||
pthread_detach(tid); /* we don't need to join */
|
||||
return 0;
|
||||
}
|
||||
BIN
engine/src/pipe.o
Normal file
BIN
engine/src/pipe.o
Normal file
Binary file not shown.
0
engine/src/plugins.c
Normal file
0
engine/src/plugins.c
Normal file
0
engine/src/plugins.h
Normal file
0
engine/src/plugins.h
Normal file
@@ -1,6 +1,7 @@
|
||||
#ifndef QUEUE_H
|
||||
#define QUEUE_H
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include "command.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
@@ -15,8 +16,8 @@ typedef struct {
|
||||
command_t buffer[QUEUE_CAPACITY];
|
||||
/* head: index where next element will be written (producer only)
|
||||
* tail: index of next element to read (consumer only) */
|
||||
int head;
|
||||
int tail;
|
||||
atomic_int head;
|
||||
atomic_int tail;
|
||||
} spsc_queue_t;
|
||||
|
||||
/* Initialise queue (must be called once before any push/pop). */
|
||||
|
||||
BIN
engine/src/queue.o
Normal file
BIN
engine/src/queue.o
Normal file
Binary file not shown.
BIN
engine/src/ringbuffer.o
Normal file
BIN
engine/src/ringbuffer.o
Normal file
Binary file not shown.
BIN
engine/src/wav.o
Normal file
BIN
engine/src/wav.o
Normal file
Binary file not shown.
BIN
engine/test_status_fifo
Executable file
BIN
engine/test_status_fifo
Executable file
Binary file not shown.
0
engine/tests/test_plugin.c
Normal file
0
engine/tests/test_plugin.c
Normal file
Reference in New Issue
Block a user