Skip to content

Commit 9f3eeb9

Browse files
committed
rename to catnip
change cli for flaggy clean up code
1 parent d3c13fb commit 9f3eeb9

21 files changed

+191
-260
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
*.code-workspace
22
.DS_Store
3-
/tavis
3+
/catnip
44
.vscode/

README.md

+13-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# tavis
1+
# catnip
22

33
[![love][withlove]][noriah-dev]
44
[![made-with-go][withgo]][go-dev]
@@ -16,7 +16,6 @@
1616

1717
this project is still in the early stages of development.
1818
roadmaps and milestones are not currently priorities.
19-
2019
expect lots of additions and changes at random times.
2120

2221
*windows needs work
@@ -31,7 +30,6 @@ expect lots of additions and changes at random times.
3130

3231
- go modules
3332
- github.com/nsf/termbox-go
34-
- github.com/urfave/cli/v2
3533
- github.com/pkg/errors
3634
- github.com/lawl/pulseaudio
3735
- gonum.org/v1/gonum
@@ -50,47 +48,35 @@ expect lots of additions and changes at random times.
5048

5149
```sh
5250
# with cgo (fftw, portaudio)
53-
go get github.com/noriah/tavis
51+
go get github.com/noriah/catnip
5452
# without cgo
55-
CGO_ENABLED=0 go get github.com/noriah/tavis
53+
CGO_ENABLED=0 go get github.com/noriah/catnip
5654
```
5755

5856
### with `git`
5957

6058
```sh
6159
# get source
62-
git clone https://github.com/noriah/tavis.git
60+
git clone https://github.com/noriah/catnip.git
6361

6462
# cd to source
65-
cd tavis
63+
cd catnip
6664

67-
# build and install tavis
65+
# build and install catnip
6866
go install
6967
# without cgo
7068
CGO_ENABLED=0 go install
7169
```
7270

73-
## usage
74-
75-
```sh
76-
NAME:
77-
tavis - terminal audio visualizer
78-
79-
USAGE:
80-
tavis [global options] command [command options] [arguments...]
81-
82-
COMMANDS:
83-
list-backends
84-
list-devices
85-
help, h Shows a list of commands or help for one command
86-
87-
```
71+
- use `catnip list-backends` to show available backends
72+
- use `catnip -b {backend} list-devices` to show available devices
73+
- use `catnip -b {backend} -d {device}` to run - use the full device name
74+
- use `catnip -h` for information on several more customizations
75+
## faq
8876

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

79+
- noriah/catnip@98f989fd45bef8706cbc5c90422209600943ebc1
9480

9581
<!-- Links -->
9682
[noriah-dev]: https://noriah.dev

tavis.go catnip.go

+59-9
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,44 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"os/signal"
78
"time"
89

9-
"github.com/noriah/tavis/dsp"
10-
"github.com/noriah/tavis/dsp/window"
11-
"github.com/noriah/tavis/graphic"
12-
"github.com/noriah/tavis/input"
10+
"github.com/noriah/catnip/dsp"
11+
"github.com/noriah/catnip/dsp/window"
12+
"github.com/noriah/catnip/graphic"
13+
"github.com/noriah/catnip/input"
1314

1415
"github.com/pkg/errors"
1516
)
1617

17-
// Run starts to draw the visualizer on the tcell Screen.
18-
func Run(cfg Config) error {
18+
// Catnip starts to draw the visualizer on the termbox screen.
19+
func Catnip(cfg *Config) error {
20+
1921
// DrawDelay is the time we wait between ticks to draw.
2022
var drawDelay = time.Second / time.Duration(
2123
int((cfg.SampleRate / float64(cfg.SampleSize))))
2224

23-
var audio, err = cfg.InputBackend.Start(input.SessionConfig{
24-
Device: cfg.InputDevice,
25+
var backend, err = initBackend(cfg)
26+
if err != nil {
27+
return err
28+
}
29+
30+
device, err := initDevice(backend, cfg)
31+
32+
if err != nil {
33+
return err
34+
}
35+
36+
audio, err := backend.Start(input.SessionConfig{
37+
Device: device,
2538
FrameSize: cfg.ChannelCount,
2639
SampleSize: cfg.SampleSize,
2740
SampleRate: cfg.SampleRate,
2841
})
29-
defer cfg.InputBackend.Close()
42+
defer backend.Close()
3043

3144
if err != nil {
3245
return errors.Wrap(err, "failed to start the input backend")
@@ -109,3 +122,40 @@ func Run(cfg Config) error {
109122
}
110123
}
111124
}
125+
126+
func initBackend(cfg *Config) (input.Backend, error) {
127+
128+
var backend = input.FindBackend(cfg.Backend)
129+
if backend == nil {
130+
return nil, fmt.Errorf("backend not found: %q", cfg.Backend)
131+
}
132+
133+
if err := backend.Init(); err != nil {
134+
return nil, errors.Wrap(err, "failed to initialize input backend")
135+
}
136+
137+
return backend, nil
138+
}
139+
140+
func initDevice(backend input.Backend, cfg *Config) (input.Device, error) {
141+
if cfg.Device == "" {
142+
var def, err = backend.DefaultDevice()
143+
if err != nil {
144+
return nil, errors.Wrap(err, "failed to get default device")
145+
}
146+
return def, nil
147+
}
148+
149+
var devices, err = backend.Devices()
150+
if err != nil {
151+
return nil, errors.Wrap(err, "failed to get devices")
152+
}
153+
154+
for idx := range devices {
155+
if devices[idx].String() == cfg.Device {
156+
return devices[idx], nil
157+
}
158+
}
159+
160+
return nil, errors.Errorf("device %q not found; check list-devices", cfg.Device)
161+
}

config.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package main
33
import (
44
"errors"
55

6-
"github.com/noriah/tavis/dsp"
7-
"github.com/noriah/tavis/graphic"
8-
"github.com/noriah/tavis/input"
6+
"github.com/noriah/catnip/dsp"
7+
"github.com/noriah/catnip/graphic"
98
)
109

1110
// Config is a temporary struct to define parameters
1211
type Config struct {
13-
// InputBackend is the backend that the input belongs to
14-
InputBackend input.Backend
15-
// InputDevice is the device we want to listen to
16-
InputDevice input.Device
12+
// Backend is the backend name from list-backends
13+
Backend string
14+
// Device is the device name from list-devices
15+
Device string
1716
// SampleRate is the rate at which samples are read
1817
SampleRate float64
1918
//LoCutFrqq is the low end of our audio spectrum
@@ -30,7 +29,7 @@ type Config struct {
3029
BarWidth int
3130
// SpaceWidth is the width of spaces, in columns
3231
SpaceWidth int
33-
// SampleSize is how much we draw. Play with it
32+
// SampleSiz is how much we draw. Play with it
3433
SampleSize int
3534
// ChannelCount is the number of channels we want to look at. DO NOT TOUCH
3635
ChannelCount int
@@ -47,8 +46,9 @@ type Config struct {
4746
// - sampleRate: 122880
4847
// - sampleSize: 2048
4948
// - super smooth detail view
50-
func NewZeroConfig() Config {
51-
return Config{
49+
func NewZeroConfig() *Config {
50+
return &Config{
51+
Backend: "portaudio",
5252
SampleRate: 44100,
5353
SmoothFactor: 50.69,
5454
WinVar: 0.50,

dsp/spectrum.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515
"math"
1616
"math/cmplx"
1717

18-
"github.com/noriah/tavis/dsp/window"
19-
"github.com/noriah/tavis/fft"
18+
"github.com/noriah/catnip/dsp/window"
19+
"github.com/noriah/catnip/fft"
2020
)
2121

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

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

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

171172
}

fft/fftw.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
package fft
44

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

19-
// FFTW is true if Tavis is built with cgo.
19+
// FFTW is true if Catnip is built with cgo.
2020
const FFTW = true
2121

2222
// Plan holds an FFTW C plan

fft/gonum.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package fft
44

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

7-
// FFTW is false if Tavis is not built with cgo. It will use gonum instead.
7+
// FFTW is false if Catnip is not built with cgo. It will use gonum instead.
88
const FFTW = false
99

1010
// Plan holds a gonum FFT plan.

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
module github.com/noriah/tavis
1+
module github.com/noriah/catnip
22

33
go 1.15
44

55
require (
6+
github.com/integrii/flaggy v1.4.4
67
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0
78
github.com/mattn/go-runewidth v0.0.9 // indirect
89
github.com/nsf/termbox-go v0.0.0-20200418040025-38ba6e5628f1
910
github.com/pkg/errors v0.9.1
10-
github.com/urfave/cli/v2 v2.2.0
1111
gonum.org/v1/gonum v0.8.1
1212
)

go.sum

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

graphic/display.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"math"
66
"sync/atomic"
77

8-
"github.com/noriah/tavis/util"
8+
"github.com/noriah/catnip/util"
99

1010
"github.com/nsf/termbox-go"
1111
)
@@ -80,8 +80,8 @@ type Display struct {
8080
// something to think about
8181
func NewDisplay(hz float64, samples int) *Display {
8282

83-
slowMax := int((ScalingSlowWindow*hz)/float64(samples)) * 2
84-
fastMax := int((ScalingFastWindow*hz)/float64(samples)) * 2
83+
slowMax := (int(ScalingSlowWindow*hz) / samples) * 2
84+
fastMax := (int(ScalingFastWindow*hz) / samples) * 2
8585

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

165165
for {
166-
// first check if we need to exit
167-
select {
168-
case <-ctx.Done():
169-
return
170-
default:
171-
}
172166

173167
var ev = termbox.PollEvent()
174168

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

219213
} // switch ev.Type
220214

215+
// check if we need to exit
216+
select {
217+
case <-ctx.Done():
218+
return
219+
default:
220+
}
221+
221222
} // for
222223

223224
}

input/common/execread/execread.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"os/exec"
1212
"sync"
1313

14-
"github.com/noriah/tavis/input"
14+
"github.com/noriah/catnip/input"
1515
"github.com/pkg/errors"
1616
)
1717

input/ffmpeg/alsa.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77
"strings"
88

9-
"github.com/noriah/tavis/input"
9+
"github.com/noriah/catnip/input"
1010
"github.com/pkg/errors"
1111
)
1212

0 commit comments

Comments
 (0)