This commit is contained in:
5684185+vsariola@users.noreply.github.com
2026-01-02 02:03:05 +02:00
parent fcb9a06249
commit 06a1fb6b52
5 changed files with 145 additions and 38 deletions

View File

@ -34,6 +34,11 @@ type (
tmpC []complex128 // temporary buffer for FFT
tmp1, tmp2 []float32 // temporary buffers for processing
}
BiquadCoeffs struct {
b0, b1, b2 float32
a0, a1, a2 float32
}
)
const (
@ -72,6 +77,60 @@ func NewSpecAnalyzer(broker *Broker) *SpecAnalyzer {
return ret
}
func (m *Model) BiquadCoeffs() (coeffs BiquadCoeffs, ok bool) {
i := m.d.InstrIndex
u := m.d.UnitIndex
if i < 0 || i >= len(m.d.Song.Patch) || u < 0 || u >= len(m.d.Song.Patch[i].Units) {
return BiquadCoeffs{}, false
}
switch m.d.Song.Patch[i].Units[u].Type {
case "filter":
p := m.d.Song.Patch[i].Units[u].Parameters
f := float32(p["frequency"]) / 128
res := float32(p["resonance"]) / 128
f2 := f * f
g := f2 / (2 - f2)
a1 := 2 * (g*g - 1)
a2 := (1 - g*(g-res))
var b0, b1, b2 float32
if p["low"] == 1 {
b0 += g * g
b1 += 2 * g * g
b2 += g * g
}
b0 += float32(p["high"])
b1 += -2 * float32(p["high"])
b2 += float32(p["high"])
b0 += g * float32(p["band"])
b2 += -g * float32(p["band"])
return BiquadCoeffs{a0: 1, a1: a1, a2: a2, b0: b0, b1: b1, b2: b2}, true
case "belleq":
f := float32(m.d.Song.Patch[i].Units[u].Parameters["frequency"]) / 128
band := float32(m.d.Song.Patch[i].Units[u].Parameters["bandwidth"]) / 128
gain := float32(m.d.Song.Patch[i].Units[u].Parameters["gain"]) / 128
omega0 := 2 * f * f
alpha := float32(math.Sin(float64(omega0))) * 2 * band
A := float32(math.Pow(2, float64(gain-.5)*6.643856189774724))
u, v := alpha*A, alpha/A
return BiquadCoeffs{
b0: 1 + u,
b1: -2 * float32(math.Cos(float64(omega0))),
b2: 1 - u,
a0: 1 + v,
a1: -2 * float32(math.Cos(float64(omega0))),
a2: 1 - v,
}, true
default:
return BiquadCoeffs{}, false
}
}
func (c *BiquadCoeffs) Gain(omega float32) float32 {
e := cmplx.Rect(1, -float64(omega))
return float32(cmplx.Abs((complex(float64(c.b0), 0) + complex(float64(c.b1), 0)*e + complex(float64(c.b2), 0)*(e*e)) /
(complex(float64(c.a0), 0) + complex(float64(c.a1), 0)*e + complex(float64(c.a2), 0)*e*e)))
}
func (s *SpecAnalyzer) Run() {
for {
select {