Phase 15: host trusted LADSPA processors with persisted graph state

This commit is contained in:
Johannes Findeisen 2026-10-27 04:00:27 +01:00
commit f72314b5a3
24 changed files with 1046 additions and 70 deletions

View file

@ -17,6 +17,7 @@ struct xtc_plan {
float input_gain;
uint64_t position;
xtc_native **native;
xtc_plugin **plugins;
};
/**
@ -33,6 +34,11 @@ void xtc_plan_destroy(xtc_plan *plan)
for (i = 0; i < plan->node_count * 2; ++i)
xtc_native_destroy(plan->native[i]);
}
if (plan->plugins) {
size_t i;
for (i = 0; i < plan->node_count * 2; ++i) xtc_plugin_destroy(plan->plugins[i]);
}
free(plan->plugins);
free(plan->native);
free(plan->nodes);
free(plan->order);
@ -81,6 +87,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate, size_t
plan->max_frames = max_frames;
plan->sample_rate = sample_rate;
plan->input_gain = 1.0f;
plan->plugins = calloc(storage_nodes * 2U,sizeof(*plan->plugins));
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));
@ -90,7 +97,7 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate, size_t
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->native == NULL || plan->nodes == NULL || plan->order == NULL || plan->offsets == NULL ||
if (plan->plugins == NULL || 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);
@ -116,6 +123,14 @@ xtc_result xtc_graph_compile(const xtc_graph *graph, double sample_rate, size_t
: terminal);
}
for (node = 0; node < plan->node_count; ++node) {
if (plan->nodes[node].type == XTC_NODE_PLUGIN) {
size_t side;
for (side = 0; side < 2; ++side) {
result = xtc_plugin_create(&plan->nodes[node].plugin,sample_rate,max_frames,
&plan->plugins[side * plan->node_count + node]);
if (result != XTC_OK) { xtc_plan_destroy(plan); return result; }
}
}
if (plan->nodes[node].type == XTC_NODE_NATIVE) {
size_t side;
for (side = 0; side < 2; ++side) {
@ -211,19 +226,19 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const flo
const float *midi_source = channel ? instrument_right : instrument;
for (index = 0U; index < plan->node_count; ++index) {
size_t node = plan->order[index];
xtc_node spec = plan->nodes[node];
const 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) {
if (spec->type == XTC_NODE_INSTRUMENT) {
sample = midi_source ? midi_source[frame] : 0.0f;
} else if (spec.type == XTC_NODE_AUDIO) {
} else if (spec->type == XTC_NODE_AUDIO) {
sample = audio_source ? audio_source[frame] : 0.0f;
} else if (spec.type == XTC_NODE_OSCILLATOR) {
} else if (spec->type == XTC_NODE_OSCILLATOR) {
sample = (float)sin(XTC_TAU * phase);
phase += (double)spec.value / plan->sample_rate;
phase += (double)spec->value / plan->sample_rate;
if (phase >= 1.0)
phase -= 1.0;
} else {
@ -232,16 +247,16 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const flo
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 (spec->type == XTC_NODE_GAIN)
sample *= spec->value;
}
sample = xtc_dsp_finite(sample);
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,
if (spec->type == XTC_NODE_NATIVE) {
xtc_native_block block = {spec->native.kind == XTC_NATIVE_EFFECT ? buffer : NULL,
buffer,
frame_count,
plan->position,
@ -255,9 +270,18 @@ xtc_result xtc_plan_process_stereo(xtc_plan *plan, const float *input, const flo
return result;
}
}
if (spec->type == XTC_NODE_PLUGIN) {
xtc_result result = xtc_plugin_process(plan->plugins[channel * plan->node_count + node],
spec->plugin.inputs ? buffer : NULL,buffer,frame_count);
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)
if (spec->type == XTC_NODE_METER)
plan->peaks[node] = channel && plan->peaks[node] > peak ? plan->peaks[node] : peak;
}
}

View file

@ -180,6 +180,7 @@ xtc_result xtc_graph_get_port(const xtc_graph *graph, size_t node_id, size_t por
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 ||
(type == XTC_NODE_PLUGIN && !graph->nodes[node_id].plugin.inputs) ||
(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;
@ -417,6 +418,27 @@ static xtc_result disconnect_ports(xtc_graph *graph, const xtc_connection_info *
return XTC_ERR_NOT_FOUND;
}
/* Independent control-thread model copy; no plugin code is loaded. */
xtc_result xtc_graph_clone(const xtc_graph *source, xtc_graph **out)
{
xtc_graph *graph;
xtc_result result;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
if (!source) return XTC_ERR_INVALID_ARGUMENT;
result = xtc_graph_create(source->max_nodes, &graph);
if (result != XTC_OK)
return result;
graph->node_count = source->node_count;
graph->edge_count = source->edge_count;
graph->next_id = source->next_id;
graph->require_output_nodes = source->require_output_nodes;
memcpy(graph->nodes, source->nodes, source->node_count * sizeof(*graph->nodes));
memcpy(graph->edges, source->edges, source->edge_count * sizeof(*graph->edges));
*out = graph;
return XTC_OK;
}
/**
* @brief Applies an edit to a new graph copy, leaving the source unchanged.
*
@ -437,15 +459,8 @@ xtc_result xtc_graph_apply_edit(const xtc_graph *source, const xtc_graph_edit *e
*out_graph = NULL;
if (source == NULL || edit == NULL)
return XTC_ERR_INVALID_ARGUMENT;
result = xtc_graph_create(source->max_nodes, &graph);
if (result != XTC_OK)
return result;
graph->node_count = source->node_count;
graph->edge_count = source->edge_count;
graph->next_id = source->next_id;
graph->require_output_nodes = source->require_output_nodes;
memcpy(graph->nodes, source->nodes, source->node_count * sizeof(*graph->nodes));
memcpy(graph->edges, source->edges, source->edge_count * sizeof(*graph->edges));
result = xtc_graph_clone(source,&graph);
if (result != XTC_OK) return result;
switch (edit->kind) {
case XTC_EDIT_ADD_NODE:
switch (edit->data.add.type) {
@ -540,3 +555,36 @@ xtc_result xtc_graph_get_native(const xtc_graph *graph, size_t id, xtc_native_de
*out = graph->nodes[i].native;
return XTC_OK;
}
xtc_result xtc_graph_add_plugin(xtc_graph *g, const xtc_plugin_spec *s, size_t *id)
{
xtc_result r = xtc_plugin_spec_validate(s);
if (r != XTC_OK) return r;
r = add_node(g,XTC_NODE_PLUGIN,0,id);
if (r == XTC_OK) g->nodes[g->node_count-1].plugin = *s;
return r;
}
xtc_result xtc_graph_get_plugin(const xtc_graph *g, size_t id, xtc_plugin_spec *out)
{
size_t i;
if (!g || !out) return XTC_ERR_INVALID_ARGUMENT;
i = node_index(g,id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
if (g->nodes[i].type != XTC_NODE_PLUGIN) return XTC_ERR_INVALID_ARGUMENT;
*out = g->nodes[i].plugin; return XTC_OK;
}
xtc_result xtc_graph_set_plugin(xtc_graph *g, size_t id, const xtc_plugin_spec *s)
{
size_t i;
xtc_result r = xtc_plugin_spec_validate(s);
if (!g || r != XTC_OK) return XTC_ERR_INVALID_ARGUMENT;
i = node_index(g,id);
if (i == SIZE_MAX) return XTC_ERR_NOT_FOUND;
if (g->nodes[i].type != XTC_NODE_PLUGIN) return XTC_ERR_INVALID_ARGUMENT;
/* Parameter edits cannot replace identity/layout or invalidate wiring. */
if (strcmp(s->path,g->nodes[i].plugin.path) || strcmp(s->label,g->nodes[i].plugin.label) ||
s->index != g->nodes[i].plugin.index || s->unique_id != g->nodes[i].plugin.unique_id ||
s->inputs != g->nodes[i].plugin.inputs || s->control_count != g->nodes[i].plugin.control_count ||
memcmp(s->ports,g->nodes[i].plugin.ports,s->control_count*sizeof(*s->ports))) return XTC_ERR_UNSUPPORTED;
g->nodes[i].plugin = *s; return XTC_OK;
}

View file

@ -3,6 +3,7 @@
#include "xtc/xtc_engine.h"
#include "xtc/xtc_native.h"
#include "xtc/xtc_plugin.h"
#define XTC_MAX_NODES 256U
#define XTC_MAX_FRAMES 65536U
@ -12,6 +13,7 @@ typedef struct {
float value;
size_t id;
xtc_native_descriptor native;
xtc_plugin_spec plugin;
} xtc_node;
typedef struct {

View file

@ -1,4 +1,5 @@
#include "graph_widget.hpp"
#include "xtc/xtc_plugin.h"
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>
@ -24,6 +25,8 @@ const char *node_name(xtc_node_type type)
return "Gain";
case XTC_NODE_METER:
return "Meter";
case XTC_NODE_PLUGIN:
return "LADSPA plugin";
case XTC_NODE_NATIVE:
return "Native processor";
case XTC_NODE_INSTRUMENT:
@ -371,7 +374,11 @@ void GraphWidget::draw()
std::snprintf(text, sizeof(text), "%.0f Hz", static_cast<double>(node.value));
else if (node.type == XTC_NODE_GAIN)
std::snprintf(text, sizeof(text), "x %.2f", static_cast<double>(node.value));
else
else if (node.type == XTC_NODE_PLUGIN) {
xtc_plugin_spec plugin{};
if (xtc_graph_get_plugin(graph_,node.id,&plugin) == XTC_OK)
std::snprintf(text,sizeof(text),"%.20s",plugin.label);
} else
std::snprintf(text, sizeof(text), "%s",
node.type == XTC_NODE_METER ? "Peak pass-through"
: node.type == XTC_NODE_INSTRUMENT ? "MIDI sine source"

View file

@ -64,6 +64,8 @@ class GraphWidget : public Fl_Widget {
*
* @return Borrowed graph pointer; may be NULL when none was supplied.
*/
size_t selected_node() const { return selected_node_; }
const xtc_graph *graph() const
{
return graph_;

View file

@ -17,6 +17,9 @@
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <cstdlib>
#include <cmath>
#include <cerrno>
namespace {
/**
@ -72,6 +75,8 @@ MainWindow::MainWindow(bool prompt_unsaved)
menu->add("Audio/Panic", FL_F + 9, panic_callback, this);
menu->add("View/Piano roll", FL_CTRL + '4', piano_callback, this);
menu->add("Audio/Record WAV", FL_SHIFT + FL_F + 5, audio_file_callback, this);
menu->add("File/Load LADSPA plugin", 0, plugin_callback, this);
menu->add("Edit/Selected plugin control", 0, plugin_callback, this);
menu->add("File/Quit", FL_CTRL + 'q', close_callback, this);
menu->add("Audio/Start engine", FL_F + 5, play_callback, this);
menu->add("Audio/Stop engine", FL_F + 8, stop_callback, this);
@ -841,6 +846,11 @@ xtc_result MainWindow::install_project(xtc_project *candidate, bool reset_histor
if (r == XTC_OK && !ready_)
r = xtc_midi_plan_create(candidate, 48000, &offline_midi);
xtc_midi_plan_destroy(offline_midi);
if (r == XTC_OK && !ready_) {
xtc_plan *graph = nullptr;
r = xtc_graph_compile(xtc_project_graph(candidate),48000,256,&graph);
xtc_plan_destroy(graph);
}
if (r != XTC_OK) {
xtc_audio_plan_destroy(offline);
refresh_status(); // A failed install may have stopped playback.
@ -891,6 +901,7 @@ xtc_result MainWindow::open_project(const char *path)
{
if (path == nullptr)
return XTC_ERR_INVALID_ARGUMENT;
project_issue_.clear();
std::string loaded_path(path);
xtc_project *candidate = nullptr;
xtc_result r = xtc_project_create(&candidate);
@ -898,9 +909,30 @@ xtc_result MainWindow::open_project(const char *path)
r = xtc_project_load(candidate, path);
if (r == XTC_OK)
r = install_project(candidate);
if (r != XTC_OK)
if (r != XTC_OK) {
if (candidate) {
const xtc_graph *graph = xtc_project_graph(candidate);
size_t nodes = 0, edges = 0;
xtc_graph_get_counts(graph,&nodes,&edges);
for (size_t i = 0; i < nodes; ++i) {
xtc_node_info node{}; xtc_plugin_spec spec{};
if (xtc_graph_get_node(graph,i,&node) != XTC_OK || node.type != XTC_NODE_PLUGIN ||
xtc_graph_get_plugin(graph,node.id,&spec) != XTC_OK) continue;
double rate = 48000; xtc_backend_status status{};
if (ready_ && xtc_backend_get_status(backend_,&status) == XTC_OK) rate = status.sample_rate;
xtc_plugin *check = nullptr;
xtc_result plugin_result = xtc_plugin_create(&spec,rate,256,&check);
xtc_plugin_destroy(check);
if (plugin_result != XTC_OK) {
project_issue_ = std::string("Missing/incompatible plugin ") + spec.label + " at " + spec.path +
"; restore a compatible module and retry Open. Saved document retained";
report_error(project_issue_.c_str(),plugin_result);
break;
}
}
}
xtc_project_destroy(candidate);
else {
} else {
path_ = loaded_path;
copy_label(path_.c_str());
}
@ -945,6 +977,7 @@ void MainWindow::file_callback(Fl_Widget *widget, void *context)
auto *menu = static_cast<Fl_Menu_Bar *>(widget);
const char *action = menu->mvalue()->label();
xtc_result r = XTC_OK;
window->project_issue_.clear();
if (std::strcmp(action, "New") == 0) {
if (!window->discard_changes())
return;
@ -972,7 +1005,7 @@ void MainWindow::file_callback(Fl_Widget *widget, void *context)
}
}
if (r != XTC_OK)
window->report_error("Project operation failed", r);
window->report_error(window->project_issue_.empty() ? "Project operation failed" : window->project_issue_.c_str(), r);
}
/**
@ -1302,3 +1335,101 @@ void MainWindow::history_callback(Fl_Widget *widget, void *context)
if (r != XTC_OK && r != XTC_ERR_NOT_FOUND)
window->report_error("Undo/redo failed", r);
}
xtc_result MainWindow::install_plugin_graph(xtc_graph *graph)
{
xtc_project *candidate = nullptr;
xtc_result r = xtc_project_clone(project_.get(),&candidate);
if (r == XTC_OK) r = xtc_project_set_graph(candidate,graph);
if (r == XTC_OK) r = install_project(candidate,false);
if (r != XTC_OK) xtc_project_destroy(candidate);
else mark_dirty();
return r;
}
xtc_result MainWindow::add_plugin(const char *path, unsigned int index, size_t *id)
{
if (recorder_) return XTC_ERR_INVALID_STATE;
double rate = 48000;
xtc_backend_status status{};
if (ready_ && xtc_backend_get_status(backend_,&status) == XTC_OK) rate = status.sample_rate;
xtc_plugin_spec spec{};
xtc_result r = xtc_plugin_probe(path,index,rate,&spec);
xtc_graph *graph = nullptr; size_t added = SIZE_MAX;
if (r == XTC_OK) r = xtc_graph_clone(xtc_project_graph(project_.get()),&graph);
if (r == XTC_OK) r = xtc_graph_add_plugin(graph,&spec,&added);
if (r == XTC_OK) r = install_plugin_graph(graph);
xtc_graph_destroy(graph);
if (r == XTC_OK && id) *id = added;
return r;
}
xtc_result MainWindow::set_plugin_control(size_t id, unsigned int port, float value)
{
if (recorder_) return XTC_ERR_INVALID_STATE;
xtc_plugin_spec spec{};
xtc_result r = xtc_graph_get_plugin(xtc_project_graph(project_.get()),id,&spec);
if (r != XTC_OK) return r;
size_t i;
for (i = 0; i < spec.control_count && spec.ports[i] != port; ++i) {}
if (i == spec.control_count) return XTC_ERR_NOT_FOUND;
spec.values[i] = value;
xtc_graph *graph = nullptr;
r = xtc_graph_clone(xtc_project_graph(project_.get()),&graph);
if (r == XTC_OK) r = xtc_graph_set_plugin(graph,id,&spec);
/* Compile before adoption even with a mocked/disconnected backend. */
xtc_plan *check = nullptr;
xtc_backend_status status{}; double rate = 48000;
if (ready_ && xtc_backend_get_status(backend_,&status) == XTC_OK) rate = status.sample_rate;
if (r == XTC_OK) r = xtc_graph_compile(graph,rate,256,&check);
xtc_plan_destroy(check);
if (r == XTC_OK) r = install_plugin_graph(graph);
xtc_graph_destroy(graph); return r;
}
void MainWindow::plugin_callback(Fl_Widget *widget, void *context)
{
auto *w = static_cast<MainWindow *>(context);
if (w->recorder_) { w->report_error("Stop recording before plugin changes",XTC_ERR_INVALID_STATE); return; }
auto *menu = static_cast<Fl_Menu_Bar *>(widget);
const char *label = menu->mvalue()->label();
xtc_result r;
if (std::strcmp(label,"Load LADSPA plugin") == 0) {
Fl_Native_File_Chooser chooser;
chooser.title("Load trusted LADSPA module (executes native code)"); chooser.filter("LADSPA\t*.so");
if (chooser.show() != 0 || !chooser.filename()) return;
double rate = 48000; xtc_backend_status status{};
if (w->ready_ && xtc_backend_get_status(w->backend_,&status) == XTC_OK) rate = status.sample_rate;
std::string choices = "Compatible descriptors (index: label):\n";
for (unsigned i = 0; i < 1024; ++i) {
xtc_plugin_spec spec{};
r = xtc_plugin_probe(chooser.filename(),i,rate,&spec);
if (r == XTC_ERR_NOT_FOUND) break;
if (r == XTC_OK) choices += std::to_string(i) + ": " + spec.label + "\n";
}
choices += "Enter descriptor index:";
const char *answer = fl_input("%s","0",choices.c_str());
if (!answer) return;
char *end = nullptr; errno = 0; unsigned long index = std::strtoul(answer,&end,10);
if (errno || !answer[0] || *end || index >= 1024) r = XTC_ERR_INVALID_ARGUMENT;
else r = w->add_plugin(chooser.filename(),static_cast<unsigned int>(index));
} else {
size_t id = w->graph_widget_->selected_node(); xtc_plugin_spec spec{};
r = xtc_graph_get_plugin(xtc_project_graph(w->project_.get()),id,&spec);
if (r == XTC_OK) {
std::string prompt = std::string(spec.label) + " control ports and values:\n";
xtc_parameter_info info[XTC_PLUGIN_CONTROLS]{};
double rate = 48000; xtc_backend_status status{};
if (w->ready_ && xtc_backend_get_status(w->backend_,&status) == XTC_OK) rate = status.sample_rate;
r = xtc_plugin_parameters(&spec,rate,info);
if (r != XTC_OK) { w->report_error("Read plugin controls failed",r); return; }
for (unsigned i = 0; i < spec.control_count; ++i)
prompt += std::to_string(spec.ports[i]) + ": " + info[i].name + " = " + std::to_string(spec.values[i]) +
" [" + std::to_string(info[i].minimum) + ", " + std::to_string(info[i].maximum) + "]\n";
prompt += "Enter port and value (for example: 2 0.5):";
const char *answer = fl_input("%s","",prompt.c_str());
if (!answer) return;
unsigned port; float value; char extra;
if (std::sscanf(answer,"%u %f %c",&port,&value,&extra) != 2 || !std::isfinite(value)) r = XTC_ERR_INVALID_ARGUMENT;
else r = w->set_plugin_control(id,port,value);
}
}
if (r != XTC_OK) w->report_error("Plugin change failed: check module, mono layout, control port/range and JACK format",r);
}

View file

@ -7,6 +7,7 @@
#include "xtc/xtc_project.h"
#include "xtc/xtc_backend.h"
#include "xtc/xtc_mixer.h"
#include "xtc/xtc_plugin.h"
class GraphWidget;
class MixerWidget;
@ -18,6 +19,9 @@ class Fl_Tabs;
class MainWindow : public Fl_Double_Window {
public:
/* Control-thread plugin commands, transactional model adoption. */
xtc_result add_plugin(const char *path, unsigned int index, size_t *id = nullptr);
xtc_result set_plugin_control(size_t id, unsigned int port, float value);
/**
* @brief Constructs the window, its widgets, and the initial project.
*
@ -284,10 +288,13 @@ class MainWindow : public Fl_Double_Window {
* @param widget Triggering Fl_Menu_Bar whose selected label names the action.
* @param context MainWindow * owning the project and file path.
*/
static void plugin_callback(Fl_Widget *widget, void *context);
xtc_result install_plugin_graph(xtc_graph *graph);
static void file_callback(Fl_Widget * widget, void *context);
GraphWidget *graph_widget_ = nullptr;
Fl_Box *graph_state_ = nullptr;
xtc_backend *backend_ = nullptr;
std::string project_issue_;
bool ready_ = false;
bool running_ = false;
bool closing_ = false;

View file

@ -125,6 +125,19 @@ static int decode_name(const char *text, char *name)
return xtc_project_valid_name(name);
}
/* Binary-safe nonempty hexadecimal strings, excluding embedded NUL. */
static int decode_bytes(const char *text, char *out, size_t capacity)
{
size_t n = strlen(text), i;
if (!n || n % 2 || n / 2 >= capacity) return 0;
for (i = 0; i < n/2; ++i) {
int hi = hex_digit(text[2*i]), lo = hex_digit(text[2*i+1]);
if (hi < 0 || lo < 0 || (!hi && !lo)) return 0;
out[i] = (char)(hi*16+lo);
}
out[n/2] = 0; return 1;
}
/**
* @brief Decodes eight hexadecimal IEEE-754 bits into a finite float.
*
@ -178,7 +191,7 @@ static xtc_result read_project(reader *input, xtc_project *project)
xtc_graph *graph = project->graph;
READ("XTC_PROJECT", 2U);
NUM(1U, version);
if (version < 1U || version > 4U)
if (version < 1U || version > 5U)
return XTC_ERR_UNSUPPORTED;
format_version = version;
READ("APP", 4U);
@ -212,9 +225,11 @@ static xtc_result read_project(reader *input, xtc_project *project)
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) ||
: format_version < 5U ? XTC_NODE_INSTRUMENT
: XTC_NODE_PLUGIN) ||
!decode_float(input->tokens[3], &node.value))
return XTC_ERR_FORMAT;
if (version == XTC_NODE_NATIVE) return XTC_ERR_UNSUPPORTED;
node.id = (size_t)value;
node.type = (xtc_node_type)version;
if (node.type == XTC_NODE_OSCILLATOR && node.value < 0.0f)
@ -223,6 +238,25 @@ static xtc_result read_project(reader *input, xtc_project *project)
node.type == XTC_NODE_INSTRUMENT) &&
node.value != 0.0f)
return XTC_ERR_FORMAT;
if (node.type == XTC_NODE_PLUGIN) {
xtc_plugin_spec *p = &node.plugin;
size_t k;
if (node.value != 0) return XTC_ERR_FORMAT;
READ("PLUGIN",6U);
NUM(1U,value); if (value > UINT32_MAX) return XTC_ERR_FORMAT; p->unique_id = (unsigned int)value;
NUM(2U,value); if (value >= 1024) return XTC_ERR_FORMAT; p->index = (unsigned int)value;
NUM(3U,value); if (value > 1) return XTC_ERR_FORMAT; p->inputs = (unsigned int)value;
NUM(4U,value); if (value > XTC_PLUGIN_CONTROLS) return XTC_ERR_FORMAT; p->control_count = (unsigned int)value;
if (!decode_bytes(input->tokens[5],p->label,sizeof(p->label))) return XTC_ERR_FORMAT;
READ("MODULE",2U);
if (!decode_bytes(input->tokens[1],p->path,sizeof(p->path))) return XTC_ERR_FORMAT;
for (k = 0; k < p->control_count; ++k) {
READ("CONTROL",3U); NUM(1U,value);
if (value >= 64 || !decode_float(input->tokens[2],p->values+k)) return XTC_ERR_FORMAT;
p->ports[k] = (unsigned int)value;
}
if (xtc_plugin_spec_validate(p) != XTC_OK) return XTC_ERR_FORMAT;
}
for (j = 0U; j < i; ++j)
if (graph->nodes[j].id == node.id)
return XTC_ERR_FORMAT;
@ -389,7 +423,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
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",
if (fprintf(file, "XTC_PROJECT 5\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, project->next_track_id,
project->next_clip_id, graph->require_output_nodes, graph->next_id, graph->node_count,
graph->edge_count) < 0)
@ -400,6 +434,17 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
if (fprintf(file, "NODE %zu %u %08" PRIx32 "\n", graph->nodes[i].id, (unsigned int)graph->nodes[i].type, bits) <
0)
return XTC_ERR_IO;
if (graph->nodes[i].type == XTC_NODE_PLUGIN) {
const xtc_plugin_spec *p = &graph->nodes[i].plugin;
size_t k;
if (xtc_plugin_spec_validate(p) != XTC_OK) return XTC_ERR_FORMAT;
if (fprintf(file,"PLUGIN %u %u %u %u ",p->unique_id,p->index,p->inputs,p->control_count) < 0 ||
!write_name(file,p->label) || fputs("MODULE ",file) == EOF || !write_name(file,p->path)) return XTC_ERR_IO;
for (k = 0; k < p->control_count; ++k) {
uint32_t value; memcpy(&value,p->values+k,4);
if (fprintf(file,"CONTROL %u %08" PRIx32 "\n",p->ports[k],value) < 0) return XTC_ERR_IO;
}
}
}
for (i = 0U; i < graph->edge_count; ++i)
if (fprintf(file, "EDGE %zu %zu\n", graph->nodes[graph->edges[i].source].id,

212
src/plugin/ladspa.c Normal file
View file

@ -0,0 +1,212 @@
#include "xtc/xtc_plugin.h"
#include "xtc/xtc_dsp.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <stdio.h>
/* Header-only optional dependency; absent development header gives explicit
* unsupported stubs, preserving metadata-only load/save of plugin documents. */
#ifdef __has_include
#if __has_include(<ladspa.h>) && !defined(XTC_DISABLE_LADSPA)
#define XTC_HAVE_LADSPA 1
#endif
#endif
static int terminated(const char *s, size_t n) { return memchr(s, 0, n) != NULL; }
xtc_result xtc_plugin_spec_validate(const xtc_plugin_spec *s)
{
size_t i;
if (!s || !terminated(s->path,sizeof(s->path)) || s->path[0] != '/' ||
!terminated(s->label,sizeof(s->label)) || !s->label[0] || s->index >= 1024 ||
s->inputs > 1 || s->control_count > XTC_PLUGIN_CONTROLS) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < s->control_count; ++i)
if (s->ports[i] >= 64 || (i && s->ports[i] <= s->ports[i-1]) || !isfinite(s->values[i]))
return XTC_ERR_INVALID_ARGUMENT;
return XTC_OK;
}
#ifdef XTC_HAVE_LADSPA
#include <ladspa.h>
#include <dlfcn.h>
#include <limits.h>
struct xtc_plugin {
void *module;
const LADSPA_Descriptor *descriptor;
LADSPA_Handle handle;
float *input, *output;
float controls[64];
size_t frames;
int activated;
};
static xtc_result load(const char *path, unsigned int index, void **module, const LADSPA_Descriptor **descriptor)
{
LADSPA_Descriptor_Function function;
void *symbol;
if (!path || path[0] != '/' || strlen(path) >= 512 || index >= 1024) return XTC_ERR_INVALID_ARGUMENT;
*module = dlopen(path,RTLD_NOW | RTLD_LOCAL);
if (!*module) return XTC_ERR_NOT_FOUND;
symbol = dlsym(*module,"ladspa_descriptor");
if (!symbol || sizeof(function) != sizeof(symbol)) { dlclose(*module); *module = NULL; return XTC_ERR_UNSUPPORTED; }
memcpy(&function,&symbol,sizeof(function));
*descriptor = function(index);
if (!*descriptor) { dlclose(*module); *module = NULL; return XTC_ERR_NOT_FOUND; }
return XTC_OK;
}
static int bounds(const LADSPA_PortRangeHint *h, double rate, float value)
{
double scale = LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor) ? rate : 1;
if (!isfinite(value) ||
(LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) && value < h->LowerBound * scale) ||
(LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) && value > h->UpperBound * scale) ||
(LADSPA_IS_HINT_INTEGER(h->HintDescriptor) && value != floorf(value)) ||
(LADSPA_IS_HINT_TOGGLED(h->HintDescriptor) && value != 0 && value != 1)) return 0;
return 1;
}
static float initial(const LADSPA_PortRangeHint *h, double rate)
{
unsigned long hint = h->HintDescriptor & LADSPA_HINT_DEFAULT_MASK;
double scale = LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor) ? rate : 1;
double lo = LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) ? h->LowerBound * scale : 0;
double hi = LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) ? h->UpperBound * scale : 0;
double v = 0, fraction = .5;
if (hint == LADSPA_HINT_DEFAULT_1) v = 1;
else if (hint == LADSPA_HINT_DEFAULT_100) v = 100;
else if (hint == LADSPA_HINT_DEFAULT_440) v = 440;
else if (hint == LADSPA_HINT_DEFAULT_MINIMUM) v = lo;
else if (hint == LADSPA_HINT_DEFAULT_MAXIMUM) v = hi;
else if (hint == LADSPA_HINT_DEFAULT_LOW || hint == LADSPA_HINT_DEFAULT_MIDDLE || hint == LADSPA_HINT_DEFAULT_HIGH) {
if (hint == LADSPA_HINT_DEFAULT_LOW) fraction = .25;
if (hint == LADSPA_HINT_DEFAULT_HIGH) fraction = .75;
v = LADSPA_IS_HINT_LOGARITHMIC(h->HintDescriptor) && lo > 0 && hi > 0 ?
exp(log(lo)*(1-fraction)+log(hi)*fraction) : lo*(1-fraction)+hi*fraction;
}
if (LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) && v < lo) v = lo;
if (LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) && v > hi) v = hi;
if (LADSPA_IS_HINT_INTEGER(h->HintDescriptor)) v = round(v);
if (LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)) v = v >= .5 ? 1 : 0;
return (float)v;
}
static xtc_result inspect(const LADSPA_Descriptor *d, double rate, xtc_plugin_spec *s)
{
unsigned long i; unsigned outputs = 0;
if (!d->Label || !d->Label[0] || strlen(d->Label) >= sizeof(s->label) || d->UniqueID > UINT32_MAX ||
!d->PortDescriptors || !d->PortRangeHints || !d->PortCount || d->PortCount > 64 ||
!d->instantiate || !d->connect_port || !d->run || !d->cleanup) return XTC_ERR_UNSUPPORTED;
s->unique_id = (unsigned int)d->UniqueID; strcpy(s->label,d->Label);
for (i = 0; i < d->PortCount; ++i) {
LADSPA_PortDescriptor p = d->PortDescriptors[i];
if (!!LADSPA_IS_PORT_INPUT(p) == !!LADSPA_IS_PORT_OUTPUT(p) ||
!!LADSPA_IS_PORT_AUDIO(p) == !!LADSPA_IS_PORT_CONTROL(p)) return XTC_ERR_UNSUPPORTED;
if (LADSPA_IS_PORT_AUDIO(p)) {
if (LADSPA_IS_PORT_INPUT(p)) ++s->inputs; else ++outputs;
} else if (LADSPA_IS_PORT_INPUT(p)) {
size_t n = s->control_count;
if (n == XTC_PLUGIN_CONTROLS) return XTC_ERR_UNSUPPORTED;
{
const LADSPA_PortRangeHint *h = d->PortRangeHints+i;
if ((LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) && !isfinite(h->LowerBound)) ||
(LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) && !isfinite(h->UpperBound)) ||
(LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) && LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) &&
h->LowerBound > h->UpperBound)) return XTC_ERR_UNSUPPORTED;
}
s->ports[n] = (unsigned int)i; s->values[n] = initial(d->PortRangeHints+i,rate);
if (!bounds(d->PortRangeHints+i,rate,s->values[n])) return XTC_ERR_UNSUPPORTED;
++s->control_count;
}
}
return outputs == 1 && s->inputs <= 1 ? XTC_OK : XTC_ERR_UNSUPPORTED;
}
xtc_result xtc_plugin_probe(const char *path, unsigned int index, double rate, xtc_plugin_spec *out)
{
void *module = NULL; const LADSPA_Descriptor *d; xtc_plugin_spec s = {0}; xtc_result r;
if (!out || !isfinite(rate) || rate < 1 || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
r = load(path,index,&module,&d); if (r != XTC_OK) return r;
r = inspect(d,rate,&s);
if (r == XTC_OK) { strcpy(s.path,path); s.index = index; *out = s; }
dlclose(module); return r;
}
xtc_result xtc_plugin_parameters(const xtc_plugin_spec *s, double rate, xtc_parameter_info out[XTC_PLUGIN_CONTROLS])
{
void *module = NULL; const LADSPA_Descriptor *d; xtc_plugin_spec actual = {0};
xtc_result r; size_t i;
if (!out || xtc_plugin_spec_validate(s) != XTC_OK || !isfinite(rate) || rate < 1 || rate > 768000)
return XTC_ERR_INVALID_ARGUMENT;
r = load(s->path,s->index,&module,&d); if (r != XTC_OK) return r;
r = inspect(d,rate,&actual);
if (r == XTC_OK && (actual.unique_id != s->unique_id || strcmp(actual.label,s->label) ||
actual.control_count != s->control_count || actual.inputs != s->inputs ||
memcmp(actual.ports,s->ports,s->control_count*sizeof(*s->ports)))) r = XTC_ERR_UNSUPPORTED;
if (r == XTC_OK) for (i = 0; i < s->control_count; ++i) {
const LADSPA_PortRangeHint *h = d->PortRangeHints+s->ports[i];
double scale = LADSPA_IS_HINT_SAMPLE_RATE(h->HintDescriptor) ? rate : 1;
xtc_parameter_info p = {0}; p.id = s->ports[i];
snprintf(p.name,sizeof(p.name),"%s",d->PortNames && d->PortNames[p.id] ? d->PortNames[p.id] : "Control");
p.minimum = LADSPA_IS_HINT_BOUNDED_BELOW(h->HintDescriptor) ? (float)(h->LowerBound*scale) : -FLT_MAX;
p.maximum = LADSPA_IS_HINT_BOUNDED_ABOVE(h->HintDescriptor) ? (float)(h->UpperBound*scale) : FLT_MAX;
p.default_value = actual.values[i]; out[i] = p;
}
dlclose(module); return r;
}
void xtc_plugin_destroy(xtc_plugin *p)
{
if (!p) return;
if (p->handle) {
if (p->activated && p->descriptor->deactivate) p->descriptor->deactivate(p->handle);
p->descriptor->cleanup(p->handle);
}
if (p->module) dlclose(p->module);
free(p->input); free(p->output); free(p);
}
xtc_result xtc_plugin_create(const xtc_plugin_spec *s, double rate, size_t frames, xtc_plugin **out)
{
xtc_plugin *p; xtc_plugin_spec actual = {0}; xtc_result r; unsigned long i; size_t c;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
r = xtc_plugin_spec_validate(s); if (r != XTC_OK) return r;
if (!isfinite(rate) || rate < 1 || rate > 768000 || !frames || frames > 65536) return XTC_ERR_INVALID_ARGUMENT;
p = calloc(1,sizeof(*p)); if (!p) return XTC_ERR_OUT_OF_MEMORY;
r = load(s->path,s->index,&p->module,&p->descriptor); if (r != XTC_OK) goto fail;
r = inspect(p->descriptor,rate,&actual); if (r != XTC_OK) goto fail;
r = XTC_ERR_UNSUPPORTED;
if (strcmp(actual.label,s->label) || actual.unique_id != s->unique_id || actual.inputs != s->inputs ||
actual.control_count != s->control_count) goto fail;
for (c = 0; c < s->control_count; ++c) {
if (s->ports[c] != actual.ports[c] || !bounds(p->descriptor->PortRangeHints+s->ports[c],rate,s->values[c])) goto fail;
p->controls[s->ports[c]] = s->values[c];
}
p->frames = frames;
p->input = calloc(frames,sizeof(float)); p->output = calloc(frames,sizeof(float));
r = XTC_ERR_OUT_OF_MEMORY; if (!p->input || !p->output) goto fail;
p->handle = p->descriptor->instantiate(p->descriptor,(unsigned long)rate);
r = XTC_ERR_INVALID_STATE; if (!p->handle) goto fail;
for (i = 0; i < p->descriptor->PortCount; ++i) {
LADSPA_PortDescriptor port = p->descriptor->PortDescriptors[i];
p->descriptor->connect_port(p->handle,i,LADSPA_IS_PORT_CONTROL(port) ? p->controls+i :
LADSPA_IS_PORT_INPUT(port) ? p->input : p->output);
}
if (p->descriptor->activate) p->descriptor->activate(p->handle);
p->activated = 1; *out = p; return XTC_OK;
fail:
xtc_plugin_destroy(p); return r;
}
xtc_result xtc_plugin_process(xtc_plugin *p, const float *input, float *output, size_t frames)
{
size_t i;
if (!p || !output || frames > p->frames) return XTC_ERR_INVALID_ARGUMENT;
if (!frames) return XTC_OK;
for (i = 0; i < frames; ++i) p->input[i] = input ? xtc_dsp_finite(input[i]) : 0;
memset(p->output,0,frames*sizeof(float));
p->descriptor->run(p->handle,(unsigned long)frames);
for (i = 0; i < frames; ++i) output[i] = xtc_dsp_finite(p->output[i]);
return XTC_OK;
}
#else
xtc_result xtc_plugin_parameters(const xtc_plugin_spec *s, double r, xtc_parameter_info out[XTC_PLUGIN_CONTROLS])
{ (void)s; (void)r; (void)out; return XTC_ERR_UNSUPPORTED; }
xtc_result xtc_plugin_probe(const char *p, unsigned int i, double r, xtc_plugin_spec *s)
{ (void)p; (void)i; (void)r; (void)s; return XTC_ERR_UNSUPPORTED; }
xtc_result xtc_plugin_create(const xtc_plugin_spec *s, double r, size_t f, xtc_plugin **out)
{ (void)s; (void)r; (void)f; if (!out) return XTC_ERR_INVALID_ARGUMENT; *out = NULL; return XTC_ERR_UNSUPPORTED; }
void xtc_plugin_destroy(xtc_plugin *p) { (void)p; }
xtc_result xtc_plugin_process(xtc_plugin *p, const float *i, float *o, size_t f)
{ (void)p; (void)i; (void)o; (void)f; return XTC_ERR_UNSUPPORTED; }
#endif