forked from noriah/catnip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catnip.go
176 lines (138 loc) · 4.16 KB
/
catnip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"context"
"fmt"
"github.com/noriah/catnip/dsp"
"github.com/noriah/catnip/fft"
"github.com/noriah/catnip/graphic"
"github.com/noriah/catnip/input"
"github.com/noriah/catnip/util"
"github.com/pkg/errors"
)
const (
// ScalingSlowWindow in seconds
ScalingSlowWindow = 5
// ScalingFastWindow in seconds
ScalingFastWindow = ScalingSlowWindow * 0.2
// ScalingDumpPercent is how much we erase on rescale
ScalingDumpPercent = 0.60
// ScalingResetDeviation standard deviations from the mean before reset
ScalingResetDeviation = 1.0
// PeakThreshold is the threshold to not draw if the peak is less.
PeakThreshold = 0.01
)
// Catnip starts to draw the visualizer on the termbox screen.
func Catnip(cfg *Config) error {
// allocate as much as possible as soon as possible
var (
// slowMax/fastMax
slowMax = ((int(ScalingSlowWindow * cfg.SampleRate)) / cfg.SampleSize) * 2
fastMax = ((int(ScalingFastWindow * cfg.SampleRate)) / cfg.SampleSize) * 2
total = ((cfg.ChannelCount * cfg.SampleSize) * 2) + (slowMax + fastMax)
floatData = make([]float64, total)
)
var sessConfig = input.SessionConfig{
FrameSize: cfg.ChannelCount,
SampleSize: cfg.SampleSize,
SampleRate: cfg.SampleRate,
}
vis := visualizer{
cfg: cfg,
slowWindow: util.MovingWindow{
Capacity: slowMax,
Data: floatData[:slowMax],
},
fastWindow: util.MovingWindow{
Capacity: fastMax,
Data: floatData[slowMax : slowMax+fastMax],
},
fftBuf: make([]complex128, cfg.SampleSize/2+1),
inputBufs: make([][]float64, cfg.ChannelCount),
barBufs: make([][]float64, cfg.ChannelCount),
plans: make([]*fft.Plan, cfg.ChannelCount),
spectrum: dsp.Spectrum{
SampleRate: cfg.SampleRate,
SampleSize: cfg.SampleSize,
Bins: make([]dsp.Bin, cfg.SampleSize),
OldValues: make([][]float64, cfg.ChannelCount),
},
bars: 0,
display: graphic.Display{},
}
var pos = slowMax + fastMax
for idx := range vis.barBufs {
vis.barBufs[idx] = floatData[pos : pos+cfg.SampleSize]
pos += cfg.SampleSize
vis.inputBufs[idx] = floatData[pos : pos+cfg.SampleSize]
pos += cfg.SampleSize
vis.plans[idx] = &fft.Plan{
Input: vis.inputBufs[idx],
Output: vis.fftBuf,
}
vis.spectrum.OldValues[idx] = make([]float64, cfg.SampleSize)
vis.plans[idx].Init()
}
// INPUT SETUP
var backend, err = initBackend(cfg)
if err != nil {
return err
}
if sessConfig.Device, err = getDevice(backend, cfg); err != nil {
return err
}
audio, err := backend.Start(sessConfig)
defer backend.Close()
if err != nil {
return errors.Wrap(err, "failed to start the input backend")
}
vis.spectrum.SetSmoothing(cfg.SmoothFactor)
vis.spectrum.SetWinVar(cfg.WinVar)
if err = vis.display.Init(); err != nil {
return err
}
defer vis.display.Close()
vis.display.SetSizes(cfg.BarSize, cfg.SpaceSize)
vis.display.SetBase(cfg.BaseSize)
vis.display.SetDrawType(graphic.DrawType(cfg.DrawType))
vis.display.SetStyles(cfg.Styles)
// Root Context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start the display
// replace our context so display can signal quit
ctx = vis.display.Start(ctx)
defer vis.display.Stop()
if err := audio.Start(ctx, vis.inputBufs, &vis); err != nil {
return errors.Wrap(err, "failed to start input session")
}
return nil
}
func initBackend(cfg *Config) (input.Backend, error) {
var backend = input.FindBackend(cfg.Backend)
if backend == nil {
return nil, fmt.Errorf("backend not found: %q; check list-backends", cfg.Backend)
}
if err := backend.Init(); err != nil {
return nil, errors.Wrap(err, "failed to initialize input backend")
}
return backend, nil
}
func getDevice(backend input.Backend, cfg *Config) (input.Device, error) {
if cfg.Device == "" {
var def, err = backend.DefaultDevice()
if err != nil {
return nil, errors.Wrap(err, "failed to get default device")
}
return def, nil
}
var devices, err = backend.Devices()
if err != nil {
return nil, errors.Wrap(err, "failed to get devices")
}
for idx := range devices {
if devices[idx].String() == cfg.Device {
return devices[idx], nil
}
}
return nil, errors.Errorf("device %q not found; check list-devices", cfg.Device)
}