Phase 8: Add stereo track mixer, automation and undo/redo

This commit is contained in:
Johannes Findeisen 2026-10-27 01:06:57 +01:00
commit 7db5fb7738
45 changed files with 1611 additions and 130 deletions

View file

@ -60,7 +60,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
plan->offsets = calloc(plan->node_count + 1U, sizeof(*plan->offsets));
plan->sources = calloc(graph->edge_count + 1U, sizeof(*plan->sources));
plan->render = calloc(storage_nodes, sizeof(*plan->render));
plan->buffers = calloc(storage_nodes * max_frames, sizeof(*plan->buffers));
plan->buffers = calloc(storage_nodes * max_frames * 2U, sizeof(*plan->buffers));
plan->peaks = calloc(storage_nodes, sizeof(*plan->peaks));
plan->phases = calloc(storage_nodes, sizeof(*plan->phases));
if (plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
@ -109,50 +109,56 @@ xtc_result xtc_plan_meter_peak(const xtc_plan *plan, size_t node_id, float *out_
return XTC_OK;
}
xtc_result xtc_plan_process_sources(xtc_plan *plan, const float *input, const float *timeline, const float *instrument, float *output,
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;
if (plan == NULL || output == NULL) return XTC_ERR_INVALID_ARGUMENT;
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) 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 (index = 0U; index < plan->node_count; ++index) {
size_t node = plan->order[index];
xtc_node spec = plan->nodes[node];
float *buffer = plan->buffers + node * plan->max_frames;
float peak = 0.0f;
double phase = plan->phases[node];
for (channel = 0; channel < (right ? 2U : 1U); ++channel) {
const float *audio_source = channel ? timeline_right : timeline;
const float *midi_source = channel ? instrument_right : instrument;
float *destination = channel ? right : output;
for (index = 0U; index < plan->node_count; ++index) {
size_t node = plan->order[index];
xtc_node spec = plan->nodes[node];
float *buffer = plan->buffers + (channel * plan->node_count + node) * plan->max_frames;
float peak = 0.0f;
double phase = plan->phases[node];
for (frame = 0U; frame < frame_count; ++frame) {
float sample = 0.0f;
if (spec.type == XTC_NODE_INSTRUMENT) {
sample = midi_source ? midi_source[frame] : 0.0f;
} else if (spec.type == XTC_NODE_AUDIO) {
sample = audio_source ? audio_source[frame] : 0.0f;
} 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;
} 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;
}
if (!isfinite(sample)) sample = 0.0f;
buffer[frame] = sample;
if (fabsf(sample) > peak) peak = fabsf(sample);
}
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;
if (spec.type == XTC_NODE_INSTRUMENT) {
sample = instrument ? instrument[frame] : 0.0f;
} else if (spec.type == XTC_NODE_AUDIO) {
sample = timeline ? timeline[frame] : 0.0f;
} 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;
} 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[plan->sources[edge] * plan->max_frames + frame];
if (spec.type == XTC_NODE_GAIN) sample *= spec.value;
}
if (!isfinite(sample)) sample = 0.0f;
buffer[frame] = sample;
if (fabsf(sample) > peak) peak = fabsf(sample);
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;
}
plan->phases[node] = phase;
if (spec.type == XTC_NODE_METER) 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[index * plan->max_frames + frame];
output[frame] = isfinite(sample) ? sample : 0.0f;
}
return XTC_OK;
}
@ -177,3 +183,6 @@ xtc_result xtc_plan_process(xtc_plan *plan, const float *input, float *output, s
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); }

View file

@ -80,11 +80,12 @@ xtc_result xtc_realtime_get_status(const xtc_realtime *engine,
return XTC_OK;
}
xtc_result xtc_realtime_process_sources(xtc_realtime *engine, const float *input, const float *timeline, const float *instrument,
float *output, 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 || frame_count > SIZE_MAX / sizeof(*output))
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 > engine->max_frames) {
@ -107,11 +108,16 @@ xtc_result xtc_realtime_process_sources(xtc_realtime *engine, const float *input
}
}
if (engine->active != NULL)
result = xtc_plan_process_sources(engine->active, input, timeline, instrument, output, frame_count);
else
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 (result != XTC_OK) {
memset(output, 0, frame_count * sizeof(*output));
if (right) memset(right, 0, frame_count * sizeof(*right));
}
if (result != XTC_OK) memset(output, 0, frame_count * sizeof(*output));
__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);
@ -123,3 +129,6 @@ xtc_result xtc_realtime_process(xtc_realtime *engine, const float *input, float
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); }