feat: add editable internal instruments, automation and versioned presets

This commit is contained in:
Johannes Findeisen 2026-11-05 23:47:54 +01:00
commit 159fa939a1
21 changed files with 295 additions and 52 deletions

View file

@ -11,7 +11,12 @@ static const char *controls[2][3] = {{"Time", "Feedback", "Mix"}, {"Decay", "Dam
static const float defaults[2][3] = {{350, .35f, .25f}, {.7f, .45f, .25f}};
static const float lows[2][3] = {{1,0,0},{0,0,0}};
static const float highs[2][3] = {{2000,.95f,1},{.95f,.95f,1}};
typedef struct { double phase, step; float level, velocity; unsigned int note, channel; int active, released, held; } voice;
static const char *synth_names[]={"Harmonic", "Attack", "Release", "Cutoff", "Bend range"};
static const float synth_low[]={0, .1f, 1, 20, 0};
static const float synth_high[]={1, 2000, 5000, 24000, 24};
static float synth_default(unsigned int id,unsigned int port)
{ static const float d[]={.3f,8,80,24000,2};return port==0 && id==1 ? 0 : d[port]; }
typedef struct { double phase, step; float level, velocity, filter; unsigned int note, channel; int active, released, held; } voice;
struct xtc_builtin {
xtc_plugin_spec spec;
double rate;
@ -22,6 +27,7 @@ struct xtc_builtin {
voice voices[32];
double frequencies[128];
int sustain[16];
float bend[16];
};
xtc_result xtc_builtin_probe(unsigned int index, xtc_plugin_spec *out)
{
@ -31,8 +37,8 @@ xtc_result xtc_builtin_probe(unsigned int index, xtc_plugin_spec *out)
strcpy(s.path,XTC_BUILTIN_PATH); strcpy(s.label,names[index]);
s.format = XTC_PLUGIN_BUILTIN; s.index = index; s.unique_id = 0x58544300U+index;
s.instrument = index < 2; s.inputs = index < 2 ? 0U : 1U; s.tempo = 120000;
s.control_count = index < 2 ? 0U : 3U;
for (i=0;i<s.control_count;++i) { s.ports[i]=i; s.values[i]=defaults[index-2][i]; }
s.control_count = index < 2 ? 5U : 3U;
for (i=0;i<s.control_count;++i) { s.ports[i]=i; s.values[i]=index<2 ? synth_default(index,i) : defaults[index-2][i]; }
*out=s; return XTC_OK;
}
xtc_result xtc_builtin_validate(const xtc_plugin_spec *s)
@ -40,11 +46,12 @@ xtc_result xtc_builtin_validate(const xtc_plugin_spec *s)
xtc_plugin_spec a; unsigned int i;
if (!s || xtc_builtin_probe(s->index,&a)!=XTC_OK || strcmp(s->path,a.path) || strcmp(s->label,a.label) ||
s->identifier[0] || s->unique_id!=a.unique_id || s->instrument!=a.instrument || s->inputs!=a.inputs ||
s->control_count!=a.control_count) return XTC_ERR_INVALID_ARGUMENT;
(s->control_count!=a.control_count && !(s->index<2 && !s->control_count))) return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<s->control_count;++i) if(s->ports[i]!=i || !isfinite(s->values[i]) ||
s->values[i]<lows[s->index-2][i] || s->values[i]>highs[s->index-2][i]) return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<s->point_count;++i) if(s->index<2 || s->points[i].port>=3 || !isfinite(s->points[i].value) ||
s->points[i].value<lows[s->index-2][s->points[i].port] || s->points[i].value>highs[s->index-2][s->points[i].port]) return XTC_ERR_INVALID_ARGUMENT;
s->values[i]<(s->index<2 ? synth_low[i] : lows[s->index-2][i]) || s->values[i]>(s->index<2 ? synth_high[i] : highs[s->index-2][i])) return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<s->point_count;++i) if(s->points[i].port>=s->control_count || !isfinite(s->points[i].value) ||
s->points[i].value<(s->index<2 ? synth_low[s->points[i].port] : lows[s->index-2][s->points[i].port]) ||
s->points[i].value>(s->index<2 ? synth_high[s->points[i].port] : highs[s->index-2][s->points[i].port])) return XTC_ERR_INVALID_ARGUMENT;
return XTC_OK;
}
xtc_result xtc_builtin_describe(const xtc_plugin_spec *s, xtc_plugin_details *out)
@ -55,8 +62,9 @@ xtc_result xtc_builtin_describe(const xtc_plugin_spec *s, xtc_plugin_details *ou
strcpy(out->maker,"xtc"); strcpy(out->copyright,"See xtc LICENSE"); out->hard_realtime=1;
for(i=0;i<s->control_count;++i) {
xtc_parameter_info *p=out->parameters+i; p->id=i;
strcpy(p->name,controls[s->index-2][i]); if(s->index==2 && i==0) strcpy(p->unit,"ms");
p->minimum=lows[s->index-2][i]; p->maximum=highs[s->index-2][i]; p->default_value=defaults[s->index-2][i];
strcpy(p->name,s->index<2 ? synth_names[i] : controls[s->index-2][i]);
if(s->index<2) {strcpy(p->unit,i==1 || i==2 ? "ms" : i==3 ? "Hz" : i==4 ? "st" : "");} if(s->index==2 && i==0) strcpy(p->unit,"ms");
p->minimum=s->index<2 ? synth_low[i] : lows[s->index-2][i]; p->maximum=s->index<2 ? synth_high[i] : highs[s->index-2][i]; p->default_value=s->index<2 ? synth_default(s->index,i) : defaults[s->index-2][i];
out->flags[i]=XTC_PLUGIN_BOUNDED_BELOW|XTC_PLUGIN_BOUNDED_ABOVE;
}
return XTC_OK;
@ -82,7 +90,7 @@ static float parameter(const xtc_builtin *p, unsigned int port, uint64_t positio
if((double)q->tick<=tick) { if(!a || q->tick>a->tick) a=q; }
else if(!b || q->tick<b->tick) b=q;
}
if(!a) return b ? b->value : p->spec.values[port];
if(!a) return b ? b->value : p->spec.control_count ? p->spec.values[port] : synth_default(p->spec.index,port);
if(!b || a->step) return a->value;
return (float)(a->value+(b->value-a->value)*(tick-(double)a->tick)/(double)(b->tick-a->tick));
}
@ -112,7 +120,8 @@ xtc_result xtc_builtin_process(xtc_builtin *p, const float *in, float *out, size
static void midi_event(xtc_builtin *p, const xtc_midi_event *e)
{
unsigned int j,slot=0;
if(e->type==XTC_MIDI_ALL_OFF) { memset(p->voices,0,sizeof(p->voices));memset(p->sustain,0,sizeof(p->sustain));return; }
if(e->type==XTC_MIDI_ALL_OFF) { for(j=0;j<32;++j)if(p->voices[j].channel==e->channel)p->voices[j].active=0;p->sustain[e->channel]=0;p->bend[e->channel]=0;return; }
if(e->type==XTC_MIDI_BEND) {p->bend[e->channel]=((float)(e->pitch+128*e->velocity)-8192)/8192;return;}
if(e->type==XTC_MIDI_CONTROL) {
if(e->pitch==64) {p->sustain[e->channel]=e->velocity>=64; if(e->velocity<64) for(j=0;j<32;++j) if(p->voices[j].channel==e->channel && p->voices[j].held) p->voices[j].released=1;}
if(e->pitch==120 || e->pitch==123) for(j=0;j<32;++j) if(p->voices[j].channel==e->channel) p->voices[j].active=0;
@ -127,23 +136,31 @@ static void midi_event(xtc_builtin *p, const xtc_midi_event *e)
for(j=0;j<32;++j) { if(!p->voices[j].active) {slot=j;break;} if(p->voices[j].level<p->voices[slot].level) slot=j; }
{voice *v=p->voices+slot; memset(v,0,sizeof(*v));v->active=1;v->note=e->pitch;v->channel=e->channel;v->velocity=(float)e->velocity/127;v->step=p->frequencies[e->pitch];}
}
xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames)
xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames, uint64_t position)
{
size_t i,k=0; unsigned int j;
if(!p || !p->spec.instrument || !out || frames>p->max_frames || count>256 || (count && !events)) return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<count;++i) if(events[i].offset>=frames || (i && events[i].offset<events[i-1].offset) ||
events[i].channel>15 || events[i].pitch>127 || events[i].velocity>127 || events[i].type<XTC_MIDI_NOTE_ON || events[i].type>XTC_MIDI_CONTROL) return XTC_ERR_INVALID_ARGUMENT;
events[i].channel>15 || events[i].pitch>127 || events[i].velocity>127 || events[i].type<XTC_MIDI_NOTE_ON || events[i].type>XTC_MIDI_BEND) return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<frames;++i) {
float sum=0; while(k<count && events[k].offset==i) midi_event(p,events+k++);
for(j=0;j<32;++j) { voice *v=p->voices+j; double wave;
float sum=0; float harmonic=parameter(p,0,position+i), attack=parameter(p,1,position+i), release=parameter(p,2,position+i);
float cutoff=parameter(p,3,position+i), range=parameter(p,4,position+i);
float attack_step=attack==8 ? p->attack : (float)(1-exp(-1/(attack*.001*p->rate)));
float release_step=release==80 ? p->release : (float)exp(-1/(release*.001*p->rate));
float filter_step=(float)(1-exp(-TAU*cutoff/p->rate));
while(k<count && events[k].offset==i) midi_event(p,events+k++);
for(j=0;j<32;++j) { voice *v=p->voices+j; double wave,step;
if(!v->active) continue;
if(v->released) v->level *= p->release;
else v->level += (1-v->level)*p->attack;
if(v->released) v->level *= release_step;
else v->level += (1-v->level)*attack_step;
if(v->released && v->level<.00001f) {v->active=0;continue;}
wave=v->step<.49 ? sin(TAU*v->phase) : 0;
if(p->spec.index==0 && v->step<.16) wave+=.3*sin(3*TAU*v->phase);
step=v->step*pow(2,p->bend[v->channel]*range/12);
wave=step<.49 ? sin(TAU*v->phase) : 0;
if(step<.16) wave+=(harmonic==.3f ? .3 : (double)harmonic)*sin(3*TAU*v->phase);
v->filter += filter_step*((float)wave-v->filter);
if(cutoff<24000) wave=v->filter;
sum+=(float)wave*v->level*v->velocity*.12f;
v->phase+=v->step; v->phase-=floor(v->phase);
v->phase+=step; v->phase-=floor(v->phase);
} out[i]=sum;
} return XTC_OK;
}

View file

@ -8,5 +8,5 @@ xtc_result xtc_builtin_describe(const xtc_plugin_spec *s, xtc_plugin_details *ou
xtc_result xtc_builtin_create(const xtc_plugin_spec *s, double rate, size_t frames, xtc_builtin **out);
void xtc_builtin_destroy(xtc_builtin *p);
xtc_result xtc_builtin_process(xtc_builtin *p, const float *in, float *out, size_t frames, uint64_t position);
xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames);
xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames, uint64_t position);
#endif

View file

@ -81,8 +81,16 @@ xtc_result xtc_plugin_process_at(xtc_plugin *p, const float *in, float *out, siz
if (r == XTC_OK) p->next = position <= UINT64_MAX-frames ? position+frames : 0;
return r;
}
xtc_result xtc_plugin_process_midi_at(xtc_plugin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames, uint64_t position)
{
xtc_result r;
if (!p || position>UINT64_MAX-frames) return XTC_ERR_INVALID_ARGUMENT;
r=p->builtin ? xtc_builtin_midi(p->builtin,events,count,out,frames,position) : p->modern ? xtc_modern_process_midi(p->modern,events,count,out,frames) : XTC_ERR_UNSUPPORTED;
if(r==XTC_OK)p->next=position+frames;
return r;
}
xtc_result xtc_plugin_process_midi(xtc_plugin *p, const xtc_midi_event *events, size_t count, float *out, size_t frames)
{ return p && p->builtin ? xtc_builtin_midi(p->builtin,events,count,out,frames) : p && p->modern ? xtc_modern_process_midi(p->modern,events,count,out,frames) : XTC_ERR_UNSUPPORTED; }
{ return xtc_plugin_process_midi_at(p,events,count,out,frames,p ? p->next : 0); }
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])
@ -114,3 +122,28 @@ xtc_result xtc_plugin_reset(xtc_plugin_spec *s, double rate, unsigned int port)
*s = candidate;
return XTC_OK;
}
xtc_result xtc_plugin_preset_save(const xtc_plugin_spec *spec, const char *path)
{
xtc_project *project=NULL;uint64_t track;xtc_result r;
if(!path || xtc_plugin_spec_validate(spec)!=XTC_OK || !spec->instrument)return XTC_ERR_INVALID_ARGUMENT;
r=xtc_project_create(&project);
if(r==XTC_OK)r=xtc_project_set_tempo(project,spec->tempo);
if(r==XTC_OK)r=xtc_project_add_track(project,"Instrument preset",&track);
if(r==XTC_OK)r=xtc_project_set_instrument(project,track,spec);
if(r==XTC_OK)r=xtc_project_save(project,path);
xtc_project_destroy(project);return r;
}
xtc_result xtc_plugin_preset_load(const char *path, xtc_plugin_spec *out)
{
xtc_project *project=NULL;xtc_track_info track;xtc_plugin_spec spec;size_t tracks,clips;xtc_result r;
if(!path || !out)return XTC_ERR_INVALID_ARGUMENT;
r=xtc_project_create(&project);
if(r==XTC_OK)r=xtc_project_load(project,path);
if(r==XTC_OK)r=xtc_project_get_counts(project,&tracks,&clips);
if(r==XTC_OK && (tracks!=1 || clips))r=XTC_ERR_FORMAT;
if(r==XTC_OK)r=xtc_project_get_track(project,0,&track);
if(r==XTC_OK)r=xtc_project_get_instrument(project,track.id,&spec);
if(r==XTC_OK)*out=spec;
xtc_project_destroy(project);return r;
}

View file

@ -391,13 +391,13 @@ xtc_result xtc_modern_process_midi(xtc_modern *p, const xtc_midi_event *events,
for (i = 0; i < count; ++i) {
const xtc_midi_event *e = events+i;
if (e->offset >= frames || (i && e->offset < events[i-1].offset) || e->channel > 15 || e->pitch > 127 || e->velocity > 127 ||
e->type < XTC_MIDI_NOTE_ON || e->type > XTC_MIDI_CONTROL || (e->type == XTC_MIDI_NOTE_ON && !e->velocity)) return XTC_ERR_INVALID_ARGUMENT;
e->type < XTC_MIDI_NOTE_ON || e->type > XTC_MIDI_BEND || (e->type == XTC_MIDI_NOTE_ON && !e->velocity)) return XTC_ERR_INVALID_ARGUMENT;
}
p->event_count = count; p->vst_count = 0; p->clap_count = 0;
p->event_list.api.lpVtbl = &event_vtable; p->event_list.owner = p;
for (i = 0; i < count; ++i) {
const xtc_midi_event *e = events+i;
unsigned char status = (unsigned char)((e->type == XTC_MIDI_NOTE_ON ? 144 : e->type == XTC_MIDI_NOTE_OFF ? 128 : 176) | e->channel);
unsigned char status = (unsigned char)((e->type == XTC_MIDI_NOTE_ON ? 144 : e->type == XTC_MIDI_NOTE_OFF ? 128 : e->type == XTC_MIDI_BEND ? 224 : 176) | e->channel);
unsigned char key = (unsigned char)(e->type == XTC_MIDI_ALL_OFF ? 120 : e->pitch);
unsigned char value = (unsigned char)(e->type == XTC_MIDI_ALL_OFF ? 0 : e->velocity);
clap_note *c = p->clap_events+p->clap_count; memset(c,0,sizeof(*c));
@ -410,9 +410,9 @@ xtc_result xtc_modern_process_midi(xtc_modern *p, const xtc_midi_event *events,
c->note.note_id = -1; c->note.port_index = 0; c->note.channel = (int16_t)e->channel;
c->note.key = e->type == XTC_MIDI_ALL_OFF ? -1 : (int16_t)e->pitch; c->note.velocity = (double)e->velocity/127;
/* No generic controller mapping in native-note CLAP or VST3. */
if (e->type == XTC_MIDI_CONTROL) c->note.header.type = UINT16_MAX;
if (e->type >= XTC_MIDI_CONTROL) c->note.header.type = UINT16_MAX;
}
if (p->clap_midi || e->type != XTC_MIDI_CONTROL) ++p->clap_count;
if (p->clap_midi || e->type < XTC_MIDI_CONTROL) ++p->clap_count;
memset(p->v2_events+i,0,sizeof(*p->v2_events));
p->v2_events[i].type = 1; p->v2_events[i].bytes = sizeof(xtc_v2_midi); p->v2_events[i].offset = (int32_t)e->offset;
p->v2_events[i].data[0] = status; p->v2_events[i].data[1] = key; p->v2_events[i].data[2] = value;