From 85e2c1ef14415a4ca5a28d28038b4b57a3ffabab Mon Sep 17 00:00:00 2001 From: Stefano D'Angelo Date: Sat, 13 Jan 2024 14:04:09 +0100 Subject: [PATCH] vst3 mem --- templates/vst3/src/vst3.c | 19 ++++++++++++++++--- test/plugin.h | 20 +++++++++----------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/templates/vst3/src/vst3.c b/templates/vst3/src/vst3.c index 7f4d1f6..69c6535 100644 --- a/templates/vst3/src/vst3.c +++ b/templates/vst3/src/vst3.c @@ -26,8 +26,6 @@ static double clamp(double x, double m, double M) { return x < m ? m : (x > M ? M : x); } -static double parameterUnmapLinear(Steinberg_Vst_ParamID id, double v) { -} static double parameterMap(Steinberg_Vst_ParamID id, double v) { return parameterData[id].flags & DATA_PARAM_MAP_LOG ? parameterData[id].min * exp(parameterData[id].mapK * v) : parameterData[id].min + (parameterData[id].max - parameterData[id].min) * v; } @@ -59,6 +57,7 @@ typedef struct pluginInstance { #if DATA_PRODUCT_CHANNELS_AUDIO_OUTPUT_N > 0 float * outputs[DATA_PRODUCT_CHANNELS_AUDIO_INPUT_N]; #endif + void * mem; } pluginInstance; static Steinberg_Vst_IComponentVtbl pluginVtblIComponent; @@ -132,6 +131,7 @@ static Steinberg_tresult pluginInitialize(void *thisInterface, struct Steinberg_ plugin_set_parameter(&p->p, parameterData[i].index, parameterData[i].def); } #endif + p->mem = NULL; return Steinberg_kResultOk; } @@ -140,6 +140,8 @@ static Steinberg_tresult pluginTerminate(void *thisInterface) { pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); p->context = NULL; plugin_fini(&p->p); + if (p->mem) + free(p->mem); return Steinberg_kResultOk; } @@ -257,8 +259,19 @@ static Steinberg_tresult pluginActivateBus(void* thisInterface, Steinberg_Vst_Me static Steinberg_tresult pluginSetActive(void* thisInterface, Steinberg_TBool state) { TRACE("plugin set active\n"); pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); + if (p->mem != NULL) { + free(p->mem); + p->mem = NULL; + } if (state) { plugin_set_sample_rate(&p->p, p->sampleRate); + size_t req = plugin_mem_req(&p->p); + if (req != 0) { + p->mem = malloc(req); + if (p->mem == NULL) + return Steinberg_kOutOfMemory; + plugin_mem_set(&p->p, p->mem); + } plugin_reset(&p->p); } return Steinberg_kResultOk; @@ -269,7 +282,7 @@ static Steinberg_tresult pluginSetActive(void* thisInterface, Steinberg_TBool st // https://stackoverflow.com/questions/2182002/how-to-convert-big-endian-to-little-endian-in-c-without-using-library-functions #define SWAP_UINT32(x) (((x) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | ((x) << 24)) -static Steinberg_tresult pluginSetState(void* thisInterface, struct Steinberg_IBStream* state) { +static Steinberg_tresult pluginSetState(void* thisInterface, struct Steinberg_IBStream* state) { TRACE("plugin set state\n"); if (state == NULL) return Steinberg_kResultFalse; diff --git a/test/plugin.h b/test/plugin.h index 7dd07dc..b9176d2 100644 --- a/test/plugin.h +++ b/test/plugin.h @@ -2,20 +2,18 @@ #include typedef struct plugin { - float gain; - char bypass; - size_t delay; - float sample_rate; size_t delay_line_length; + float gain; + float delay; + char bypass; + float * delay_line; size_t delay_line_cur; } plugin; static void plugin_init(plugin *instance) { - instance->gain = 1.f; - instance->bypass = 0; } static void plugin_fini(plugin *instance) { @@ -42,10 +40,10 @@ static void plugin_reset(plugin *instance) { static void plugin_set_parameter(plugin *instance, size_t index, float value) { switch (index) { case 0: - instance->gain = value; + instance->gain = powf(10.f, 0.05f * value); break; case 1: - instance->delay = roundf(instance->sample_rate * 0.001f * value); + instance->delay = 0.001f * value; break; case 2: instance->bypass = value >= 0.5f; @@ -63,13 +61,13 @@ static size_t calc_index(size_t cur, size_t delay, size_t len) { } static void plugin_process(plugin *instance, const float **inputs, float **outputs, size_t n_samples) { - const float g = powf(10.f, 0.05f * instance->gain); + size_t delay = roundf(instance->sample_rate * instance->delay); for (size_t i = 0; i < n_samples; i++) { instance->delay_line[instance->delay_line_cur] = inputs[0][i]; - const float y = g * instance->delay_line[calc_index(instance->delay_line_cur, instance->delay, instance->delay_line_length)]; + const float y = instance->delay_line[calc_index(instance->delay_line_cur, delay, instance->delay_line_length)]; instance->delay_line_cur++; if (instance->delay_line_cur == instance->delay_line_length) instance->delay_line_cur = 0; - outputs[0][i] = instance->bypass ? inputs[0][i] : y; + outputs[0][i] = instance->bypass ? inputs[0][i] : instance->gain * y; } }