1
0
Fork 0
forked from hanez/xtc

Phase 12: Improve audio transitions and verify sample-accurate timing

This commit is contained in:
Johannes Findeisen 2026-10-27 03:37:19 +01:00
commit 05140c2e93
29 changed files with 795 additions and 33 deletions

View file

@ -2,6 +2,7 @@
#include "wave_internal.h"
#include "../mixer/mix_internal.h"
#include "../graph/graph_internal.h"
#include "xtc/xtc_dsp.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
@ -12,6 +13,7 @@ typedef struct {
} region;
struct xtc_audio_plan {
size_t fade_frames;
xtc_mix_snapshot mix;
xtc_wave waves[64];
char paths[64][XTC_ASSET_PATH_BYTES];
@ -209,6 +211,13 @@ size_t xtc_audio_plan_issues(const xtc_audio_plan *plan, const char **path)
return plan ? plan->issues : 0;
}
xtc_result xtc_audio_plan_set_fade_frames(xtc_audio_plan *plan, size_t frames)
{
if (!plan || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
plan->fade_frames = frames;
return XTC_OK;
}
/**
* @brief Renders a block of the plan at unity gain with linear rate conversion.
*
@ -235,8 +244,16 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
for (i = 0; i < plan->count; ++i) {
const region *r = plan->regions + i;
const xtc_wave *wave = plan->waves + r->asset;
uint64_t audible_end = r->end;
uint64_t begin = position > r->start ? position : r->start;
uint64_t end = position + frames < r->end ? position + frames : r->end;
uint64_t end;
if (plan->fade_frames && wave->samples) {
uint64_t duration = ((uint64_t)wave->frames * plan->rate + wave->rate - 1U) / wave->rate;
if (duration <= r->offset) continue;
if (duration - r->offset < audible_end - r->start)
audible_end = r->start + duration - r->offset;
}
end = position + frames < audible_end ? position + frames : audible_end;
if (!wave->samples || end <= begin)
continue;
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
@ -250,16 +267,21 @@ xtc_result xtc_audio_plan_render_stereo(const xtc_audio_plan *plan, uint64_t pos
float sample =
(float)(wave->samples[index] * (1.0 - fraction) +
(index + 1 < wave->frames ? wave->samples[index + 1] : wave->samples[index]) * fraction);
if (plan->fade_frames) {
uint64_t age = position + j - r->start;
uint64_t tail = audible_end - 1U - (position + j);
uint64_t distance = age < tail ? age : tail;
if (distance < plan->fade_frames)
sample *= (float)distance / (float)plan->fade_frames;
}
output[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, right ? 0U : 2U);
if (right)
right[j] += sample * xtc_mix_level(&plan->mix, r->track, position + j, 1);
}
}
for (j = 0; j < frames; ++j) {
if (!isfinite(output[j]))
output[j] = 0;
if (right && !isfinite(right[j]))
right[j] = 0;
output[j] = xtc_dsp_finite(output[j]);
if (right) right[j] = xtc_dsp_finite(right[j]);
}
return XTC_OK;
}

View file

@ -1,5 +1,6 @@
#include "xtc/xtc_backend.h"
#include "xtc/xtc_realtime.h"
#include "xtc/xtc_dsp.h"
#include <stdlib.h>
#include <string.h>
@ -19,6 +20,10 @@ struct xtc_backend {
xtc_recorder *recorder; /* Borrowed, installed while callbacks quiescent. */
float *timeline, *instrument;
size_t max_frames;
size_t transition_frames; /* Control owner while stopped. */
xtc_gain_ramp ramp; /* Audio owner while active. */
unsigned int stop_requested, stop_done;
int stop_seen; /* Audio owner. */
uint64_t sample_position;
unsigned int expected_rate;
unsigned int rate, frames, lost, rate_changed, xruns;
@ -104,6 +109,13 @@ static int process(jack_nframes_t frames, void *context)
__atomic_store_n(&backend->process_result, (unsigned int)XTC_ERR_IO, __ATOMIC_RELAXED);
return 0;
}
if (__atomic_load_n(&backend->stop_done, __ATOMIC_ACQUIRE)) {
memset(output, 0, (size_t)frames * sizeof(*output));
if (right) memset(right, 0, (size_t)frames * sizeof(*right));
__atomic_store_n(&backend->peak_left, 0U, __ATOMIC_RELAXED);
__atomic_store_n(&backend->peak_right, 0U, __ATOMIC_RELAXED);
return 0;
}
if (__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE)) {
result = XTC_ERR_INVALID_STATE;
@ -128,6 +140,15 @@ static int process(jack_nframes_t frames, void *context)
backend->midi && right ? backend->instrument + backend->max_frames : NULL,
output, right, (size_t)frames);
}
if (result == XTC_OK && backend->transition_frames) {
if (__atomic_load_n(&backend->stop_requested, __ATOMIC_ACQUIRE) && !backend->stop_seen) {
(void)xtc_gain_ramp_target(&backend->ramp, 0, backend->transition_frames);
backend->stop_seen = 1;
}
(void)xtc_gain_ramp_process(&backend->ramp, output, right, (size_t)frames);
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)
@ -361,6 +382,11 @@ xtc_result xtc_backend_start(xtc_backend *backend)
return XTC_ERR_INVALID_STATE;
xtc_midi_plan_reset(backend->midi);
backend->panic_seen = __atomic_load_n(&backend->panic_requested, __ATOMIC_ACQUIRE);
__atomic_store_n(&backend->stop_requested, 0U, __ATOMIC_RELAXED);
__atomic_store_n(&backend->stop_done, 0U, __ATOMIC_RELAXED);
backend->stop_seen = 0;
(void)xtc_gain_ramp_reset(&backend->ramp, backend->transition_frames ? 0 : 1);
(void)xtc_gain_ramp_target(&backend->ramp, 1, backend->transition_frames);
if (jack_activate(backend->client) != 0)
return XTC_ERR_IO;
backend->running = 1;
@ -386,6 +412,8 @@ xtc_result xtc_backend_stop(xtc_backend *backend)
if (jack_deactivate(backend->client) != 0)
return XTC_ERR_IO;
backend->running = 0;
__atomic_store_n(&backend->stop_requested, 0U, __ATOMIC_RELAXED);
__atomic_store_n(&backend->stop_done, 0U, __ATOMIC_RELAXED);
xtc_midi_plan_reset(backend->midi);
xtc_realtime_collect(backend->engine);
return XTC_OK;
@ -462,8 +490,12 @@ xtc_result xtc_backend_prepare_project(xtc_backend *backend, const xtc_project *
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_audio_plan_set_fade_frames(candidate, backend->transition_frames);
if (result == XTC_OK)
result = xtc_midi_plan_create(project, backend->expected_rate, &midi);
if (result == XTC_OK)
result = xtc_midi_plan_set_fade_frames(midi, backend->transition_frames);
if (result == XTC_OK)
result = xtc_backend_submit(backend, xtc_project_graph(project));
if (result != XTC_OK) {
@ -547,15 +579,38 @@ xtc_result xtc_backend_midi_port_name(const xtc_backend *backend, const char **o
return XTC_OK;
}
/**
* @brief Reclaims retired realtime plans on the control thread.
*
* @param backend Backend handle.
*/
/* Control-only configuration; applied to source plans on next preparation. */
xtc_result xtc_backend_set_transition_frames(xtc_backend *backend, size_t frames)
{
if (!backend || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->running) return XTC_ERR_INVALID_STATE;
backend->transition_frames = frames;
return XTC_OK;
}
xtc_result xtc_backend_request_stop(xtc_backend *backend)
{
if (!backend) return XTC_ERR_INVALID_ARGUMENT;
if (!backend->ready || backend->recorder) return XTC_ERR_INVALID_STATE;
if (!backend->running || !backend->transition_frames ||
__atomic_load_n(&backend->lost, __ATOMIC_ACQUIRE) ||
__atomic_load_n(&backend->rate_changed, __ATOMIC_ACQUIRE) ||
__atomic_load_n(&backend->process_result, __ATOMIC_RELAXED) != (unsigned int)XTC_OK)
return xtc_backend_stop(backend);
__atomic_store_n(&backend->stop_requested, 1U, __ATOMIC_RELEASE);
return XTC_OK;
}
/* Control thread: acknowledgement is not quiescence until stop succeeds. */
void xtc_backend_collect(xtc_backend *backend)
{
if (backend != NULL)
if (backend != NULL) {
if (__atomic_load_n(&backend->stop_done, __ATOMIC_ACQUIRE)) {
if (xtc_backend_stop(backend) != XTC_OK)
__atomic_store_n(&backend->process_result, (unsigned int)XTC_ERR_IO, __ATOMIC_RELAXED);
}
xtc_realtime_collect(backend->engine);
}
}
/**
@ -577,6 +632,7 @@ xtc_result xtc_backend_get_status(const xtc_backend *backend, xtc_backend_status
backend->running ? (float)__atomic_load_n(&backend->peak_left, __ATOMIC_RELAXED) / 1000000.0f : 0;
out_status->peak_right =
backend->running ? (float)__atomic_load_n(&backend->peak_right, __ATOMIC_RELAXED) / 1000000.0f : 0;
out_status->stopping = (int)__atomic_load_n(&backend->stop_requested, __ATOMIC_ACQUIRE);
out_status->midi_dropped = __atomic_load_n(&backend->midi_dropped, __ATOMIC_RELAXED);
out_status->asset_issues = (unsigned int)xtc_audio_plan_issues(backend->audio, NULL);
out_status->sample_rate = __atomic_load_n(&backend->rate, __ATOMIC_RELAXED);
@ -968,4 +1024,15 @@ xtc_result xtc_backend_disconnect(xtc_backend *backend, xtc_port_direction direc
(void)remote;
return XTC_ERR_UNSUPPORTED;
}
xtc_result xtc_backend_set_transition_frames(xtc_backend *backend, size_t frames)
{
(void)backend; (void)frames;
return XTC_ERR_UNSUPPORTED;
}
xtc_result xtc_backend_request_stop(xtc_backend *backend)
{
(void)backend;
return XTC_ERR_UNSUPPORTED;
}
#endif

38
src/dsp/ramp.c Normal file
View file

@ -0,0 +1,38 @@
#include "xtc/xtc_dsp.h"
#include <math.h>
float xtc_dsp_finite(float sample)
{
return !isfinite(sample) || fpclassify(sample) == FP_SUBNORMAL ? 0.0f : sample;
}
xtc_result xtc_gain_ramp_reset(xtc_gain_ramp *ramp, float value)
{
if (!ramp || !isfinite(value)) return XTC_ERR_INVALID_ARGUMENT;
ramp->current = ramp->target = value;
ramp->remaining = 0;
return XTC_OK;
}
xtc_result xtc_gain_ramp_target(xtc_gain_ramp *ramp, float target, size_t frames)
{
if (!ramp || !isfinite(target) || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
ramp->target = target;
ramp->remaining = frames;
if (!frames) ramp->current = target;
return XTC_OK;
}
xtc_result xtc_gain_ramp_process(xtc_gain_ramp *ramp, float *left, float *right, size_t frames)
{
size_t i;
if (!ramp || !left || (right && left == right) || frames > 65536)
return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < frames; ++i) {
if (ramp->remaining) {
ramp->current = (float)((double)ramp->current +
((double)ramp->target - ramp->current) / (double)ramp->remaining);
if (!--ramp->remaining) ramp->current = ramp->target;
}
left[i] = xtc_dsp_finite(left[i] * ramp->current);
if (right) right[i] = xtc_dsp_finite(right[i] * ramp->current);
}
return XTC_OK;
}

View file

@ -1,3 +1,4 @@
#include "xtc/xtc_dsp.h"
#include "../graph/graph_internal.h"
#include <math.h>
#include <stdlib.h>
@ -234,8 +235,7 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const flo
if (spec.type == XTC_NODE_GAIN)
sample *= spec.value;
}
if (!isfinite(sample))
sample = 0.0f;
sample = xtc_dsp_finite(sample);
buffer[frame] = sample;
if (fabsf(sample) > peak)
peak = fabsf(sample);
@ -269,7 +269,7 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const flo
for (index = 0U; index < plan->node_count; ++index)
if (plan->render[index])
sample += plan->buffers[(channel * plan->node_count + index) * plan->max_frames + frame];
destination[frame] = isfinite(sample) ? sample : 0.0f;
destination[frame] = xtc_dsp_finite(sample);
}
}
plan->position += frame_count;

View file

@ -366,7 +366,12 @@ void MainWindow::open_backend()
if (result != XTC_OK)
report_error("Open JACK failed", result);
else {
result = xtc_backend_submit(backend_, xtc_project_graph(project_.get()));
xtc_backend_status format{};
result = xtc_backend_get_status(backend_, &format);
if (result == XTC_OK)
result = xtc_backend_set_transition_frames(backend_, format.sample_rate / 200U);
if (result == XTC_OK)
result = xtc_backend_submit(backend_, xtc_project_graph(project_.get()));
if (result != XTC_OK) {
ready_ = false; // Handle is retained for close/reconnect; graph was not installed.
report_error("Submit graph failed", result);
@ -410,7 +415,7 @@ void MainWindow::start_engine()
}
/**
* @brief Stops the audio engine, finalizing any active recording.
* @brief Requests a graceful playback stop, or immediately finalizes recording.
*
* Does nothing when disconnected. Failures are reported in the window.
*/
@ -418,7 +423,7 @@ void MainWindow::stop_engine()
{
if (!ready_)
return;
xtc_result result = xtc_backend_stop(backend_);
xtc_result result = recorder_ ? xtc_backend_stop(backend_) : xtc_backend_request_stop(backend_);
if (result != XTC_OK)
report_error("Stop engine failed", result);
else {
@ -431,7 +436,7 @@ void MainWindow::stop_engine()
xtc_result MainWindow::pause_playback()
{
xtc_result result = recorder_ || !ready_ ? XTC_ERR_INVALID_STATE : xtc_backend_stop(backend_);
xtc_result result = recorder_ || !ready_ ? XTC_ERR_INVALID_STATE : xtc_backend_request_stop(backend_);
if (result != XTC_OK)
report_error("Pause failed; use Stop to finish recording or Reconnect audio", result);
else
@ -504,6 +509,7 @@ void MainWindow::refresh_status()
indicator_->labelcolor(fl_rgb_color(255, 155, 120));
set_label(graph_state_, "Graph: offline — edits stored locally; Reconnect to play");
} else {
xtc_backend_collect(backend_); // Completes an acknowledged graceful stop before reading status.
xtc_backend_status status = {};
xtc_result result = xtc_backend_get_status(backend_, &status);
if (result != XTC_OK) {
@ -523,6 +529,7 @@ void MainWindow::refresh_status()
const char *state = status.server_lost ? "Audio: server lost — Reconnect required"
: status.sample_rate_changed ? "Audio: sample rate changed — Reconnect required"
: status.process_result != XTC_OK ? "Audio: processing error"
: status.stopping ? "Audio: stopping — fading to silence"
: recorder_ ? "Audio: recording JACK input — Stop to finish"
: running_ ? "Audio: running — modular graph"
: "Audio: ready / stopped";
@ -546,7 +553,6 @@ void MainWindow::refresh_status()
else
set_label(graph_state_, running_ ? "Graph: active in engine" : "Graph: stopped");
}
xtc_backend_collect(backend_);
}
if (can_play)
play_->activate();
@ -683,7 +689,7 @@ void MainWindow::view_callback(Fl_Widget *widget, void *view)
*/
void MainWindow::about_callback(Fl_Widget *, void *)
{
fl_message("%s %s\nModular audio workstation\nPhase 11: create, arrange, save, reopen and play.",
fl_message("%s %s\nModular audio workstation\nPhase 12: timing, transitions and audio quality.",
XTC_PROJECT_NAME, XTC_VERSION_STRING);
}

View file

@ -148,8 +148,9 @@ class MainWindow : public Fl_Double_Window {
/** Creates a fresh untitled project; the File menu checks unsaved changes first.
* On failure the current document remains owned by the window. */
xtc_result new_project();
/** Stops playback, retaining its exact sample position. Recording must be
* finalized with Stop instead. Returns the backend failure, if any. */
/** Requests a playback fade/stop; the timer completes deactivation after
* callback acknowledgement. Recording must use immediate Stop instead.
* Returns request acceptance or the backend failure. */
xtc_result pause_playback();
/** Stops playback and seeks to zero. Refuses during recording. A failed
* stop/seek is reported without moving the displayed cursor to zero. */
@ -332,7 +333,7 @@ class MainWindow : public Fl_Double_Window {
*/
void start_engine();
/**
* @brief Stops the audio engine, finalizing any active recording.
* @brief Requests a graceful playback stop, or immediately finalizes recording.
*
* Does nothing when disconnected. Failures are reported in the window.
*/

View file

@ -1,4 +1,5 @@
#include "xtc/xtc_midi.h"
#include "xtc/xtc_dsp.h"
#include "../mixer/mix_internal.h"
#include <math.h>
#include <stdlib.h>
@ -24,6 +25,7 @@ typedef struct {
} voice;
struct xtc_midi_plan {
size_t fade_frames;
xtc_mix_snapshot mix;
span notes[XTC_PROJECT_MAX_NOTES];
size_t count;
@ -281,6 +283,13 @@ static void event(xtc_midi_plan *p, const xtc_midi_event *e, uint64_t position)
* @param right Destination right-channel buffer; NULL selects mono rendering.
* @param frames Number of audio frames.
*/
xtc_result xtc_midi_plan_set_fade_frames(xtc_midi_plan *plan, size_t frames)
{
if (!plan || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
plan->fade_frames = frames;
return XTC_OK;
}
static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *right, size_t frames)
{
size_t i, j;
@ -294,6 +303,13 @@ static void segment(xtc_midi_plan *p, uint64_t position, float *out, float *righ
for (j = (size_t)(begin - position); j < (size_t)(end - position); ++j) {
double phase = fmod((double)(position + j - s->start + s->age) * step, 1.0);
float sample = (float)(sin(TAU * phase) * 0.1 * s->velocity / 127.0);
if (p->fade_frames) {
uint64_t age = position + j - s->start;
uint64_t tail = s->end - 1U - (position + j);
uint64_t distance = age < tail ? age : tail;
if (distance < p->fade_frames)
sample *= (float)distance / (float)p->fade_frames;
}
out[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, right ? 0U : 2U);
if (right)
right[j] += sample * xtc_mix_level(&p->mix, s->track, position + j, 1);
@ -356,6 +372,10 @@ xtc_result xtc_midi_plan_render_stereo(xtc_midi_plan *p, uint64_t position, cons
cursor = next;
}
segment(p, position + cursor, out + cursor, right ? right + cursor : NULL, frames - cursor);
for (i = 0; i < frames; ++i) {
out[i] = xtc_dsp_finite(out[i]);
if (right) right[i] = xtc_dsp_finite(right[i]);
}
p->next_position = position + frames;
return XTC_OK;
}

View file

@ -1,3 +1,4 @@
#include "xtc/xtc_dsp.h"
#include "xtc/xtc_native.h"
#include <math.h>
#include <stdlib.h>
@ -139,8 +140,7 @@ xtc_result xtc_native_process(xtc_native *p, const xtc_native_block *b)
return r;
}
for (i = 0; i < b->frames; ++i)
if (!isfinite(b->output[i]))
b->output[i] = 0;
b->output[i] = xtc_dsp_finite(b->output[i]);
p->next_position = b->position + b->frames;
return XTC_OK;
}