Skip to content

Commit 9f1f9ae

Browse files
committed
add normalize flag
1 parent 015194c commit 9f1f9ae

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

cmd/catnip/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type config struct {
3838
drawType int
3939
// Combine determines if we merge streams (stereo -> mono)
4040
combine bool
41+
// Don't run math.Log on the output of the analyzer
42+
dontNormalize bool
4143
// Use threaded processor
4244
useThreaded bool
4345
// Invert the order of bin drawing
@@ -67,6 +69,7 @@ func newZeroConfig() config {
6769
spaceSize: 1,
6870
channelCount: 2,
6971
drawType: int(graphic.DrawDefault),
72+
dontNormalize: false,
7073
combine: false,
7174
useThreaded: false,
7275
invertDraw: false,

cmd/catnip/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,11 @@ func main() {
7777
Output: display,
7878
Windower: window.Lanczos(),
7979
Analyzer: dsp.NewAnalyzer(dsp.AnalyzerConfig{
80-
SampleRate: cfg.sampleRate,
81-
SampleSize: cfg.sampleSize,
82-
SquashLow: true,
83-
BinMethod: dsp.MaxSampleValue(),
80+
SampleRate: cfg.sampleRate,
81+
SampleSize: cfg.sampleSize,
82+
SquashLow: true,
83+
DontNormalize: cfg.dontNormalize,
84+
BinMethod: dsp.MaxSampleValue(),
8485
}),
8586
Smoother: dsp.NewSmoother(dsp.SmootherConfig{
8687
SampleSize: cfg.sampleSize,
@@ -137,6 +138,7 @@ func doFlags(cfg *config) bool {
137138
parser.Int(&cfg.barSize, "bw", "bar", "bar width [1, +Inf)")
138139
parser.Int(&cfg.spaceSize, "bs", "space", "space width [0, +Inf)")
139140
parser.Int(&cfg.drawType, "dt", "draw", "draw type (1, 2, 3, 4, 5, 6)")
141+
parser.Bool(&cfg.dontNormalize, "dn", "dont-normalize", "dont normalize analyzer output")
140142
parser.Bool(&cfg.useThreaded, "t", "threaded", "use the threaded processor")
141143
parser.Bool(&cfg.invertDraw, "i", "invert", "invert the direction of bin drawing")
142144

dsp/analyzer.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ import "math"
1515
type BinMethod func(int, float64, float64) float64
1616

1717
type AnalyzerConfig struct {
18-
SampleRate float64 // audio sample rate
19-
SampleSize int // number of samples per slice
20-
SquashLow bool // squash the low end the spectrum
21-
SquashLowOld bool // squash the low end using the old method
22-
BinMethod BinMethod // method used for calculating bin value
18+
SampleRate float64 // audio sample rate
19+
SampleSize int // number of samples per slice
20+
SquashLow bool // squash the low end the spectrum
21+
SquashLowOld bool // squash the low end using the old method
22+
DontNormalize bool // dont run math.Log on output
23+
BinMethod BinMethod // method used for calculating bin value
2324
}
2425

2526
type Analyzer interface {
@@ -30,10 +31,11 @@ type Analyzer interface {
3031

3132
// analyzer is an audio spectrum in a buffer
3233
type analyzer struct {
33-
cfg AnalyzerConfig // the analyzer config
34-
bins []bin // bins for processing
35-
binCount int // number of bins we look at
36-
fftSize int // number of fft bins
34+
cfg AnalyzerConfig // the analyzer config
35+
bins []bin // bins for processing
36+
binCount int // number of bins we look at
37+
fftSize int // number of fft bins
38+
dontNormalize bool // dont normalize the output
3739
}
3840

3941
// Bin is a helper struct for spectrum
@@ -107,9 +109,10 @@ func MinNonZeroSampleValue() BinMethod {
107109

108110
func NewAnalyzer(cfg AnalyzerConfig) Analyzer {
109111
return &analyzer{
110-
cfg: cfg,
111-
bins: make([]bin, cfg.SampleSize),
112-
fftSize: cfg.SampleSize/2 + 1,
112+
cfg: cfg,
113+
bins: make([]bin, cfg.SampleSize),
114+
fftSize: cfg.SampleSize/2 + 1,
115+
dontNormalize: cfg.DontNormalize,
113116
}
114117
}
115118

@@ -151,7 +154,9 @@ func (az *analyzer) ProcessBin(idx int, src []complex128) float64 {
151154
return 0.0
152155
}
153156

154-
mag = math.Log(mag)
157+
if !az.dontNormalize {
158+
mag = math.Log(mag)
159+
}
155160

156161
if mag < 0.0 {
157162
return 0.0

0 commit comments

Comments
 (0)