forked from remogatto/mandala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_android.go
96 lines (81 loc) · 2.32 KB
/
sound_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
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
// +build android
package mandala
import (
"fmt"
"sync"
"unsafe"
)
// #include <android/native_activity.h>
// #include <SLES/OpenSLES.h>
// #include <SLES/OpenSLES_Android.h>
// #include "sound_android.h"
//
// #cgo LDFLAGS: -landroid -lOpenSLES
import "C"
type bufferQueuePlayer struct {
bqPlayerObject C.SLObjectItf
bqPlayerPlay C.SLPlayItf
bqPlayerBufferQueue C.SLAndroidSimpleBufferQueueItf
bqPlayerVolume C.SLVolumeItf
}
type audioPlayer struct {
rwMutex sync.RWMutex
bqPlayer *bufferQueuePlayer
}
func jniBool(value C.jboolean) bool {
if value == C.JNI_TRUE {
return true
}
return false
}
func initOpenSL() error {
result := C.initOpenSL()
if result != C.SL_RESULT_SUCCESS {
return fmt.Errorf("Unable to initialize the native audio library. Error code is %x", int(result))
}
return nil
}
func shutdownOpenSL() {
Debugf("Shutting down OpenSL ES")
C.shutdownOpenSL()
}
func newAudioPlayer() (*audioPlayer, error) {
ap := new(audioPlayer)
ap.bqPlayer = new(bufferQueuePlayer)
result := C.createBufferQueueAudioPlayer((*C.t_buffer_queue_ap)(ap.bqPlayer))
if result != C.SL_RESULT_SUCCESS {
return nil, fmt.Errorf("Error %d occured trying to create a buffer queue player", result)
}
return ap, nil
}
func (ap *audioPlayer) play(buffer []byte, doneCh chan bool) {
ap.enqueue(buffer)
}
func (ap *audioPlayer) destroy() {
Debugf("Destroying audio player at 0x%x", ap)
ap.rwMutex.Lock()
C.destroyBufferQueueAudioPlayer((*C.t_buffer_queue_ap)(ap.bqPlayer))
ap.rwMutex.Unlock()
}
func (ap *audioPlayer) enqueue(buffer []byte) {
C.enqueueBuffer((*C.t_buffer_queue_ap)(ap.bqPlayer), unsafe.Pointer(&buffer[0]), C.SLuint32(len(buffer)))
}
func (ap *audioPlayer) setVolumeLevel(value int) error {
result := C.setVolumeLevel((*C.t_buffer_queue_ap)(ap.bqPlayer), C.SLmillibel(value))
if result != C.SL_RESULT_SUCCESS {
return fmt.Errorf("Unable to set volume level. Error code is %x", int(result))
}
return nil
}
func (ap *audioPlayer) getMaxVolumeLevel() (int, error) {
var maxLevel C.SLmillibel
result := C.getMaxVolumeLevel((*C.t_buffer_queue_ap)(ap.bqPlayer), &maxLevel)
if result != C.SL_RESULT_SUCCESS {
return 0, fmt.Errorf("Unable to get max volume level. Error code is %x", int(result))
}
return int(maxLevel), nil
}
//export playerCallback
func playerCallback() {
Logf("Player done!\n")
}