Skip to content

Commit

Permalink
my cat still tries to lick my speakers
Browse files Browse the repository at this point in the history
  • Loading branch information
noriah committed Oct 29, 2020
1 parent 7c3245b commit d2b559a
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 133 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
tavis
===
# tavis

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

## early development - expect things to change and break

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.
this project is still in the early stages of development.
roadmaps and milestones are not currently priorities.

expect lots of additions and changes at random times.

*windows needs work

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

- binaries (optional)
- ffmpeg
- parec
- binaries
- ffmpeg (required for FFmpeg backends)
- parec (required for PulseAudio backend with parec)

## installation

Expand Down
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Config struct {
func NewZeroConfig() Config {
return Config{
SampleRate: 44100,
SmoothFactor: 19.69,
SmoothFactor: 50.69,
WinVar: 0.50,
BaseThick: 1,
BarWidth: 2,
Expand Down Expand Up @@ -91,9 +91,9 @@ func sanitizeConfig(cfg *Config) error {
}

switch {
case cfg.SmoothFactor > 100.0:
cfg.SmoothFactor = 1.0
case cfg.SmoothFactor <= 0.0:
case cfg.SmoothFactor > 99.99:
cfg.SmoothFactor = 0.9999
case cfg.SmoothFactor < 0.00001:
cfg.SmoothFactor = 0.00001
default:
cfg.SmoothFactor /= 100.0
Expand Down
95 changes: 49 additions & 46 deletions dsp/spectrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,24 @@ const (
SpectrumDefault = SpectrumLog
)

// misc coonstants
const (
MaxStreams = 2
)

// Spectrum is an audio spectrum in a buffer
type Spectrum struct {
numBins int // number of bins we look at
fftSize int // number of fft bins
sampleSize int // number of samples per slice
sType SpectrumType // the type of spectrum distribution
sampleRate float64 // audio sample rate
winVar float64 // window variable
smoothFactor float64 // smothing factor
fftBuf []complex128 // fft return buffer
bins []bin // bins for processing
streams []*stream // streams of data
numBins int // number of bins we look at
numStreams int // number of streams we process
fftSize int // number of fft bins
sampleSize int // number of samples per slice
sType SpectrumType // the type of spectrum distribution
sampleRate float64 // audio sample rate
winVar float64 // window variable
smoothFactor float64 // smothing factor
fftBuf []complex128 // fft return buffer
bins []bin // bins for processing
streams [MaxStreams]stream // streams of data
}

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

var fftSize = (size / 2) + 1

var sp = &Spectrum{
return &Spectrum{
fftSize: fftSize,
sampleSize: size,
sampleRate: hz,
smoothFactor: 0.1969,
smoothFactor: 0.5069,
winVar: 0.5,
fftBuf: make([]complex128, fftSize),
bins: make([]bin, size+1),
streams: make([]*stream, 0, 2),
}

return sp
}

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

// AddStream adds an input buffer to the spectrum
func (sp *Spectrum) AddStream(input []float64) {
var s = &stream{
input: input,
buf: make([]float64, sp.sampleSize),
pBuf: make([]float64, sp.sampleSize),
plan: fft.NewPlan(input, sp.fftBuf),
func (sp *Spectrum) AddStream(input ...[]float64) {

for idx := range input {
sp.streams[idx].input = input[idx]
sp.streams[idx].buf = make([]float64, sp.sampleSize)
sp.streams[idx].pBuf = make([]float64, sp.sampleSize)
sp.streams[idx].plan = fft.NewPlan(input[idx], sp.fftBuf)
sp.numStreams++
}

sp.streams = append(sp.streams, s)
}

// BinBuffers returns our bin buffers
func (sp *Spectrum) BinBuffers() [][]float64 {
var buf = make([][]float64, len(sp.streams))
for idx, stream := range sp.streams {
buf[idx] = stream.buf
var buf = make([][]float64, sp.numStreams)
for idx := range sp.streams[:sp.numStreams] {
buf[idx] = sp.streams[idx].buf
}

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

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

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

var bassCut = sp.freqToIdx(Frequencies[2], math.Ceil)
var bassCut = sp.freqToIdx(Frequencies[2], math.Floor)
var fBassCut = float64(bassCut)

for _, stream := range sp.streams {
for idx := range sp.streams {

win(stream.input)
win(sp.streams[idx].input)

stream.plan.Execute()
sp.streams[idx].plan.Execute()

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

case sp.bins[xB].floorFFT < bassCut:
pow *= math.Max(0.5, float64(xF)/fBassCut)

}

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

mag *= (1.0 - sf)
mag += stream.pBuf[xB] * sf
stream.pBuf[xB] = mag
stream.buf[xB] = mag
mag += sp.streams[idx].pBuf[xB] * sf
sp.streams[idx].pBuf[xB] = mag
sp.streams[idx].buf[xB] = mag

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

var loLog = math.Log10(lo)
var hiLog = math.Log10(hi)
// var loLog = math.Log10(lo)
// var hiLog = math.Log10(hi)

var cF = (hiLog - loLog) / float64(bins)

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

var cCoef = 100.0 / float64(bins+1)

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

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

if xB > 0 {
Expand Down
15 changes: 1 addition & 14 deletions fft/fftw.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,14 @@ func (p *Plan) destroy() {
C.fftw_destroy_plan(p.cPlan)
}

// func AllocateReal(real int) []float64 {
// var r = C.fftw_alloc_real(C.ulong(real))

// var rSlice []float64

// var sliceHead = (*reflect.SliceHeader)(unsafe.Pointer(&rSlice))
// sliceHead.Cap = real
// sliceHead.Len = real
// sliceHead.Data = uintptr(unsafe.Pointer(&r))

// return rSlice
// }

// NewPlan returns a new FFTW Plan for use with FFTW
func NewPlan(in []float64, out []complex128) *Plan {
var plan = &Plan{
input: in,
output: out,
cPlan: C.fftw_plan_dft_r2c_1d(
C.int(len(in)),
(*C.double)(&in[0]),
(*C.double)(unsafe.Pointer(&in[0])),
(*C.fftw_complex)(unsafe.Pointer(&out[0])),
C.FFTW_MEASURE,
),
Expand Down
14 changes: 6 additions & 8 deletions graphic/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const (
// ScalingDumpPercent is how much we erase on rescale
ScalingDumpPercent = 0.75
// ScalingResetDeviation standard deviations from the mean before reset
ScalingResetDeviation = 1
ScalingResetDeviation = 1.0
)

// DrawType is the type
Expand Down Expand Up @@ -71,8 +71,8 @@ type Config struct {
type Display struct {
running uint32
cfg Config
slowWindow *util.MovingWindow
fastWindow *util.MovingWindow
slowWindow util.MovingWindow
fastWindow util.MovingWindow
}

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

var d = &Display{
slowWindow: util.NewMovingWindow(slowMax),
fastWindow: util.NewMovingWindow(fastMax),
return &Display{
cfg: Config{
BarWidth: 2,
SpaceWidth: 1,
BinWidth: 3,
BaseThick: 1,
DrawType: DrawDefault,
},
slowWindow: util.NewMovingWindow(slowMax),
fastWindow: util.NewMovingWindow(fastMax),
}

return d
}

// Draw takes data and draws
Expand Down
5 changes: 1 addition & 4 deletions tavis.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ func Run(cfg Config) error {
spectrum.SetSmoothing(cfg.SmoothFactor)
spectrum.SetWinVar(cfg.WinVar)
spectrum.SetType(dsp.SpectrumType(cfg.SpectrumType))

for ch := 0; ch < cfg.ChannelCount; ch++ {
spectrum.AddStream(audio.SampleBuffers()[ch])
}
spectrum.AddStream(audio.SampleBuffers()...)

var barBuffers = spectrum.BinBuffers()

Expand Down
Loading

0 comments on commit d2b559a

Please sign in to comment.