Skip to content

Commit e16e20e

Browse files
committed
default device selection
1 parent 8ddfbe3 commit e16e20e

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

cmd/catnip/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/noriah/catnip/dsp"
77
"github.com/noriah/catnip/graphic"
8+
"github.com/noriah/catnip/input"
89
)
910

1011
// Config is a temporary struct to define parameters
@@ -58,6 +59,7 @@ type config struct {
5859
// - super smooth detail view
5960
func newZeroConfig() config {
6061
return config{
62+
backend: input.DefaultBackend(),
6163
sampleRate: 44100,
6264
sampleSize: 1024,
6365
smoothFactor: 74.15,

cmd/catnip/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,17 @@ func doFlags(cfg *config) bool {
157157

158158
switch {
159159
case listBackendsCmd.Used:
160+
defaultBackend := input.DefaultBackend()
161+
162+
fmt.Println("all backends. '*' marks default")
163+
160164
for _, backend := range input.Backends {
161-
fmt.Printf("- %s\n", backend.Name)
165+
star := ' '
166+
if defaultBackend == backend.Name {
167+
star = '*'
168+
}
169+
170+
fmt.Printf("- %s %c\n", backend.Name, star)
162171
}
163172

164173
return true

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ require (
1010
gonum.org/v1/gonum v0.11.0
1111
)
1212

13-
require github.com/mattn/go-runewidth v0.0.9 // indirect
13+
require (
14+
github.com/mattn/go-runewidth v0.0.9 // indirect
15+
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
88
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=
99
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1010
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
11-
golang.org/x/exp v0.0.0-20191002040644-a1355ae1e2c3 h1:n9HxLrNxWWtEb1cA950nuEEj3QnKbtsCJ6KjcgisNUs=
1211
gonum.org/v1/gonum v0.11.0 h1:f1IJhK4Km5tBJmaiJXtk/PkL4cdVX6J+tGiM187uT5E=
1312
gonum.org/v1/gonum v0.11.0/go.mod h1:fSG4YDCxxUZQJ7rKsQrj0gMOg00Il0Z96/qMA4bVQhA=

input/backend.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package input
22

33
import (
44
"fmt"
5+
"os/exec"
6+
"runtime"
57

68
"github.com/pkg/errors"
79
)
@@ -32,6 +34,55 @@ func RegisterBackend(name string, b Backend) {
3234
})
3335
}
3436

37+
// Get all installed backend names.
38+
func GetAllBackendNames() []string {
39+
out := make([]string, len(Backends))
40+
for i, backend := range Backends {
41+
out[i] = backend.Name
42+
}
43+
return out
44+
}
45+
46+
// Get the default backend depending on
47+
func DefaultBackend() string {
48+
switch runtime.GOOS {
49+
case "windows":
50+
if HasBackend("ffmpeg-dshow") {
51+
return "ffmpeg-dshow"
52+
}
53+
54+
case "darwin":
55+
if backend := FindBackend("portaudio"); backend != nil {
56+
if HasBackend("portaudio") {
57+
return "portaudio"
58+
}
59+
}
60+
61+
if HasBackend("ffmpeg-avfoundation") {
62+
return "ffmpeg-avfoundation"
63+
}
64+
65+
case "linux":
66+
if path, _ := exec.LookPath("pw-cat"); path != "" {
67+
if HasBackend("pipewire") {
68+
return "pipewire"
69+
}
70+
}
71+
72+
if path, _ := exec.LookPath("parec"); path != "" {
73+
if HasBackend("parec") {
74+
return "parec"
75+
}
76+
}
77+
78+
if HasBackend("ffmpeg-alsa") {
79+
return "ffmpeg-alsa"
80+
}
81+
}
82+
83+
return ""
84+
}
85+
3586
// FindBackend is a helper function that finds a backend. It returns nil if the
3687
// backend is not found.
3788
func FindBackend(name string) Backend {
@@ -43,6 +94,15 @@ func FindBackend(name string) Backend {
4394
return nil
4495
}
4596

97+
func HasBackend(name string) bool {
98+
for _, backend := range Backends {
99+
if backend.Name == name {
100+
return true
101+
}
102+
}
103+
return false
104+
}
105+
46106
func InitBackend(bknd string) (Backend, error) {
47107
backend := FindBackend(bknd)
48108
if backend == nil {

0 commit comments

Comments
 (0)