diff --git a/templates/api/src/plugin_api.h b/templates/api/src/plugin_api.h index a3946e9..a1ce7a5 100644 --- a/templates/api/src/plugin_api.h +++ b/templates/api/src/plugin_api.h @@ -1,7 +1,7 @@ /* * Tibia * - * Copyright (C) 2024 Orastron Srl unipersonale + * Copyright (C) 2024, 2025 Orastron Srl unipersonale * * Tibia is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,6 +26,8 @@ typedef struct { const char * format; const char * (*get_bindir)(void *handle); const char * (*get_datadir)(void *handle); + int (*write_state)(void *handle, const char *data, size_t length); + void (*load_parameter)(void *handle, size_t index, float value); } plugin_callbacks; typedef struct { diff --git a/templates/vst3/src/data.h b/templates/vst3/src/data.h index 9fb5ebc..8618ce8 100644 --- a/templates/vst3/src/data.h +++ b/templates/vst3/src/data.h @@ -1,7 +1,7 @@ /* * Tibia * - * Copyright (C) 2023, 2024 Orastron Srl unipersonale + * Copyright (C) 2023-2025 Orastron Srl unipersonale * * Tibia is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -123,6 +123,7 @@ static uint32_t midiInIndex[DATA_PRODUCT_BUSES_MIDI_INPUT_N] = { #endif #define DATA_PRODUCT_PARAMETERS_N {{=it.product.parameters.filter(x => !x.isLatency).length}} +#define DATA_PRODUCT_PARAMETERS_IN_N {{=it.product.parameters.filter(x => x.direction == "input").length}} #if DATA_PRODUCT_PARAMETERS_N + DATA_PRODUCT_BUSES_MIDI_INPUT_N > 0 static struct Steinberg_Vst_ParameterInfo parameterInfo[DATA_PRODUCT_PARAMETERS_N + 3 * DATA_PRODUCT_BUSES_MIDI_INPUT_N] = { @@ -223,3 +224,7 @@ static struct { #define DATA_UI #define DATA_UI_USER_RESIZABLE {{=it.product.ui.userResizable ? 1 : 0}} {{?}} + +{{?it.product.state && it.product.state.dspCustom}} +#define STATE_DSP_CUSTOM +{{?}} diff --git a/templates/vst3/src/vst3.c b/templates/vst3/src/vst3.c index 5e3466c..220dd16 100644 --- a/templates/vst3/src/vst3.c +++ b/templates/vst3/src/vst3.c @@ -1,7 +1,7 @@ /* * Tibia * - * Copyright (C) 2023, 2024 Orastron Srl unipersonale + * Copyright (C) 2023-2025 Orastron Srl unipersonale * * Tibia is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -146,12 +146,12 @@ static char *x_asprintf(const char * restrict format, ...) { static char *bindir; static char *datadir; -static const char * get_bindir_cb(void *handle) { +static const char * getBindirCb(void *handle) { (void)handle; return bindir; } -static const char * get_datadir_cb(void *handle) { +static const char * getDatadirCb(void *handle) { (void)handle; return datadir; } @@ -215,11 +215,43 @@ typedef struct pluginInstance { char midiOutputsActive[DATA_PRODUCT_BUSES_MIDI_OUTPUT_N]; #endif void * mem; +#ifdef STATE_DSP_CUSTOM + struct Steinberg_IBStream * state; +#endif } pluginInstance; static Steinberg_Vst_IComponentVtbl pluginVtblIComponent; static Steinberg_Vst_IAudioProcessorVtbl pluginVtblIAudioProcessor; +#ifdef STATE_DSP_CUSTOM +static int pluginWriteStateCb(void *handle, const char *data, size_t length) { + pluginInstance *p = (pluginInstance *)handle; + size_t written = 0; + while (written < length) { + Steinberg_int32 w = (length - written) <= 0x7fffffff ? length - written : 0x7fffffff; + Steinberg_int32 n; + p->state->lpVtbl->write(p->state, (void *)data, length, &n); + if (n != w) + return -1; + written += n; + } + return 0; +} + +# if DATA_PRODUCT_PARAMETERS_IN_N > 0 +static void pluginLoadParameterCb(void *handle, size_t index, float value) { + size_t i = index; +# ifdef DATA_PARAM_LATENCY_INDEX + if (i >= DATA_PARAM_LATENCY_INDEX) + i--; +# endif + pluginInstance *p = (pluginInstance *)handle; + p->parameters[i] = parameterAdjust(i, value); + plugin_set_parameter(&p->p, index, p->parameters[i]); +} +# endif +#endif + static Steinberg_tresult pluginQueryInterface(pluginInstance *p, const Steinberg_TUID iid, void ** obj) { // This seems to violate the way multiple inheritance should work in COM, but hosts like it, so what do I know... size_t offset; @@ -285,8 +317,19 @@ static Steinberg_tresult pluginInitialize(void *thisInterface, struct Steinberg_ plugin_callbacks cbs = { /* .handle = */ (void *)p, /* .format = */ "vst3", - /* .get_bindir = */ get_bindir_cb, - /* .get_datadir = */ get_datadir_cb + /* .get_bindir = */ getBindirCb, + /* .get_datadir = */ getDatadirCb, +#ifdef STATE_DSP_CUSTOM + /* .write_state = */ pluginWriteStateCb, +# if DATA_PRODUCT_PARAMETERS_IN_N > 0 + /* .load_parameter = */ pluginLoadParameterCb +# else + /* .load_parameter = */ NULL +# endif +#else + /* .write_state = */ NULL, + /* .load_parameter = */ NULL +#endif }; plugin_init(&p->p, &cbs); #if DATA_PRODUCT_PARAMETERS_N > 0 @@ -511,7 +554,33 @@ static Steinberg_tresult pluginSetState(void* thisInterface, struct Steinberg_IB TRACE("plugin set state\n"); if (state == NULL) return Steinberg_kResultFalse; -#if DATA_PRODUCT_PARAMETERS_N > 0 +#ifdef STATE_DSP_CUSTOM + pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); + if (state->lpVtbl->seek(state, 0, Steinberg_IBStream_IStreamSeekMode_kIBSeekEnd, NULL) != Steinberg_kResultOk) + return Steinberg_kResultFalse; + Steinberg_int64 length; + if (state->lpVtbl->tell(state, &length) != Steinberg_kResultOk) + return Steinberg_kResultFalse; + if (state->lpVtbl->seek(state, 0, Steinberg_IBStream_IStreamSeekMode_kIBSeekSet, NULL) != Steinberg_kResultOk) + return Steinberg_kResultFalse; + char *data = malloc(length); + if (data == NULL) + return Steinberg_kResultFalse; + Steinberg_int64 read = 0; + while (read < length) { + Steinberg_int32 r = (length - read) <= 0x7fffffff ? length - read : 0x7fffffff; + Steinberg_int32 n; + state->lpVtbl->read(state, data + read, length, &n); + if (n != r) { + free(data); + return Steinberg_kResultFalse; + } + read += n; + } + plugin_state_load(&p->p, data, length); + free(data); +#else +# if DATA_PRODUCT_PARAMETERS_N > 0 pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); for (size_t i = 0; i < DATA_PRODUCT_PARAMETERS_N; i++) { if (parameterInfo[i].flags & Steinberg_Vst_ParameterInfo_ParameterFlags_kIsReadOnly) @@ -526,6 +595,7 @@ static Steinberg_tresult pluginSetState(void* thisInterface, struct Steinberg_IB p->parameters[i] = parameterAdjust(i, v.f); plugin_set_parameter(&p->p, parameterData[i].index, p->parameters[i]); } +# endif #endif return Steinberg_kResultOk; } @@ -534,7 +604,13 @@ static Steinberg_tresult pluginGetState(void* thisInterface, struct Steinberg_IB TRACE("plugin get state\n"); if (state == NULL) return Steinberg_kResultFalse; -#if DATA_PRODUCT_PARAMETERS_N > 0 +#ifdef STATE_DSP_CUSTOM + pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); + p->state = state; + if (plugin_state_save(&p->p) != 0) + return Steinberg_kResultFalse; +#else +# if DATA_PRODUCT_PARAMETERS_N > 0 pluginInstance *p = (pluginInstance *)((char *)thisInterface - offsetof(pluginInstance, vtblIComponent)); for (size_t i = 0; i < DATA_PRODUCT_PARAMETERS_N; i++) { if (parameterInfo[i].flags & Steinberg_Vst_ParameterInfo_ParameterFlags_kIsReadOnly) @@ -548,6 +624,7 @@ static Steinberg_tresult pluginGetState(void* thisInterface, struct Steinberg_IB if (n != 4) return Steinberg_kResultFalse; } +# endif #endif return Steinberg_kResultOk; } @@ -1172,8 +1249,8 @@ static Steinberg_tresult plugViewAttached(void* thisInterface, void* parent, Ste plugin_ui_callbacks cbs = { /* .handle = */ (void *)v, /* .format = */ "vst3", - /* .get_bindir = */ get_bindir_cb, - /* .get_datadir = */ get_datadir_cb, + /* .get_bindir = */ getBindirCb, + /* .get_datadir = */ getDatadirCb, # if DATA_PRODUCT_PARAMETERS_N > 0 /* .set_parameter_begin = */ plugViewSetParameterBeginCb, /* .set_parameter = */ plugViewSetParameterCb, @@ -1737,9 +1814,10 @@ static Steinberg_tresult controllerSetParamNormalized(void* thisInterface, Stein controller *c = (controller *)((char *)thisInterface - offsetof(controller, vtblIEditController)); c->parameters[pi] = pi >= DATA_PRODUCT_PARAMETERS_N ? value : parameterAdjust(pi, parameterMap(pi, value)); # ifdef DATA_UI - for (size_t i = 0; i < c->viewsCount; i++) - if(c->views[i]) - plugViewUpdateParameter(c->views[i], pi); + if (pi < DATA_PRODUCT_PARAMETERS_N) + for (size_t i = 0; i < c->viewsCount; i++) + if(c->views[i]) + plugViewUpdateParameter(c->views[i], pi); # endif return Steinberg_kResultTrue; #else diff --git a/test/plugin.h b/test/plugin.h index 56adce4..26f88cc 100644 --- a/test/plugin.h +++ b/test/plugin.h @@ -1,7 +1,7 @@ /* * Tibia * - * Copyright (C) 2023, 2024 Orastron Srl unipersonale + * Copyright (C) 2023-2025 Orastron Srl unipersonale * * Tibia is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,25 +18,28 @@ * File author: Stefano D'Angelo */ +#include + typedef struct plugin { - float sample_rate; - size_t delay_line_length; + plugin_callbacks cbs; - float gain; - float delay; - float cutoff; - char bypass; + float sample_rate; + size_t delay_line_length; - float * delay_line; - size_t delay_line_cur; - float z1; - float cutoff_k; - float yz1; + float gain; + float delay; + float cutoff; + char bypass; + + float * delay_line; + size_t delay_line_cur; + float z1; + float cutoff_k; + float yz1; } plugin; static void plugin_init(plugin *instance, plugin_callbacks *cbs) { - (void)instance; - (void)cbs; + instance->cbs = *cbs; } static void plugin_fini(plugin *instance) { @@ -73,7 +76,7 @@ static void plugin_set_parameter(plugin *instance, size_t index, float value) { instance->gain = ((2.6039890429412597e-4f * value + 0.032131027163547855f) * value + 1.f) / ((0.0012705124328080768f * value - 0.0666763481312185f) * value + 1.f); break; case plugin_parameter_delay: - instance->delay = 0.001f * value; + instance->delay = value; break; case plugin_parameter_cutoff: instance->cutoff = value; @@ -94,8 +97,10 @@ 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) { - //approx size_t delay = roundf(instance->sample_rate * instance->delay); - size_t delay = (size_t)(instance->sample_rate * instance->delay + 0.5f); + //approx const float gain = powf(10.f, 0.05f * instance->gain); + const float gain = ((2.6039890429412597e-4f * instance->gain + 0.032131027163547855f) * instance->gain + 1.f) / ((0.0012705124328080768f * instance->gain - 0.0666763481312185f) * instance->gain + 1.f); + //approx const size_t delay = roundf(instance->sample_rate * 0.001f * instance->delay); + const size_t delay = (size_t)(instance->sample_rate * 0.001f * instance->delay + 0.5f); const float mA1 = instance->sample_rate / (instance->sample_rate + 6.283185307179586f * instance->cutoff * instance->cutoff_k); for (size_t i = 0; i < n_samples; i++) { instance->delay_line[instance->delay_line_cur] = inputs[0][i]; @@ -105,7 +110,7 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu instance->delay_line_cur = 0; const float y = x + mA1 * (instance->z1 - x); instance->z1 = y; - outputs[0][i] = instance->bypass ? inputs[0][i] : instance->gain * y; + outputs[0][i] = instance->bypass ? inputs[0][i] : gain * y; instance->yz1 = outputs[0][i]; } } @@ -116,3 +121,40 @@ static void plugin_midi_msg_in(plugin *instance, size_t index, const uint8_t * d //approx instance->cutoff_k = powf(2.f, (1.f / 12.f) * (note - 60)); instance->cutoff_k = data[1] < 64 ? (-0.19558034980097166f * data[1] - 2.361735109225749f) / (data[1] - 75.57552349522389f) : (393.95397927344214f - 7.660826245588588f * data[1]) / (data[1] - 139.0755234952239f); } + +static void serialize_float(char *dest, float f) { + union { float f; uint32_t u; } v; + v.f = f; + dest[0] = v.u & 0xff; + dest[1] = (v.u & 0xff00) >> 8; + dest[2] = (v.u & 0xff0000) >> 16; + dest[3] = (v.u & 0xff000000) >> 24; +} + +static float parse_float(const char *data) { + union { float f; uint32_t u; } v; + v.u = 0; + v.u |= data[0]; + v.u |= (data[1] << 8); + v.u |= (data[2] << 16); + v.u |= (data[3] << 24); + return v.f; +} + +static int plugin_state_save(plugin *instance) { + char data[13]; + serialize_float(data, instance->gain); + serialize_float(data + 4, instance->delay); + serialize_float(data + 8, instance->cutoff); + data[12] = instance->bypass ? 1 : 0; + return instance->cbs.write_state(instance->cbs.handle, data, 13); +} + +static void plugin_state_load(plugin *instance, const char *data, size_t length) { + (void)length; + + instance->cbs.load_parameter(instance->cbs.handle, plugin_parameter_gain, parse_float(data)); + instance->cbs.load_parameter(instance->cbs.handle, plugin_parameter_delay, parse_float(data + 4)); + instance->cbs.load_parameter(instance->cbs.handle, plugin_parameter_cutoff, parse_float(data + 8)); + instance->cbs.load_parameter(instance->cbs.handle, plugin_parameter_bypass, data[12] ? 1.f : 0.f); +} diff --git a/test/product.json b/test/product.json index 0e1f1c7..7b9e221 100644 --- a/test/product.json +++ b/test/product.json @@ -137,6 +137,9 @@ "ui": { "userResizable": true, "selfResizable": false + }, + "state": { + "dspCustom": true } } }