Phase 16: persist sample-timed effect automation and add expressive MIDI controls

This commit is contained in:
Johannes Findeisen 2026-10-27 06:25:30 +01:00
commit 7fac0a4f6a
23 changed files with 374 additions and 39 deletions

View file

@ -1,5 +1,6 @@
#include "xtc/xtc_plugin.h"
#include "xtc/xtc_dsp.h"
#include "xtc/xtc_project.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
@ -18,10 +19,20 @@ 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;
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;
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;
for (i = 0; i < s->point_count; ++i) {
size_t j;
const xtc_plugin_point *a = s->points+i;
if (a->tick > XTC_PROJECT_MAX_TICK || a->step > 1 || !isfinite(a->value)) return XTC_ERR_INVALID_ARGUMENT;
for (j = 0; j < s->control_count && s->ports[j] != a->port; ++j) {}
if (j == s->control_count) return XTC_ERR_INVALID_ARGUMENT;
for (j = 0; j < i; ++j) if (s->points[j].port == a->port && s->points[j].tick == a->tick)
return XTC_ERR_INVALID_ARGUMENT;
}
return XTC_OK;
}
#ifdef XTC_HAVE_LADSPA
@ -36,6 +47,9 @@ struct xtc_plugin {
float controls[64];
size_t frames;
int activated;
xtc_plugin_point points[XTC_PLUGIN_POINTS];
uint64_t samples[XTC_PLUGIN_POINTS], next;
unsigned int point_count;
};
static xtc_result load(const char *path, unsigned int index, void **module, const LADSPA_Descriptor **descriptor)
{
@ -91,6 +105,7 @@ static xtc_result inspect(const LADSPA_Descriptor *d, double rate, xtc_plugin_sp
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->tempo = 120000;
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];
@ -173,6 +188,15 @@ xtc_result xtc_plugin_create(const xtc_plugin_spec *s, double rate, size_t frame
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];
}
for (c = 0; c < s->point_count; ++c) {
const xtc_plugin_point *a = s->points+c;
const LADSPA_PortRangeHint *h = p->descriptor->PortRangeHints+a->port;
if (!bounds(h,rate,a->value) || (!a->step &&
(LADSPA_IS_HINT_INTEGER(h->HintDescriptor) || LADSPA_IS_HINT_TOGGLED(h->HintDescriptor)))) goto fail;
p->points[c] = *a;
p->samples[c] = a->tick * (uint64_t)rate * UINT64_C(60000) / ((uint64_t)s->tempo * XTC_TICKS_PER_QUARTER);
}
p->point_count = s->point_count;
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;
@ -188,17 +212,51 @@ xtc_result xtc_plugin_create(const xtc_plugin_spec *s, double rate, size_t frame
fail:
xtc_plugin_destroy(p); return r;
}
xtc_result xtc_plugin_process(xtc_plugin *p, const float *input, float *output, size_t frames)
static void automate(xtc_plugin *p, uint64_t position)
{
unsigned long port;
for (port = 0; port < p->descriptor->PortCount; ++port) {
size_t i, before = SIZE_MAX, after = SIZE_MAX;
for (i = 0; i < p->point_count; ++i) if (p->points[i].port == port) {
if (p->samples[i] <= position) {
if (before == SIZE_MAX || p->points[i].tick > p->points[before].tick) before = i;
} else if (after == SIZE_MAX || p->points[i].tick < p->points[after].tick) after = i;
}
if (before == SIZE_MAX) { if (after != SIZE_MAX) p->controls[port] = p->points[after].value; }
else {
double value = p->points[before].value;
if (after != SIZE_MAX && !p->points[before].step)
value += (double)(position-p->samples[before]) / (double)(p->samples[after]-p->samples[before]) *
((double)p->points[after].value-p->points[before].value);
p->controls[port] = (float)value;
}
}
}
xtc_result xtc_plugin_process_at(xtc_plugin *p, const float *input, float *output, size_t frames, uint64_t position)
{
size_t i;
if (!p || !output || frames > p->frames) return XTC_ERR_INVALID_ARGUMENT;
if (!p || !output || frames > p->frames || position > UINT64_MAX-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;
if (!p->point_count) {
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]);
} else {
/* Buffers stay connected: run(1) reads/writes slot zero, one control
* evaluation per absolute sample. Exact aliasing is safe. */
for (i = 0; i < frames; ++i) {
automate(p,position+i);
p->input[0] = input ? xtc_dsp_finite(input[i]) : 0; p->output[0] = 0;
p->descriptor->run(p->handle,1);
output[i] = xtc_dsp_finite(p->output[0]);
}
}
p->next = position+frames; return XTC_OK;
}
xtc_result xtc_plugin_process(xtc_plugin *p, const float *input, float *output, size_t frames)
{ return xtc_plugin_process_at(p,input,output,frames,p ? p->next : 0); }
#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; }
@ -207,6 +265,8 @@ xtc_result xtc_plugin_probe(const char *p, unsigned int i, double r, xtc_plugin_
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_at(xtc_plugin *p, const float *i, float *o, size_t f, uint64_t pos)
{ (void)p; (void)i; (void)o; (void)f; (void)pos; return XTC_ERR_UNSUPPORTED; }
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