@@ -10,39 +10,43 @@ import (
1010
1111// Spectrum is an audio spectrum in a buffer
1212type Spectrum struct {
13- numBins int
14-
15- fftSize int
16-
17- sampleSize int
18- sampleRate float64
19-
20- winVar float64
21-
22- smoothFactor float64
23-
24- bins []bin
25-
26- fftBuf []complex128
27-
28- streams []* stream
13+ numBins int // number of bins we look at
14+ fftSize int // number of fft bins
15+ sampleSize int // number of samples per slice
16+ sampleRate float64 // audio sample rate
17+ winVar float64 // window variable
18+ smoothFactor float64 // smothing factor
19+ bins []bin // bins for processing
20+ fftBuf []complex128 // fft return buffer
21+ streams []* stream // streams of data
2922}
3023
3124type bin struct {
32- eqVal float64
33-
34- floorFFT int
35- ceilFFT int
36- widthFFT int
25+ eqVal float64 // equalizer value
26+ floorFFT int // floor fft index
27+ ceilFFT int // ceiling fft index
28+ widthFFT int // fft floor-ceiling index delta
3729}
3830
3931type stream struct {
40- input []float64
41- buf []float64
42- pBuf []float64
43- plan * fft.Plan
32+ input []float64 // input data buffer
33+ buf []float64 // bar bin buffer
34+ pBuf []float64 // previous run bin buffer
35+ plan * fft.Plan // fft plan
4436}
4537
38+ // SpectrumType is the type of calculation we run
39+ type SpectrumType int
40+
41+ // Spectrum calculation types
42+ const (
43+ SpectrumLog SpectrumType = iota
44+ SpectrumEqual
45+
46+ // SpectrumDefault is the default spectrum type
47+ SpectrumDefault = SpectrumLog
48+ )
49+
4650// Frequencies are the dividing frequencies
4751var Frequencies = []float64 {
4852 // sub sub bass
@@ -60,18 +64,6 @@ var Frequencies = []float64{
6064 // everything else
6165}
6266
63- // SpectrumType is the type of calculation we run
64- type SpectrumType int
65-
66- // Spectrum calculation types
67- const (
68- SpectrumLog SpectrumType = iota
69- SpectrumEqual
70-
71- // SpectrumDefault is the default spectrum type
72- SpectrumDefault = SpectrumLog
73- )
74-
7567// Some notes:
7668//
7769// https://stackoverflow.com/questions/3694918/how-to-extract-frequency-associated-with-fft-values-in-python
@@ -90,7 +82,7 @@ func NewSpectrum(hz float64, size int) *Spectrum {
9082 fftSize : fftSize ,
9183 sampleSize : size ,
9284 sampleRate : hz ,
93- smoothFactor : 0.655 ,
85+ smoothFactor : 0.1969 ,
9486 winVar : 1.0 ,
9587 bins : make ([]bin , size + 1 ),
9688 fftBuf : make ([]complex128 , fftSize ),
@@ -161,7 +153,7 @@ func (sp *Spectrum) Process(win window.Function) {
161153
162154 // mag /= float64(sp.bins[xB].widthFFT)
163155
164- var pow = 0.6
156+ var pow = 0.65
165157
166158 switch {
167159 case mag < 0.0 :
@@ -172,9 +164,10 @@ func (sp *Spectrum) Process(win window.Function) {
172164 pow *= math .Max (0.6 , float64 (xF )/ fBassCut )
173165
174166 default :
175- mag *= sp .bins [xB ].eqVal
176167 }
177168
169+ mag *= sp .bins [xB ].eqVal
170+
178171 mag = math .Pow (mag , pow )
179172
180173 // Smoothing
@@ -319,18 +312,28 @@ func (sp *Spectrum) freqToIdx(freq float64, round mathFunc) int {
319312
320313// SetWinVar sets the winVar used for distribution spread
321314func (sp * Spectrum ) SetWinVar (g float64 ) {
322- if g <= 0.0 {
323- g = 1
315+ switch {
316+
317+ case g <= 0.0 :
318+ sp .winVar = 1.0
319+
320+ default :
321+ sp .winVar = g
322+
324323 }
325324
326- sp .winVar = g
327325}
328326
329327// SetSmoothing sets the smoothing parameters
330328func (sp * Spectrum ) SetSmoothing (factor float64 ) {
331- if factor <= 0 {
332- factor = math .SmallestNonzeroFloat64
329+ switch {
330+
331+ case factor <= 0.0 :
332+ sp .smoothFactor = math .SmallestNonzeroFloat64
333+
334+ default :
335+ sp .smoothFactor = factor
336+
333337 }
334338
335- sp .smoothFactor = factor
336339}
0 commit comments