Phase 9: Add native processors and reproducible Linux release tooling

This commit is contained in:
Johannes Findeisen 2026-10-27 01:21:04 +01:00
commit de403b37d7
43 changed files with 1656 additions and 19 deletions

View file

@ -14,11 +14,17 @@ struct xtc_plan {
double *phases;
double sample_rate;
float input_gain;
uint64_t position;
xtc_native **native;
};
void xtc_plan_destroy(xtc_plan *plan)
{
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]);
}
free(plan->native);
free(plan->nodes);
free(plan->order);
free(plan->offsets);
@ -55,6 +61,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
plan->max_frames = max_frames;
plan->sample_rate = sample_rate;
plan->input_gain = 1.0f;
plan->native = calloc(storage_nodes * 2U, sizeof(*plan->native));
plan->nodes = calloc(storage_nodes, sizeof(*plan->nodes));
plan->order = calloc(storage_nodes, sizeof(*plan->order));
plan->offsets = calloc(plan->node_count + 1U, sizeof(*plan->offsets));
@ -63,7 +70,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
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 ||
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) {
xtc_plan_destroy(plan);
@ -83,6 +90,16 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate,
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; }
}
}
}
plan->offsets[plan->node_count] = cursor;
*out_plan = plan;
return XTC_OK;
@ -91,6 +108,8 @@ 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]); }
plan->position = 0;
memset(plan->phases, 0, plan->node_count * sizeof(*plan->phases));
memset(plan->peaks, 0, plan->node_count * sizeof(*plan->peaks));
return XTC_OK;
@ -115,7 +134,7 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
{
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 > 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. */
@ -150,6 +169,16 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
buffer[frame] = 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};
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));
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;
}
@ -160,6 +189,7 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input,
destination[frame] = isfinite(sample) ? sample : 0.0f;
}
}
plan->position += frame_count;
return XTC_OK;
}

View file

@ -74,7 +74,8 @@ xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id,
node_id = node_index(graph, node_id);
if (node_id == SIZE_MAX) return XTC_ERR_NOT_FOUND;
type = graph->nodes[node_id].type;
if (port_id > 1U || (port_id == 0U && (type == XTC_NODE_OSCILLATOR || type == XTC_NODE_AUDIO || type == XTC_NODE_INSTRUMENT)) ||
if (port_id > 1U || (port_id == 0U && (type == XTC_NODE_OSCILLATOR || type == XTC_NODE_AUDIO || type == XTC_NODE_INSTRUMENT ||
(type == XTC_NODE_NATIVE && graph->nodes[node_id].native.kind != XTC_NATIVE_EFFECT))) ||
(port_id == 1U && type == XTC_NODE_OUTPUT)) return XTC_ERR_NOT_FOUND;
out_port->direction = port_id == 0U ? XTC_PORT_INPUT : XTC_PORT_OUTPUT;
out_port->signal_type = XTC_SIGNAL_AUDIO;
@ -250,3 +251,22 @@ xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *e
*out_graph = graph;
return XTC_OK;
}
xtc_result xtc_graph_add_native(xtc_graph *graph, const xtc_native_descriptor *descriptor, float value, size_t *out)
{
xtc_result result = xtc_native_validate(descriptor);
if (result != XTC_OK) return result;
if (descriptor->kind == XTC_NATIVE_INSTRUMENT) return XTC_ERR_UNSUPPORTED;
result = add_node(graph, XTC_NODE_NATIVE, value, out);
if (result == XTC_OK) graph->nodes[graph->node_count - 1].native = *descriptor;
return result;
}
xtc_result xtc_graph_get_native(const xtc_graph *graph, size_t id, xtc_native_descriptor *out)
{
size_t i;
if (!graph || !out) return XTC_ERR_INVALID_ARGUMENT;
i = node_index(graph, id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
if (graph->nodes[i].type != XTC_NODE_NATIVE) return XTC_ERR_INVALID_ARGUMENT;
*out = graph->nodes[i].native; return XTC_OK;
}

View file

@ -1,9 +1,10 @@
#ifndef XTC_GRAPH_INTERNAL_H
#define XTC_GRAPH_INTERNAL_H
#include "xtc/xtc_engine.h"
#include "xtc/xtc_native.h"
#define XTC_MAX_NODES 256U
#define XTC_MAX_FRAMES 65536U
typedef struct { xtc_node_type type; float value; size_t id; } xtc_node;
typedef struct { xtc_node_type type; float value; size_t id; xtc_native_descriptor native; } xtc_node;
typedef struct { size_t source; size_t destination; } xtc_edge;
struct xtc_graph {
size_t max_nodes, node_count, edge_count, next_id;

View file

@ -14,6 +14,7 @@ const char *node_name(xtc_node_type type)
case XTC_NODE_OSCILLATOR: return "Oscillator";
case XTC_NODE_GAIN: return "Gain";
case XTC_NODE_METER: return "Meter";
case XTC_NODE_NATIVE: return "Native processor";
case XTC_NODE_INSTRUMENT: return "Instrument";
case XTC_NODE_AUDIO: return "Audio clips";
case XTC_NODE_OUTPUT: return "Output";

View file

@ -3,15 +3,29 @@
#include <cstdlib>
#include <exception>
#include "main_window.hpp"
#include "xtc/xtc_version.h"
#include <cstring>
int main(int argc, char **argv)
{
if (argc == 2 && !std::strcmp(argv[1], "--version")) {
std::printf("%s %s\n", XTC_PROJECT_NAME, XTC_VERSION_STRING); return EXIT_SUCCESS;
}
if (argc == 2 && !std::strcmp(argv[1], "--help")) {
std::puts("Usage: xtc [PROJECT] or xtc [FLTK options]\n--version and --help work without a display or audio server."); return EXIT_SUCCESS;
}
const char *project_path = nullptr;
if (argc == 2 && argv[1][0] != '-') { project_path = argv[1]; argc = 1; argv[1] = nullptr; }
try {
Fl::scheme("flat");
Fl::background(34, 36, 40);
Fl::background2(24, 26, 30);
Fl::foreground(230, 232, 236);
MainWindow window;
if (project_path) {
xtc_result result = window.open_project(project_path);
if (result != XTC_OK) { std::fprintf(stderr, "Cannot open project: %s\n", xtc_result_string(result)); return EXIT_FAILURE; }
}
window.show(argc, argv);
return Fl::run();
} catch (const std::exception &error) {

View file

@ -384,7 +384,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 8: stereo track mixer, automation, meters and undo/redo.",
fl_message("%s %s\nModular audio workstation\nPhase 9: native C processors and Linux release preparation.",
XTC_PROJECT_NAME, XTC_VERSION_STRING);
}
void MainWindow::timer_callback(void *context)

View file

@ -3,9 +3,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "xtc/xtc_project.h"
int main(void)
int main(int argc, char **argv)
{
if (argc == 2 && !strcmp(argv[1], "--version")) {
printf("%s %s\n", XTC_PROJECT_NAME, XTC_VERSION_STRING); return EXIT_SUCCESS;
}
if (argc == 2 && !strcmp(argv[1], "--help")) {
puts("Usage: xtc-check [--version | --help | --validate PROJECT]\nNo arguments checks core startup/shutdown. Validation checks metadata only."); return EXIT_SUCCESS;
}
if (argc == 3 && !strcmp(argv[1], "--validate")) {
xtc_project *project = NULL;
xtc_result result = xtc_project_create(&project);
if (result == XTC_OK) result = xtc_project_load(project, argv[2]);
xtc_project_destroy(project);
if (result != XTC_OK) { fprintf(stderr, "Project validation failed: %s\n", xtc_result_string(result)); return EXIT_FAILURE; }
puts("Project metadata valid (external assets not loaded)."); return EXIT_SUCCESS;
}
if (argc != 1) { fputs("Usage: xtc-check [--version | --help | --validate PROJECT]\n", stderr); return EXIT_FAILURE; }
xtc_app *app = NULL;
xtc_result result = xtc_app_create(&app);

5
src/native/README.md Normal file
View file

@ -0,0 +1,5 @@
# Native processor host
`native.c` validates and copies ABI-1 descriptors, prepares/destroys control-thread
instances and validates bounded realtime blocks. User callback code is trusted.
See ../../docs/NATIVE_API.md for ownership, failures and graph/event integration.

105
src/native/native.c Normal file
View file

@ -0,0 +1,105 @@
#include "xtc/xtc_native.h"
#include <math.h>
#include <stdlib.h>
struct xtc_native {
xtc_native_descriptor descriptor;
void *state;
size_t max_frames;
uint64_t next_position;
};
xtc_result xtc_native_validate(const xtc_native_descriptor *d)
{
size_t i;
if (!d)
return XTC_ERR_INVALID_ARGUMENT;
if (d->abi_version != XTC_NATIVE_ABI_VERSION || d->struct_size != sizeof(*d))
return XTC_ERR_UNSUPPORTED;
if (d->kind < XTC_NATIVE_EFFECT || d->kind > XTC_NATIVE_INSTRUMENT || !d->prepare ||
!d->reset || !d->process || !d->destroy || !d->identifier[0])
return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < sizeof(d->identifier); ++i) {
unsigned char c = (unsigned char)d->identifier[i];
if (!c)
return XTC_OK;
if (c < 33 || c > 126)
return XTC_ERR_INVALID_ARGUMENT;
}
return XTC_ERR_INVALID_ARGUMENT;
}
xtc_result xtc_native_create(const xtc_native_descriptor *d, double rate, size_t frames,
float value, xtc_native **out)
{
xtc_native *p;
xtc_result r;
if (!out)
return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
r = xtc_native_validate(d);
if (r != XTC_OK)
return r;
if (!isfinite(rate) || rate <= 0 || rate > 768000 || !frames || frames > 65536 ||
!isfinite(value))
return XTC_ERR_INVALID_ARGUMENT;
p = calloc(1, sizeof(*p));
if (!p)
return XTC_ERR_OUT_OF_MEMORY;
p->descriptor = *d;
p->max_frames = frames;
p->next_position = UINT64_MAX;
r = d->prepare(rate, frames, value, &p->state);
if (r != XTC_OK) {
xtc_native_destroy(p);
return r;
}
p->descriptor.reset(p->state);
*out = p;
return XTC_OK;
}
void xtc_native_destroy(xtc_native *p)
{
if (p) {
p->descriptor.destroy(p->state);
free(p);
}
}
void xtc_native_reset(xtc_native *p)
{
if (p) {
p->descriptor.reset(p->state);
p->next_position = UINT64_MAX;
}
}
xtc_result xtc_native_process(xtc_native *p, const xtc_native_block *b)
{
size_t i;
xtc_result r;
if (!p || !b || !b->output || b->position > UINT64_MAX - b->frames ||
b->event_count > XTC_MIDI_MAX_EVENTS || (b->event_count && !b->events) ||
(b->event_count && p->descriptor.kind != XTC_NATIVE_INSTRUMENT))
return XTC_ERR_INVALID_ARGUMENT;
if (b->frames > p->max_frames)
return XTC_ERR_CAPACITY;
for (i = 0; i < b->event_count; ++i) {
const xtc_midi_event *e = b->events + i;
if (e->offset >= b->frames || (i && e->offset < b->events[i - 1].offset) ||
e->type < XTC_MIDI_NOTE_ON || e->type > XTC_MIDI_ALL_OFF || e->channel > 15 ||
e->pitch > 127 || e->velocity > 127 || (e->type == XTC_MIDI_NOTE_ON && !e->velocity))
return XTC_ERR_INVALID_ARGUMENT;
}
if (!b->frames)
return XTC_OK;
if (p->next_position != b->position)
p->descriptor.reset(p->state);
r = p->descriptor.process(p->state, b);
if (r != XTC_OK) {
for (i = 0; i < b->frames; ++i)
b->output[i] = 0;
xtc_native_reset(p);
return r;
}
for (i = 0; i < b->frames; ++i)
if (!isfinite(b->output[i]))
b->output[i] = 0;
p->next_position = b->position + b->frames;
return XTC_OK;
}

View file

@ -102,7 +102,7 @@ static xtc_result read_project(reader *input, xtc_project *project)
if (value > 1U || next_id > SIZE_MAX || node_count > 64U || edge_count > node_count * node_count) return XTC_ERR_FORMAT;
graph->require_output_nodes = (int)value; graph->next_id = (size_t)next_id;
for (i = 0U; i < (size_t)node_count; ++i) {
xtc_node node;
xtc_node node = {0};
READ("NODE", 4U); NUM(1U, value); NUM(2U, version);
if (value >= next_id || value >= SIZE_MAX || version < XTC_NODE_GAIN || version > (format_version == 1U ? XTC_NODE_OUTPUT : format_version == 2U ? XTC_NODE_AUDIO : XTC_NODE_INSTRUMENT) ||
!decode_float(input->tokens[3], &node.value)) return XTC_ERR_FORMAT;
@ -203,6 +203,8 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
{
const xtc_graph *graph = project->graph;
size_t i;
for (i = 0; i < graph->node_count; ++i)
if (graph->nodes[i].type == XTC_NODE_NATIVE) return XTC_ERR_UNSUPPORTED;
if (sizeof(float) != sizeof(uint32_t) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128) return XTC_ERR_UNSUPPORTED;
if (fprintf(file, "XTC_PROJECT 4\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
XTC_VERSION_MAJOR, XTC_VERSION_MINOR, XTC_VERSION_PATCH, project->tempo,