1
0
Fork 0
forked from hanez/xtc

Phase 6: Add WAV clips, graph playback and bounded recording

This commit is contained in:
Johannes Findeisen 2026-10-27 00:31:03 +01:00
commit 8a37090b81
40 changed files with 1450 additions and 69 deletions

View file

@ -11,6 +11,9 @@ struct xtc_backend {
jack_client_t *client;
jack_port_t *input, *output;
xtc_realtime *engine;
xtc_audio_plan *audio;
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
float *timeline;
size_t max_frames;
uint64_t sample_position;
unsigned int expected_rate;
@ -25,6 +28,7 @@ static int process(jack_nframes_t frames, void *context)
float *output = jack_port_get_buffer(backend->output, frames);
xtc_result result = XTC_OK;
if (output == NULL) {
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
__atomic_store_n(&backend->process_result, (unsigned int)XTC_ERR_IO, __ATOMIC_RELAXED);
return 0;
}
@ -35,14 +39,23 @@ static int process(jack_nframes_t frames, void *context)
result = XTC_ERR_CAPACITY;
} else {
const float *input = jack_port_get_buffer(backend->input, frames);
result = xtc_realtime_process(backend->engine, input, output, (size_t)frames);
uint64_t position = __atomic_load_n(&backend->sample_position, __ATOMIC_RELAXED);
if (backend->recorder) xtc_recorder_push(backend->recorder, input, (size_t)frames);
if (backend->audio)
result = xtc_audio_plan_render(backend->audio, position, backend->timeline, (size_t)frames);
if (result == XTC_OK)
result = xtc_realtime_process_audio(backend->engine, input,
backend->audio ? backend->timeline : NULL, output, (size_t)frames);
}
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) memset(output, 0, (size_t)frames * sizeof(*output));
if (result != XTC_OK) {
if (backend->recorder) xtc_recorder_push(backend->recorder, NULL, 1U);
memset(output, 0, (size_t)frames * sizeof(*output));
}
__atomic_store_n(&backend->process_result, (unsigned int)result, __ATOMIC_RELAXED);
return 0; /* Keep connected and silent on recoverable processing errors. */
}
@ -87,9 +100,11 @@ xtc_result xtc_backend_open(const xtc_backend_config *config, xtc_backend **out_
if (strlen(config->client_name) >= (size_t)jack_client_name_size()) return XTC_ERR_INVALID_ARGUMENT;
backend = calloc(1U, sizeof(*backend));
if (backend == NULL) return XTC_ERR_OUT_OF_MEMORY;
backend->timeline = calloc(config->max_frames, sizeof(float));
if (!backend->timeline) { free(backend); return XTC_ERR_OUT_OF_MEMORY; }
backend->max_frames = config->max_frames;
backend->client = jack_client_open(config->client_name, JackNoStartServer, &status);
if (backend->client == NULL) { free(backend); return XTC_ERR_IO; }
if (backend->client == NULL) { free(backend->timeline); free(backend); return XTC_ERR_IO; }
backend->expected_rate = (unsigned int)jack_get_sample_rate(backend->client);
backend->rate = backend->expected_rate;
backend->frames = (unsigned int)jack_get_buffer_size(backend->client);
@ -118,6 +133,8 @@ fail:
return XTC_ERR_IO;
}
xtc_realtime_destroy(backend->engine);
xtc_audio_plan_destroy(backend->audio);
free(backend->timeline);
free(backend);
return result;
}
@ -128,6 +145,8 @@ xtc_result xtc_backend_close(xtc_backend *backend)
/* JACK removes this client and joins callbacks before returning success. */
if (jack_client_close(backend->client) != 0) return XTC_ERR_IO;
xtc_realtime_destroy(backend->engine);
xtc_audio_plan_destroy(backend->audio);
free(backend->timeline);
free(backend);
return XTC_OK;
}
@ -182,6 +201,33 @@ xtc_result xtc_backend_submit(xtc_backend *backend, const xtc_graph *graph)
}
return xtc_realtime_submit(backend->engine, graph);
}
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project)
{
xtc_audio_plan *candidate = NULL;
xtc_result result;
if (!backend || !project) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->running) return XTC_ERR_INVALID_STATE;
result = xtc_audio_plan_create(project, backend->expected_rate, &candidate);
if (result == XTC_OK) result = xtc_backend_submit(backend, xtc_project_graph(project));
if (result != XTC_OK) { xtc_audio_plan_destroy(candidate); return result; }
xtc_audio_plan_destroy(backend->audio); backend->audio = candidate;
return XTC_OK;
}
const char *xtc_backend_asset_issue(const xtc_backend *backend)
{
const char *path = "";
if (backend) xtc_audio_plan_issues(backend->audio, &path);
return path;
}
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder)
{
if (!backend) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->running) return XTC_ERR_INVALID_STATE;
if (recorder && (__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE))) return XTC_ERR_INVALID_STATE;
backend->recorder = recorder;
return XTC_OK;
}
void xtc_backend_collect(xtc_backend *backend)
{ if (backend != NULL) xtc_realtime_collect(backend->engine); }
xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status *out_status)
@ -190,6 +236,7 @@ xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status
if (backend == NULL || out_status == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready) return XTC_ERR_INVALID_STATE;
(void)xtc_realtime_get_status(backend->engine, &engine_status);
out_status->asset_issues = (unsigned int)xtc_audio_plan_issues(backend->audio, NULL);
out_status->sample_rate = __atomic_load_n(&backend->rate, __ATOMIC_RELAXED);
out_status->buffer_frames = __atomic_load_n(&backend->frames, __ATOMIC_RELAXED);
out_status->xruns = __atomic_load_n(&backend->xruns, __ATOMIC_RELAXED);
@ -250,6 +297,12 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
{ return route(backend, direction, remote, 1); }
#else
xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *project)
{ (void)backend; (void)project; return XTC_ERR_UNSUPPORTED; }
const char *xtc_backend_asset_issue(const xtc_backend *backend)
{ (void)backend; return ""; }
xtc_result xtc_backend_set_recorder(xtc_backend *backend, xtc_recorder *recorder)
{ (void)backend; (void)recorder; return XTC_ERR_UNSUPPORTED; }
xtc_result xtc_backend_seek(xtc_backend *backend, uint64_t sample)
{ (void)backend; (void)sample; return XTC_ERR_UNSUPPORTED; }
/* Keep core/offline builds available on machines without JACK headers. */