2026-01-17 20:25:58 +01:00
|
|
|
package audio
|
|
|
|
|
|
|
|
|
|
import (
|
2026-01-17 20:49:16 +01:00
|
|
|
"github.com/moutend/go-equalizer/pkg/equalizer"
|
2026-01-17 20:25:58 +01:00
|
|
|
)
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// EQChain manages a cascade of filters using go-equalizer library
|
|
|
|
|
// Now supports Stereo processing (Left/Right)
|
|
|
|
|
// EQChain manages a cascade of filters using go-equalizer library
|
|
|
|
|
// Now supports Stereo processing (Left/Right)
|
2026-01-17 20:25:58 +01:00
|
|
|
type EQChain struct {
|
2026-01-17 20:49:16 +01:00
|
|
|
FiltersLeft []*equalizer.Filter
|
|
|
|
|
FiltersRight []*equalizer.Filter
|
|
|
|
|
buffer []float64 // Reusable scratch buffer for processing
|
|
|
|
|
currentGains []float64 // Cache of current gain values
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// NewEQChain creates the standard 5-band EQ chain (Stereo)
|
2026-01-17 20:25:58 +01:00
|
|
|
func NewEQChain(sampleRate float64) *EQChain {
|
2026-01-17 20:49:16 +01:00
|
|
|
// Standard bands: 100, 350, 1000, 3000, 8000
|
|
|
|
|
// Width = 1.0 (approx 1 octave)
|
|
|
|
|
|
|
|
|
|
createChain := func() []*equalizer.Filter {
|
|
|
|
|
f1 := equalizer.NewPeaking(sampleRate, 100, 1.0, 0)
|
|
|
|
|
f2 := equalizer.NewPeaking(sampleRate, 350, 1.0, 0)
|
|
|
|
|
f3 := equalizer.NewPeaking(sampleRate, 1000, 1.0, 0)
|
|
|
|
|
f4 := equalizer.NewPeaking(sampleRate, 3000, 1.0, 0)
|
|
|
|
|
f5 := equalizer.NewPeaking(sampleRate, 8000, 1.0, 0)
|
|
|
|
|
return []*equalizer.Filter{f1, f2, f3, f4, f5}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:25:58 +01:00
|
|
|
return &EQChain{
|
2026-01-17 20:49:16 +01:00
|
|
|
FiltersLeft: createChain(),
|
|
|
|
|
FiltersRight: createChain(),
|
|
|
|
|
buffer: make([]float64, 1920), // Pre-allocate for Stereo 20ms frame (960*2)
|
|
|
|
|
currentGains: make([]float64, 5), // Initialize cache with 0.0
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetGain sets the gain for a specific band index (0-4)
|
|
|
|
|
func (e *EQChain) SetGain(bandIdx int, dbGain float64) {
|
2026-01-17 20:49:16 +01:00
|
|
|
if bandIdx < 0 || bandIdx >= 5 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Optimization: If gain hasn't changed, DO NOT recreate filter.
|
|
|
|
|
// Recreating the filter resets its internal history state (bi-quad delay buffers),
|
|
|
|
|
// causing audible clicks/pops (discontinuities) at every 20ms frame boundary.
|
|
|
|
|
const epsilon = 0.001
|
|
|
|
|
if delta := dbGain - e.currentGains[bandIdx]; delta > -epsilon && delta < epsilon {
|
2026-01-17 20:25:58 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rate := 48000.0 // Assuming fixed rate for now
|
|
|
|
|
// Frequencies map to our standard bands
|
|
|
|
|
freqs := []float64{100, 350, 1000, 3000, 8000}
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// Create new filter with updated gain
|
|
|
|
|
// We use width=1.0 consistent with constructor
|
|
|
|
|
// Update BOTH Left and Right to keep balance
|
|
|
|
|
e.FiltersLeft[bandIdx] = equalizer.NewPeaking(rate, freqs[bandIdx], 1.0, dbGain)
|
|
|
|
|
e.FiltersRight[bandIdx] = equalizer.NewPeaking(rate, freqs[bandIdx], 1.0, dbGain)
|
|
|
|
|
|
|
|
|
|
// Update cache
|
|
|
|
|
e.currentGains[bandIdx] = dbGain
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset clears history of all filters
|
|
|
|
|
func (e *EQChain) Reset() {
|
2026-01-17 20:49:16 +01:00
|
|
|
// The library does not expose a Reset method.
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// Process processes a slice of samples (Interleaved Stereo)
|
2026-01-17 20:25:58 +01:00
|
|
|
func (e *EQChain) Process(samples []int16) []int16 {
|
2026-01-17 20:49:16 +01:00
|
|
|
// Grow buffer if needed
|
|
|
|
|
if cap(e.buffer) < len(samples) {
|
|
|
|
|
e.buffer = make([]float64, len(samples))
|
|
|
|
|
}
|
|
|
|
|
e.buffer = e.buffer[:len(samples)]
|
|
|
|
|
|
|
|
|
|
// Float conversion with normalization (-1.0 to 1.0)
|
|
|
|
|
// We also apply a slight pre-attenuation (Headroom) to avoid clipping when boosting EQ.
|
|
|
|
|
// -3dB = 0.707
|
|
|
|
|
const headroom = 0.707
|
|
|
|
|
const norm = 1.0 / 32768.0
|
2026-01-17 20:25:58 +01:00
|
|
|
|
|
|
|
|
for i, s := range samples {
|
2026-01-17 20:49:16 +01:00
|
|
|
e.buffer[i] = float64(s) * norm * headroom
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Filter processing
|
|
|
|
|
// Input is assumed to be Interleaved Stereo: L, R, L, R...
|
|
|
|
|
// We iterate by 2 to process pairs.
|
|
|
|
|
|
|
|
|
|
for i := 0; i < len(e.buffer); i += 2 {
|
|
|
|
|
if i+1 >= len(e.buffer) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
valL := e.buffer[i]
|
|
|
|
|
valR := e.buffer[i+1]
|
|
|
|
|
|
|
|
|
|
// Run through LEFT chain
|
|
|
|
|
for _, f := range e.FiltersLeft {
|
|
|
|
|
valL = f.Apply(valL)
|
|
|
|
|
}
|
2026-01-17 20:25:58 +01:00
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// Run through RIGHT chain
|
|
|
|
|
for _, f := range e.FiltersRight {
|
|
|
|
|
valR = f.Apply(valR)
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// Write back to buffer
|
|
|
|
|
e.buffer[i] = valL
|
|
|
|
|
e.buffer[i+1] = valR
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert back to int16
|
|
|
|
|
for i, val := range e.buffer {
|
|
|
|
|
// Denormalize
|
|
|
|
|
val = val * 32767.0
|
|
|
|
|
|
|
|
|
|
// Hard clipping
|
2026-01-17 20:25:58 +01:00
|
|
|
if val > 32767 {
|
|
|
|
|
val = 32767
|
|
|
|
|
} else if val < -32768 {
|
|
|
|
|
val = -32768
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 20:49:16 +01:00
|
|
|
// Write back directly to samples
|
|
|
|
|
samples[i] = int16(val)
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|
2026-01-17 20:49:16 +01:00
|
|
|
|
|
|
|
|
return samples
|
2026-01-17 20:25:58 +01:00
|
|
|
}
|