From e6c66ed72caee3037f32ed71d75530dd2362b8f1 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Mon, 30 Sep 2024 09:25:24 +0200 Subject: [PATCH] refactor: do param bounds checking in C code Some more code re-arranging Signed-off-by: Christopher Arndt --- examples/faustlpf_plugin.nim | 20 ++++++++------------ examples/lpf.h | 17 ++++++----------- examples/minarch.h | 6 ------ 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/examples/faustlpf_plugin.nim b/examples/faustlpf_plugin.nim index 657ef77..4f82f98 100644 --- a/examples/faustlpf_plugin.nim +++ b/examples/faustlpf_plugin.nim @@ -2,24 +2,22 @@ import nymph -{.emit: "#include \"lpf.h\"".} +{.emit: """#include "lpf.h"""".} const PluginUri = "urn:nymph:examples:faustlpf" - minFreq = 16.0 - maxFreq = 15_000.0 type - SampleBuffer = UncheckedArray[cfloat] - faustlpf {.importc, header: "lpf.h".} = object - # struct field, which represents the value of the - # FAUST UI element, which controls the cutoff + # struct field representing the value of the FAUST UI element, + # which controls the filter cutoff frequency fHslider0: cfloat PluginPort {.pure.} = enum Input, Output, Frequency + SampleBuffer = UncheckedArray[cfloat] + FaustLPFPlugin = object input: ptr SampleBuffer output: ptr SampleBuffer @@ -34,6 +32,8 @@ proc initfaustlpf(dsp: ptr faustlpf, sample_rate: cint) {.importc.} proc instanceClearfaustlpf(dsp: ptr faustlpf) {.importc.} proc computefaustlpf(dsp: ptr faustlpf, count: cint, inputs, outputs: ptr ptr SampleBuffer) {.importc.} +proc NimMain() {.cdecl, importc.} + proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble; bundlePath: cstring; features: ptr UncheckedArray[ptr Lv2Feature]): @@ -66,8 +66,7 @@ proc activate(instance: Lv2Handle) {.cdecl.} = proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} = let plug = cast[ptr FaustLPFPlugin](instance) - plug.flt.fHslider0 = plug.freq[].clamp(minFreq, maxFreq) - + plug.flt.fHslider0 = plug.freq[] computefaustlpf(plug.flt, nSamples.cint, addr plug.input, addr plug.output) @@ -85,9 +84,6 @@ proc extensionData(uri: cstring): pointer {.cdecl.} = return nil -proc NimMain() {.cdecl, importc.} - - proc lv2Descriptor(index: cuint): ptr Lv2Descriptor {. cdecl, exportc, dynlib, extern: "lv2_descriptor".} = NimMain() diff --git a/examples/lpf.h b/examples/lpf.h index e50c469..f14f608 100644 --- a/examples/lpf.h +++ b/examples/lpf.h @@ -5,7 +5,7 @@ license: "MIT" name: "FaustLPF" version: "0.1.0" Code generated with Faust 2.74.6 (https://faust.grame.fr) -Compilation options: -a ./examples/minarch.h -lang c -ct 1 -cn faustlpf -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0 -vec -lv 0 -vs 32 +Compilation options: -a ./examples/minarch.h -lang c -rui -ct 1 -fm def -cn faustlpf -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0 -vec -lv 0 -vs 32 ------------------------------------------------------------ */ #ifndef __faustlpf_H__ @@ -17,14 +17,8 @@ Compilation options: -a ./examples/minarch.h -lang c -ct 1 -cn faustlpf -es 1 -m FAUST Architecture File for generating a very minimal C interface ************************************************************************/ -#include -#include - #include "faust/gui/CInterface.h" -#define max(a,b) ((a < b) ? b : a) -#define min(a,b) ((a < b) ? a : b) - /****************************************************************************** VECTOR INTRINSICS *******************************************************************************/ @@ -47,6 +41,7 @@ extern "C" { #define RESTRICT __restrict__ #endif +#include "faust/dsp/fastmath.cpp" #include #include #include @@ -86,7 +81,7 @@ void deletefaustlpf(faustlpf* dsp) { void metadatafaustlpf(MetaGlue* m) { m->declare(m->metaInterface, "author", "Christopher Arndt"); - m->declare(m->metaInterface, "compile_options", "-a ./examples/minarch.h -lang c -ct 1 -cn faustlpf -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0 -vec -lv 0 -vs 32"); + m->declare(m->metaInterface, "compile_options", "-a ./examples/minarch.h -lang c -rui -ct 1 -fm def -cn faustlpf -es 1 -mcd 16 -mdd 1024 -mdy 33 -single -ftz 0 -vec -lv 0 -vs 32"); m->declare(m->metaInterface, "copyright", "Christopher Arndt, 2024"); m->declare(m->metaInterface, "filename", "lpf.dsp"); m->declare(m->metaInterface, "filters.lib/fir:author", "Julius O. Smith III"); @@ -191,7 +186,7 @@ void buildUserInterfacefaustlpf(faustlpf* dsp, UIGlue* ui_interface) { void computefaustlpf(faustlpf* dsp, int count, FAUSTFLOAT** RESTRICT inputs, FAUSTFLOAT** RESTRICT outputs) { FAUSTFLOAT* input0_ptr = inputs[0]; FAUSTFLOAT* output0_ptr = outputs[0]; - float fSlow0 = dsp->fConst1 * (float)(dsp->fHslider0); + float fSlow0 = dsp->fConst1 * fmaxf(16.0f, fminf(1.5e+04f, (float)(dsp->fHslider0))); float fRec1_tmp[36]; float* fRec1 = &fRec1_tmp[4]; float fZec0[32]; @@ -236,7 +231,7 @@ void computefaustlpf(faustlpf* dsp, int count, FAUSTFLOAT** RESTRICT inputs, FAU { int i; for (i = 0; i < vsize; i = i + 1) { - fZec0[i] = tanf(dsp->fConst3 * fRec1[i]); + fZec0[i] = fast_tanf(dsp->fConst3 * fRec1[i]); } } /* Vectorizable loop 2 */ @@ -328,7 +323,7 @@ void computefaustlpf(faustlpf* dsp, int count, FAUSTFLOAT** RESTRICT inputs, FAU { int i; for (i = 0; i < vsize; i = i + 1) { - fZec0[i] = tanf(dsp->fConst3 * fRec1[i]); + fZec0[i] = fast_tanf(dsp->fConst3 * fRec1[i]); } } /* Vectorizable loop 2 */ diff --git a/examples/minarch.h b/examples/minarch.h index 6b8fef4..64c9851 100644 --- a/examples/minarch.h +++ b/examples/minarch.h @@ -4,14 +4,8 @@ FAUST Architecture File for generating a very minimal C interface ************************************************************************/ -#include -#include - #include "faust/gui/CInterface.h" -#define max(a,b) ((a < b) ? b : a) -#define min(a,b) ((a < b) ? a : b) - /****************************************************************************** VECTOR INTRINSICS *******************************************************************************/