133 lines
3.4 KiB
Go
133 lines
3.4 KiB
Go
package audio
|
|
|
|
import (
|
|
"math"
|
|
"math/cmplx"
|
|
|
|
"github.com/BeatGlow/beatglow/internal/calc"
|
|
"gonum.org/v1/gonum/dsp/fourier"
|
|
)
|
|
|
|
// FrequencySpectrumAnalyzer structure to hold analysis parameters
|
|
type FrequencySpectrumAnalyzer struct {
|
|
SampleRate float64
|
|
BitDepth int
|
|
NumBins int
|
|
MinFrequency float64
|
|
MaxFrequency float64
|
|
avgSamples []float64
|
|
coefficients []complex128
|
|
bins []float64
|
|
}
|
|
|
|
// dBFS calculates dBFS for the given amplitude value
|
|
func dBFS(amplitude float64) float64 {
|
|
if amplitude == 0 {
|
|
return -math.Inf(1) // -Infinity for silence
|
|
}
|
|
return 20 * math.Log10(amplitude)
|
|
}
|
|
|
|
func normalize(values []float64) {
|
|
var max float64
|
|
for _, v := range values {
|
|
if v = math.Abs(v); v > max {
|
|
max = v
|
|
}
|
|
}
|
|
if max == 0 {
|
|
return
|
|
}
|
|
for i := range values {
|
|
values[i] /= max
|
|
}
|
|
}
|
|
|
|
// AnalyzeFrequencySpectrum performs the FFT and returns the energy levels in dBFS for each frequency bin
|
|
func (a *FrequencySpectrumAnalyzer) AnalyzeFrequencySpectrum(buffer *Buffer) []float64 {
|
|
samples := len(buffer.Samples[0])
|
|
//if len(a.avgSamples) != samples {
|
|
a.avgSamples = make([]float64, samples)
|
|
a.coefficients = make([]complex128, samples/2+1)
|
|
//}
|
|
|
|
channels := len(buffer.Samples)
|
|
if channels == 1 {
|
|
for i, sample := range buffer.Samples[0] {
|
|
a.avgSamples[i] = float64(sample)
|
|
}
|
|
} else {
|
|
for i := range buffer.Samples[0] {
|
|
a.avgSamples[i] = float64(buffer.Samples[0][i]+buffer.Samples[1][i]) / 2
|
|
}
|
|
}
|
|
|
|
// Apply windowing function
|
|
//normalize(a.avgSamples)
|
|
//calc.Hamming(a.avgSamples, a.avgSamples)
|
|
calc.Hann(a.avgSamples, a.avgSamples)
|
|
|
|
// Perform FFT on the signal
|
|
fft := fourier.NewFFT(samples)
|
|
fft.Coefficients(a.coefficients, a.avgSamples)
|
|
|
|
// Calculate the energy levels in dBFS for each frequency bin
|
|
var (
|
|
frequencyBinSize = a.SampleRate / float64(samples)
|
|
minBin = int(math.Round(a.MinFrequency / frequencyBinSize))
|
|
maxBin = int(math.Round(a.MaxFrequency / frequencyBinSize))
|
|
)
|
|
|
|
// Make sure the minimum bin is not lower than 0, and the maximum bin doesn't exceed half of the buffer size
|
|
if minBin < 0 {
|
|
minBin = 0
|
|
}
|
|
if maxBin > samples/2 {
|
|
maxBin = samples / 2
|
|
}
|
|
|
|
//if len(a.bins) != a.NumBins {
|
|
a.bins = make([]float64, a.NumBins)
|
|
//}
|
|
|
|
// Compute dBFS levels for the selected frequency range
|
|
for i := 0; i < a.NumBins; i++ {
|
|
var (
|
|
// Calculate the frequency range for this bin
|
|
frequencyScale = (a.MaxFrequency - a.MinFrequency) / float64(a.NumBins)
|
|
frequencyStart = a.MinFrequency + float64(i)*frequencyScale
|
|
frequencyEnd = a.MinFrequency + float64(i+1)*frequencyScale
|
|
|
|
// Determine the FFT bin range corresponding to the frequency range of this bin
|
|
startBin = int((frequencyStart / frequencyBinSize) + .5)
|
|
endBin = int((frequencyEnd / frequencyBinSize) + .5)
|
|
)
|
|
|
|
// Ensure the bins are within the valid range
|
|
if startBin < 0 {
|
|
startBin = 0
|
|
}
|
|
if endBin > samples/2 {
|
|
endBin = samples / 2
|
|
}
|
|
|
|
// Average the magnitudes of the FFT bins in this range and accumulate the result
|
|
var totalAmplitude float64
|
|
var count int
|
|
for bin := startBin; bin < endBin; bin++ {
|
|
x := a.coefficients[bin]
|
|
//amplitude := real(x)*real(x) + imag(x)*imag(x) // math.Hypot(real(x), imag(x))
|
|
amplitude := math.Min(cmplx.Abs(x), 1.0)
|
|
totalAmplitude += amplitude
|
|
count++
|
|
}
|
|
|
|
// Calculate the average dBFS for this frequency bin
|
|
if count > 0 {
|
|
a.bins[i] = math.Min((totalAmplitude/float64(count))*10, 1.0)
|
|
}
|
|
}
|
|
|
|
return a.bins
|
|
}
|