Skip to content

Commit

Permalink
rename to catnip
Browse files Browse the repository at this point in the history
change cli for flaggy
clean up code
  • Loading branch information
noriah committed Oct 30, 2020
1 parent d3c13fb commit 9f3eeb9
Show file tree
Hide file tree
Showing 21 changed files with 191 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.code-workspace
.DS_Store
/tavis
/catnip
.vscode/
40 changes: 13 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tavis
# catnip

[![love][withlove]][noriah-dev]
[![made-with-go][withgo]][go-dev]
Expand All @@ -16,7 +16,6 @@

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 @@ -31,7 +30,6 @@ expect lots of additions and changes at random times.

- go modules
- github.com/nsf/termbox-go
- github.com/urfave/cli/v2
- github.com/pkg/errors
- github.com/lawl/pulseaudio
- gonum.org/v1/gonum
Expand All @@ -50,47 +48,35 @@ expect lots of additions and changes at random times.

```sh
# with cgo (fftw, portaudio)
go get github.com/noriah/tavis
go get github.com/noriah/catnip
# without cgo
CGO_ENABLED=0 go get github.com/noriah/tavis
CGO_ENABLED=0 go get github.com/noriah/catnip
```

### with `git`

```sh
# get source
git clone https://github.com/noriah/tavis.git
git clone https://github.com/noriah/catnip.git

# cd to source
cd tavis
cd catnip

# build and install tavis
# build and install catnip
go install
# without cgo
CGO_ENABLED=0 go install
```

## usage

```sh
NAME:
tavis - terminal audio visualizer

USAGE:
tavis [global options] command [command options] [arguments...]

COMMANDS:
list-backends
list-devices
help, h Shows a list of commands or help for one command

```
- use `catnip list-backends` to show available backends
- use `catnip -b {backend} list-devices` to show available devices
- use `catnip -b {backend} -d {device}` to run - use the full device name
- use `catnip -h` for information on several more customizations
## faq

- use `tavis list-backends` to show available backends
- use `tavis -b {backend} list-devices` to show available devices
- use `tavis -b {backend} -d {device}` to run - use the full device name
- use `tavis -h` for information on several more customizations
### catnip?

- noriah/catnip@98f989fd45bef8706cbc5c90422209600943ebc1

<!-- Links -->
[noriah-dev]: https://noriah.dev
Expand Down
68 changes: 59 additions & 9 deletions tavis.go → catnip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,44 @@ package main

import (
"context"
"fmt"
"os"
"os/signal"
"time"

"github.com/noriah/tavis/dsp"
"github.com/noriah/tavis/dsp/window"
"github.com/noriah/tavis/graphic"
"github.com/noriah/tavis/input"
"github.com/noriah/catnip/dsp"
"github.com/noriah/catnip/dsp/window"
"github.com/noriah/catnip/graphic"
"github.com/noriah/catnip/input"

"github.com/pkg/errors"
)

// Run starts to draw the visualizer on the tcell Screen.
func Run(cfg Config) error {
// Catnip starts to draw the visualizer on the termbox screen.
func Catnip(cfg *Config) error {

// DrawDelay is the time we wait between ticks to draw.
var drawDelay = time.Second / time.Duration(
int((cfg.SampleRate / float64(cfg.SampleSize))))

var audio, err = cfg.InputBackend.Start(input.SessionConfig{
Device: cfg.InputDevice,
var backend, err = initBackend(cfg)
if err != nil {
return err
}

device, err := initDevice(backend, cfg)

if err != nil {
return err
}

audio, err := backend.Start(input.SessionConfig{
Device: device,
FrameSize: cfg.ChannelCount,
SampleSize: cfg.SampleSize,
SampleRate: cfg.SampleRate,
})
defer cfg.InputBackend.Close()
defer backend.Close()

if err != nil {
return errors.Wrap(err, "failed to start the input backend")
Expand Down Expand Up @@ -109,3 +122,40 @@ func Run(cfg Config) error {
}
}
}

func initBackend(cfg *Config) (input.Backend, error) {

var backend = input.FindBackend(cfg.Backend)
if backend == nil {
return nil, fmt.Errorf("backend not found: %q", cfg.Backend)
}

if err := backend.Init(); err != nil {
return nil, errors.Wrap(err, "failed to initialize input backend")
}

return backend, nil
}

func initDevice(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)
}
20 changes: 10 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package main
import (
"errors"

"github.com/noriah/tavis/dsp"
"github.com/noriah/tavis/graphic"
"github.com/noriah/tavis/input"
"github.com/noriah/catnip/dsp"
"github.com/noriah/catnip/graphic"
)

// Config is a temporary struct to define parameters
type Config struct {
// InputBackend is the backend that the input belongs to
InputBackend input.Backend
// InputDevice is the device we want to listen to
InputDevice input.Device
// Backend is the backend name from list-backends
Backend string
// Device is the device name from list-devices
Device string
// SampleRate is the rate at which samples are read
SampleRate float64
//LoCutFrqq is the low end of our audio spectrum
Expand All @@ -30,7 +29,7 @@ type Config struct {
BarWidth int
// SpaceWidth is the width of spaces, in columns
SpaceWidth int
// SampleSize is how much we draw. Play with it
// SampleSiz is how much we draw. Play with it
SampleSize int
// ChannelCount is the number of channels we want to look at. DO NOT TOUCH
ChannelCount int
Expand All @@ -47,8 +46,9 @@ type Config struct {
// - sampleRate: 122880
// - sampleSize: 2048
// - super smooth detail view
func NewZeroConfig() Config {
return Config{
func NewZeroConfig() *Config {
return &Config{
Backend: "portaudio",
SampleRate: 44100,
SmoothFactor: 50.69,
WinVar: 0.50,
Expand Down
9 changes: 5 additions & 4 deletions dsp/spectrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"math"
"math/cmplx"

"github.com/noriah/tavis/dsp/window"
"github.com/noriah/tavis/fft"
"github.com/noriah/catnip/dsp/window"
"github.com/noriah/catnip/fft"
)

// SpectrumType is the type of calculation we run
Expand Down Expand Up @@ -149,7 +149,8 @@ func (sp *Spectrum) Process(win window.Function) {
var mag = 0.0

var xF = sp.bins[xB].floorFFT
for xF < sp.bins[xB].ceilFFT && xF < sp.fftSize {
var lF = sp.bins[xB].ceilFFT
for xF < lF && xF < sp.fftSize {
if power := cmplx.Abs(sp.fftBuf[xF]); mag < power {
mag = power
}
Expand All @@ -165,7 +166,7 @@ func (sp *Spectrum) Process(win window.Function) {
case mag < 0.0:
mag = 0.0

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

}
Expand Down
4 changes: 2 additions & 2 deletions fft/fftw.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package fft

// This only included bindings are those that are needed by tavis.
// This only included bindings are those that are needed by catnip.
// This includes the use of `fftw_plan_dft_r2c_2d`.
// It is the only fftw plan we need, and the only one we have chosen to
// implement here.
Expand All @@ -16,7 +16,7 @@ import (
"unsafe"
)

// FFTW is true if Tavis is built with cgo.
// FFTW is true if Catnip is built with cgo.
const FFTW = true

// Plan holds an FFTW C plan
Expand Down
2 changes: 1 addition & 1 deletion fft/gonum.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package fft

import "gonum.org/v1/gonum/dsp/fourier"

// FFTW is false if Tavis is not built with cgo. It will use gonum instead.
// FFTW is false if Catnip is not built with cgo. It will use gonum instead.
const FFTW = false

// Plan holds a gonum FFT plan.
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/noriah/tavis
module github.com/noriah/catnip

go 1.15

require (
github.com/integrii/flaggy v1.4.4
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1
github.com/pkg/errors v0.9.1
github.com/urfave/cli/v2 v2.2.0
gonum.org/v1/gonum v0.8.1
)
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/integrii/flaggy v1.4.4 h1:8fGyiC14o0kxhTqm2VBoN19fDKPZsKipP7yggreTMDc=
github.com/integrii/flaggy v1.4.4/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0 h1:JrvOwrr1teFiqsp0EQxgEPJsm0pet+YLTL+HdYmnMx0=
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
Expand All @@ -14,14 +12,6 @@ github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1 h1:lh3PyZvY+B9nFliS
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1/go.mod h1:IuKpRQcYE1Tfu+oAQqaLisqDeXgjyyltCfsaoYN18NQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4=
github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2 h1:y102fOLFqhV41b+4GPiJoa0k/x+pJcEi2/HB1Y5T6fU=
Expand All @@ -35,6 +25,4 @@ gonum.org/v1/gonum v0.8.1/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0=
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0 h1:OE9mWmgKkjJyEmDAAtGMPjXu+YNeGvK9VTSHY6+Qihc=
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
19 changes: 10 additions & 9 deletions graphic/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math"
"sync/atomic"

"github.com/noriah/tavis/util"
"github.com/noriah/catnip/util"

"github.com/nsf/termbox-go"
)
Expand Down Expand Up @@ -80,8 +80,8 @@ type Display struct {
// something to think about
func NewDisplay(hz float64, samples int) *Display {

slowMax := int((ScalingSlowWindow*hz)/float64(samples)) * 2
fastMax := int((ScalingFastWindow*hz)/float64(samples)) * 2
slowMax := (int(ScalingSlowWindow*hz) / samples) * 2
fastMax := (int(ScalingFastWindow*hz) / samples) * 2

return &Display{
cfg: Config{
Expand Down Expand Up @@ -163,12 +163,6 @@ func eventPoller(ctx context.Context, fn context.CancelFunc, d *Display) {
defer atomic.StoreUint32(&d.running, 0)

for {
// first check if we need to exit
select {
case <-ctx.Done():
return
default:
}

var ev = termbox.PollEvent()

Expand Down Expand Up @@ -218,6 +212,13 @@ func eventPoller(ctx context.Context, fn context.CancelFunc, d *Display) {

} // switch ev.Type

// check if we need to exit
select {
case <-ctx.Done():
return
default:
}

} // for

}
Expand Down
2 changes: 1 addition & 1 deletion input/common/execread/execread.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os/exec"
"sync"

"github.com/noriah/tavis/input"
"github.com/noriah/catnip/input"
"github.com/pkg/errors"
)

Expand Down
2 changes: 1 addition & 1 deletion input/ffmpeg/alsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"strings"

"github.com/noriah/tavis/input"
"github.com/noriah/catnip/input"
"github.com/pkg/errors"
)

Expand Down
Loading

0 comments on commit 9f3eeb9

Please sign in to comment.