-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.go
261 lines (223 loc) · 5.03 KB
/
audio.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
// This module is responsible for driving the audio
// output of this project.
//
// The audio portion of this project requires that
// files be converted to aiff files in 2 channel,
// 44100 Hz, pcm_s16le (16 Bit Signed Little Endian).
//
// The conversion from ogg format files to this format
// can be done using the libav-tools package installed
// using "sudo apt-get install libav-tools". The
// conversion is done using a command line such as,
// "avconv -i assets/sounds/e-ambient.ogg -ar 44100 -ac 2 -acodec pcm_s16le assets/sounds/e-ambient.aiff".
//
// Playback using the same tools for testing purposes
// can be done using
// "aplay -D plug:dmix -f S16_LE -c 2 -r 44100 assets/sounds/e-ambient.aiff"
import (
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"sync"
"time"
"github.com/cvanderschuere/alsa-go"
)
var (
audioDir = flag.String("audioDir", "assets/sounds", "The directory in which the audio aiff formatted event files can be found")
)
func initAudio(ambientC <-chan string, sfxC <-chan []string, quitC <-chan bool) (err error) {
go runAudio(ambientC, sfxC, quitC)
return nil
}
type effects struct {
wakeup chan bool
sfxs []string
sync.Mutex
}
var (
sfxs = effects{
wakeup: make(chan bool, 1),
sfxs: []string{},
}
)
func playSFX(quitC <-chan bool) {
//Open ALSA pipe
controlC := make(chan bool)
//Create stream
streamC := alsa.Init(controlC)
stream := alsa.AudioStream{Channels: 2,
Rate: int(44100),
SampleFormat: alsa.INT16_TYPE,
DataStream: make(chan alsa.AudioData, 100),
}
streamC <- stream
defer func() {
sfxs.Lock()
close(sfxs.wakeup)
sfxs.Unlock()
}()
sfxs.Lock()
wakeup := sfxs.wakeup
sfxs.Unlock()
for {
for {
fp := ""
sfxs.Lock()
if len(sfxs.sfxs) != 0 {
fp = sfxs.sfxs[0]
sfxs.sfxs = sfxs.sfxs[1:]
}
sfxs.Unlock()
if len(fp) == 0 {
break
}
logW.Debug(fmt.Sprintf("playing %s", fp))
func() {
file, err := os.Open(fp)
if err != nil {
logW.Warn(fmt.Sprintf("sfx file %s open failed due to %s", fp, err.Error()))
return
}
defer file.Close()
data := make([]byte, 8192)
for {
data = data[:cap(data)]
n, err := file.Read(data)
if err != nil {
if err == io.EOF {
return
}
logW.Warn(err.Error())
return
}
data = data[:n]
select {
case stream.DataStream <- append([]byte(nil), data[:n]...):
case <-quitC:
return
}
}
}()
}
select {
case <-wakeup:
case <-time.After(time.Second):
case <-quitC:
return
}
}
}
// Sounds possible at this point
//
// e-ambient, r-ambient, n-ambient
//
// e-capture, r-capture, n-capture
// e-loss, r-loss, n-loss
// e-resonator-deployed, r-resonator-deployed
// e-resonator-destroyed, r-resonator-destroyed
func runAudio(ambientC <-chan string, sfxC <-chan []string, quitC <-chan bool) {
go playAmbient(ambientC, quitC)
go playSFX(quitC)
for {
select {
case fns := <-sfxC:
if len(fns) != 0 {
sfxs.Lock()
for _, fn := range fns {
sfxs.sfxs = append(sfxs.sfxs, filepath.Join(*audioDir, fn+".aiff"))
}
// Wait a maximum of three seconds to wake up the audio
// player for sound effects
select {
case sfxs.wakeup <- true:
case <-time.After(3 * time.Second):
}
sfxs.Unlock()
}
case <-quitC:
return
}
}
}
type ambientFP struct {
fp string
file *os.File
sync.Mutex
}
func playAmbient(ambientC <-chan string, quitC <-chan bool) {
ambient := ambientFP{}
go func() {
for {
select {
case fn := <-ambientC:
ambient.Lock()
ambient.fp = filepath.Join(*audioDir, fn)
ambient.fp += ".aiff"
ambient.Unlock()
case <-quitC:
return
}
}
}()
//Open ALSA pipe
controlC := make(chan bool)
//Create stream
streamC := alsa.Init(controlC)
stream := alsa.AudioStream{Channels: 2,
Rate: int(44100),
SampleFormat: alsa.INT16_TYPE,
DataStream: make(chan alsa.AudioData, 100),
}
streamC <- stream
data := make([]byte, 8192)
func() {
fp := ""
err := errors.New("")
for {
ambient.Lock()
if fp != ambient.fp {
if ambient.file != nil {
logW.Debug(fmt.Sprintf("playback of %s stopped", fp))
ambient.file.Close()
ambient.file = nil
}
if ambient.fp != "" {
if ambient.file, err = os.Open(ambient.fp); err != nil {
logW.Warn(fmt.Sprintf("ambient file %s open failed due to %s, clearing request", ambient.fp, err.Error()))
ambient.fp = ""
continue
}
fp = ambient.fp
logW.Debug(fmt.Sprintf("playback of %s starting", ambient.fp))
}
}
ambient.Unlock()
if ambient.file == nil {
select {
case <-time.After(250 * time.Millisecond):
}
continue
}
data = data[:cap(data)]
n, err := ambient.file.Read(data)
if err != nil {
if err == io.EOF {
ambient.file.Seek(0, 0)
logW.Trace(fmt.Sprintf("rewound %s", fp))
continue
}
logW.Warn(err.Error())
continue
}
select {
case stream.DataStream <- append([]byte(nil), data[:n]...):
case <-quitC:
return
}
}
}()
}