package audio import ( "github.com/moutend/go-equalizer/pkg/equalizer" ) // 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) type EQChain struct { FiltersLeft []*equalizer.Filter FiltersRight []*equalizer.Filter buffer []float64 // Reusable scratch buffer for processing currentGains []float64 // Cache of current gain values } // NewEQChain creates the standard 5-band EQ chain (Stereo) func NewEQChain(sampleRate float64) *EQChain { // 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} } return &EQChain{ 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 } } // SetGain sets the gain for a specific band index (0-4) func (e *EQChain) SetGain(bandIdx int, dbGain float64) { 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 { return } rate := 48000.0 // Assuming fixed rate for now // Frequencies map to our standard bands freqs := []float64{100, 350, 1000, 3000, 8000} // 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 } // Reset clears history of all filters func (e *EQChain) Reset() { // The library does not expose a Reset method. } // Process processes a slice of samples (Interleaved Stereo) func (e *EQChain) Process(samples []int16) []int16 { // 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 for i, s := range samples { 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) } // Run through RIGHT chain for _, f := range e.FiltersRight { valR = f.Apply(valR) } // 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 if val > 32767 { val = 32767 } else if val < -32768 { val = -32768 } // Write back directly to samples samples[i] = int16(val) } return samples }