From 4dc371c7de6577f44464301dc7d0f7b2f77d26b3 Mon Sep 17 00:00:00 2001 From: Christopher Arndt Date: Sun, 28 Apr 2024 12:37:14 +0200 Subject: [PATCH] Minor filter example fixes and tweaks Signed-off-by: Christopher Arndt --- examples/multimode_filter.nim | 4 ++-- examples/svf.nim | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/multimode_filter.nim b/examples/multimode_filter.nim index 6ad8096..5bfe683 100644 --- a/examples/multimode_filter.nim +++ b/examples/multimode_filter.nim @@ -24,7 +24,7 @@ type proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble; bundlePath: cstring; features: ptr ptr Lv2Feature): Lv2Handle {.cdecl.} = - var plug = createShared(SVFPlugin) + let plug = createShared(SVFPlugin) plug.svf = initFilterSV(fmLowPass, sampleRate) return plug @@ -56,7 +56,7 @@ proc activate(instance: Lv2Handle) {.cdecl.} = proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} = let plug = cast[ptr SVFPlugin](instance) - plug.svf.setMode(plug.mode[].int.clamp(0, 4).FilterMode) + plug.svf.setMode(plug.mode[].int.clamp(0, 3).FilterMode) plug.svf.setCutoff(plug.cutoff[].clamp(16.0, 7_000.0)) plug.svf.setQ(plug.q[].clamp(0.8, 10.0)) plug.svf.calcCoef() diff --git a/examples/svf.nim b/examples/svf.nim index 0d79d9e..fe0e9db 100644 --- a/examples/svf.nim +++ b/examples/svf.nim @@ -1,5 +1,7 @@ ## State Variable Filter after Hal Chamberlin, Musical Applications of Microprocessors +## ## Only stable up to fc ~= fs / 6.5 ! +## Sensible range for q ~= 0.8 .. 10.0 import std/math @@ -18,23 +20,23 @@ type a: float b: float maxCutoff: float - sampleRate: float + sampleRate: float64 needs_update: bool proc reset*(self: var FilterSV) = - self.lowPass = 0 - self.hiPass = 0 - self.bandPass = 0 - self.bandReject = 0 + self.lowPass = 0.0 + self.hiPass = 0.0 + self.bandPass = 0.0 + self.bandReject = 0.0 -proc initFilterSV*(mode: FilterMode = fmLowPass, sampleRate: float = 48_000): FilterSV = +proc initFilterSV*(mode: FilterMode = fmLowPass, sampleRate: float64 = 48_000.0): FilterSV = result.mode = mode result.sampleRate = sampleRate result.reset() - result.a = 0 - result.b = 0 + result.a = 0.0 + result.b = 0.0 result.maxCutoff = sampleRate / 6.0 result.needs_update = true @@ -52,7 +54,7 @@ proc calcCoef*(self: var FilterSV) = proc setCutoff*(self: var FilterSV, cutoff: float) = - let fc = if cutoff > self.maxCutoff: self.maxCutoff else: cutoff + let fc = min(self.maxCutoff, cutoff) if fc != self.cutoff: self.cutoff = fc