Repository navigation
Expand file tree
/
Copy pathaudio.go
More file actions
246 lines (215 loc) · 5.33 KB
/
Copy pathaudio.go
File metadata and controls
246 lines (215 loc) · 5.33 KB
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
package common
import (
"errors"
"io"
"log"
"github.com/EngoEngine/ecs"
"github.com/EngoEngine/engo"
"github.com/hajimehoshi/oto"
)
const (
channelNum = 2
bytesPerSample = 2
mask = ^(channelNum*bytesPerSample - 1)
)
var otoPlayer io.WriteCloser
var closeCh, loopClosedCh chan struct{}
// stepPlayer is used for headless mode audio, such as for tests
// you can control exactly how many steps it takes which allows for verification
// of the PCM data written to it. This replaces oto.Player when run in headless
type stepPlayer struct {
ThrowWriteError bool
stepStart chan []byte
stepDone chan struct{}
}
func (l *stepPlayer) Write(b []byte) (int, error) {
if l.ThrowWriteError {
return 0, errors.New("write error")
}
l.stepStart <- b
<-l.stepDone
return len(b), nil
}
func (l *stepPlayer) Close() error {
return nil
}
func (l *stepPlayer) Bytes() []byte {
return <-l.stepStart
}
func (l *stepPlayer) Step() {
if len(l.stepDone) > 0 {
return
}
l.stepDone <- struct{}{}
}
// AudioComponent is a Component which is used by the AudioSystem
type AudioComponent struct {
Player *Player
}
type audioEntity struct {
*ecs.BasicEntity
*AudioComponent
}
// AudioSystem is a System that allows for sound effects and / or music
type AudioSystem struct {
entities []audioEntity
bufsize int
pauseCh, restartCh chan struct{}
playerCh chan []*Player
}
// New is called when the AudioSystem is added to the world.
func (a *AudioSystem) New(w *ecs.World) {
switch engo.CurrentBackEnd {
case engo.BackEndMobile:
a.bufsize = 12288
default:
a.bufsize = 8192
}
if engo.Headless() {
otoPlayer = &stepPlayer{
stepStart: make(chan []byte),
stepDone: make(chan struct{}, 1),
}
} else {
if otoPlayer == nil {
c, err := oto.NewContext(SampleRate, channelNum, bytesPerSample, a.bufsize)
if err != nil {
log.Printf("audio error. Unable to create new OtoContext: %v \n\r", err)
}
otoPlayer = c.NewPlayer()
} else {
closeCh <- struct{}{}
<-loopClosedCh
}
}
// run oto on a separate thread so it doesn't slow down updates
closeCh = make(chan struct{}, 1)
a.pauseCh = make(chan struct{}, 1)
a.restartCh = make(chan struct{}, 1)
a.playerCh = make(chan []*Player, 25)
loopClosedCh = make(chan struct{})
go func() {
players := make([]*Player, 0)
loop:
for {
select {
case <-closeCh:
break loop
case <-a.pauseCh:
<-a.restartCh
case players = <-a.playerCh:
default:
buf := make([]byte, 2048)
a.read(buf, players)
if _, err := otoPlayer.Write(buf); err != nil {
log.Printf("error copying to OtoPlayer: %v \r\n", err)
}
}
}
loopClosedCh <- struct{}{}
}()
masterVolume = 1
}
// Add adds an entity to the AudioSystem
func (a *AudioSystem) Add(basic *ecs.BasicEntity, audio *AudioComponent) {
a.entities = append(a.entities, audioEntity{basic, audio})
}
// AddByInterface Allows an Entity to be added directly using the Audioable interface,
// which every entity containing the BasicEntity and AnimationComponent anonymously,
// automatically satisfies.
func (a *AudioSystem) AddByInterface(i ecs.Identifier) {
o, _ := i.(Audioable)
a.Add(o.GetBasicEntity(), o.GetAudioComponent())
}
// Remove removes an entity from the AudioSystem
func (a *AudioSystem) Remove(basic ecs.BasicEntity) {
delete := -1
for index, e := range a.entities {
if e.BasicEntity.ID() == basic.ID() {
delete = index
break
}
}
if delete >= 0 {
a.entities = append(a.entities[:delete], a.entities[delete+1:]...)
}
}
// Update doesn't do anything since audio is run on it's own thread
func (a *AudioSystem) Update(dt float32) {
if len(a.playerCh) >= 25 { //if the channel is full just return so we don't block the update loop
return
}
players := make([]*Player, 0)
for _, e := range a.entities {
if e.Player.isPlaying {
players = append(players, e.Player)
}
}
a.playerCh <- players
}
// Read reads from all the currently playing entities and combines them into a
// single stream that is passed to the oto player.
func (a *AudioSystem) read(b []byte, players []*Player) (int, error) {
l := len(b)
l &= mask
if len(players) == 0 {
copy(b, make([]byte, l))
return l, nil
}
b16s := [][]int16{}
for _, player := range players {
buf, err := player.bufferToInt16(l)
if err != nil {
return 0, err
}
b16s = append(b16s, buf)
}
for i := 0; i < l/2; i++ {
x := 0
for _, b16 := range b16s {
x += int(b16[i])
}
if x > (1<<15)-1 {
x = (1 << 15) - 1
}
if x < -(1 << 15) {
x = -(1 << 15)
}
b[2*i] = byte(x)
b[2*i+1] = byte(x >> 8)
}
for _, player := range players {
if player.eof() {
if player.Repeat {
player.Rewind()
} else {
player.Pause()
}
}
}
return l, nil
}
// Close closes the AudioSystem's loop. After this is called the AudioSystem
// can no longer play audio.
// Blocks until loop actually closes.
func (a *AudioSystem) Close() {
if len(closeCh) > 0 { //so it doesn't block
return
}
closeCh <- struct{}{}
<-loopClosedCh
}
// Pause pauses the AudioSystem's loop. Call Restart to continue playing audio.
func (a *AudioSystem) Pause() {
if len(a.pauseCh) > 0 { // so it doesn't block
return
}
a.pauseCh <- struct{}{}
}
// Restart restarts the AudioSystem's loop when it's paused.
func (a *AudioSystem) Restart() {
if len(a.restartCh) > 0 { // so it doesn't block
return
}
a.restartCh <- struct{}{}
}