-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay_ios.go
55 lines (49 loc) · 1016 Bytes
/
play_ios.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
// +build ios
package audio
/*
#cgo LDFLAGS: -framework AVFoundation -framework AudioToolbox -framework Foundation
extern const char* start();
extern const char* stop();
*/
import "C"
import (
"errors"
"unsafe"
)
var (
playing bool
voice Voice
ctrl PlayControl
)
func startPlaying(v Voice, c PlayControl) error {
if playing {
return errors.New("audio.Play doesn't yet support multiple simultaneous voices on iOS.")
}
playing = true
voice = v
ctrl = c
Init(voice, Params{SampleRate: 44100})
if err := C.start(); err != nil {
return errors.New(C.GoString(err))
}
return nil
}
//export streamCallback
func streamCallback(buf *float32, len uint32) {
for p := uintptr(unsafe.Pointer(buf)); len > 0; len-- {
*(*float32)(unsafe.Pointer(p)) = float32(voice.Sing())
p += unsafe.Sizeof(float32(0))
}
if voice.Done() {
ctrl.Stop()
}
}
func stopPlaying() error {
if playing {
playing = false
if err := C.stop(); err != nil {
return errors.New(C.GoString(err))
}
}
return nil
}