forked from gen2brain/malgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device.go
188 lines (159 loc) · 5.37 KB
/
device.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
package malgo
// #include "malgo.h"
import "C"
import (
"sync"
"unsafe"
)
// DataProc type.
type DataProc func(pOutputSample, pInputSamples []byte, framecount uint32)
// StopProc type.
type StopProc func()
// DeviceCallbacks contains callbacks for one initialized device.
type DeviceCallbacks struct {
// Data is called for the full duplex IO.
Data DataProc
// Stop is called when the device stopped.
Stop StopProc
}
// Device represents a streaming instance.
type Device struct {
ptr *unsafe.Pointer
}
// InitDevice initializes a device.
//
// The device ID can be nil, in which case the default device is used. Otherwise, you
// can retrieve the ID by calling Context.Devices() and use the ID from the returned data.
//
// Set device ID to nil to use the default device. Do _not_ rely on the first device ID returned
// by Context.Devices() to be the default device.
//
// The returned instance has to be cleaned up using Uninit().
func InitDevice(context Context, deviceConfig DeviceConfig, deviceCallbacks DeviceCallbacks) (*Device, error) {
ptr := C.ma_malloc(C.sizeof_ma_device, nil)
dev := Device{
ptr: &ptr,
}
if uintptr(*dev.ptr) == 0 {
return nil, ErrOutOfMemory
}
devConfigC, release := deviceConfig.toC()
defer release()
rawDevice := dev.cptr()
C.goSetDeviceConfigCallbacks(&devConfigC)
result := C.ma_device_init(context.cptr(), &devConfigC, rawDevice)
if result != 0 {
dev.free()
return nil, errorFromResult(result)
}
deviceMutex.Lock()
dataCallbacks[rawDevice] = deviceCallbacks.Data
stopCallbacks[rawDevice] = deviceCallbacks.Stop
deviceMutex.Unlock()
return &dev, nil
}
func (dev Device) cptr() *C.ma_device {
return (*C.ma_device)(*dev.ptr)
}
func (dev Device) free() {
if dev.ptr != nil {
C.ma_free(*dev.ptr, nil)
}
}
// Type returns device type.
func (dev *Device) Type() DeviceType {
return DeviceType(dev.cptr()._type)
}
// PlaybackFormat returns device playback format.
func (dev *Device) PlaybackFormat() FormatType {
return FormatType(dev.cptr().playback.format)
}
// CaptureFormat returns device capture format.
func (dev *Device) CaptureFormat() FormatType {
return FormatType(dev.cptr().capture.format)
}
// PlaybackChannels returns number of playback channels.
func (dev *Device) PlaybackChannels() uint32 {
return uint32(dev.cptr().playback.channels)
}
// CaptureChannels returns number of playback channels.
func (dev *Device) CaptureChannels() uint32 {
return uint32(dev.cptr().capture.channels)
}
// SampleRate returns sample rate.
func (dev *Device) SampleRate() uint32 {
return uint32(dev.cptr().sampleRate)
}
// Start activates the device.
// For playback devices this begins playback. For capture devices it begins recording.
//
// For a playback device, this will retrieve an initial chunk of audio data from the client before
// returning. The reason for this is to ensure there is valid audio data in the buffer, which needs
// to be done _before_ the device begins playback.
//
// This API waits until the backend device has been started for real by the worker thread. It also
// waits on a mutex for thread-safety.
func (dev *Device) Start() error {
result := C.ma_device_start(dev.cptr())
return errorFromResult(result)
}
// IsStarted determines whether or not the device is started.
func (dev *Device) IsStarted() bool {
result := C.ma_device_is_started(dev.cptr())
return result != 0
}
// Stop puts the device to sleep, but does not uninitialize it. Use Start() to start it up again.
//
// This API needs to wait on the worker thread to stop the backend device properly before returning. It
// also waits on a mutex for thread-safety. In addition, some backends need to wait for the device to
// finish playback/recording of the current fragment which can take some time (usually proportionate to
// the buffer size that was specified at initialization time).
func (dev *Device) Stop() error {
result := C.ma_device_stop(dev.cptr())
return errorFromResult(result)
}
// Uninit uninitializes a device.
//
// This will explicitly stop the device. You do not need to call Stop() beforehand, but it's
// harmless if you do.
func (dev *Device) Uninit() {
rawDevice := dev.cptr()
deviceMutex.Lock()
delete(dataCallbacks, rawDevice)
delete(stopCallbacks, rawDevice)
deviceMutex.Unlock()
C.ma_device_uninit(rawDevice)
dev.free()
}
var deviceMutex sync.Mutex
var dataCallbacks = make(map[*C.ma_device]DataProc)
var stopCallbacks = make(map[*C.ma_device]StopProc)
//export goDataCallback
func goDataCallback(pDevice *C.ma_device, pOutput, pInput unsafe.Pointer, frameCount C.ma_uint32) {
deviceMutex.Lock()
callback := dataCallbacks[pDevice]
deviceMutex.Unlock()
if callback != nil {
var inputSamples, outputSamples []byte
if pOutput != nil {
sampleCount := uint32(frameCount) * uint32(pDevice.playback.channels)
sizeInBytes := uint32(C.ma_get_bytes_per_sample(pDevice.playback.format))
outputSamples = unsafe.Slice((*byte)(pOutput), sampleCount*sizeInBytes)
}
if pInput != nil {
sampleCount := uint32(frameCount) * uint32(pDevice.capture.channels)
sizeInBytes := uint32(C.ma_get_bytes_per_sample(pDevice.capture.format))
inputSamples = unsafe.Slice((*byte)(pInput), sampleCount*sizeInBytes)
}
callback(outputSamples, inputSamples, uint32(frameCount))
}
}
//export goStopCallback
func goStopCallback(pDevice *C.ma_device) {
deviceMutex.Lock()
callback := stopCallbacks[pDevice]
deviceMutex.Unlock()
if callback != nil {
callback()
}
}