feat(tui): add interactive 5-band per-user equalizer
This commit is contained in:
135
pkg/audio/biquad.go
Normal file
135
pkg/audio/biquad.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package audio
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// BiquadFilter represents a second-order IIR filter.
|
||||
// Formulas from RBJ Audio-EQ-Cookbook.
|
||||
type BiquadFilter struct {
|
||||
// Coefficients
|
||||
b0, b1, b2, a1, a2 float64
|
||||
|
||||
// State (history)
|
||||
x1, x2, y1, y2 float64
|
||||
}
|
||||
|
||||
// NewPeakingEQ creates a peaking EQ filter (boost/cut at specific frequency)
|
||||
// rate: sample rate (e.g. 48000)
|
||||
// freq: center frequency in Hz
|
||||
// q: quality factor (width of the bell)
|
||||
// dbGain: gain in decibels (e.g. +3.0, -6.0)
|
||||
func NewPeakingEQ(rate, freq, q, dbGain float64) *BiquadFilter {
|
||||
f := &BiquadFilter{}
|
||||
f.Configure(rate, freq, q, dbGain)
|
||||
return f
|
||||
}
|
||||
|
||||
// Configure recalculates coefficients
|
||||
func (f *BiquadFilter) Configure(rate, freq, q, dbGain float64) {
|
||||
// Intermediate variables
|
||||
A := math.Pow(10, dbGain/40)
|
||||
omega := 2 * math.Pi * freq / rate
|
||||
sn := math.Sin(omega)
|
||||
cs := math.Cos(omega)
|
||||
alpha := sn / (2 * q)
|
||||
|
||||
// Coefficients
|
||||
b0 := 1 + alpha*A
|
||||
b1 := -2 * cs
|
||||
b2 := 1 - alpha*A
|
||||
a0 := 1 + alpha/A
|
||||
a1 := -2 * cs
|
||||
a2 := 1 - alpha/A
|
||||
|
||||
// Normalize by a0
|
||||
invA0 := 1 / a0
|
||||
f.b0 = b0 * invA0
|
||||
f.b1 = b1 * invA0
|
||||
f.b2 = b2 * invA0
|
||||
f.a1 = a1 * invA0
|
||||
f.a2 = a2 * invA0
|
||||
}
|
||||
|
||||
// Process processes a single sample
|
||||
func (f *BiquadFilter) Process(in float64) float64 {
|
||||
// Difference equation:
|
||||
// y[n] = b0*x[n] + b1*x[n-1] + b2*x[n-2] - a1*y[n-1] - a2*y[n-2]
|
||||
out := f.b0*in + f.b1*f.x1 + f.b2*f.x2 - f.a1*f.y1 - f.a2*f.y2
|
||||
|
||||
// Update history
|
||||
f.x2 = f.x1
|
||||
f.x1 = in
|
||||
f.y2 = f.y1
|
||||
f.y1 = out
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
// Reset clears the filter memory
|
||||
func (f *BiquadFilter) Reset() {
|
||||
f.x1, f.x2, f.y1, f.y2 = 0, 0, 0, 0
|
||||
}
|
||||
|
||||
// EQChain manages a cascade of filters (our 5 bands)
|
||||
type EQChain struct {
|
||||
Filters []*BiquadFilter
|
||||
}
|
||||
|
||||
// NewEQChain creates the standard 5-band EQ chain
|
||||
func NewEQChain(sampleRate float64) *EQChain {
|
||||
return &EQChain{
|
||||
Filters: []*BiquadFilter{
|
||||
NewPeakingEQ(sampleRate, 100, 1.0, 0), // SUB (Reduced from 1000 to proper bass freq)
|
||||
NewPeakingEQ(sampleRate, 350, 1.0, 0), // LOW
|
||||
NewPeakingEQ(sampleRate, 1000, 1.0, 0), // MID
|
||||
NewPeakingEQ(sampleRate, 3000, 1.0, 0), // HI
|
||||
NewPeakingEQ(sampleRate, 8000, 1.0, 0), // AIR
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// SetGain sets the gain for a specific band index (0-4)
|
||||
func (e *EQChain) SetGain(bandIdx int, dbGain float64) {
|
||||
if bandIdx < 0 || bandIdx >= len(e.Filters) {
|
||||
return
|
||||
}
|
||||
|
||||
rate := 48000.0 // Assuming fixed rate for now
|
||||
// Frequencies map to our standard bands
|
||||
freqs := []float64{100, 350, 1000, 3000, 8000}
|
||||
|
||||
e.Filters[bandIdx].Configure(rate, freqs[bandIdx], 1.0, dbGain)
|
||||
}
|
||||
|
||||
// Reset clears history of all filters
|
||||
func (e *EQChain) Reset() {
|
||||
for _, f := range e.Filters {
|
||||
f.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
// ProcessBlock processes a slice of samples in-place (or returns new slice)
|
||||
// We'll return a new float buffer for FFT analysis anyway
|
||||
func (e *EQChain) Process(samples []int16) []int16 {
|
||||
out := make([]int16, len(samples))
|
||||
|
||||
for i, s := range samples {
|
||||
val := float64(s)
|
||||
|
||||
// Run through cascade
|
||||
for _, f := range e.Filters {
|
||||
val = f.Process(val)
|
||||
}
|
||||
|
||||
// Clip
|
||||
if val > 32767 {
|
||||
val = 32767
|
||||
} else if val < -32768 {
|
||||
val = -32768
|
||||
}
|
||||
|
||||
out[i] = int16(val)
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user