Skip to content

Commit d3c13fb

Browse files
committed
my cat still tries to lick my speakers
1 parent 41f7b4d commit d3c13fb

File tree

7 files changed

+94
-133
lines changed

7 files changed

+94
-133
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
tavis
2-
===
1+
# tavis
32

43
[![love][withlove]][noriah-dev]
54
[![made-with-go][withgo]][go-dev]
@@ -8,14 +7,17 @@ tavis
87
> terminal audio visualizer for linux/unix/macOS/windows*
98
109
<p align="center">
11-
<a href="https://www.youtube.com/watch?v=NGtCoEsgJww" target="_blank">
10+
<a href="https://www.youtube.com/watch?v=NGtCoEsgJww">
1211
<img src="../media/preview0.gif?raw=true"/>
1312
</a>
1413
</p>
1514

1615
## early development - expect things to change and break
1716

18-
we are working on this project all the time. its a sort of time filler for us at this point. expect lots of additions and changes at random times.
17+
this project is still in the early stages of development.
18+
roadmaps and milestones are not currently priorities.
19+
20+
expect lots of additions and changes at random times.
1921

2022
*windows needs work
2123

@@ -38,9 +40,9 @@ we are working on this project all the time. its a sort of time filler for us at
3840
- fftw (fftw3)
3941
- portaudio (portaudio-2.0)
4042

41-
- binaries (optional)
42-
- ffmpeg
43-
- parec
43+
- binaries
44+
- ffmpeg (required for FFmpeg backends)
45+
- parec (required for PulseAudio backend with parec)
4446

4547
## installation
4648

config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Config struct {
5050
func NewZeroConfig() Config {
5151
return Config{
5252
SampleRate: 44100,
53-
SmoothFactor: 19.69,
53+
SmoothFactor: 50.69,
5454
WinVar: 0.50,
5555
BaseThick: 1,
5656
BarWidth: 2,
@@ -91,9 +91,9 @@ func sanitizeConfig(cfg *Config) error {
9191
}
9292

9393
switch {
94-
case cfg.SmoothFactor > 100.0:
95-
cfg.SmoothFactor = 1.0
96-
case cfg.SmoothFactor <= 0.0:
94+
case cfg.SmoothFactor > 99.99:
95+
cfg.SmoothFactor = 0.9999
96+
case cfg.SmoothFactor < 0.00001:
9797
cfg.SmoothFactor = 0.00001
9898
default:
9999
cfg.SmoothFactor /= 100.0

dsp/spectrum.go

+49-46
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,24 @@ const (
3131
SpectrumDefault = SpectrumLog
3232
)
3333

34+
// misc coonstants
35+
const (
36+
MaxStreams = 2
37+
)
38+
3439
// Spectrum is an audio spectrum in a buffer
3540
type Spectrum struct {
36-
numBins int // number of bins we look at
37-
fftSize int // number of fft bins
38-
sampleSize int // number of samples per slice
39-
sType SpectrumType // the type of spectrum distribution
40-
sampleRate float64 // audio sample rate
41-
winVar float64 // window variable
42-
smoothFactor float64 // smothing factor
43-
fftBuf []complex128 // fft return buffer
44-
bins []bin // bins for processing
45-
streams []*stream // streams of data
41+
numBins int // number of bins we look at
42+
numStreams int // number of streams we process
43+
fftSize int // number of fft bins
44+
sampleSize int // number of samples per slice
45+
sType SpectrumType // the type of spectrum distribution
46+
sampleRate float64 // audio sample rate
47+
winVar float64 // window variable
48+
smoothFactor float64 // smothing factor
49+
fftBuf []complex128 // fft return buffer
50+
bins []bin // bins for processing
51+
streams [MaxStreams]stream // streams of data
4652
}
4753

4854
type bin struct {
@@ -81,18 +87,15 @@ func NewSpectrum(hz float64, size int) *Spectrum {
8187

8288
var fftSize = (size / 2) + 1
8389

84-
var sp = &Spectrum{
90+
return &Spectrum{
8591
fftSize: fftSize,
8692
sampleSize: size,
8793
sampleRate: hz,
88-
smoothFactor: 0.1969,
94+
smoothFactor: 0.5069,
8995
winVar: 0.5,
9096
fftBuf: make([]complex128, fftSize),
9197
bins: make([]bin, size+1),
92-
streams: make([]*stream, 0, 2),
9398
}
94-
95-
return sp
9699
}
97100

98101
// StreamCount returns the number of streams in our buffers
@@ -101,22 +104,22 @@ func (sp *Spectrum) StreamCount() int {
101104
}
102105

103106
// AddStream adds an input buffer to the spectrum
104-
func (sp *Spectrum) AddStream(input []float64) {
105-
var s = &stream{
106-
input: input,
107-
buf: make([]float64, sp.sampleSize),
108-
pBuf: make([]float64, sp.sampleSize),
109-
plan: fft.NewPlan(input, sp.fftBuf),
107+
func (sp *Spectrum) AddStream(input ...[]float64) {
108+
109+
for idx := range input {
110+
sp.streams[idx].input = input[idx]
111+
sp.streams[idx].buf = make([]float64, sp.sampleSize)
112+
sp.streams[idx].pBuf = make([]float64, sp.sampleSize)
113+
sp.streams[idx].plan = fft.NewPlan(input[idx], sp.fftBuf)
114+
sp.numStreams++
110115
}
111-
112-
sp.streams = append(sp.streams, s)
113116
}
114117

115118
// BinBuffers returns our bin buffers
116119
func (sp *Spectrum) BinBuffers() [][]float64 {
117-
var buf = make([][]float64, len(sp.streams))
118-
for idx, stream := range sp.streams {
119-
buf[idx] = stream.buf
120+
var buf = make([][]float64, sp.numStreams)
121+
for idx := range sp.streams[:sp.numStreams] {
122+
buf[idx] = sp.streams[idx].buf
120123
}
121124

122125
return buf
@@ -129,18 +132,18 @@ func (sp *Spectrum) BinCount() int {
129132

130133
// Process makes numBins and dumps them in the buffer
131134
func (sp *Spectrum) Process(win window.Function) {
132-
var sf = math.Pow(10.0, (1-sp.smoothFactor)*(-10.0))
135+
var sf = math.Pow(10.0, (1.0-sp.smoothFactor)*(-20.0))
133136

134137
sf = math.Pow(sf, float64(sp.sampleSize)/sp.sampleRate)
135138

136-
var bassCut = sp.freqToIdx(Frequencies[2], math.Ceil)
139+
var bassCut = sp.freqToIdx(Frequencies[2], math.Floor)
137140
var fBassCut = float64(bassCut)
138141

139-
for _, stream := range sp.streams {
142+
for idx := range sp.streams {
140143

141-
win(stream.input)
144+
win(sp.streams[idx].input)
142145

143-
stream.plan.Execute()
146+
sp.streams[idx].plan.Execute()
144147

145148
for xB := 0; xB < sp.numBins; xB++ {
146149
var mag = 0.0
@@ -164,6 +167,7 @@ func (sp *Spectrum) Process(win window.Function) {
164167

165168
case sp.bins[xB].floorFFT < bassCut:
166169
pow *= math.Max(0.5, float64(xF)/fBassCut)
170+
167171
}
168172

169173
mag *= sp.bins[xB].eqVal
@@ -173,9 +177,9 @@ func (sp *Spectrum) Process(win window.Function) {
173177
// time smoothing
174178

175179
mag *= (1.0 - sf)
176-
mag += stream.pBuf[xB] * sf
177-
stream.pBuf[xB] = mag
178-
stream.buf[xB] = mag
180+
mag += sp.streams[idx].pBuf[xB] * sf
181+
sp.streams[idx].pBuf[xB] = mag
182+
sp.streams[idx].buf[xB] = mag
179183

180184
// mag += stream.pBuf[xB] * sp.smoothFactor
181185
// stream.pBuf[xB] = mag * (1 - (1 / (1 + (mag * 2))))
@@ -230,25 +234,24 @@ func (sp *Spectrum) Recalculate(bins int) int {
230234
// it is a best guess naive attempt right now.
231235
// i will continue work on it - winter
232236
func (sp *Spectrum) distributeLog(bins int) {
233-
var lo = (Frequencies[1])
234-
var hi = Frequencies[4]
237+
var lo = Frequencies[1]
238+
var hi = math.Min(sp.sampleRate/2, Frequencies[4])
235239

236-
var loLog = math.Log10(lo)
237-
var hiLog = math.Log10(hi)
240+
// var loLog = math.Log10(lo)
241+
// var hiLog = math.Log10(hi)
238242

239-
var cF = (hiLog - loLog) / float64(bins)
240-
241-
var getBinBase = func(b int) int {
242-
var vFreq = ((float64(b) * cF) + loLog)
243-
vFreq = math.Pow(10.0, vFreq)
244-
return sp.freqToIdx(vFreq, math.Floor)
245-
}
243+
// var cF = (hiLog - loLog) / float64(bins)
244+
var cF = math.Log10(lo/hi) / ((1 / float64(bins)) - 1)
246245

247246
var cCoef = 100.0 / float64(bins+1)
248247

249248
for xB := 0; xB <= bins; xB++ {
250249

251-
sp.bins[xB].floorFFT = getBinBase(xB)
250+
// var vFreq = ((float64(b) * cF) + loLog)
251+
// vFreq = math.Pow(10.0, vFreq)
252+
var vFreq = ((float64(xB) / float64(bins)) * cF) - cF
253+
vFreq = math.Pow(10.0, vFreq) * hi
254+
sp.bins[xB].floorFFT = sp.freqToIdx(vFreq, math.Floor)
252255
sp.bins[xB].eqVal = math.Log2(float64(xB)+2) * cCoef
253256

254257
if xB > 0 {

fft/fftw.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,14 @@ func (p *Plan) destroy() {
3636
C.fftw_destroy_plan(p.cPlan)
3737
}
3838

39-
// func AllocateReal(real int) []float64 {
40-
// var r = C.fftw_alloc_real(C.ulong(real))
41-
42-
// var rSlice []float64
43-
44-
// var sliceHead = (*reflect.SliceHeader)(unsafe.Pointer(&rSlice))
45-
// sliceHead.Cap = real
46-
// sliceHead.Len = real
47-
// sliceHead.Data = uintptr(unsafe.Pointer(&r))
48-
49-
// return rSlice
50-
// }
51-
5239
// NewPlan returns a new FFTW Plan for use with FFTW
5340
func NewPlan(in []float64, out []complex128) *Plan {
5441
var plan = &Plan{
5542
input: in,
5643
output: out,
5744
cPlan: C.fftw_plan_dft_r2c_1d(
5845
C.int(len(in)),
59-
(*C.double)(&in[0]),
46+
(*C.double)(unsafe.Pointer(&in[0])),
6047
(*C.fftw_complex)(unsafe.Pointer(&out[0])),
6148
C.FFTW_MEASURE,
6249
),

graphic/display.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const (
4040
// ScalingDumpPercent is how much we erase on rescale
4141
ScalingDumpPercent = 0.75
4242
// ScalingResetDeviation standard deviations from the mean before reset
43-
ScalingResetDeviation = 1
43+
ScalingResetDeviation = 1.0
4444
)
4545

4646
// DrawType is the type
@@ -71,8 +71,8 @@ type Config struct {
7171
type Display struct {
7272
running uint32
7373
cfg Config
74-
slowWindow *util.MovingWindow
75-
fastWindow *util.MovingWindow
74+
slowWindow util.MovingWindow
75+
fastWindow util.MovingWindow
7676
}
7777

7878
// NewDisplay sets up a new display
@@ -83,19 +83,17 @@ func NewDisplay(hz float64, samples int) *Display {
8383
slowMax := int((ScalingSlowWindow*hz)/float64(samples)) * 2
8484
fastMax := int((ScalingFastWindow*hz)/float64(samples)) * 2
8585

86-
var d = &Display{
87-
slowWindow: util.NewMovingWindow(slowMax),
88-
fastWindow: util.NewMovingWindow(fastMax),
86+
return &Display{
8987
cfg: Config{
9088
BarWidth: 2,
9189
SpaceWidth: 1,
9290
BinWidth: 3,
9391
BaseThick: 1,
9492
DrawType: DrawDefault,
9593
},
94+
slowWindow: util.NewMovingWindow(slowMax),
95+
fastWindow: util.NewMovingWindow(fastMax),
9696
}
97-
98-
return d
9997
}
10098

10199
// Draw takes data and draws

tavis.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ func Run(cfg Config) error {
3737
spectrum.SetSmoothing(cfg.SmoothFactor)
3838
spectrum.SetWinVar(cfg.WinVar)
3939
spectrum.SetType(dsp.SpectrumType(cfg.SpectrumType))
40-
41-
for ch := 0; ch < cfg.ChannelCount; ch++ {
42-
spectrum.AddStream(audio.SampleBuffers()[ch])
43-
}
40+
spectrum.AddStream(audio.SampleBuffers()...)
4441

4542
var barBuffers = spectrum.BinBuffers()
4643

0 commit comments

Comments
 (0)