Phase 31: add CLAP, VST2 and VST3 mono effect hosting

This commit is contained in:
Johannes Findeisen 2026-10-27 17:34:46 +01:00
commit 9e0e4562d7
114 changed files with 10369 additions and 75 deletions

View file

@ -77,7 +77,7 @@ void xtc_app_destroy(xtc_app *app)
unsigned int xtc_build_features(void)
{
unsigned int features = 0;
unsigned int features = XTC_FEATURE_CLAP | XTC_FEATURE_VST2 | XTC_FEATURE_VST3;
#ifdef XTC_HAVE_JACK
features |= XTC_FEATURE_JACK;
#endif

View file

@ -590,7 +590,8 @@ xtc_result xtc_graph_set_plugin(xtc_graph *g, size_t id, const xtc_plugin_spec *
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) ||
if (s->format != g->nodes[i].plugin.format || strcmp(s->identifier,g->nodes[i].plugin.identifier) ||
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;

View file

@ -22,7 +22,7 @@ int main(int argc, char **argv)
}
if (argc == 2 && !std::strcmp(argv[1], "--diagnose")) {
unsigned int features = xtc_build_features();
std::printf("xtc %s; project formats 1..%u; JACK %s; LADSPA %s\nFor limits and recovery commands run xtc-check --diagnose/--help.\n",
std::printf("xtc %s; project formats 1..%u; JACK %s; LADSPA %s; CLAP/VST2/VST3 mono effects compiled\nFor limits and recovery commands run xtc-check --diagnose/--help.\n",
XTC_VERSION_STRING,XTC_PROJECT_FORMAT_VERSION,features & XTC_FEATURE_JACK ? "compiled" : "disabled",
features & XTC_FEATURE_LADSPA ? "compiled" : "disabled");
return EXIT_SUCCESS;

View file

@ -88,9 +88,9 @@ 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("File/Load plugin", 0, plugin_callback, this);
menu->add("Edit/Replace selected plugin", 0, plugin_callback, this);
menu->add("Edit/Insert LADSPA on selected wire", 0, plugin_callback, this);
menu->add("Edit/Insert plugin on selected wire", 0, plugin_callback, this);
menu->add("Edit/Reset selected plugin", 0, plugin_callback, this);
menu->add("Edit/Selected plugin automation", 0, plugin_callback, this);
menu->add("Edit/Selected plugin control", 0, plugin_callback, this);
@ -765,6 +765,7 @@ void MainWindow::about_callback(Fl_Widget *, void *)
*/
void MainWindow::timer_callback(void *context)
{
xtc_plugin_idle();
auto *window = static_cast<MainWindow *>(context);
if (window->recorder_) {
xtc_result r = xtc_recorder_drain(window->recorder_);
@ -1579,12 +1580,12 @@ void MainWindow::plugin_callback(Fl_Widget *widget, void *context)
xtc_result r;
if (std::strcmp(label,"Reset selected plugin") == 0) {
r = w->reset_plugin(w->graph_widget_->selected_node());
} else if (std::strcmp(label,"Load LADSPA plugin") == 0 ||
} else if (std::strcmp(label,"Load plugin") == 0 ||
std::strcmp(label,"Replace selected plugin") == 0 ||
std::strcmp(label,"Insert LADSPA on selected wire") == 0) {
std::strcmp(label,"Insert plugin on selected wire") == 0) {
const size_t selected = w->graph_widget_->selected_node();
xtc_connection_info wire{};
const bool insert = std::strcmp(label,"Insert LADSPA on selected wire") == 0;
const bool insert = std::strcmp(label,"Insert plugin on selected wire") == 0;
const bool replace = std::strcmp(label,"Replace selected plugin") == 0;
if (insert && !w->graph_widget_->selected_connection(wire)) {
w->report_error("Select an existing wire before inserting an effect",XTC_ERR_INVALID_STATE); return;
@ -1592,10 +1593,10 @@ void MainWindow::plugin_callback(Fl_Widget *widget, void *context)
if (replace) {
xtc_plugin_spec old{};
r = xtc_graph_get_plugin(xtc_project_graph(w->project_.get()),selected,&old);
if (r != XTC_OK) { w->report_error("Select a LADSPA node to replace",r); return; }
if (r != XTC_OK) { w->report_error("Select a plugin node to replace",r); return; }
}
Fl_Native_File_Chooser chooser;
chooser.title("Choose trusted LADSPA module (executes native code)"); chooser.filter("LADSPA\t*.so");
chooser.title("Choose trusted plugin binary (LADSPA, CLAP, VST2, VST3)"); chooser.filter("Plugin binaries\t*.{so,clap}\nAll files\t*");
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;
@ -1603,7 +1604,7 @@ void MainWindow::plugin_callback(Fl_Widget *widget, void *context)
xtc_plugin_spec probe{};
r = xtc_plugin_probe(chooser.filename(),0,rate,&probe);
if (r != XTC_OK && r != XTC_ERR_UNSUPPORTED) {
w->report_error("Cannot discover LADSPA module; check path, dependencies and ladspa_descriptor export",r); return;
w->report_error("Cannot discover plugin; check binary path, format, mono layout and dependencies",r); return;
}
if (!xtc_choose_plugin(chooser.filename(),rate,index)) return;
if (replace) r = w->replace_plugin(selected,chooser.filename(),index);

View file

@ -24,7 +24,7 @@ struct Dialog : Fl_Double_Window {
}
bool xtc_choose_plugin(const char *path, double rate, unsigned int &index)
{
Dialog dialog(760,450,"LADSPA descriptors");
Dialog dialog(760,450,"Plugin descriptors");
auto *heading = new Fl_Box(15,10,730,40);
heading->copy_label(path); heading->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_CLIP);
auto *list = new Fl_Hold_Browser(15,55,730,325);
@ -56,9 +56,9 @@ bool xtc_edit_plugin(const xtc_plugin_spec &spec, const xtc_plugin_details &deta
Dialog dialog(800,600,"Plugin identity and controls");
auto *identity = new Fl_Box(15,10,770,125);
std::string text = std::string(details.name) + " — " + details.maker + "\n" + spec.label +
" | LADSPA ID " + std::to_string(spec.unique_id) + " | descriptor " + std::to_string(spec.index) +
" | " + std::string(xtc_plugin_format_name(spec.format)) + " ID " + std::to_string(spec.unique_id) + " | descriptor " + std::to_string(spec.index) + " | identity " + spec.identifier +
"\n" + spec.path + "\n" + details.copyright +
"\nMetadata verified; Apply validates initialization. Units unspecified by LADSPA.";
"\nMetadata verified; Apply validates initialization. Adapter may not expose units or editable controls.";
identity->copy_label(text.c_str()); identity->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE|FL_ALIGN_WRAP);
auto *scroll = new Fl_Scroll(10,140,780,370);
std::array<Fl_Value_Input *,XTC_PLUGIN_CONTROLS> inputs{};

View file

@ -55,7 +55,8 @@ static int inspect(const xtc_project *project, uint64_t version)
xtc_graph_get_node(graph,i,&node);
if (xtc_graph_get_plugin(graph,node.id,&spec) == XTC_OK) {
int available = readable_file(spec.path);
printf("LADSPA node %zu ID %u descriptor %u ",node.id,spec.unique_id,spec.index);
printf("%s node %zu ID %u descriptor %u ",xtc_plugin_format_name(spec.format),node.id,spec.unique_id,spec.index);
if (spec.identifier[0]) { quoted(spec.identifier); putchar(' '); }
quoted(spec.label); printf(": %s ",available ? "available" : "missing/unreadable");
quoted(spec.path); putchar('\n');
if (!available) ++missing;
@ -86,7 +87,7 @@ int main(int argc, char **argv)
}
if (argc == 2 && !strcmp(argv[1], "--diagnose")) {
unsigned int features = xtc_build_features();
printf("xtc %s; %zu-bit process\nProject formats: 1..%u\nJACK compiled: %s\nLADSPA compiled: %s\n"
printf("xtc %s; %zu-bit process\nProject formats: 1..%u\nJACK compiled: %s\nLADSPA compiled: %s\nCLAP/VST2/VST3: compiled (mono effects, default configuration)\n"
"Limits: 256 graph nodes (64/project), %u tracks, %u clips, %u notes, 65536 frames/block\n"
"No server, device or plugin probed. Runtime errors appear in GUI status/stderr.\n",
XTC_VERSION_STRING,sizeof(void *)*CHAR_BIT,XTC_PROJECT_FORMAT_VERSION,

View file

@ -259,6 +259,12 @@ static xtc_result read_project(reader *input, xtc_project *project, const char *
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;
if (format_version >= 11U) {
READ("FORMAT",3U); NUM(1U,value);
if (value > XTC_PLUGIN_VST3) return XTC_ERR_UNSUPPORTED;
p->format = (xtc_plugin_format)value;
if (strcmp(input->tokens[2],"-") && !decode_bytes(input->tokens[2],p->identifier,sizeof(p->identifier))) 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;
@ -506,6 +512,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project, const ch
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;
if (fprintf(file,"FORMAT %u ",(unsigned int)p->format) < 0 || (p->identifier[0] ? !write_name(file,p->identifier) : fputs("-\n",file) == EOF)) 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;

108
src/plugin/host.c Normal file
View file

@ -0,0 +1,108 @@
#include "modern.h"
#include <dlfcn.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct xtc_ladspa_plugin xtc_ladspa_plugin;
xtc_result xtc_ladspa_probe(const char *, unsigned int, double, xtc_plugin_spec *);
xtc_result xtc_ladspa_describe(const xtc_plugin_spec *, double, xtc_plugin_details *);
xtc_result xtc_ladspa_create(const xtc_plugin_spec *, double, size_t, xtc_ladspa_plugin **);
void xtc_ladspa_destroy(xtc_ladspa_plugin *);
xtc_result xtc_ladspa_process_at(xtc_ladspa_plugin *, const float *, float *, size_t, uint64_t);
struct xtc_plugin { xtc_ladspa_plugin *ladspa; xtc_modern *modern; uint64_t next; };
const char *xtc_plugin_format_name(xtc_plugin_format f)
{ static const char *names[] = {"LADSPA", "CLAP", "VST2", "VST3"}; return f >= 0 && f <= XTC_PLUGIN_VST3 ? names[f] : "Unknown"; }
xtc_result xtc_plugin_probe_format(xtc_plugin_format f, const char *path, unsigned int index, double rate, xtc_plugin_spec *out)
{
xtc_modern *p = NULL; xtc_result r;
if (!out || !path || path[0] != '/' || strlen(path) >= 512 || index >= 1024 || !isfinite(rate) || rate < 1 || rate > 768000)
return XTC_ERR_INVALID_ARGUMENT;
if (f == XTC_PLUGIN_LADSPA) return xtc_ladspa_probe(path,index,rate,out);
if (f < XTC_PLUGIN_CLAP || f > XTC_PLUGIN_VST3) return XTC_ERR_INVALID_ARGUMENT;
r = xtc_modern_open(f,path,index,rate,256,&p,out);
xtc_modern_destroy(p); return r;
}
xtc_result xtc_plugin_probe(const char *path, unsigned int index, double rate, xtc_plugin_spec *out)
{
void *module; xtc_plugin_format f = XTC_PLUGIN_LADSPA;
if (!path || path[0] != '/' || strlen(path) >= 512) return XTC_ERR_INVALID_ARGUMENT;
module = dlopen(path,RTLD_NOW | RTLD_LOCAL);
if (!module) return XTC_ERR_NOT_FOUND;
if (dlsym(module,"clap_entry")) f = XTC_PLUGIN_CLAP;
else if (dlsym(module,"VSTPluginMain")) f = XTC_PLUGIN_VST2;
else if (dlsym(module,"GetPluginFactory")) f = XTC_PLUGIN_VST3;
dlclose(module);
return xtc_plugin_probe_format(f,path,index,rate,out);
}
xtc_result xtc_plugin_describe(const xtc_plugin_spec *s, double rate, xtc_plugin_details *out)
{
xtc_plugin_spec actual; xtc_result r;
if (!out || xtc_plugin_spec_validate(s) != XTC_OK) return XTC_ERR_INVALID_ARGUMENT;
if (s->format == XTC_PLUGIN_LADSPA) return xtc_ladspa_describe(s,rate,out);
r = xtc_plugin_probe_format(s->format,s->path,s->index,rate,&actual);
if (r != XTC_OK) return r;
if (actual.unique_id != s->unique_id || strcmp(actual.identifier,s->identifier) || strcmp(actual.label,s->label)) return XTC_ERR_UNSUPPORTED;
memset(out,0,sizeof(*out)); snprintf(out->name,sizeof(out->name),"%s",s->label);
snprintf(out->maker,sizeof(out->maker),"Not exposed by this adapter");
snprintf(out->copyright,sizeof(out->copyright),"See plugin license");
return XTC_OK;
}
void xtc_plugin_destroy(xtc_plugin *p)
{ if (p) { xtc_ladspa_destroy(p->ladspa); xtc_modern_destroy(p->modern); free(p); } }
xtc_result xtc_plugin_create(const xtc_plugin_spec *s, double rate, size_t frames, xtc_plugin **out)
{
xtc_plugin *p; xtc_result r; xtc_plugin_spec actual;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
*out = NULL;
r = xtc_plugin_spec_validate(s); if (r != XTC_OK) return r;
if (!frames || frames > 65536 || !isfinite(rate) || rate < 1 || rate > 768000) return XTC_ERR_INVALID_ARGUMENT;
p = calloc(1,sizeof(*p)); if (!p) return XTC_ERR_OUT_OF_MEMORY;
if (s->format == XTC_PLUGIN_LADSPA) r = xtc_ladspa_create(s,rate,frames,&p->ladspa);
else {
r = xtc_modern_open(s->format,s->path,s->index,rate,frames,&p->modern,&actual);
if (r == XTC_OK && (actual.unique_id != s->unique_id || actual.inputs != s->inputs ||
strcmp(actual.identifier,s->identifier) || strcmp(actual.label,s->label))) r = XTC_ERR_UNSUPPORTED;
}
if (r != XTC_OK) { xtc_plugin_destroy(p); return r; }
*out = p; return XTC_OK;
}
xtc_result xtc_plugin_process_at(xtc_plugin *p, const float *in, float *out, size_t frames, uint64_t position)
{
xtc_result r;
if (!p) return XTC_ERR_INVALID_ARGUMENT;
r = p->modern ? xtc_modern_process(p->modern,in,out,frames) : xtc_ladspa_process_at(p->ladspa,in,out,frames,position);
if (r == XTC_OK) p->next = position <= UINT64_MAX-frames ? position+frames : 0;
return r;
}
xtc_result xtc_plugin_process(xtc_plugin *p, const float *in, float *out, size_t frames)
{ return xtc_plugin_process_at(p,in,out,frames,p ? p->next : 0); }
xtc_result xtc_plugin_parameters(const xtc_plugin_spec *s, double rate, xtc_parameter_info out[XTC_PLUGIN_CONTROLS])
{
xtc_plugin_details details;
xtc_result r;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
r = xtc_plugin_describe(s,rate,&details);
if (r == XTC_OK) memcpy(out,details.parameters,sizeof(details.parameters));
return r;
}
xtc_result xtc_plugin_reset(xtc_plugin_spec *s, double rate, unsigned int port)
{
xtc_plugin_details details;
xtc_plugin_spec candidate;
size_t i, count = 0;
int found = port == UINT32_MAX;
xtc_result r = xtc_plugin_describe(s,rate,&details);
if (r != XTC_OK) return r;
candidate = *s;
for (i = 0; i < s->control_count; ++i) if (port == UINT32_MAX || s->ports[i] == port) {
candidate.values[i] = details.parameters[i].default_value;
found = 1;
}
if (!found) return XTC_ERR_NOT_FOUND;
for (i = 0; i < s->point_count; ++i)
if (port != UINT32_MAX && s->points[i].port != port) candidate.points[count++] = s->points[i];
candidate.point_count = (unsigned int)count;
*s = candidate;
return XTC_OK;
}

View file

@ -1,3 +1,10 @@
#define xtc_plugin xtc_ladspa_plugin
#define xtc_plugin_probe xtc_ladspa_probe
#define xtc_plugin_describe xtc_ladspa_describe
#define xtc_plugin_create xtc_ladspa_create
#define xtc_plugin_destroy xtc_ladspa_destroy
#define xtc_plugin_process_at xtc_ladspa_process_at
#define xtc_plugin_process xtc_ladspa_process
#include "xtc/xtc_plugin.h"
#include "xtc/xtc_dsp.h"
#include "xtc/xtc_project.h"
@ -17,6 +24,20 @@ xtc_result xtc_plugin_spec_validate(const xtc_plugin_spec *s)
!terminated(s->label,sizeof(s->label)) || !s->label[0] || s->index >= 1024 ||
s->inputs > 1 || s->control_count > XTC_PLUGIN_CONTROLS ||
s->tempo < 20000 || s->tempo > 300000 || s->point_count > XTC_PLUGIN_POINTS) return XTC_ERR_INVALID_ARGUMENT;
if (s->format < XTC_PLUGIN_LADSPA || s->format > XTC_PLUGIN_VST3 ||
!terminated(s->identifier,sizeof(s->identifier))) return XTC_ERR_INVALID_ARGUMENT;
if (s->format != XTC_PLUGIN_LADSPA) {
if (s->inputs != 1 || s->control_count || s->point_count ||
((s->format == XTC_PLUGIN_CLAP || s->format == XTC_PLUGIN_VST3) && !s->identifier[0]))
return XTC_ERR_UNSUPPORTED;
if (s->format == XTC_PLUGIN_VST2 && s->identifier[0]) return XTC_ERR_INVALID_ARGUMENT;
if (s->format == XTC_PLUGIN_VST3) {
if (strlen(s->identifier) != 32) return XTC_ERR_INVALID_ARGUMENT;
for (i = 0; i < 32; ++i)
if (!((s->identifier[i] >= '0' && s->identifier[i] <= '9') ||
(s->identifier[i] >= 'a' && s->identifier[i] <= 'f'))) return XTC_ERR_INVALID_ARGUMENT;
}
} else if (s->identifier[0]) 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;
@ -282,33 +303,3 @@ xtc_result xtc_plugin_process_at(xtc_plugin *p, const float *i, float *o, size_t
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
xtc_result xtc_plugin_parameters(const xtc_plugin_spec *s, double rate, xtc_parameter_info out[XTC_PLUGIN_CONTROLS])
{
xtc_plugin_details details;
xtc_result r;
if (!out) return XTC_ERR_INVALID_ARGUMENT;
r = xtc_plugin_describe(s,rate,&details);
if (r == XTC_OK) memcpy(out,details.parameters,sizeof(details.parameters));
return r;
}
xtc_result xtc_plugin_reset(xtc_plugin_spec *s, double rate, unsigned int port)
{
xtc_plugin_details details;
xtc_plugin_spec candidate;
size_t i, count = 0;
int found = port == UINT32_MAX;
xtc_result r = xtc_plugin_describe(s,rate,&details);
if (r != XTC_OK) return r;
candidate = *s;
for (i = 0; i < s->control_count; ++i) if (port == UINT32_MAX || s->ports[i] == port) {
candidate.values[i] = details.parameters[i].default_value;
found = 1;
}
if (!found) return XTC_ERR_NOT_FOUND;
for (i = 0; i < s->point_count; ++i)
if (port != UINT32_MAX && s->points[i].port != port) candidate.points[count++] = s->points[i];
candidate.point_count = (unsigned int)count;
*s = candidate;
return XTC_OK;
}

315
src/plugin/modern.c Normal file
View file

@ -0,0 +1,315 @@
#define _POSIX_C_SOURCE 200809L
#include "modern.h"
#include "vst2_abi.h"
#include "xtc/xtc_dsp.h"
#include "xtc/xtc_version.h"
#include <clap/clap.h>
#include <clap/ext/thread-check.h>
#include <vst3_c_api.h>
#include <dlfcn.h>
#include <math.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Module cache is control-thread owned. Entry initialization is once per loaded
* module, even when stereo preparation and retired plans overlap. */
typedef struct module {
void *handle;
xtc_plugin_format format;
unsigned int references;
const clap_plugin_entry_t *entry;
bool (*exit3)(void);
struct module *next;
} module;
static module *modules;
struct xtc_modern {
module *module;
xtc_plugin_format format;
const clap_plugin_t *clap;
clap_host_t host;
xtc_v2_effect *v2;
Steinberg_IPluginFactory *factory;
Steinberg_Vst_IComponent *component;
Steinberg_Vst_IAudioProcessor *processor;
float *input, *output;
size_t frames;
double rate;
pthread_t control_thread;
int initialized, active, started;
unsigned int callback, restart;
struct xtc_modern *next;
};
static xtc_modern *instances;
static __thread int in_audio;
static __thread xtc_modern *constructing;
static int symbol(void *handle, const char *name, void *out, size_t size)
{
void *value = dlsym(handle,name);
if (!value || size != sizeof(value)) return 0;
memcpy(out,&value,size); return 1;
}
static void module_release(module *m)
{
module **slot;
if (!m || --m->references) return;
for (slot = &modules; *slot && *slot != m; slot = &(*slot)->next) {}
if (*slot) *slot = m->next;
if (m->entry) m->entry->deinit();
if (m->exit3) m->exit3();
dlclose(m->handle); free(m);
}
static xtc_result module_open(xtc_plugin_format format, const char *path, module **out)
{
void *handle = dlopen(path,RTLD_NOW | RTLD_LOCAL);
module *m; xtc_result r = XTC_ERR_UNSUPPORTED;
if (!handle) return XTC_ERR_NOT_FOUND;
for (m = modules; m; m = m->next) if (m->handle == handle && m->format == format) {
++m->references; dlclose(handle); *out = m; return XTC_OK;
}
m = calloc(1,sizeof(*m));
if (!m) { dlclose(handle); return XTC_ERR_OUT_OF_MEMORY; }
m->handle = handle; m->format = format; m->references = 1;
if (format == XTC_PLUGIN_CLAP) {
const clap_plugin_entry_t *entry = dlsym(handle,"clap_entry");
if (!entry || !clap_version_is_compatible(entry->clap_version) || !entry->init || !entry->deinit || !entry->get_factory) goto fail;
if (!entry->init(path)) { r = XTC_ERR_INVALID_STATE; goto fail; }
m->entry = entry;
} else if (format == XTC_PLUGIN_VST3) {
bool (*entry)(void *) = NULL; bool (*exit_module)(void) = NULL;
if (!symbol(handle,"ModuleEntry",&entry,sizeof(entry)) || !symbol(handle,"ModuleExit",&exit_module,sizeof(exit_module))) goto fail;
if (!entry(handle)) { r = XTC_ERR_INVALID_STATE; goto fail; }
m->exit3 = exit_module;
}
m->next = modules; modules = m; *out = m; return XTC_OK;
fail:
module_release(m); return r;
}
static void request_callback(const clap_host_t *h)
{ xtc_modern *p = h->host_data; __atomic_store_n(&p->callback,1U,__ATOMIC_RELEASE); }
static void request_restart(const clap_host_t *h)
{ xtc_modern *p = h->host_data; __atomic_store_n(&p->restart,1U,__ATOMIC_RELEASE); }
static void request_process(const clap_host_t *h) { (void)h; /* Always processed when transport runs. */ }
static bool is_main(const clap_host_t *h)
{ const xtc_modern *p = h->host_data; return !in_audio && pthread_equal(pthread_self(),p->control_thread); }
static bool is_audio(const clap_host_t *h) { (void)h; return in_audio != 0; }
static const void *host_extension(const clap_host_t *h, const char *id)
{
static const clap_host_thread_check_t check = {is_main,is_audio};
(void)h; return id && !strcmp(id,CLAP_EXT_THREAD_CHECK) ? &check : NULL;
}
void xtc_plugin_idle(void)
{
xtc_modern *p;
for (p = instances; p; p = p->next)
if (p->clap && __atomic_exchange_n(&p->callback,0U,__ATOMIC_ACQ_REL)) p->clap->on_main_thread(p->clap);
}
static uint32_t event_count(const clap_input_events_t *list) { (void)list; return 0; }
static const clap_event_header_t *event_get(const clap_input_events_t *list, uint32_t index)
{ (void)list; (void)index; return NULL; }
static bool event_push(const clap_output_events_t *list, const clap_event_header_t *event)
{ (void)list; (void)event; return false; }
static xtc_result open_clap(xtc_modern *p, unsigned int index, xtc_plugin_spec *s)
{
const clap_plugin_factory_t *factory = p->module->entry->get_factory(CLAP_PLUGIN_FACTORY_ID);
const clap_plugin_descriptor_t *d; const clap_plugin_audio_ports_t *ports;
clap_audio_port_info_t info;
if (!factory || !factory->get_plugin_count || !factory->get_plugin_descriptor || !factory->create_plugin) return XTC_ERR_UNSUPPORTED;
if (index >= factory->get_plugin_count(factory)) return XTC_ERR_NOT_FOUND;
d = factory->get_plugin_descriptor(factory,index);
if (!d || !clap_version_is_compatible(d->clap_version) || !d->id || !d->name ||
!d->id[0] || strlen(d->id) >= sizeof(s->identifier) || !d->name[0]) return XTC_ERR_UNSUPPORTED;
snprintf(s->identifier,sizeof(s->identifier),"%s",d->id); snprintf(s->label,sizeof(s->label),"%s",d->name);
p->host = (clap_host_t){CLAP_VERSION,p,"xtc","xtc","",XTC_VERSION_STRING,host_extension,request_restart,request_process,request_callback};
p->clap = factory->create_plugin(factory,&p->host,d->id);
if (!p->clap) return XTC_ERR_INVALID_STATE;
if (!p->clap->init || !p->clap->destroy || !p->clap->activate || !p->clap->deactivate ||
!p->clap->start_processing || !p->clap->stop_processing || !p->clap->process || !p->clap->get_extension || !p->clap->on_main_thread)
return XTC_ERR_UNSUPPORTED;
if (!p->clap->init(p->clap)) return XTC_ERR_INVALID_STATE;
ports = p->clap->get_extension(p->clap,CLAP_EXT_AUDIO_PORTS);
if (!ports || !ports->count || !ports->get || ports->count(p->clap,true) != 1 || ports->count(p->clap,false) != 1 ||
!ports->get(p->clap,0,true,&info) || info.channel_count != 1 ||
!ports->get(p->clap,0,false,&info) || info.channel_count != 1) return XTC_ERR_UNSUPPORTED;
{ const clap_plugin_note_ports_t *notes = p->clap->get_extension(p->clap,CLAP_EXT_NOTE_PORTS);
if (notes && (!notes->count || notes->count(p->clap,true) || notes->count(p->clap,false))) return XTC_ERR_UNSUPPORTED; }
if (!p->clap->activate(p->clap,p->rate,1,(uint32_t)p->frames)) return XTC_ERR_INVALID_STATE;
p->active = 1;
{ const clap_plugin_latency_t *latency = p->clap->get_extension(p->clap,CLAP_EXT_LATENCY);
if (latency && (!latency->get || latency->get(p->clap))) return XTC_ERR_UNSUPPORTED; }
return XTC_OK;
}
static intptr_t v2_host(xtc_v2_effect *effect, int32_t opcode, int32_t index, intptr_t value, void *ptr, float option)
{
xtc_modern *p = constructing ? constructing : effect ? effect->user : NULL;
(void)index; (void)value; (void)option;
switch (opcode) {
case 1: return 2400; /* host API version */
case 13: if (p) __atomic_store_n(&p->restart,1U,__ATOMIC_RELEASE); return 0;
case 16: return p ? (intptr_t)p->rate : 48000;
case 17: return p ? (intptr_t)p->frames : 256;
case 32: if (ptr) memcpy(ptr,"xtc",4); return 1;
case 33: if (ptr) memcpy(ptr,"xtc",4); return 1;
case 34: return 1;
case 23: return in_audio ? 2 : 1; /* Realtime or user/main process level. */
default: return 0;
}
}
static xtc_result open_v2(xtc_modern *p, unsigned int index, xtc_plugin_spec *s)
{
xtc_v2_effect *(*entry)(xtc_v2_host) = NULL;
char name[64] = {0};
if (index) return XTC_ERR_NOT_FOUND;
if (!symbol(p->module->handle,"VSTPluginMain",&entry,sizeof(entry))) return XTC_ERR_UNSUPPORTED;
constructing = p; p->v2 = entry(v2_host); constructing = NULL;
if (!p->v2 || p->v2->magic != XTC_V2_MAGIC || !p->v2->dispatch) return XTC_ERR_UNSUPPORTED;
p->v2->user = p;
p->v2->dispatch(p->v2,0,0,0,NULL,0); p->initialized = 1;
if (p->v2->inputs != 1 || p->v2->outputs != 1 || !p->v2->replace || !(p->v2->flags & 16) ||
(p->v2->flags & 256) || p->v2->initial_delay) return XTC_ERR_UNSUPPORTED;
p->v2->dispatch(p->v2,45,0,0,name,0); name[63] = 0;
if (!name[0]) return XTC_ERR_UNSUPPORTED;
snprintf(s->label,sizeof(s->label),"%s",name); s->unique_id = (unsigned int)p->v2->unique_id;
p->v2->dispatch(p->v2,10,0,0,NULL,(float)p->rate);
p->v2->dispatch(p->v2,11,0,(intptr_t)p->frames,NULL,0);
p->v2->dispatch(p->v2,12,0,1,NULL,0); p->active = 1;
p->v2->dispatch(p->v2,71,0,0,NULL,0); p->started = 1;
return XTC_OK;
}
static Steinberg_tresult host_query(void *self, const Steinberg_TUID iid, void **out)
{
if (!out || !iid) return Steinberg_kInvalidArgument;
*out = NULL;
if (!memcmp(iid,Steinberg_FUnknown_iid,16) || !memcmp(iid,Steinberg_Vst_IHostApplication_iid,16)) { *out = self; return 0; }
return Steinberg_kNoInterface;
}
static Steinberg_uint32 host_ref(void *self) { (void)self; return 1; }
static Steinberg_tresult host_name(void *self, Steinberg_Vst_String128 name)
{ (void)self; if (!name) return Steinberg_kInvalidArgument; memset(name,0,sizeof(Steinberg_Vst_String128)); name[0] = 'x'; name[1] = 't'; name[2] = 'c'; return 0; }
static Steinberg_tresult host_create(void *self, Steinberg_TUID cid, Steinberg_TUID iid, void **out)
{ (void)self; (void)cid; (void)iid; if (!out) return Steinberg_kInvalidArgument; *out = NULL; return Steinberg_kNoInterface; }
static Steinberg_Vst_IHostApplicationVtbl host_vtable = {host_query,host_ref,host_ref,host_name,host_create};
static Steinberg_Vst_IHostApplication application = {&host_vtable};
static xtc_result open_v3(xtc_modern *p, unsigned int index, xtc_plugin_spec *s)
{
Steinberg_IPluginFactory *(*entry)(void) = NULL;
struct Steinberg_PClassInfo info;
struct Steinberg_Vst_ProcessSetup setup = {0,0,(Steinberg_int32)p->frames,p->rate};
Steinberg_Vst_SpeakerArrangement mono = Steinberg_Vst_SpeakerArr_kMono;
unsigned int i;
if (!symbol(p->module->handle,"GetPluginFactory",&entry,sizeof(entry))) return XTC_ERR_UNSUPPORTED;
p->factory = entry();
if (!p->factory || !p->factory->lpVtbl || !p->factory->lpVtbl->release ||
!p->factory->lpVtbl->countClasses || !p->factory->lpVtbl->getClassInfo || !p->factory->lpVtbl->createInstance) return XTC_ERR_UNSUPPORTED;
if ((Steinberg_int32)index >= p->factory->lpVtbl->countClasses(p->factory)) return XTC_ERR_NOT_FOUND;
memset(&info,0,sizeof(info));
if (p->factory->lpVtbl->getClassInfo(p->factory,(Steinberg_int32)index,&info) != 0 ||
!memchr(info.category,0,sizeof(info.category)) || strcmp(info.category,"Audio Module Class") ||
!memchr(info.name,0,sizeof(info.name)) || !info.name[0]) return XTC_ERR_UNSUPPORTED;
for (i = 0; i < 16; ++i) snprintf(s->identifier+i*2,3,"%02x",(unsigned char)info.cid[i]);
snprintf(s->label,sizeof(s->label),"%s",info.name);
if (p->factory->lpVtbl->createInstance(p->factory,info.cid,Steinberg_Vst_IComponent_iid,(void **)&p->component) != 0 || !p->component)
return XTC_ERR_INVALID_STATE;
if (!p->component->lpVtbl || !p->component->lpVtbl->initialize || !p->component->lpVtbl->terminate ||
!p->component->lpVtbl->queryInterface || !p->component->lpVtbl->release || !p->component->lpVtbl->getBusCount ||
!p->component->lpVtbl->getBusInfo || !p->component->lpVtbl->activateBus || !p->component->lpVtbl->setActive) return XTC_ERR_UNSUPPORTED;
if (p->component->lpVtbl->initialize(p->component,(Steinberg_FUnknown *)&application) != 0) return XTC_ERR_INVALID_STATE;
p->initialized = 1;
if (p->component->lpVtbl->queryInterface(p->component,Steinberg_Vst_IAudioProcessor_iid,(void **)&p->processor) != 0 || !p->processor)
return XTC_ERR_UNSUPPORTED;
if (!p->processor->lpVtbl || !p->processor->lpVtbl->release || !p->processor->lpVtbl->canProcessSampleSize ||
!p->processor->lpVtbl->setBusArrangements || !p->processor->lpVtbl->setupProcessing || !p->processor->lpVtbl->getLatencySamples ||
!p->processor->lpVtbl->setProcessing || !p->processor->lpVtbl->process) return XTC_ERR_UNSUPPORTED;
if (p->component->lpVtbl->getBusCount(p->component,0,0) != 1 || p->component->lpVtbl->getBusCount(p->component,0,1) != 1 ||
p->component->lpVtbl->getBusCount(p->component,1,0) || p->component->lpVtbl->getBusCount(p->component,1,1) ||
p->processor->lpVtbl->canProcessSampleSize(p->processor,0) != 0 ||
p->processor->lpVtbl->setBusArrangements(p->processor,&mono,1,&mono,1) != 0) return XTC_ERR_UNSUPPORTED;
for (i = 0; i < 2; ++i) {
struct Steinberg_Vst_BusInfo bus;
if (p->component->lpVtbl->getBusInfo(p->component,0,(Steinberg_int32)i,0,&bus) != 0 || bus.channelCount != 1 ||
p->component->lpVtbl->activateBus(p->component,0,(Steinberg_int32)i,0,1) != 0) return XTC_ERR_UNSUPPORTED;
}
if (p->processor->lpVtbl->setupProcessing(p->processor,&setup) != 0 || p->processor->lpVtbl->getLatencySamples(p->processor)) return XTC_ERR_UNSUPPORTED;
if (p->component->lpVtbl->setActive(p->component,1) != 0) return XTC_ERR_INVALID_STATE;
p->active = 1;
if (p->processor->lpVtbl->setProcessing(p->processor,1) != 0) return XTC_ERR_INVALID_STATE;
p->started = 1; return XTC_OK;
}
xtc_result xtc_modern_open(xtc_plugin_format format, const char *path, unsigned int index, double rate, size_t frames, xtc_modern **out, xtc_plugin_spec *spec)
{
xtc_modern *p; xtc_plugin_spec s = {0}; xtc_result r;
if (!__atomic_always_lock_free(sizeof(unsigned int),0)) return XTC_ERR_UNSUPPORTED;
p = calloc(1,sizeof(*p));
if (!p) return XTC_ERR_OUT_OF_MEMORY;
p->format = format; p->frames = frames; p->rate = rate; p->control_thread = pthread_self();
p->input = calloc(frames,sizeof(float)); p->output = calloc(frames,sizeof(float));
if (!p->input || !p->output) { xtc_modern_destroy(p); return XTC_ERR_OUT_OF_MEMORY; }
r = module_open(format,path,&p->module);
s.format = format; s.index = index; s.inputs = 1; s.tempo = 120000;
snprintf(s.path,sizeof(s.path),"%s",path);
if (r == XTC_OK) r = format == XTC_PLUGIN_CLAP ? open_clap(p,index,&s) : format == XTC_PLUGIN_VST2 ? open_v2(p,index,&s) : open_v3(p,index,&s);
if (r != XTC_OK) { xtc_modern_destroy(p); return r; }
p->next = instances; instances = p; *out = p; *spec = s; return XTC_OK;
}
void xtc_modern_destroy(xtc_modern *p)
{
xtc_modern **slot;
if (!p) return;
for (slot = &instances; *slot && *slot != p; slot = &(*slot)->next) {}
if (*slot) *slot = p->next;
if (p->clap) {
if (p->started) { in_audio = 1; p->clap->stop_processing(p->clap); in_audio = 0; }
if (p->active) p->clap->deactivate(p->clap);
if (p->clap->destroy) p->clap->destroy(p->clap);
}
if (p->v2 && p->v2->magic == XTC_V2_MAGIC && p->v2->dispatch && p->initialized) {
if (p->started) p->v2->dispatch(p->v2,72,0,0,NULL,0);
if (p->active) p->v2->dispatch(p->v2,12,0,0,NULL,0);
p->v2->dispatch(p->v2,1,0,0,NULL,0);
}
if (p->processor && p->processor->lpVtbl) {
if (p->started) p->processor->lpVtbl->setProcessing(p->processor,0);
if (p->active) p->component->lpVtbl->setActive(p->component,0);
if (p->processor->lpVtbl->release) p->processor->lpVtbl->release(p->processor);
}
if (p->component && p->component->lpVtbl) {
if (p->initialized) p->component->lpVtbl->terminate(p->component);
if (p->component->lpVtbl->release) p->component->lpVtbl->release(p->component);
}
if (p->factory && p->factory->lpVtbl && p->factory->lpVtbl->release) p->factory->lpVtbl->release(p->factory);
module_release(p->module); free(p->input); free(p->output); free(p);
}
xtc_result xtc_modern_process(xtc_modern *p, const float *input, float *output, size_t frames)
{
size_t i; int ok = 1;
if (!p || !output || frames > p->frames) return XTC_ERR_INVALID_ARGUMENT;
if (!frames) return XTC_OK;
if (__atomic_load_n(&p->restart,__ATOMIC_ACQUIRE)) { memset(output,0,frames*sizeof(float)); return XTC_ERR_INVALID_STATE; }
for (i = 0; i < frames; ++i) p->input[i] = input ? xtc_dsp_finite(input[i]) : 0;
memset(p->output,0,frames*sizeof(float));
in_audio = 1;
if (p->clap) {
clap_input_events_t events = {NULL,event_count,event_get}; clap_output_events_t sink = {NULL,event_push};
clap_audio_buffer_t in = {&p->input,NULL,1,0,0}, out = {&p->output,NULL,1,0,0};
clap_process_t block = {-1,(uint32_t)frames,NULL,&in,&out,1,1,&events,&sink};
if (!p->started) { p->started = p->clap->start_processing(p->clap); if (!p->started) ok = 0; }
if (ok) {
clap_process_status status = p->clap->process(p->clap,&block);
ok = status >= CLAP_PROCESS_CONTINUE && status <= CLAP_PROCESS_SLEEP;
}
} else if (p->v2) p->v2->replace(p->v2,&p->input,&p->output,(int32_t)frames);
else {
struct Steinberg_Vst_AudioBusBuffers in = {0}, out = {0};
struct Steinberg_Vst_ProcessData data = {0};
in.numChannels = out.numChannels = 1;
in.Steinberg_Vst_AudioBusBuffers_channelBuffers32 = &p->input;
out.Steinberg_Vst_AudioBusBuffers_channelBuffers32 = &p->output;
data.numSamples = (Steinberg_int32)frames; data.numInputs = data.numOutputs = 1; data.inputs = &in; data.outputs = &out;
ok = p->processor->lpVtbl->process(p->processor,&data) == 0;
}
in_audio = 0;
for (i = 0; i < frames; ++i) output[i] = ok ? xtc_dsp_finite(p->output[i]) : 0;
return ok ? XTC_OK : XTC_ERR_INVALID_STATE;
}

8
src/plugin/modern.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef XTC_MODERN_H
#define XTC_MODERN_H
#include "xtc/xtc_plugin.h"
typedef struct xtc_modern xtc_modern;
xtc_result xtc_modern_open(xtc_plugin_format format, const char *path, unsigned int index, double rate, size_t frames, xtc_modern **out, xtc_plugin_spec *spec);
xtc_result xtc_modern_process(xtc_modern *plugin, const float *input, float *output, size_t frames);
void xtc_modern_destroy(xtc_modern *plugin);
#endif

26
src/plugin/vst2_abi.h Normal file
View file

@ -0,0 +1,26 @@
#ifndef XTC_VST2_ABI_H
#define XTC_VST2_ABI_H
/* Original, minimal Linux ABI declarations for legacy VST2 interoperability.
* No Steinberg VST2 SDK code is included. Not a complete SDK or VST2 wrapper. */
#include <stdint.h>
typedef struct xtc_v2_effect xtc_v2_effect;
typedef intptr_t (*xtc_v2_host)(xtc_v2_effect *, int32_t, int32_t, intptr_t, void *, float);
typedef intptr_t (*xtc_v2_dispatch)(xtc_v2_effect *, int32_t, int32_t, intptr_t, void *, float);
struct xtc_v2_effect {
int32_t magic;
xtc_v2_dispatch dispatch;
void (*process)(xtc_v2_effect *, float **, float **, int32_t);
void (*set_parameter)(xtc_v2_effect *, int32_t, float);
float (*get_parameter)(xtc_v2_effect *, int32_t);
int32_t programs, parameters, inputs, outputs, flags;
intptr_t reserved1, reserved2;
int32_t initial_delay, quality1, quality2;
float ratio;
void *object, *user;
int32_t unique_id, version;
void (*replace)(xtc_v2_effect *, float **, float **, int32_t);
void (*replace64)(xtc_v2_effect *, double **, double **, int32_t);
char future[56];
};
#define XTC_V2_MAGIC INT32_C(0x56737450)
#endif