feat: add a bounded WAV sampler with looping, relinking and saved presets
This commit is contained in:
parent
159fa939a1
commit
35bf6a00a1
24 changed files with 213 additions and 32 deletions
|
|
@ -1,16 +1,19 @@
|
|||
/* Original, dependency-free mono devices. All storage is prepared off-thread. */
|
||||
#include "builtin.h"
|
||||
#include "../audio/wave_internal.h"
|
||||
#include "xtc/xtc_midi.h"
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#define TAU 6.2831853071795864769
|
||||
static const char *names[] = {"Soft Keys", "Sub Bass", "Delay", "Reverb"};
|
||||
static const char *names[] = {"Soft Keys", "Sub Bass", "Delay", "Reverb", "Sample Player"};
|
||||
static const char *controls[2][3] = {{"Time", "Feedback", "Mix"}, {"Decay", "Damping", "Mix"}};
|
||||
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}};
|
||||
static const char *sample_names[]={"Root key", "Loop", "Attack", "Release", "Gain"};
|
||||
static const float sample_low[]={0,0,.1f,1,0},sample_high[]={127,1,2000,5000,4},sample_defaults[]={60,0,8,80,1};
|
||||
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};
|
||||
|
|
@ -28,17 +31,19 @@ struct xtc_builtin {
|
|||
double frequencies[128];
|
||||
int sustain[16];
|
||||
float bend[16];
|
||||
xtc_wave sample;
|
||||
size_t sample_issue;
|
||||
};
|
||||
xtc_result xtc_builtin_probe(unsigned int index, xtc_plugin_spec *out)
|
||||
{
|
||||
unsigned int i; xtc_plugin_spec s = {0};
|
||||
if (!out) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (index >= 4) return XTC_ERR_NOT_FOUND;
|
||||
if (index >= 5) return XTC_ERR_NOT_FOUND;
|
||||
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 ? 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]; }
|
||||
s.instrument = index < 2 || index==4; s.inputs = s.instrument ? 0U : 1U; s.tempo = 120000;
|
||||
s.control_count = s.instrument ? 5U : 3U;
|
||||
for (i=0;i<s.control_count;++i) { s.ports[i]=i; s.values[i]=index==4 ? sample_defaults[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)
|
||||
|
|
@ -48,10 +53,12 @@ xtc_result xtc_builtin_validate(const xtc_plugin_spec *s)
|
|||
s->identifier[0] || s->unique_id!=a.unique_id || s->instrument!=a.instrument || s->inputs!=a.inputs ||
|
||||
(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]<(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;
|
||||
s->values[i]<(s->index==4 ? sample_low[i] : s->index<2 ? synth_low[i] : lows[s->index-2][i]) || s->values[i]>(s->index==4 ? sample_high[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;
|
||||
s->points[i].value<(s->index==4 ? sample_low[s->points[i].port] : s->index<2 ? synth_low[s->points[i].port] : lows[s->index-2][s->points[i].port]) ||
|
||||
s->points[i].value>(s->index==4 ? sample_high[s->points[i].port] : s->index<2 ? synth_high[s->points[i].port] : highs[s->index-2][s->points[i].port])) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if(s->index==4) {if(s->values[0]!=floorf(s->values[0]) || s->values[1]!=floorf(s->values[1]))return XTC_ERR_INVALID_ARGUMENT;
|
||||
for(i=0;i<s->point_count;++i)if(s->points[i].port<2 && (!s->points[i].step || s->points[i].value!=floorf(s->points[i].value)))return XTC_ERR_INVALID_ARGUMENT;}
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_builtin_describe(const xtc_plugin_spec *s, xtc_plugin_details *out)
|
||||
|
|
@ -62,10 +69,12 @@ 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,s->index<2 ? synth_names[i] : controls[s->index-2][i]);
|
||||
strcpy(p->name,s->index==4 ? sample_names[i] : 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];
|
||||
p->minimum=s->index==4 ? sample_low[i] : s->index<2 ? synth_low[i] : lows[s->index-2][i]; p->maximum=s->index==4 ? sample_high[i] : s->index<2 ? synth_high[i] : highs[s->index-2][i]; p->default_value=s->index==4 ? sample_defaults[i] : s->index<2 ? synth_default(s->index,i) : defaults[s->index-2][i];
|
||||
out->flags[i]=XTC_PLUGIN_BOUNDED_BELOW|XTC_PLUGIN_BOUNDED_ABOVE;
|
||||
if(s->index==4 && i<2)out->flags[i]|=XTC_PLUGIN_INTEGER;
|
||||
if(s->index==4 && (i==2 || i==3))strcpy(p->unit,"ms");
|
||||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
@ -78,9 +87,16 @@ xtc_result xtc_builtin_create(const xtc_plugin_spec *s, double rate, size_t fram
|
|||
if(!s->instrument) { p->line=calloc(p->length*4,sizeof(float)); if(!p->line) {free(p);return XTC_ERR_OUT_OF_MEMORY;} }
|
||||
for(i=0;i<128;++i) p->frequencies[i]=440*pow(2,((double)i-69)/12)/rate;
|
||||
for(i=0;i<4;++i) { static const double times[]={.0297,.0371,.0411,.0437}; p->comb[i]=(size_t)(rate*times[i])+1; }
|
||||
if(s->index==4) {
|
||||
xtc_result r=s->sample_path[0] ? xtc_wave_load(s->sample_path,&p->sample) : XTC_ERR_NOT_FOUND;
|
||||
if(r==XTC_ERR_OUT_OF_MEMORY || r==XTC_ERR_CAPACITY) {xtc_builtin_destroy(p);return r;}
|
||||
p->sample_issue=r!=XTC_OK;
|
||||
}
|
||||
*out=p; return XTC_OK;
|
||||
}
|
||||
void xtc_builtin_destroy(xtc_builtin *p) { if(p) {free(p->line);free(p);} }
|
||||
size_t xtc_builtin_sample_frames(const xtc_builtin *p) {return p ? p->sample.frames : 0;}
|
||||
size_t xtc_builtin_asset_issues(const xtc_builtin *p) {return p ? p->sample_issue : 0;}
|
||||
void xtc_builtin_destroy(xtc_builtin *p) { if(p) {free(p->sample.samples);free(p->line);free(p);} }
|
||||
static float parameter(const xtc_builtin *p, unsigned int port, uint64_t position)
|
||||
{
|
||||
const xtc_plugin_point *a=NULL,*b=NULL; unsigned int i;
|
||||
|
|
@ -117,6 +133,19 @@ xtc_result xtc_builtin_process(xtc_builtin *p, const float *in, float *out, size
|
|||
}
|
||||
return XTC_OK;
|
||||
}
|
||||
static float sample_voice(xtc_builtin *p,voice *v,uint64_t position)
|
||||
{
|
||||
size_t a,b;float value;double advance;
|
||||
if(!p->sample.frames)return 0;
|
||||
if(v->phase>=(double)p->sample.frames) {v->active=0;return 0;}
|
||||
a=(size_t)v->phase;b=a+1;
|
||||
if(b>=p->sample.frames)b=parameter(p,1,position)>=.5f ? 0 : a;
|
||||
value=p->sample.samples[a]+(p->sample.samples[b]-p->sample.samples[a])*(float)(v->phase-(double)a);
|
||||
advance=(double)p->sample.rate/p->rate*pow(2,((double)v->note-parameter(p,0,position)+p->bend[v->channel]*2)/12);
|
||||
v->phase+=advance;
|
||||
if(parameter(p,1,position)>=.5f)v->phase=fmod(v->phase,(double)p->sample.frames);
|
||||
return value*v->level*v->velocity*parameter(p,4,position);
|
||||
}
|
||||
static void midi_event(xtc_builtin *p, const xtc_midi_event *e)
|
||||
{
|
||||
unsigned int j,slot=0;
|
||||
|
|
@ -145,6 +174,7 @@ xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t
|
|||
for(i=0;i<frames;++i) {
|
||||
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);
|
||||
if(p->spec.index==4) {attack=parameter(p,2,position+i);release=parameter(p,3,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));
|
||||
|
|
@ -154,6 +184,7 @@ xtc_result xtc_builtin_midi(xtc_builtin *p, const xtc_midi_event *events, size_t
|
|||
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;}
|
||||
if(p->spec.index==4) {sum+=sample_voice(p,v,position+i);continue;}
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ xtc_result xtc_builtin_probe(unsigned int index, xtc_plugin_spec *out);
|
|||
xtc_result xtc_builtin_validate(const xtc_plugin_spec *s);
|
||||
xtc_result xtc_builtin_describe(const xtc_plugin_spec *s, xtc_plugin_details *out);
|
||||
xtc_result xtc_builtin_create(const xtc_plugin_spec *s, double rate, size_t frames, xtc_builtin **out);
|
||||
size_t xtc_builtin_sample_frames(const xtc_builtin *p);
|
||||
size_t xtc_builtin_asset_issues(const xtc_builtin *p);
|
||||
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, uint64_t position);
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ xtc_result xtc_plugin_describe(const xtc_plugin_spec *s, double rate, xtc_plugin
|
|||
snprintf(out->copyright,sizeof(out->copyright),"See plugin license");
|
||||
return XTC_OK;
|
||||
}
|
||||
size_t xtc_plugin_sample_frames(const xtc_plugin *p) {return p ? xtc_builtin_sample_frames(p->builtin) : 0;}
|
||||
size_t xtc_plugin_asset_issues(const xtc_plugin *p) {return p ? xtc_builtin_asset_issues(p->builtin) : 0;}
|
||||
void xtc_plugin_destroy(xtc_plugin *p)
|
||||
{ if (p) { xtc_builtin_destroy(p->builtin); 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)
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ xtc_result xtc_plugin_spec_validate(const xtc_plugin_spec *s)
|
|||
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_BUILTIN ||
|
||||
!terminated(s->identifier,sizeof(s->identifier))) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (!terminated(s->sample_path,sizeof(s->sample_path)) || (s->sample_path[0] && (s->sample_path[0]!='/' || s->format!=XTC_PLUGIN_BUILTIN || s->index!=4)))return XTC_ERR_INVALID_ARGUMENT;
|
||||
if (s->format == XTC_PLUGIN_BUILTIN) {
|
||||
if (xtc_builtin_validate(s) != XTC_OK) return XTC_ERR_INVALID_ARGUMENT;
|
||||
} else if (s->format != XTC_PLUGIN_LADSPA) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue