-
Notifications
You must be signed in to change notification settings - Fork 2
/
soundio.go
248 lines (208 loc) · 6.3 KB
/
soundio.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
/*
* Copyright (c) 2019 Zenichi Amano
*
* This file is part of libsoundio, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
// Package soundio is as set of bindings for the libsoundio sound library.
package soundio
/*
#cgo LDFLAGS: -lsoundio -lm
#include "soundio.h"
#include <stdlib.h>
*/
import "C"
import (
"context"
"runtime"
"unsafe"
)
const (
// MaxChannels is support channel max count.
MaxChannels int = C.SOUNDIO_MAX_CHANNELS
)
// SoundIo is used for selecting and initializing the relevant backends.
type SoundIo struct {
backend Backend
ptr *C.struct_SoundIo
appName string
onDevicesChange func(*SoundIo)
onBackendDisconnect func(*SoundIo, error)
onEventsSignal func(*SoundIo)
}
//export soundioOnDevicesChange
func soundioOnDevicesChange(nativeIo *C.struct_SoundIo) {
io := (*SoundIo)(nativeIo.userdata)
if io.onDevicesChange != nil {
io.onDevicesChange(io)
}
}
//export soundioOnBackendDisconnect
func soundioOnBackendDisconnect(nativeIo *C.struct_SoundIo, err C.int) {
io := (*SoundIo)(nativeIo.userdata)
if io.onBackendDisconnect != nil {
io.onBackendDisconnect(io, convertToError(err))
}
}
//export soundioOnEventsSignal
func soundioOnEventsSignal(nativeIo *C.struct_SoundIo) {
io := (*SoundIo)(nativeIo.userdata)
if io.onEventsSignal != nil {
io.onEventsSignal(io)
}
}
// fields
// CurrentBackend returns current backend.
func (s *SoundIo) CurrentBackend() Backend {
return Backend(int(s.ptr.current_backend))
}
// AppName returns application name.
func (s *SoundIo) AppName() string {
return C.GoString(s.ptr.app_name)
}
// functions
// Version returns the version number string of libsoundio.
func Version() string {
return C.GoString(C.soundio_version_string())
}
// VersionMajor returns the major version number of libsoundio.
func VersionMajor() int {
return int(C.soundio_version_major())
}
// VersionMinor returns the minor version number of libsoundio.
func VersionMinor() int {
return int(C.soundio_version_minor())
}
// VersionPatch returns the patch version number of libsoundio.
func VersionPatch() int {
return int(C.soundio_version_patch())
}
// BytesPerSample returns bytes per sample.
// Returns -1 on invalid format.
func BytesPerSample(format Format) int {
return int(C.soundio_get_bytes_per_sample(uint32(format)))
}
// BytesPerFrame returns bytes per frame.
// A frame is one sample per channel.
func BytesPerFrame(format Format, channelCount int) int {
return int(C.soundio_get_bytes_per_frame(uint32(format), C.int(channelCount)))
}
// BytesPerSecond returns bytes per second.
// Sample rate is the number of frames per second.
func BytesPerSecond(format Format, channelCount int, sampleRate int) int {
return int(C.soundio_get_bytes_per_second(uint32(format), C.int(channelCount), C.int(sampleRate)))
}
// Create a SoundIo context. You may create multiple instances of this to connect to multiple backends. Sets all fields to defaults.
func Create(opts ...Option) *SoundIo {
ptr := C.soundio_create()
io := &SoundIo{
backend: BackendNone,
ptr: ptr,
appName: "SoundIo",
}
ptr.userdata = unsafe.Pointer(io)
C.setSoundIoCallback(ptr)
for _, opt := range opts {
opt(io)
}
io.ptr.app_name = C.CString(io.appName)
runtime.SetFinalizer(io, destroySoundIo)
return io
}
// destroySoundIo releases resources.
func destroySoundIo(s *SoundIo) {
if s.ptr != nil {
C.free(unsafe.Pointer(s.ptr.app_name))
C.soundio_destroy(s.ptr)
s.ptr = nil
}
}
// functions
// Connect tries to connect on all available backends in order.
func (s *SoundIo) Connect() error {
var err error
if s.backend == BackendNone {
err = convertToError(C.soundio_connect(s.ptr))
} else {
err = convertToError(C.soundio_connect_backend(s.ptr, uint32(s.backend)))
}
if err == nil {
s.FlushEvents()
}
return err
}
// Disconnect disconnect from backend.
func (s *SoundIo) Disconnect() {
C.soundio_disconnect(s.ptr)
}
// BackendCount returns the number of available backends.
func (s *SoundIo) BackendCount() int {
return int(C.soundio_backend_count(s.ptr))
}
// Backend returns the available backend at the specified index (0 <= index < BackendCount)
func (s *SoundIo) Backend(index int) Backend {
return Backend(C.soundio_get_backend(s.ptr, C.int(index)))
}
// FlushEvents atomically updates information for all connected devices.
func (s *SoundIo) FlushEvents() {
C.soundio_flush_events(s.ptr)
}
// WaitEvents calls FlushEvents then blocks until context canceled.
func (s *SoundIo) WaitEvents(ctx context.Context) error {
go func() {
select {
case <-ctx.Done():
if s.CurrentBackend() != BackendNone {
C.soundio_wakeup(s.ptr)
}
}
}()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if s.CurrentBackend() != BackendNone {
C.soundio_wait_events(s.ptr)
} else {
return ctx.Err()
}
}
}
}
// ForceDeviceScan rescan device If necessary.
func (s *SoundIo) ForceDeviceScan() {
C.soundio_force_device_scan(s.ptr)
}
// InputDeviceCount returns the number of input devices.
// Returns -1 if you never called FlushEvents.
func (s *SoundIo) InputDeviceCount() int {
return int(C.soundio_input_device_count(s.ptr))
}
// OutputDeviceCount returns the number of output devices.
// Returns -1 if you never called FlushEvents.
func (s *SoundIo) OutputDeviceCount() int {
return int(C.soundio_output_device_count(s.ptr))
}
// InputDevice returns a device.
// Call RemoveReference when done.
// `index` must be 0 <= index < InputDeviceCount.
func (s *SoundIo) InputDevice(index int) *Device {
return newDevice(C.soundio_get_input_device(s.ptr, C.int(index)))
}
// OutputDevice returns a device.
// Call RemoveReference when done.
// `index` must be 0 <= index < OutputDeviceCount
func (s *SoundIo) OutputDevice(index int) *Device {
return newDevice(C.soundio_get_output_device(s.ptr, C.int(index)))
}
// DefaultInputDeviceIndex returns the index of the default input device
// returns -1 if there are no devices or if you never called FlushEvents.
func (s *SoundIo) DefaultInputDeviceIndex() int {
return int(C.soundio_default_input_device_index(s.ptr))
}
// DefaultOutputDeviceIndex returns the index of the default output device
// returns -1 if there are no devices or if you never called FlushEvents.
func (s *SoundIo) DefaultOutputDeviceIndex() int {
return int(C.soundio_default_output_device_index(s.ptr))
}