Skip to content

Commit 2d5b876

Browse files
committed
lets slim down!
1 parent 51054ce commit 2d5b876

File tree

7 files changed

+59
-141
lines changed

7 files changed

+59
-141
lines changed

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func NewZeroConfig() Config {
6161
}
6262

6363
func sanitizeConfig(cfg *Config) error {
64+
6465
switch {
6566
case cfg.WinVar > 1.0:
6667
cfg.WinVar = 1.0

dsp/spectrum.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ func (sp *Spectrum) Process(win window.Function) {
161161
switch {
162162
case mag < 0.0:
163163
mag = 0.0
164-
continue
165164

166165
case sp.bins[xB].floorFFT < bassCut:
167-
pow *= math.Max(0.6, float64(xF)/fBassCut)
168-
166+
pow *= math.Max(0.75, float64(xF)/fBassCut)
169167
}
170168

171169
mag *= sp.bins[xB].eqVal
@@ -216,11 +214,12 @@ func (sp *Spectrum) Recalculate(bins int) int {
216214
sp.distributeEqual(bins)
217215

218216
default:
217+
sp.distributeLog(bins)
218+
219219
}
220220

221221
// set widths
222222
for xB := 0; xB < bins; xB++ {
223-
224223
sp.bins[xB].widthFFT = sp.bins[xB].ceilFFT - sp.bins[xB].floorFFT
225224
}
226225

@@ -237,9 +236,9 @@ func (sp *Spectrum) distributeLog(bins int) {
237236
var cF = math.Log10(lo/hi) / ((1 / float64(bins)) - 1)
238237

239238
var getBinBase = func(b int) int {
240-
var vFreq = ((float64(b+1) / float64(bins)) * cF) - cF
239+
var vFreq = ((float64(b) / float64(bins)) * cF) - cF
241240
vFreq = math.Pow(10.0, vFreq) * hi
242-
return sp.freqToIdx(vFreq, math.Round)
241+
return sp.freqToIdx(vFreq, math.Floor)
243242
}
244243

245244
var cCoef = 100.0 / float64(bins+1)
@@ -282,7 +281,6 @@ func (sp *Spectrum) distributeEqual(bins int) {
282281
}
283282

284283
for xB := 0; xB < lBins; xB++ {
285-
sp.bins[xB].widthFFT = spread
286284
sp.bins[xB].floorFFT = start
287285
start += spread
288286

@@ -292,7 +290,6 @@ func (sp *Spectrum) distributeEqual(bins int) {
292290
if last > 0 {
293291
sp.bins[lBins].floorFFT = start
294292
sp.bins[lBins].ceilFFT = start + last
295-
sp.bins[lBins].widthFFT = last
296293
}
297294
}
298295

@@ -328,7 +325,6 @@ func (sp *Spectrum) SetWinVar(g float64) {
328325
sp.winVar = g
329326

330327
}
331-
332328
}
333329

334330
// SetSmoothing sets the smoothing parameters
@@ -342,5 +338,4 @@ func (sp *Spectrum) SetSmoothing(factor float64) {
342338
sp.smoothFactor = factor
343339

344340
}
345-
346341
}

graphic/display.go

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const (
1515

1616
// Bar Constants
1717

18-
// DisplaySpace is the block we use for space (if we were to print one)
19-
DisplaySpace = '\u0020'
20-
2118
BarRuneR = '\u2580'
2219
BarRune = '\u2588'
2320

@@ -101,6 +98,44 @@ func NewDisplay(hz float64, samples int) *Display {
10198
return d
10299
}
103100

101+
// Draw takes data and draws
102+
func (d *Display) Draw(bufs [][]float64, channels, bins int) error {
103+
var peak = 0.0
104+
105+
for xCh := 0; xCh < channels; xCh++ {
106+
for xBin := 0; xBin < bins; xBin++ {
107+
if v := bufs[xCh][xBin]; peak < v {
108+
peak = v
109+
}
110+
}
111+
}
112+
113+
var scale = d.updateWindow(peak)
114+
115+
var err error
116+
117+
switch d.cfg.DrawType {
118+
case DrawUp:
119+
err = drawUp(bufs, bins, d.cfg, scale)
120+
case DrawUpDown:
121+
err = drawUpDown(bufs, bins, d.cfg, scale)
122+
case DrawDown:
123+
err = drawDown(bufs, bins, d.cfg, scale)
124+
default:
125+
return nil
126+
}
127+
128+
if err != nil {
129+
return err
130+
}
131+
132+
termbox.Flush()
133+
134+
termbox.Clear(StyleDefault, StyleDefaultBack)
135+
136+
return nil
137+
}
138+
104139
// Init initializes the display
105140
func (d *Display) Init() error {
106141
if err := termbox.Init(); err != nil {
@@ -295,44 +330,6 @@ func (d *Display) updateWindow(peak float64) float64 {
295330
return 1.0
296331
}
297332

298-
// Draw takes data and draws
299-
func (d *Display) Draw(bufs [][]float64, channels, bins int) error {
300-
var peak = 0.0
301-
302-
for xCh := 0; xCh < channels; xCh++ {
303-
for xBin := 0; xBin < bins; xBin++ {
304-
if v := bufs[xCh][xBin]; peak < v {
305-
peak = v
306-
}
307-
}
308-
}
309-
310-
var scale = d.updateWindow(peak)
311-
312-
var err error
313-
314-
switch d.cfg.DrawType {
315-
case DrawUp:
316-
err = drawUp(bufs, bins, d.cfg, scale)
317-
case DrawUpDown:
318-
err = drawUpDown(bufs, bins, d.cfg, scale)
319-
case DrawDown:
320-
err = drawDown(bufs, bins, d.cfg, scale)
321-
default:
322-
return nil
323-
}
324-
325-
if err != nil {
326-
return err
327-
}
328-
329-
termbox.Flush()
330-
331-
termbox.Clear(StyleDefault, StyleDefaultBack)
332-
333-
return nil
334-
}
335-
336333
func stopAndTop(value float64, height int, up bool) (int, rune) {
337334
if stop := int(value * NumRunes); stop < height*NumRunes {
338335

input/portaudio/portaudio.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ func (s *Session) Read(ctx context.Context) error {
157157
s.retBufs[i%s.config.FrameSize][i/s.config.FrameSize] = float64(v)
158158
}
159159

160+
// for xBuf := range s.retBufs {
161+
// for xSmpl := range s.retBufs[xBuf] {
162+
// s.retBufs[xBuf][xSmpl] = input.Sample(s.sampleBuf[(xSmpl*s.config.FrameSize)+xBuf])
163+
// }
164+
// }
165+
160166
if err != portaudio.InputOverflowed {
161167
return err
162168
}

input/portaudio/portaudio/cb.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

input/portaudio/portaudio/portaudio.go

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package portaudio
66

77
// #cgo pkg-config: portaudio-2.0
88
// #include <portaudio.h>
9-
// extern PaStreamCallback* paStreamCallback;
109
import "C"
1110

1211
import (
@@ -60,10 +59,6 @@ const (
6059
OutputUnderflowed Error = C.paOutputUnderflowed
6160
HostApiNotFound Error = C.paHostApiNotFound
6261
InvalidHostApi Error = C.paInvalidHostApi
63-
CanNotReadFromACallbackStream Error = C.paCanNotReadFromACallbackStream
64-
CanNotWriteToACallbackStream Error = C.paCanNotWriteToACallbackStream
65-
CanNotReadFromAnOutputOnlyStream Error = C.paCanNotReadFromAnOutputOnlyStream
66-
CanNotWriteToAnInputOnlyStream Error = C.paCanNotWriteToAnInputOnlyStream
6762
IncompatibleStreamHostApi Error = C.paIncompatibleStreamHostApi
6863
BadBufferPtr Error = C.paBadBufferPtr
6964
)
@@ -399,8 +394,6 @@ type Stream struct {
399394
paStream unsafe.Pointer
400395
inParams, outParams *C.PaStreamParameters
401396
in, out *reflect.SliceHeader
402-
timeInfo StreamCallbackTimeInfo
403-
flags StreamCallbackFlags
404397
args []reflect.Value
405398
callback reflect.Value
406399
closed bool
@@ -438,47 +431,6 @@ func delStream(s *Stream) {
438431
delete(streams, s.id)
439432
}
440433

441-
// StreamCallbackTimeInfo contains timing information for the
442-
// buffers passed to the stream callback.
443-
type StreamCallbackTimeInfo struct {
444-
InputBufferAdcTime, CurrentTime, OutputBufferDacTime time.Duration
445-
}
446-
447-
// StreamCallbackFlags are flag bit constants for the statusFlags to StreamCallback.
448-
type StreamCallbackFlags C.PaStreamCallbackFlags
449-
450-
// PortAudio stream callback flags.
451-
const (
452-
// In a stream opened with FramesPerBufferUnspecified,
453-
// InputUnderflow indicates that input data is all silence (zeros)
454-
// because no real data is available.
455-
//
456-
// In a stream opened without FramesPerBufferUnspecified,
457-
// InputUnderflow indicates that one or more zero samples have been inserted
458-
// into the input buffer to compensate for an input underflow.
459-
InputUnderflow StreamCallbackFlags = C.paInputUnderflow
460-
461-
// In a stream opened with FramesPerBufferUnspecified,
462-
// indicates that data prior to the first sample of the
463-
// input buffer was discarded due to an overflow, possibly
464-
// because the stream callback is using too much CPU time.
465-
//
466-
// Otherwise indicates that data prior to one or more samples
467-
// in the input buffer was discarded.
468-
InputOverflow StreamCallbackFlags = C.paInputOverflow
469-
470-
// Indicates that output data (or a gap) was inserted,
471-
// possibly because the stream callback is using too much CPU time.
472-
OutputUnderflow StreamCallbackFlags = C.paOutputUnderflow
473-
474-
// Indicates that output data will be discarded because no room is available.
475-
OutputOverflow StreamCallbackFlags = C.paOutputOverflow
476-
477-
// Some of all of the output data will be used to prime the stream,
478-
// input data may be zero.
479-
PrimingOutput StreamCallbackFlags = C.paPrimingOutput
480-
)
481-
482434
// OpenStream creates an instance of a Stream.
483435
//
484436
// For an input- or output-only stream, p.Output.Device or p.Input.Device must be nil, respectively.
@@ -494,11 +446,7 @@ func OpenStream(p StreamParameters, args ...interface{}) (*Stream, error) {
494446
delStream(s)
495447
return nil, err
496448
}
497-
cb := C.paStreamCallback
498-
if !s.callback.IsValid() {
499-
cb = nil
500-
}
501-
paErr := C.Pa_OpenStream(&s.paStream, s.inParams, nil, C.double(p.SampleRate), C.ulong(p.FramesPerBuffer), C.PaStreamFlags(p.Flags), cb, unsafe.Pointer(s.id))
449+
paErr := C.Pa_OpenStream(&s.paStream, s.inParams, nil, C.double(p.SampleRate), C.ulong(p.FramesPerBuffer), C.PaStreamFlags(p.Flags), nil, unsafe.Pointer(s.id))
502450
if paErr != C.paNoError {
503451
delStream(s)
504452
return nil, newError(paErr)
@@ -627,21 +575,6 @@ func (s *Stream) Start() error {
627575
return newError(C.Pa_StartStream(s.paStream))
628576
}
629577

630-
//export streamCallback
631-
func streamCallback(inputBuffer, outputBuffer unsafe.Pointer, frames C.ulong, timeInfo *C.PaStreamCallbackTimeInfo, statusFlags C.PaStreamCallbackFlags, userData unsafe.Pointer) {
632-
// defer func() {
633-
// // Don't let PortAudio silently swallow panics.
634-
// if x := recover(); x != nil {
635-
// buf := make([]byte, 1<<10)
636-
// for runtime.Stack(buf, true) == len(buf) {
637-
// buf = make([]byte, 2*len(buf))
638-
// }
639-
// fmt.Fprintf(os.Stderr, "panic in portaudio stream callback: %s\n\n%s", x, buf)
640-
// os.Exit(2)
641-
// }
642-
// }()
643-
}
644-
645578
// Stop terminates audio processing. It waits until all pending
646579
// audio buffers have been played before it returns.
647580
func (s *Stream) Stop() error {
@@ -702,12 +635,6 @@ func (s *Stream) AvailableToRead() (int, error) {
702635
// Read uses the buffer provided to OpenStream.
703636
// The number of samples to read is determined by the size of the buffer.
704637
func (s *Stream) Read() error {
705-
if s.callback.IsValid() {
706-
return CanNotReadFromACallbackStream
707-
}
708-
if s.in == nil {
709-
return CanNotReadFromAnOutputOnlyStream
710-
}
711638
buf, frames, err := getBuffer(s.in, s.inParams)
712639
if err != nil {
713640
return err

tavis.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ import (
1818
func Run(cfg Config) error {
1919
// DrawDelay is the time we wait between ticks to draw.
2020
var drawDelay = time.Second / time.Duration(
21-
int((cfg.SampleRate/float64(cfg.SampleSize))+1))
21+
int((cfg.SampleRate / float64(cfg.SampleSize))))
2222

2323
var audio, err = cfg.InputBackend.Start(input.SessionConfig{
2424
Device: cfg.InputDevice,
2525
FrameSize: cfg.ChannelCount,
2626
SampleSize: cfg.SampleSize,
2727
SampleRate: cfg.SampleRate,
2828
})
29+
defer cfg.InputBackend.Close()
2930

3031
if err != nil {
3132
return errors.Wrap(err, "failed to start the input backend")
@@ -54,9 +55,6 @@ func Run(cfg Config) error {
5455
display.SetBase(cfg.BaseThick)
5556
display.SetDrawType(graphic.DrawType(cfg.DrawType))
5657

57-
var timer = time.NewTimer(0)
58-
defer timer.Stop()
59-
6058
var endSig = make(chan os.Signal, 2)
6159
signal.Notify(endSig, os.Interrupt)
6260
signal.Notify(endSig, os.Kill)
@@ -82,12 +80,15 @@ func Run(cfg Config) error {
8280
}
8381
defer audio.Stop()
8482

83+
var timer = time.NewTimer(0)
84+
defer timer.Stop()
85+
8586
for {
8687
if audio.ReadyRead() < cfg.SampleSize {
8788
continue
8889
}
8990

90-
if err := audio.Read(ctx); err != nil {
91+
if err = audio.Read(ctx); err != nil {
9192
return errors.Wrap(err, "failed to read audio input")
9293
}
9394

@@ -97,8 +98,8 @@ func Run(cfg Config) error {
9798

9899
spectrum.Process(win)
99100

100-
if err := display.Draw(barBuffers, cfg.ChannelCount, barCount); err != nil {
101-
return errors.Wrap(err, "graphic threw an error. this should not happen.")
101+
if err = display.Draw(barBuffers, cfg.ChannelCount, barCount); err != nil {
102+
return errors.Wrap(err, "graphic threw error. this should not happen.")
102103
}
103104

104105
select {

0 commit comments

Comments
 (0)