-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay_android.go
70 lines (60 loc) · 1.27 KB
/
play_android.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
// +build android
package audio
/*
#cgo LDFLAGS: -landroid -lOpenSLES
typedef struct {
short *outBuffer;
int outBufferSampleLength;
int outBufferByteLen;
} stream_t;
extern void start(int channels);
extern void stop();
*/
import "C"
import (
"unsafe"
)
var (
voice interface{}
ctrl PlayControl
)
func startPlaying(v interface{}, c PlayControl) error {
voice = v
ctrl = c
Init(voice, Params{SampleRate: 48000}) // corresponds with SL_SAMPLINGRATE_48 in play_android.c
channels := 1
if _, ok := voice.(StereoVoice); ok {
channels = 2
}
C.start(C.int(channels))
return nil
}
//export streamCallback
func streamCallback(s *C.stream_t) {
p := uintptr(unsafe.Pointer(s.outBuffer))
switch voice := voice.(type) {
case Voice:
for i := s.outBufferSampleLength; i > 0; i-- {
*(*int16)(unsafe.Pointer(p)) = int16(voice.Sing() * 32767)
p += unsafe.Sizeof(int16(0))
}
if voice.Done() {
ctrl.Stop()
}
case StereoVoice:
for i := s.outBufferSampleLength; i > 0; i-- {
left, right := voice.Sing()
*(*int16)(unsafe.Pointer(p)) = int16(left * 32767)
p += unsafe.Sizeof(int16(0))
*(*int16)(unsafe.Pointer(p)) = int16(right * 32767)
p += unsafe.Sizeof(int16(0))
}
if voice.Done() {
ctrl.Stop()
}
}
}
func stopPlaying() error {
C.stop()
return nil
}