-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_webaudio.go
301 lines (237 loc) · 6.74 KB
/
audio_webaudio.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) 2019 Thomas MILLET. All rights reserved.
// +build js
package audio
import (
fmt "fmt"
js "syscall/js"
tge "github.com/thommil/tge"
)
type plugin struct {
isInit bool
jsTge *js.Value
audioCtx *js.Value
}
func (p *plugin) Init(runtime tge.Runtime) error {
switch host := runtime.GetHost().(type) {
case *js.Value:
jsTge := host.Get("tge")
if jsTge == js.Undefined() || jsTge == js.Null() {
return fmt.Errorf("javascript tge not found")
}
p.jsTge = &jsTge
default:
return fmt.Errorf("Runtime host must be a *syscall/js.Value")
}
return nil
}
func (p *plugin) Dispose() {
p.isInit = false
p.audioCtx = nil
}
// -------------------------------------------------------------------- //
// Implementation
// -------------------------------------------------------------------- //
type buffer struct {
value *js.Value
}
func (b *buffer) Delete() {
b.value = nil
}
type node struct {
value *js.Value
}
func (n *node) Connect(to Node) Node {
switch to.(type) {
case *bufferSourceNode:
n.value.Call("connect", *(to.(*bufferSourceNode).value))
case *mediaElementSourceNode:
n.value.Call("connect", *(to.(*mediaElementSourceNode).value))
case *destinationNode:
n.value.Call("connect", *(to.(*destinationNode).value))
case *stereoPannerNode:
n.value.Call("connect", *(to.(*stereoPannerNode).value))
case *gainNode:
n.value.Call("connect", *(to.(*gainNode).value))
}
return to
}
func (n *node) Disconnect(to Node) {
switch to.(type) {
case *bufferSourceNode:
n.value.Call("disconnect", *(to.(*bufferSourceNode).value))
case *mediaElementSourceNode:
n.value.Call("disconnect", *(to.(*mediaElementSourceNode).value))
case *destinationNode:
n.value.Call("disconnect", *(to.(*destinationNode).value))
case *stereoPannerNode:
n.value.Call("disconnect", *(to.(*stereoPannerNode).value))
case *gainNode:
n.value.Call("disconnect", *(to.(*gainNode).value))
}
}
type bufferSourceNode struct {
node
}
func (n *bufferSourceNode) Start(delay, offset, duration float32, loop bool, loopStart, loopEnd float32) {
if loop {
n.value.Set("loop", loop)
n.value.Set("loopStart", loopStart)
if loopEnd > 0 {
n.value.Set("loopEnd", loopEnd)
}
}
if duration > 0 {
n.value.Call("start", delay, offset, duration)
} else {
n.value.Call("start", delay, offset)
}
}
func (n *bufferSourceNode) Stop() {
n.value.Call("stop")
}
type mediaElementSourceNode struct {
node
htmlElement *js.Value
}
func (n *mediaElementSourceNode) Delete() {
n.htmlElement.Call("remove")
}
func (n *mediaElementSourceNode) Play(loop bool) {
n.htmlElement.Set("loop", loop)
n.htmlElement.Call("play")
}
func (n *mediaElementSourceNode) Pause() {
n.htmlElement.Call("pause")
}
type destinationNode struct {
node
}
type stereoPannerNode struct {
node
}
func (n *stereoPannerNode) Pan(value float32) {
n.value.Get("pan").Set("value", value)
}
type gainNode struct {
node
}
func (n *gainNode) Gain(value float32) {
n.value.Get("gain").Set("value", value)
}
func createContext() error {
audioContextClass := js.Global().Get("AudioContext")
if audioContextClass == js.Undefined() || audioContextClass == js.Null() {
audioContextClass = js.Global().Get("webkitAudioContext")
}
if audioContextClass == js.Undefined() || audioContextClass == js.Null() {
return fmt.Errorf("AudioContext not supported")
}
audioCtx := audioContextClass.New()
if audioCtx == js.Undefined() || audioCtx == js.Null() {
return fmt.Errorf("failed to instanciate AudioContext")
}
_pluginInstance.audioCtx = &audioCtx
_pluginInstance.isInit = true
return nil
}
func createBuffer(path string) (Buffer, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
var err error
doneState := make(chan bool)
buffer := buffer{}
onCreateAudioBufferCallback := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if args[1] != js.Null() {
err = fmt.Errorf(args[1].String())
doneState <- false
} else {
buffer.value = &args[0]
doneState <- true
}
return false
})
defer onCreateAudioBufferCallback.Release()
_pluginInstance.jsTge.Call("createAudioBuffer", *(_pluginInstance.audioCtx), path, onCreateAudioBufferCallback)
<-doneState
if err != nil {
return nil, err
}
return &buffer, nil
}
func createBufferSourceNode(buf Buffer) (BufferSourceNode, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
jsBufferSourceNode := _pluginInstance.audioCtx.Call("createBufferSource")
if jsBufferSourceNode == js.Undefined() || jsBufferSourceNode == js.Null() {
return nil, fmt.Errorf("failed to create JS AudioBufferSourceNode")
}
jsBufferSourceNode.Set("buffer", *(buf.(*buffer).value))
node := &bufferSourceNode{}
node.value = &jsBufferSourceNode
return node, nil
}
func createMediaElementSourceNode(path string) (MediaElementSourceNode, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
jsMediaElementObjet := _pluginInstance.jsTge.Call("createMediaAudioElement", *(_pluginInstance.audioCtx), path)
if jsMediaElementObjet == js.Undefined() || jsMediaElementObjet == js.Null() {
return nil, fmt.Errorf("failed to create JS MediaElementSourceNode")
}
node := &mediaElementSourceNode{}
jsMediaElementSourceNode := jsMediaElementObjet.Get("mediaAudioElement")
jsHtmlElement := jsMediaElementObjet.Get("htmlElement")
node.value = &jsMediaElementSourceNode
node.htmlElement = &jsHtmlElement
return node, nil
}
func createDestinationNode() (DestinationNode, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
jsDestinationNode := _pluginInstance.audioCtx.Get("destination")
if jsDestinationNode == js.Undefined() || jsDestinationNode == js.Null() {
return nil, fmt.Errorf("failed to create JS AudioDestinationNode")
}
node := &destinationNode{}
node.value = &jsDestinationNode
return node, nil
}
func createStereoPannerNode() (StereoPannerNode, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
jsStereoPannerNode := _pluginInstance.audioCtx.Call("createStereoPanner")
if jsStereoPannerNode == js.Undefined() || jsStereoPannerNode == js.Null() {
return nil, fmt.Errorf("failed to create JS StereoPannerNode")
}
node := &stereoPannerNode{}
node.value = &jsStereoPannerNode
return node, nil
}
func createGainNode() (GainNode, error) {
if !_pluginInstance.isInit {
if err := createContext(); err != nil {
return nil, err
}
}
jsGainNode := _pluginInstance.audioCtx.Call("createGain")
if jsGainNode == js.Undefined() || jsGainNode == js.Null() {
return nil, fmt.Errorf("failed to create JS GainNode")
}
node := &gainNode{}
node.value = &jsGainNode
return node, nil
}