feat: implement bell filter unit for equalizing

This commit is contained in:
5684185+vsariola@users.noreply.github.com
2025-12-28 18:08:12 +02:00
parent 33ee80a908
commit 4d09e04a49
13 changed files with 245 additions and 29 deletions

View File

@ -198,6 +198,68 @@ su_op_filter_skipneghighpass:
{{end}}
{{- if .HasOp "belleq"}}
;-------------------------------------------------------------------------------
; BELLEQ opcode: perform second order bell eq filtering on the signal
;-------------------------------------------------------------------------------
; Mono: x -> eq(x)
; Stereo: l r -> eq(l) eq(r)
;-------------------------------------------------------------------------------
{{.Func "su_op_belleq" "Opcode"}}
{{- if .Stereo "belleq"}}
{{.Call "su_effects_stereohelper"}}
{{- end}}
; Note: we calculate the gain first because su_power needs temp stack and everything here was crafted to stay altogether below max 4 temp stack
; The cost of staying at max 4 stack was a few extra instructions because of stack juggling.
; The bell filter biquad coefficients (see go_synth.go):
; b0, b1, b2 = 1+u, -2*cos(w), 1-u
; a0, a1, a2 = 1+v, b1, 1-v
; where w=freq*freq, u=alpha*A, v=alpha/A, alpha=sin(w)*2*bandwidth, A=gain. The filter is implemented as:
; y = (b0*x+s1)/a0 = ((1+u)*x + s1) / (1+v) = (x+u*x+s1)/(1+v)
; s1' = b1*x - a1*y + s2 = b1*(x-y)+s2 = 2*cos(w)*(y-x)+s2
; s2' = b2*x - a2*y = (1-u)*x-(1-v)*y = x-y-u*x+v*y
fld dword [{{.Input "belleq" "gain"}}] ; g x
{{- .Float 0.5 | .Prepare | indent 4}}
fsub dword [{{.Float 0.5 | .Use}}] ; g-0.5 x
{{- .Float 6.643856189774724 | .Prepare | indent 4}}
fmul dword [{{.Use (.Float 6.643856189774724)}}] ; (g-0.5)*6.643856189774724 x
{{.Call "su_power"}} ; A=2^((g-0.5)*6.643856189774724) x
fld dword [{{.Input "belleq" "frequency"}}] ; f A x
fmul st0, st0 ; f*f A x
fadd st0, st0 ; w=2*f*f
fsincos ; cos(w) sin(w) A x
fadd st0, st0 ; r=2*cos(w) sin(w) A x
fld dword [{{.Input "belleq" "bandwidth"}}] ; b r sin(w) A x
fadd st0, st0 ; 2*b r sin(w) A x
fmulp st2, st0 ; r alpha=sin(w)*2*b A x
fxch st0, st1 ; alpha r A x
fdivr st2, st0 ; alpha r v=alpha/A x
fmul st0, st0 ; alpha*alpha r v x
fdiv st0, st2 ; u=alpha*A r v x
fld1 ; 1 u r v x
faddp st3, st0 ; u r v+1 x
fmul st0, st3 ; u*x r v+1 x
fld dword [{{.WRK}}] ; s1 u*x r v+1 x
fadd st0, st1 ; s1+u*x u*x r v+1 x
fadd st0, st4 ; s1+u*x+x u*x r v+1 x
fdiv st0, st3 ; y=(s1+u*x+x)/(v+1) u*x r v+1 x
{{- .Float 0.5 | .Prepare | indent 4}}
fadd dword [{{.Float 0.5 | .Use}}] ; add and sub small offset to prevent denormalization
fsub dword [{{.Float 0.5 | .Use}}] ; See for example: https://stackoverflow.com/questions/36781881/why-denormalized-floats-are-so-much-slower-than-other-floats-from-hardware-arch
fmul st3, st0 ; y u*x r v*y+y x
fsub st3, st0 ; y u*x r v*y x
fxch st4, st0 ; x u*x r v*y y
fsubr st0, st4 ; y-x u*x r v*y y
fmul st2, st0 ; y-x u*x r*(y-x) v*y y
fsubp st3, st0 ; u*x r*(y-x) x-y+v*y y
fsubp st2, st0 ; r*(y-x) x-y+v*y-u*x y
fadd dword [{{.WRK}}+4] ; s2+r*(y-x) x-y+v*y-u*x y
fstp dword [{{.WRK}}] ; x-y+v*y-u*x y
fstp dword [{{.WRK}}+4] ; y
ret
{{end}}
{{- if .HasOp "clip"}}
;-------------------------------------------------------------------------------
; CLIP opcode: clips the signal into [-1,1] range

View File

@ -203,6 +203,62 @@
{{end}}
{{- if .HasOp "belleq"}}
;;-------------------------------------------------------------------------------
;; BELLEQ opcode: perform second order bell eq filtering on the signal
;;-------------------------------------------------------------------------------
;; Mono: x -> eq(x)
;; Stereo: l r -> eq(l) eq(r)
;;-------------------------------------------------------------------------------
(func $su_op_belleq (param $stereo i32) (local $sinw f32) (local $A f32) (local $u f32) (local $v f32) (local $x f32) (local $y f32) (local $d f32) (local $alpha f32)
{{- if .Stereo "belleq"}}
(call $stereoHelper (local.get $stereo) (i32.const {{div (.GetOp "belleq") 2}}))
{{- end}}
(global.get $WRK)
(local.tee $x (call $pop)) ;; x WRK
(f32.mul
(call $input (i32.const {{.InputNumber "belleq" "frequency"}}))
(call $input (i32.const {{.InputNumber "belleq" "frequency"}}))
)
(f32.mul (f32.const 2))
(local.tee $sinw (call $sin)) ;; sinw x WRK
(call $input (i32.const {{.InputNumber "belleq" "bandwidth"}})) ;; b sinw x WRK
(f32.mul (f32.const 2)) ;; 2*b sinw x WRK
(local.tee $alpha (f32.mul)) ;; alpha=sinw*2*b x WRK
(f32.sub (call $input (i32.const {{.InputNumber "belleq" "gain"}})) (f32.const 0.5)) ;; g-0.5 alpha x WRK
(f32.mul (f32.const 6.643856189774724))
(local.tee $A (call $pow2)) ;; A=2^((g-0.5)*6.643856189774724) alpha x WRK
(local.tee $u (f32.mul)) ;; u=A*alpha x WRK
;; Computing (y=x+u*x+s1)/(1+v)
(f32.mul (local.get $x)) ;; u*x x WRK
(f32.add) ;; ux+x WRK
(f32.load (global.get $WRK)) ;; s1 ux+x WRK
(f32.add) ;; ux+x+s1 WRK
;; Compute v=alpha/A
(local.tee $v (f32.div (local.get $alpha) (local.get $A))) ;; v ux+x+s1 WRK
(f32.add (f32.const 1)) ;; 1+v ux+x+s1 WRK
(local.tee $y (f32.div)) ;; y WRK
;; s1' = 2*cos(w)*(y-x)+s2
(f32.sub (local.get $x)) ;; y-x WRK
;; need to compute cos(w) as sqrt(1-sin(w)^2)
(f32.sqrt (f32.sub (f32.const 1) (f32.mul (local.get $sinw) (local.get $sinw)))) ;; cos(w) y-x WRK
(f32.mul) ;; cos(w)*(y-x) WRK
(f32.mul (f32.const 2)) ;; 2*cos(w)*(y-x) WRK
(f32.add (f32.load offset=4 (global.get $WRK))) ;; s2+2*cos(w)*(y-x) WRK
(f32.store) ;; s1'=s2+2*cos(w)*(y-x)
;; s2' = x-y+v*y-u*x
(global.get $WRK)
(f32.sub (local.get $x) (local.get $y)) ;; x-y WRK
(f32.mul (local.get $v) (local.get $y))
(f32.mul (local.get $u) (local.get $x))
(f32.sub) ;; v*y-u*x x-y WRK
(f32.add) ;; v*y-u*x+x-y WRK
(f32.store offset=4)
(call $push (local.get $y))
)
{{end}}
{{- if .HasOp "clip"}}
;;-------------------------------------------------------------------------------
;; CLIP opcode: clips the signal into [-1,1] range