feat: implement scene infrastructure for multi-scene looper support
Co-authored-by: aider (deepseek/deepseek-reasoner) <aider@aider.chat>
This commit is contained in:
196
src/looper.c
196
src/looper.c
@@ -66,7 +66,9 @@ static void apply_command(command_t cmd) {
|
||||
switch (cmd.type) {
|
||||
case CMD_CYCLE:
|
||||
if (cmd.channel >= 0 && cmd.channel < cap) {
|
||||
int cst = atomic_load(&cur[cmd.channel].state);
|
||||
int sc_idx = cur[cmd.channel].current_scene;
|
||||
scene_t *sc = &cur[cmd.channel].scenes[sc_idx];
|
||||
int cst = atomic_load(&sc->state);
|
||||
int next;
|
||||
switch (cst) {
|
||||
case STATE_IDLE:
|
||||
@@ -85,23 +87,29 @@ static void apply_command(command_t cmd) {
|
||||
next = STATE_IDLE;
|
||||
break;
|
||||
}
|
||||
atomic_store(&cur[cmd.channel].state, next);
|
||||
atomic_store(&sc->state, next);
|
||||
}
|
||||
break;
|
||||
case CMD_STOP:
|
||||
if (cmd.channel >= 0 && cmd.channel < cap) {
|
||||
atomic_store(&cur[cmd.channel].state, STATE_IDLE);
|
||||
cur[cmd.channel].loop_count = 0;
|
||||
cur[cmd.channel].record_pos = 0;
|
||||
cur[cmd.channel].playback_pos = 0;
|
||||
cur[cmd.channel].prev_state = -1;
|
||||
struct channel_t *ch = &cur[cmd.channel];
|
||||
for (int s = 0; s < ch->scene_count; s++) {
|
||||
atomic_store(&ch->scenes[s].state, STATE_IDLE);
|
||||
ch->scenes[s].loop_count = 0;
|
||||
ch->scenes[s].record_pos = 0;
|
||||
ch->scenes[s].playback_pos = 0;
|
||||
ch->scenes[s].prev_state = -1;
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < cap; i++) {
|
||||
atomic_store(&cur[i].state, STATE_IDLE);
|
||||
cur[i].loop_count = 0;
|
||||
cur[i].record_pos = 0;
|
||||
cur[i].playback_pos = 0;
|
||||
cur[i].prev_state = -1;
|
||||
struct channel_t *ch = &cur[i];
|
||||
for (int s = 0; s < ch->scene_count; s++) {
|
||||
atomic_store(&ch->scenes[s].state, STATE_IDLE);
|
||||
ch->scenes[s].loop_count = 0;
|
||||
ch->scenes[s].record_pos = 0;
|
||||
ch->scenes[s].playback_pos = 0;
|
||||
ch->scenes[s].prev_state = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -158,6 +166,10 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Obtain current scene pointer */
|
||||
int sc_idx = active_channels[c].current_scene;
|
||||
scene_t *sc = &active_channels[c].scenes[sc_idx];
|
||||
|
||||
const jack_default_audio_sample_t *in =
|
||||
(const jack_default_audio_sample_t *)jack_port_get_buffer(
|
||||
active_channels[c].audio_in, nframes);
|
||||
@@ -167,18 +179,18 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
if (!out)
|
||||
continue;
|
||||
|
||||
int state = atomic_load(&active_channels[c].state);
|
||||
int state = atomic_load(&sc->state);
|
||||
|
||||
if (state != active_channels[c].prev_state) {
|
||||
if (state != sc->prev_state) {
|
||||
switch (state) {
|
||||
case STATE_RECORD:
|
||||
active_channels[c].record_pos = 0;
|
||||
active_channels[c].loop_count = 0;
|
||||
sc->record_pos = 0;
|
||||
sc->loop_count = 0;
|
||||
break;
|
||||
case STATE_LOOPING:
|
||||
if (active_channels[c].record_pos > 0)
|
||||
active_channels[c].loop_count = active_channels[c].record_pos;
|
||||
active_channels[c].playback_pos = 0;
|
||||
if (sc->record_pos > 0)
|
||||
sc->loop_count = sc->record_pos;
|
||||
sc->playback_pos = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -197,20 +209,14 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
for (jack_nframes_t j = 0; j < nevents; j++) {
|
||||
if (jack_midi_event_get(&ev, midi_in_buf, j) != 0)
|
||||
continue;
|
||||
if (active_channels[c].record_pos < MAX_MIDI_EVENTS) {
|
||||
active_channels[c]
|
||||
.loop.midi_events[active_channels[c].record_pos]
|
||||
.timestamp = ev.time;
|
||||
active_channels[c]
|
||||
.loop.midi_events[active_channels[c].record_pos]
|
||||
.status = ev.buffer[0];
|
||||
active_channels[c]
|
||||
.loop.midi_events[active_channels[c].record_pos]
|
||||
.note = (ev.size > 1) ? ev.buffer[1] : 0;
|
||||
active_channels[c]
|
||||
.loop.midi_events[active_channels[c].record_pos]
|
||||
.velocity = (ev.size > 2) ? ev.buffer[2] : 0;
|
||||
active_channels[c].record_pos++;
|
||||
if (sc->record_pos < MAX_MIDI_EVENTS) {
|
||||
sc->loop.midi_events[sc->record_pos].timestamp = ev.time;
|
||||
sc->loop.midi_events[sc->record_pos].status = ev.buffer[0];
|
||||
sc->loop.midi_events[sc->record_pos].note =
|
||||
(ev.size > 1) ? ev.buffer[1] : 0;
|
||||
sc->loop.midi_events[sc->record_pos].velocity =
|
||||
(ev.size > 2) ? ev.buffer[2] : 0;
|
||||
sc->record_pos++;
|
||||
}
|
||||
}
|
||||
/* forward incoming MIDI to output during record */
|
||||
@@ -232,15 +238,13 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
jack_port_get_buffer(active_channels[c].midi_out, nframes);
|
||||
if (midi_out_buf) {
|
||||
jack_midi_clear_buffer(midi_out_buf);
|
||||
int cnt =
|
||||
active_channels[c].loop_count; /* number of recorded events */
|
||||
int cnt = sc->loop_count;
|
||||
if (cnt > 0) {
|
||||
/* simple: output all recorded events at frame 0 of each cycle */
|
||||
for (int e = 0; e < cnt; e++) {
|
||||
unsigned char msg[3];
|
||||
msg[0] = active_channels[c].loop.midi_events[e].status;
|
||||
msg[1] = active_channels[c].loop.midi_events[e].note;
|
||||
msg[2] = active_channels[c].loop.midi_events[e].velocity;
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -251,7 +255,6 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
/* no output */
|
||||
break;
|
||||
default: /* IDLE */
|
||||
/* pass through MIDI input to output */
|
||||
{
|
||||
void *midi_in_buf =
|
||||
jack_port_get_buffer(active_channels[c].midi_in, nframes);
|
||||
@@ -270,9 +273,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* for MIDI channels, the loop_count holds number of recorded events */
|
||||
if (state == STATE_LOOPING) {
|
||||
active_channels[c].loop_count = active_channels[c].record_pos;
|
||||
sc->loop_count = sc->record_pos;
|
||||
}
|
||||
} else {
|
||||
/* audio channel handling */
|
||||
@@ -283,9 +285,8 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
float *f_out = (float *)out;
|
||||
const float *f_in = (const float *)in;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
if (active_channels[c].record_pos < LOOP_BUF_SIZE)
|
||||
active_channels[c]
|
||||
.loop.audio_buffer[active_channels[c].record_pos++] = f_in[i];
|
||||
if (sc->record_pos < LOOP_BUF_SIZE)
|
||||
sc->loop.audio_buffer[sc->record_pos++] = f_in[i];
|
||||
f_out[i] = f_in[i];
|
||||
}
|
||||
} else {
|
||||
@@ -294,14 +295,11 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
break;
|
||||
|
||||
case STATE_LOOPING:
|
||||
if (active_channels[c].loop_count > 0) {
|
||||
if (sc->loop_count > 0) {
|
||||
float *outf = (float *)out;
|
||||
for (i = 0; i < nframes; i++) {
|
||||
outf[i] = active_channels[c]
|
||||
.loop.audio_buffer[active_channels[c].playback_pos];
|
||||
active_channels[c].playback_pos =
|
||||
(active_channels[c].playback_pos + 1) %
|
||||
active_channels[c].loop_count;
|
||||
outf[i] = sc->loop.audio_buffer[sc->playback_pos];
|
||||
sc->playback_pos = (sc->playback_pos + 1) % sc->loop_count;
|
||||
}
|
||||
} else {
|
||||
memset(out, 0, sizeof(jack_default_audio_sample_t) * nframes);
|
||||
@@ -322,7 +320,7 @@ int process_callback(jack_nframes_t nframes, void *arg) {
|
||||
}
|
||||
}
|
||||
|
||||
active_channels[c].prev_state = state;
|
||||
sc->prev_state = state;
|
||||
}
|
||||
|
||||
/* MIDI clock events – affect channel 0 only */
|
||||
@@ -392,12 +390,14 @@ int looper_init(jack_client_t *client) {
|
||||
}
|
||||
struct channel_t *init = atomic_load(&channels);
|
||||
/* channel 0 */
|
||||
init[0].active = 1;
|
||||
atomic_store(&init[0].state, STATE_IDLE);
|
||||
init[0].prev_state = -1;
|
||||
init[0].loop_count = 0;
|
||||
init[0].record_pos = 0;
|
||||
init[0].playback_pos = 0;
|
||||
atomic_store(&init[0].active, 1);
|
||||
init[0].scene_count = 1;
|
||||
init[0].current_scene = 0;
|
||||
init[0].scenes[0].loop_count = 0;
|
||||
init[0].scenes[0].record_pos = 0;
|
||||
init[0].scenes[0].playback_pos = 0;
|
||||
atomic_store(&init[0].scenes[0].state, STATE_IDLE);
|
||||
init[0].scenes[0].prev_state = -1;
|
||||
|
||||
init[0].audio_in = jack_port_register(
|
||||
client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
|
||||
@@ -471,6 +471,46 @@ void looper_process_commands(jack_client_t *client) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_ADD_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_add_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_REMOVE_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_remove_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_NEXT_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_next_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_PREV_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_prev_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -519,6 +559,46 @@ void looper_process_commands(jack_client_t *client) {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_ADD_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_add_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_REMOVE_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_remove_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_NEXT_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_next_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CMD_PREV_SCENE: {
|
||||
int cap = atomic_load(&channel_capacity);
|
||||
struct channel_t *cur = get_channels_array();
|
||||
int bind = atomic_load(&bind_channel);
|
||||
int ch = bind;
|
||||
if (ch < cap) {
|
||||
channel_prev_scene(client, ch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user