-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbeatbox.go
170 lines (157 loc) · 3.1 KB
/
beatbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"context"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/anisse/librespot-golang/src/librespot/core"
)
const (
TAGLEN = 16
EMPTY = "00000000000000000000000000000000"
SHUTDOWN = "01050000000000000000000000000000"
STOPTAG = "020200000"
NEXTDELAY = 5 * time.Second
DATADIR = "/perm/beatbox-data/"
)
type player struct {
tag string
list []string
index int
stopped time.Time
spotify bool
session *core.Session
stop context.CancelFunc
w chan error
}
func (p *player) sessionCheck() error {
if p.session == nil {
session, err := openSession()
if err != nil {
return err
}
p.session = session
}
return nil
}
func (p *player) play() context.CancelFunc {
ctx, stop := context.WithCancel(context.Background())
fmt.Println("Now playing:", p.list[p.index])
go func() {
if p.spotify {
if err := p.sessionCheck(); err != nil {
p.w <- err
return
}
p.w <- playTrack(ctx, p.session, p.list[p.index])
} else {
p.w <- playMp3(ctx, p.list[p.index])
}
}()
return stop
}
func newPlayer() (*player, error) {
return &player{
stop: func() {},
w: make(chan error),
}, nil
}
func (p *player) process(s string) {
switch {
case s == SHUTDOWN:
p.stop()
case strings.HasPrefix(s, STOPTAG):
p.stopped = time.Now()
p.stop()
case s == p.tag && time.Since(p.stopped) < NEXTDELAY &&
len(p.list) > p.index+1:
//next
p.index++
p.stop()
p.stop = p.play()
default:
list, err := filepath.Glob(DATADIR + s + "/*.mp3")
if err != nil || len(list) == 0 {
// try with spotify
playlist, err := ioutil.ReadFile(DATADIR + s)
if err == nil && len(playlist) > 0 {
if err := p.sessionCheck(); err != nil {
fmt.Println("Unknown playlist/track and no spotify session:",
err.Error(), string(playlist), s)
return
}
list, err = playlistTracks(p.session, string(playlist))
if err != nil {
fmt.Println("Unknown playlist/track:",
err.Error(), string(playlist), s)
return
}
p.spotify = true
} else {
fmt.Println("Unknown tag/command:", s)
return
}
} else {
p.spotify = false
}
p.index = 0 //first
p.tag = s
p.list = list
p.stop()
p.stop = p.play()
}
}
func (p *player) trackFinished() {
//next
if len(p.list) > p.index+1 {
p.index++
p.stop = p.play()
}
}
func (p *player) run(c <-chan string) {
for {
select {
case s := <-c:
p.process(s)
case err := <-p.w:
if err != nil {
if err.Error() != "context canceled" { //this is us doing the cancellations
fmt.Println("Error playing ", err)
}
continue
}
p.trackFinished()
}
}
}
func main() {
f, err := os.Open("/dev/hidraw0")
if err != nil {
fmt.Println("Error opening HID device", err)
return
}
p, err := newPlayer()
if err != nil {
fmt.Println("Error creating player", err)
return
}
c := make(chan string)
go p.run(c)
for {
t := make([]byte, TAGLEN)
_, err := io.ReadAtLeast(f, t, TAGLEN)
if err != nil {
fmt.Println("Error reading", err)
return
}
s := hex.EncodeToString(t)
if s != EMPTY {
c <- s
}
}
}