Some code semantics refactoring.

This commit is contained in:
Johannes Findeisen 2026-10-27 01:37:21 +01:00
commit 0f04e056b2
42 changed files with 3374 additions and 1357 deletions

View file

@ -20,9 +20,12 @@ struct xtc_plan {
void xtc_plan_destroy(xtc_plan *plan)
{
if (plan == NULL) return;
if (plan == NULL)
return;
if (plan->native) {
size_t i; for (i = 0; i < plan->node_count * 2; ++i) xtc_native_destroy(plan->native[i]);
size_t i;
for (i = 0; i < plan->node_count * 2; ++i)
xtc_native_destroy(plan->native[i]);
}
free(plan->native);
free(plan->nodes);
@ -36,27 +39,29 @@ void xtc_plan_destroy(xtc_plan *plan)
free(plan);
}
xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
size_t max_frames, xtc_plan **out_plan)
xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate, size_t max_frames, xtc_plan **out_plan)
{
xtc_plan *plan;
xtc_result result;
size_t node, edge, storage_nodes, cursor = 0U;
int has_output = 0;
if (out_plan == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (out_plan == NULL)
return XTC_ERR_INVALID_ARGUMENT;
*out_plan = NULL;
if (graph == NULL || !isfinite(sample_rate) || sample_rate <= 0.0 ||
max_frames == 0U || max_frames > XTC_MAX_FRAMES) return XTC_ERR_INVALID_ARGUMENT;
if (graph == NULL || !isfinite(sample_rate) || sample_rate <= 0.0 || max_frames == 0U ||
max_frames > XTC_MAX_FRAMES)
return XTC_ERR_INVALID_ARGUMENT;
storage_nodes = graph->node_count == 0U ? 1U : graph->node_count;
for (node = 0U; node < graph->node_count; ++node) {
if (graph->nodes[node].type == XTC_NODE_OSCILLATOR &&
(double)graph->nodes[node].value > sample_rate * 0.5)
if (graph->nodes[node].type == XTC_NODE_OSCILLATOR && (double)graph->nodes[node].value > sample_rate * 0.5)
return XTC_ERR_INVALID_ARGUMENT;
if (graph->nodes[node].type == XTC_NODE_OUTPUT) has_output = 1;
if (graph->nodes[node].type == XTC_NODE_OUTPUT)
has_output = 1;
}
/* Fixed graph/frame limits bound all allocation products below. */
plan = calloc(1U, sizeof(*plan));
if (plan == NULL) return XTC_ERR_OUT_OF_MEMORY;
if (plan == NULL)
return XTC_ERR_OUT_OF_MEMORY;
plan->node_count = graph->node_count;
plan->max_frames = max_frames;
plan->sample_rate = sample_rate;
@ -71,13 +76,16 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
plan->peaks = calloc(storage_nodes, sizeof(*plan->peaks));
plan->phases = calloc(storage_nodes, sizeof(*plan->phases));
if (plan->native == NULL || plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
plan->sources == NULL || plan->render == NULL || plan->buffers == NULL ||
plan->peaks == NULL || plan->phases == NULL) {
plan->sources == NULL || plan->render == NULL || plan->buffers == NULL || plan->peaks == NULL ||
plan->phases == NULL) {
xtc_plan_destroy(plan);
return XTC_ERR_OUT_OF_MEMORY;
}
result = xtc_graph_order(graph, plan->order);
if (result != XTC_OK) { xtc_plan_destroy(plan); return result; }
if (result != XTC_OK) {
xtc_plan_destroy(plan);
return result;
}
memcpy(plan->nodes, graph->nodes, plan->node_count * sizeof(*plan->nodes));
for (node = 0U; node < plan->node_count; ++node) {
int terminal = 1;
@ -85,18 +93,23 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
for (edge = 0U; edge < graph->edge_count; ++edge) {
if (graph->edges[edge].destination == node)
plan->sources[cursor++] = graph->edges[edge].source;
if (graph->edges[edge].source == node) terminal = 0;
if (graph->edges[edge].source == node)
terminal = 0;
}
plan->render[node] = (unsigned char)(has_output || graph->require_output_nodes ?
plan->nodes[node].type == XTC_NODE_OUTPUT : terminal);
plan->render[node] =
(unsigned char)(has_output || graph->require_output_nodes ? plan->nodes[node].type == XTC_NODE_OUTPUT
: terminal);
}
for (node = 0; node < plan->node_count; ++node) {
if (plan->nodes[node].type == XTC_NODE_NATIVE) {
size_t side;
for (side = 0; side < 2; ++side) {
result = xtc_native_create(&plan->nodes[node].native, sample_rate, max_frames,
plan->nodes[node].value, &plan->native[side * plan->node_count + node]);
if (result != XTC_OK) { xtc_plan_destroy(plan); return result; }
result = xtc_native_create(&plan->nodes[node].native, sample_rate, max_frames, plan->nodes[node].value,
&plan->native[side * plan->node_count + node]);
if (result != XTC_OK) {
xtc_plan_destroy(plan);
return result;
}
}
}
}
@ -107,8 +120,13 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
xtc_result xtc_plan_reset(xtc_plan *plan)
{
if (plan == NULL) return XTC_ERR_INVALID_ARGUMENT;
{ size_t i; for (i = 0; i < plan->node_count * 2; ++i) xtc_native_reset(plan->native[i]); }
if (plan == NULL)
return XTC_ERR_INVALID_ARGUMENT;
{
size_t i;
for (i = 0; i < plan->node_count * 2; ++i)
xtc_native_reset(plan->native[i]);
}
plan->position = 0;
memset(plan->phases, 0, plan->node_count * sizeof(*plan->phases));
memset(plan->peaks, 0, plan->node_count * sizeof(*plan->peaks));
@ -117,25 +135,32 @@ xtc_result xtc_plan_reset(xtc_plan *plan)
xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_peak)
{
if (plan == NULL || out_peak == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (plan == NULL || out_peak == NULL)
return XTC_ERR_INVALID_ARGUMENT;
size_t index;
for (index = 0U; index < plan->node_count; ++index)
if (plan->nodes[index].id == node_id) break;
if (index == plan->node_count) return XTC_ERR_NOT_FOUND;
if (plan->nodes[index].id == node_id)
break;
if (index == plan->node_count)
return XTC_ERR_NOT_FOUND;
node_id = index;
if (plan->nodes[node_id].type != XTC_NODE_METER) return XTC_ERR_INVALID_ARGUMENT;
if (plan->nodes[node_id].type != XTC_NODE_METER)
return XTC_ERR_INVALID_ARGUMENT;
*out_peak = plan->peaks[node_id];
return XTC_OK;
}
xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
const float *timeline, const float *timeline_right, const float *instrument, const float *instrument_right,
float *output, float *right, size_t frame_count)
xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const float *timeline,
const float *timeline_right, const float *instrument, const float *instrument_right,
float *output, float *right, size_t frame_count)
{
size_t index, frame, edge, channel;
if (plan == NULL || output == NULL || (right && right == output)) return XTC_ERR_INVALID_ARGUMENT;
if (frame_count > plan->max_frames || plan->position > UINT64_MAX - frame_count) return XTC_ERR_CAPACITY;
if (frame_count == 0U) return XTC_OK;
if (plan == NULL || output == NULL || (right && right == output))
return XTC_ERR_INVALID_ARGUMENT;
if (frame_count > plan->max_frames || plan->position > UINT64_MAX - frame_count)
return XTC_ERR_CAPACITY;
if (frame_count == 0U)
return XTC_OK;
/* All external input reads finish before output is written, allowing aliasing.
* Nonfinite input/arithmetic becomes silence per sample, never persistent DSP state. */
for (channel = 0; channel < (right ? 2U : 1U); ++channel) {
@ -157,35 +182,48 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
} else if (spec.type == XTC_NODE_OSCILLATOR) {
sample = (float)sin(XTC_TAU * phase);
phase += (double)spec.value / plan->sample_rate;
if (phase >= 1.0) phase -= 1.0;
if (phase >= 1.0)
phase -= 1.0;
} else {
if (plan->offsets[node] == plan->offsets[node + 1U])
sample = input == NULL ? 0.0f : input[frame] * plan->input_gain;
for (edge = plan->offsets[node]; edge < plan->offsets[node + 1U]; ++edge)
sample += plan->buffers[(channel * plan->node_count + plan->sources[edge]) * plan->max_frames + frame];
if (spec.type == XTC_NODE_GAIN) sample *= spec.value;
sample += plan->buffers[(channel * plan->node_count + plan->sources[edge]) * plan->max_frames +
frame];
if (spec.type == XTC_NODE_GAIN)
sample *= spec.value;
}
if (!isfinite(sample)) sample = 0.0f;
if (!isfinite(sample))
sample = 0.0f;
buffer[frame] = sample;
if (fabsf(sample) > peak) peak = fabsf(sample);
if (fabsf(sample) > peak)
peak = fabsf(sample);
}
if (spec.type == XTC_NODE_NATIVE) {
xtc_native_block block = {spec.native.kind == XTC_NATIVE_EFFECT ? buffer : NULL,
buffer, frame_count, plan->position, NULL, 0};
buffer,
frame_count,
plan->position,
NULL,
0};
xtc_result result = xtc_native_process(plan->native[channel * plan->node_count + node], &block);
if (result != XTC_OK) {
memset(output, 0, frame_count * sizeof(*output));
if (right) memset(right, 0, frame_count * sizeof(*right));
if (right)
memset(right, 0, frame_count * sizeof(*right));
return result;
}
}
if (!right || channel == 1) plan->phases[node] = phase;
if (spec.type == XTC_NODE_METER) plan->peaks[node] = channel && plan->peaks[node] > peak ? plan->peaks[node] : peak;
if (!right || channel == 1)
plan->phases[node] = phase;
if (spec.type == XTC_NODE_METER)
plan->peaks[node] = channel && plan->peaks[node] > peak ? plan->peaks[node] : peak;
}
for (frame = 0U; frame < frame_count; ++frame) {
float sample = 0.0f;
for (index = 0U; index < plan->node_count; ++index)
if (plan->render[index]) sample += plan->buffers[(channel * plan->node_count + index) * plan->max_frames + frame];
if (plan->render[index])
sample += plan->buffers[(channel * plan->node_count + index) * plan->max_frames + frame];
destination[frame] = isfinite(sample) ? sample : 0.0f;
}
}
@ -193,15 +231,16 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
return XTC_OK;
}
xtc_result xtc_graph_process_offline(xtc_graph *graph, const float *input,
float *output, size_t frame_count, float input_gain)
xtc_result xtc_graph_process_offline(xtc_graph *graph, const float *input, float *output, size_t frame_count,
float input_gain)
{
xtc_plan *plan = NULL;
xtc_result result;
if (input == NULL || output == NULL || !isfinite(input_gain))
return XTC_ERR_INVALID_ARGUMENT;
result = xtc_graph_compile(graph, 48000.0, frame_count == 0U ? 1U : frame_count, &plan);
if (result != XTC_OK) return result;
if (result != XTC_OK)
return result;
plan->input_gain = input_gain;
result = xtc_plan_process(plan, input, output, frame_count);
xtc_plan_destroy(plan);
@ -209,10 +248,18 @@ xtc_result xtc_graph_process_offline(xtc_graph *graph, const float *input,
}
xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output, size_t frames)
{ return xtc_plan_process_audio(plan, input, NULL, output, frames); }
{
return xtc_plan_process_audio(plan, input, NULL, output, frames);
}
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline, float *output, size_t frames)
{ return xtc_plan_process_sources(plan, input, timeline, NULL, output, frames); }
xtc_result xtc_plan_process_audio(xtc_plan *plan, const float *input, const float *timeline, float *output,
size_t frames)
{
return xtc_plan_process_sources(plan, input, timeline, NULL, output, frames);
}
xtc_result xtc_plan_process_sources(xtc_plan *p, const float *in, const float *audio, const float *midi, float *out, size_t frames)
{ return xtc_plan_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames); }
xtc_result xtc_plan_process_sources(xtc_plan *p, const float *in, const float *audio, const float *midi, float *out,
size_t frames)
{
return xtc_plan_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames);
}

View file

@ -14,18 +14,19 @@ struct xtc_realtime {
unsigned int submitted_revision, pending_revision, active_revision;
};
xtc_result xtc_realtime_create(double sample_rate, size_t max_frames,
xtc_realtime **out_engine)
xtc_result xtc_realtime_create(double sample_rate, size_t max_frames, xtc_realtime **out_engine)
{
xtc_realtime *engine;
if (out_engine == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (out_engine == NULL)
return XTC_ERR_INVALID_ARGUMENT;
*out_engine = NULL;
if (!isfinite(sample_rate) || sample_rate <= 0.0 || max_frames == 0U ||
max_frames > 65536U) return XTC_ERR_INVALID_ARGUMENT;
if (!__atomic_always_lock_free(sizeof(xtc_plan *), 0) ||
!__atomic_always_lock_free(sizeof(unsigned int), 0)) return XTC_ERR_UNSUPPORTED;
if (!isfinite(sample_rate) || sample_rate <= 0.0 || max_frames == 0U || max_frames > 65536U)
return XTC_ERR_INVALID_ARGUMENT;
if (!__atomic_always_lock_free(sizeof(xtc_plan *), 0) || !__atomic_always_lock_free(sizeof(unsigned int), 0))
return XTC_ERR_UNSUPPORTED;
engine = calloc(1U, sizeof(*engine));
if (engine == NULL) return XTC_ERR_OUT_OF_MEMORY;
if (engine == NULL)
return XTC_ERR_OUT_OF_MEMORY;
engine->sample_rate = sample_rate;
engine->max_frames = max_frames;
*out_engine = engine;
@ -35,14 +36,16 @@ xtc_result xtc_realtime_create(double sample_rate, size_t max_frames,
void xtc_realtime_collect(xtc_realtime *engine)
{
xtc_plan *retired;
if (engine == NULL) return;
if (engine == NULL)
return;
retired = __atomic_exchange_n(&engine->retired, NULL, __ATOMIC_ACQ_REL);
xtc_plan_destroy(retired);
}
void xtc_realtime_destroy(xtc_realtime *engine)
{
if (engine == NULL) return;
if (engine == NULL)
return;
/* Lifecycle contract: no callback can still execute. */
xtc_plan_destroy(engine->active);
xtc_plan_destroy(engine->pending);
@ -54,12 +57,14 @@ xtc_result xtc_realtime_submit(xtc_realtime *engine, const xtc_graph *graph)
{
xtc_plan *plan = NULL;
xtc_result result;
if (engine == NULL || graph == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (engine == NULL || graph == NULL)
return XTC_ERR_INVALID_ARGUMENT;
xtc_realtime_collect(engine);
if (__atomic_load_n(&engine->pending, __ATOMIC_ACQUIRE) != NULL)
return XTC_ERR_CAPACITY;
result = xtc_graph_compile(graph, engine->sample_rate, engine->max_frames, &plan);
if (result != XTC_OK) return result;
if (result != XTC_OK)
return result;
{
unsigned int revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED) + 1U;
__atomic_store_n(&engine->pending_revision, revision, __ATOMIC_RELAXED);
@ -69,10 +74,10 @@ xtc_result xtc_realtime_submit(xtc_realtime *engine, const xtc_graph *graph)
return XTC_OK;
}
xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
xtc_realtime_status *out_status)
xtc_result xtc_realtime_get_status(const xtc_realtime *engine, xtc_realtime_status *out_status)
{
if (engine == NULL || out_status == NULL) return XTC_ERR_INVALID_ARGUMENT;
if (engine == NULL || out_status == NULL)
return XTC_ERR_INVALID_ARGUMENT;
out_status->processed_blocks = __atomic_load_n(&engine->blocks, __ATOMIC_RELAXED);
out_status->submitted_revision = __atomic_load_n(&engine->submitted_revision, __ATOMIC_RELAXED);
out_status->active_revision = __atomic_load_n(&engine->active_revision, __ATOMIC_RELAXED);
@ -80,14 +85,15 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
return XTC_OK;
}
xtc_result xtc_realtime_process_stereo(xtc_realtime *engine, const float *input,
const float *timeline, const float *timeline_right, const float *instrument, const float *instrument_right,
float *output, float *right, size_t frame_count)
xtc_result xtc_realtime_process_stereo(xtc_realtime *engine, const float *input, const float *timeline,
const float *timeline_right, const float *instrument,
const float *instrument_right, float *output, float *right, size_t frame_count)
{
xtc_result result = XTC_OK;
if (engine == NULL || output == NULL || (right && right == output) || frame_count > SIZE_MAX / sizeof(*output))
return XTC_ERR_INVALID_ARGUMENT;
if (frame_count == 0U) return XTC_OK;
if (frame_count == 0U)
return XTC_OK;
if (frame_count > engine->max_frames) {
result = XTC_ERR_CAPACITY;
} else {
@ -108,27 +114,37 @@ xtc_result xtc_realtime_process_stereo(xtc_realtime *engine, const float *input,
}
}
if (engine->active != NULL)
result = xtc_plan_process_stereo(engine->active, input, timeline, timeline_right, instrument, instrument_right, output, right, frame_count);
result = xtc_plan_process_stereo(engine->active, input, timeline, timeline_right, instrument,
instrument_right, output, right, frame_count);
else {
memset(output, 0, frame_count * sizeof(*output));
if (right) memset(right, 0, frame_count * sizeof(*right));
if (right)
memset(right, 0, frame_count * sizeof(*right));
}
}
if (result != XTC_OK) {
memset(output, 0, frame_count * sizeof(*output));
if (right) memset(right, 0, frame_count * sizeof(*right));
if (right)
memset(right, 0, frame_count * sizeof(*right));
}
__atomic_store_n(&engine->result, (unsigned int)result, __ATOMIC_RELAXED);
__atomic_store_n(&engine->blocks,
__atomic_load_n(&engine->blocks, __ATOMIC_RELAXED) + 1U, __ATOMIC_RELAXED);
__atomic_store_n(&engine->blocks, __atomic_load_n(&engine->blocks, __ATOMIC_RELAXED) + 1U, __ATOMIC_RELAXED);
return result;
}
xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input, float *output, size_t frames)
{ return xtc_realtime_process_audio(engine, input, NULL, output, frames); }
{
return xtc_realtime_process_audio(engine, input, NULL, output, frames);
}
xtc_result xtc_realtime_process_audio(xtc_realtime *engine, const float *input, const float *timeline, float *output, size_t frames)
{ return xtc_realtime_process_sources(engine, input, timeline, NULL, output, frames); }
xtc_result xtc_realtime_process_audio(xtc_realtime *engine, const float *input, const float *timeline, float *output,
size_t frames)
{
return xtc_realtime_process_sources(engine, input, timeline, NULL, output, frames);
}
xtc_result xtc_realtime_process_sources(xtc_realtime *p, const float *in, const float *audio, const float *midi, float *out, size_t frames)
{ return xtc_realtime_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames); }
xtc_result xtc_realtime_process_sources(xtc_realtime *p, const float *in, const float *audio, const float *midi,
float *out, size_t frames)
{
return xtc_realtime_process_stereo(p, in, audio, NULL, midi, NULL, out, NULL, frames);
}