Minor filter example fixes and tweaks

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2024-04-28 12:37:14 +02:00
parent 8fc3663bc3
commit 4dc371c7de
2 changed files with 13 additions and 11 deletions

View File

@ -24,7 +24,7 @@ type
proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble; proc instantiate(descriptor: ptr Lv2Descriptor; sampleRate: cdouble;
bundlePath: cstring; features: ptr ptr Lv2Feature): bundlePath: cstring; features: ptr ptr Lv2Feature):
Lv2Handle {.cdecl.} = Lv2Handle {.cdecl.} =
var plug = createShared(SVFPlugin) let plug = createShared(SVFPlugin)
plug.svf = initFilterSV(fmLowPass, sampleRate) plug.svf = initFilterSV(fmLowPass, sampleRate)
return plug return plug
@ -56,7 +56,7 @@ proc activate(instance: Lv2Handle) {.cdecl.} =
proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} = proc run(instance: Lv2Handle; nSamples: cuint) {.cdecl.} =
let plug = cast[ptr SVFPlugin](instance) 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.setCutoff(plug.cutoff[].clamp(16.0, 7_000.0))
plug.svf.setQ(plug.q[].clamp(0.8, 10.0)) plug.svf.setQ(plug.q[].clamp(0.8, 10.0))
plug.svf.calcCoef() plug.svf.calcCoef()

View File

@ -1,5 +1,7 @@
## State Variable Filter after Hal Chamberlin, Musical Applications of Microprocessors ## State Variable Filter after Hal Chamberlin, Musical Applications of Microprocessors
##
## Only stable up to fc ~= fs / 6.5 ! ## Only stable up to fc ~= fs / 6.5 !
## Sensible range for q ~= 0.8 .. 10.0
import std/math import std/math
@ -18,23 +20,23 @@ type
a: float a: float
b: float b: float
maxCutoff: float maxCutoff: float
sampleRate: float sampleRate: float64
needs_update: bool needs_update: bool
proc reset*(self: var FilterSV) = proc reset*(self: var FilterSV) =
self.lowPass = 0 self.lowPass = 0.0
self.hiPass = 0 self.hiPass = 0.0
self.bandPass = 0 self.bandPass = 0.0
self.bandReject = 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.mode = mode
result.sampleRate = sampleRate result.sampleRate = sampleRate
result.reset() result.reset()
result.a = 0 result.a = 0.0
result.b = 0 result.b = 0.0
result.maxCutoff = sampleRate / 6.0 result.maxCutoff = sampleRate / 6.0
result.needs_update = true result.needs_update = true
@ -52,7 +54,7 @@ proc calcCoef*(self: var FilterSV) =
proc setCutoff*(self: var FilterSV, cutoff: float) = 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: if fc != self.cutoff:
self.cutoff = fc self.cutoff = fc