-
Notifications
You must be signed in to change notification settings - Fork 51
/
azul3d_audio.go
194 lines (152 loc) · 4.38 KB
/
azul3d_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
// +build !sdl,!apudebug,!js
package nes
import (
"errors"
"fmt"
"os"
"time"
"unsafe"
"azul3d.org/engine/native/al"
)
type Azul3DAudio struct {
paused bool
frequency int
speed float32
device *al.Device
source uint32
buffers []uint32
sampleSize int
input chan int16
}
func NewAudio(frequency int, sampleSize int) (audio *Azul3DAudio, err error) {
var device *al.Device
device, err = al.OpenDevice("", nil)
if err != nil {
return
}
audio = &Azul3DAudio{
frequency: frequency,
speed: 1.0,
device: device,
sampleSize: sampleSize,
buffers: make([]uint32, 2),
input: make(chan int16, sampleSize),
}
al.SetErrorHandler(func(e error) {
err = e
})
device.GenSources(1, &audio.source)
if !device.IsSource(audio.source) {
err = errors.New("IsSource returned false")
return
}
device.Sourcei(audio.source, al.LOOPING, al.FALSE)
device.GenBuffers(int32(len(audio.buffers)), &audio.buffers[0])
for i := range audio.buffers {
if !device.IsBuffer(audio.buffers[i]) {
err = errors.New(fmt.Sprintf("IsBuffer[%v] returned false", i))
return
}
}
return
}
func (audio *Azul3DAudio) Input() chan int16 {
return audio.input
}
func (audio *Azul3DAudio) bufferDuration() (duration time.Duration) {
duration = time.Millisecond *
time.Duration(1000*(float32(audio.sampleSize)/(float32(audio.frequency)*audio.speed)))
return duration
}
func (audio *Azul3DAudio) stream(schan chan []int16) {
samples := []int16{}
for {
samples = append(samples, <-audio.input)
if len(samples) == audio.sampleSize {
schan <- samples
samples = []int16{}
}
}
}
func (audio *Azul3DAudio) bufferData(buffer uint32, samples []int16) (err error) {
al.SetErrorHandler(func(e error) {
err = e
})
audio.device.BufferData(buffer, al.FORMAT_MONO16, unsafe.Pointer(&samples[0]),
int32(int(unsafe.Sizeof(samples[0]))*len(samples)), int32(float32(audio.frequency)*audio.speed))
return
}
func (audio *Azul3DAudio) Run() {
running := true
handler := func(e error) {
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
running = false
}
}
al.SetErrorHandler(handler)
schan := make(chan []int16, len(audio.buffers))
go audio.stream(schan)
audio.device.SourceQueueBuffers(audio.source, audio.buffers)
audio.device.SourcePlay(audio.source)
state := al.PLAYING
queued := int32(len(audio.buffers))
processed := int32(0)
var samples []int16
for running {
// Wait for one audio buffer to be prepared.
samples = <-schan
// Wait for at least one buffer to be processed by OpenAL, so we can refill
// it.
for {
audio.device.GetSourcei(audio.source, al.BUFFERS_PROCESSED, &processed)
if processed > 0 {
break
}
}
// In situations where OpenAL runs out of buffers to play (e.g. if the app
// stalled because the user's system was doing a lot of work), OpenAL will
// stop the audio source from playing.
//
// We wait until each of the buffers are done playing in this case, so that
// we may keep are playhead at the first buffer to keep our double
// buffering.
if audio.device.GetSourcei(audio.source, al.SOURCE_STATE, &state); state != al.PLAYING {
// fmt.Println("nes: Failed to feed audio to OpenAL fast enough; resynching...")
for queued > 0 {
audio.device.GetSourcei(audio.source, al.BUFFERS_PROCESSED, &processed)
if processed == queued {
break
}
}
}
// Dequeue the buffers that were processed by OpenAL.
pbuffers := make([]uint32, processed)
audio.device.SourceUnqueueBuffers(audio.source, pbuffers)
// Fill each buffer with data and queue them again.
for i := range pbuffers {
if samples == nil {
samples = <-schan
}
handler(audio.bufferData(pbuffers[i], samples))
audio.device.SourceQueueBuffers(audio.source, []uint32{pbuffers[i]})
samples = nil
}
audio.device.GetSourcei(audio.source, al.BUFFERS_QUEUED, &queued)
// Begin playing the source now that we've filled all the buffers.
if audio.device.GetSourcei(audio.source, al.SOURCE_STATE, &state); state != al.PLAYING {
audio.device.SourcePlay(audio.source)
}
}
}
func (audio *Azul3DAudio) TogglePaused() {
audio.paused = !audio.paused
}
func (audio *Azul3DAudio) SetSpeed(speed float32) {
audio.speed = speed
}
func (audio *Azul3DAudio) Close() {
audio.device.DeleteSources(1, &audio.source)
audio.device.DeleteBuffers(int32(len(audio.buffers)), &audio.buffers[0])
audio.device.Close()
}