Phase 35: add persistent preferences and sequencer loop playback

This commit is contained in:
Johannes Findeisen 2026-10-28 10:37:01 +01:00
commit 47b7d38a66
21 changed files with 852 additions and 108 deletions

View file

@ -29,6 +29,8 @@ struct xtc_backend {
unsigned int stop_requested, stop_done;
int stop_seen; /* Audio owner. */
uint64_t sample_position;
uint64_t loop_start, loop_end;
int loop_enabled;
unsigned int expected_rate;
unsigned int rate, frames, lost, rate_changed, xruns;
unsigned int process_result;
@ -45,22 +47,22 @@ static void publish_peak(unsigned int *slot, float peak)
}
/**
* @brief Decodes queued JACK MIDI and renders the instrument for the current audio block.
* @brief Decodes queued JACK MIDI once for the current audio block.
*
* @param backend Backend handle.
* @param position Absolute sample position at the start of the block.
* @param frames Number of audio frames.
* @return XTC_OK on success, or an error code describing the failure.
* @param events Caller-owned bounded event storage.
* @return Number of decoded events; malformed input requests a MIDI panic.
*/
static xtc_result render_midi(xtc_backend *backend, uint64_t position, jack_nframes_t frames)
static size_t decode_midi(xtc_backend *backend, uint64_t position, jack_nframes_t frames, xtc_midi_event events[XTC_MIDI_MAX_EVENTS])
{
xtc_midi_event events[XTC_MIDI_MAX_EVENTS];
size_t count = 0;
uint32_t i, total;
unsigned int requested = __atomic_load_n(&backend->panic_requested, __ATOMIC_ACQUIRE);
void *buffer;
if (!backend->midi)
return XTC_OK;
return 0;
if (requested != backend->panic_seen) {
xtc_midi_plan_panic(backend->midi, position);
backend->panic_seen = requested;
@ -91,9 +93,7 @@ static xtc_result render_midi(xtc_backend *backend, uint64_t position, jack_nfra
}
++count;
}
return xtc_midi_plan_render_stereo(backend->midi, position, events, count, backend->instrument,
backend->output_right ? backend->instrument + backend->max_frames : NULL,
(size_t)frames);
return count;
}
/**
@ -137,27 +137,50 @@ static int process(jack_nframes_t frames, void *context)
} else {
const float *input = jack_port_get_buffer(backend->input, frames);
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
(void)xtc_realtime_position(backend->engine,position);
if (backend->recorder)
xtc_recorder_push(backend->recorder, input, (size_t)frames);
{
size_t channel;
xtc_midi_event events[XTC_MIDI_MAX_EVENTS], segment_events[XTC_MIDI_MAX_EVENTS];
size_t count = decode_midi(backend,position,frames,events), offset = 0;
if (backend->recorder) xtc_recorder_push(backend->recorder,input,(size_t)frames);
while (offset < frames && result == XTC_OK) {
size_t n = (size_t)frames-offset, i, queued = 0, channel;
if (backend->loop_enabled) {
if (position < backend->loop_start || position >= backend->loop_end) position = backend->loop_start;
if (backend->loop_end-position < n) n = (size_t)(backend->loop_end-position);
}
if (position > UINT64_MAX-n) { result = XTC_ERR_CAPACITY; break; }
for (channel = 0; channel < backend->meter_count*2; ++channel)
memset(backend->monitor + channel*backend->max_frames, 0, (size_t)frames*sizeof(float));
memset(backend->monitor+channel*backend->max_frames,0,n*sizeof(float));
(void)xtc_realtime_position(backend->engine,position);
if (backend->audio)
result = xtc_audio_plan_render_stereo(backend->audio,position,backend->timeline,
right ? backend->timeline+backend->max_frames : NULL,n);
for (i = 0; i < count; ++i) if (events[i].offset >= offset && events[i].offset < offset+n) {
segment_events[queued] = events[i]; segment_events[queued++].offset -= (unsigned int)offset;
}
if (result == XTC_OK && backend->midi)
result = xtc_midi_plan_render_stereo(backend->midi,position,segment_events,queued,backend->instrument,
right ? backend->instrument+backend->max_frames : NULL,n);
if (result == XTC_OK)
result = xtc_realtime_process_stereo(backend->engine,input ? input+offset : NULL,
backend->audio ? backend->timeline : NULL,
backend->audio && right ? backend->timeline+backend->max_frames : NULL,
backend->midi ? backend->instrument : NULL,
backend->midi && right ? backend->instrument+backend->max_frames : NULL,
output+offset,right ? right+offset : NULL,n);
if (result == XTC_OK) {
size_t track, side, frame;
for (track = 0; track < backend->meter_count; ++track) for (side = 0; side < 2; ++side) {
float peak = 0;
for (frame = 0; frame < n; ++frame) {
float value = fabsf(backend->monitor[(track*2+side)*backend->max_frames+frame]);
if (isfinite(value) && value > peak) peak = value;
}
publish_peak(&backend->meter_peaks[track][side],peak);
}
}
offset += n; position += n;
if (backend->loop_enabled && position == backend->loop_end) position = backend->loop_start;
}
if (backend->audio)
result =
xtc_audio_plan_render_stereo(backend->audio, position, backend->timeline,
right ? backend->timeline + backend->max_frames : NULL, (size_t)frames);
if (result == XTC_OK)
result = render_midi(backend, position, frames);
if (result == XTC_OK)
result =
xtc_realtime_process_stereo(backend->engine, input, backend->audio ? backend->timeline : NULL,
backend->audio && right ? backend->timeline + backend->max_frames : NULL,
backend->midi ? backend->instrument : NULL,
backend->midi && right ? backend->instrument + backend->max_frames : NULL,
output, right, (size_t)frames);
if (result == XTC_OK) __atomic_store_n(&backend->sample_position,position,__ATOMIC_RELAXED);
}
if (result == XTC_OK && backend->transition_frames) {
if (__atomic_load_n(&backend->stop_requested, __ATOMIC_ACQUIRE) && !backend->stop_seen) {
@ -168,13 +191,6 @@ static int process(jack_nframes_t frames, void *context)
if (backend->stop_seen && !backend->ramp.remaining)
__atomic_store_n(&backend->stop_done, 1U, __ATOMIC_RELEASE);
}
if (result == XTC_OK) {
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
if (position > UINT64_MAX - frames)
result = XTC_ERR_CAPACITY;
else
__atomic_store_n(&backend->sample_position, position + frames, __ATOMIC_RELAXED);
}
if (result != XTC_OK) {
(void)__atomic_fetch_add(&backend->panic_requested, 1U, __ATOMIC_RELEASE);
if (backend->recorder)
@ -195,18 +211,6 @@ static int process(jack_nframes_t frames, void *context)
}
publish_peak(backend->master_peaks, left_peak);
publish_peak(backend->master_peaks+1, right_peak);
if (result == XTC_OK) {
size_t track, side, frame;
for (track = 0; track < backend->meter_count; ++track)
for (side = 0; side < 2; ++side) {
float peak = 0;
for (frame = 0; frame < frames; ++frame) {
float value = fabsf(backend->monitor[(track*2+side)*backend->max_frames+frame]);
if (isfinite(value) && value > peak) peak = value;
}
publish_peak(&backend->meter_peaks[track][side], peak);
}
}
__atomic_store_n(&backend->peak_left, (unsigned int)(fminf(left_peak, 1000) * 1000000), __ATOMIC_RELAXED);
__atomic_store_n(&backend->peak_right, (unsigned int)(fminf(right_peak, 1000) * 1000000), __ATOMIC_RELAXED);
}
@ -592,6 +596,7 @@ const char *xtc_backend_asset_issue(const xtc_backend *backend)
*/
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder)
{
if (backend && recorder && backend->loop_enabled) return XTC_ERR_INVALID_STATE;
if (!backend)
return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->running)
@ -639,6 +644,13 @@ xtc_result xtc_backend_midi_port_name(const xtc_backend *backend, const char **o
}
/* Control-only configuration; applied to source plans on next preparation. */
xtc_result xtc_backend_set_loop(xtc_backend *backend, uint64_t start, uint64_t end, int enabled)
{
if (!backend || (enabled != 0 && enabled != 1) || (enabled && (end <= start || end-start < 64))) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->running || (enabled && backend->recorder)) return XTC_ERR_INVALID_STATE;
backend->loop_start = start; backend->loop_end = end; backend->loop_enabled = enabled; return XTC_OK;
}
xtc_result xtc_backend_set_transition_frames(xtc_backend *backend, size_t frames)
{
if (!backend || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
@ -1099,6 +1111,12 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
(void)remote;
return XTC_ERR_UNSUPPORTED;
}
xtc_result xtc_backend_set_loop(xtc_backend *backend, uint64_t start, uint64_t end, int enabled)
{
(void)start; (void)end; (void)enabled;
return backend ? XTC_ERR_UNSUPPORTED : XTC_ERR_INVALID_ARGUMENT;
}
xtc_result xtc_backend_set_transition_frames(xtc_backend *backend, size_t frames)
{
(void)backend; (void)frames;