forked from veandco/go-sdl2
/
audio.go
475 lines (409 loc) · 16.8 KB
/
audio.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package sdl
/*
#include "sdl_wrapper.h"
#if !(SDL_VERSION_ATLEAST(2,0,4))
#pragma message("SDL_QueueAudio is not supported before SDL 2.0.4")
static int SDL_QueueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len)
{
return -1;
}
static Uint32 SDL_GetQueuedAudioSize(SDL_AudioDeviceID dev_id)
{
return 0;
}
static void SDL_ClearQueuedAudio(SDL_AudioDeviceID dev)
{
}
#endif
#if !(SDL_VERSION_ATLEAST(2,0,5))
#pragma message("SDL_DequeueAudio is not supported before SDL 2.0.5")
static int SDL_DequeueAudio(SDL_AudioDeviceID dev, const void *data, Uint32 len)
{
return -1;
}
#endif
*/
import "C"
import (
"reflect"
"unsafe"
)
// Audio format masks.
// (https://wiki.libsdl.org/SDL_AudioFormat)
const (
AUDIO_MASK_BITSIZE = C.SDL_AUDIO_MASK_BITSIZE // (0xFF)
AUDIO_MASK_DATATYPE = C.SDL_AUDIO_MASK_DATATYPE // (1<<8)
AUDIO_MASK_ENDIAN = C.SDL_AUDIO_MASK_ENDIAN // (1<<12)
AUDIO_MASK_SIGNED = C.SDL_AUDIO_MASK_SIGNED // (1<<15)
)
// Audio format values.
// (https://wiki.libsdl.org/SDL_AudioFormat)
const (
AUDIO_S8 = C.AUDIO_S8 // unsigned 8-bit samples
AUDIO_U8 = C.AUDIO_U8 // signed 8-bit samples
AUDIO_S16LSB = C.AUDIO_S16LSB // signed 16-bit samples in little-endian byte order
AUDIO_S16MSB = C.AUDIO_S16MSB // signed 16-bit samples in big-endian byte order
AUDIO_S16SYS = C.AUDIO_S16SYS // signed 16-bit samples in native byte order
AUDIO_S16 = C.AUDIO_S16 // AUDIO_S16LSB
AUDIO_U16LSB = C.AUDIO_U16LSB // unsigned 16-bit samples in little-endian byte order
AUDIO_U16MSB = C.AUDIO_U16MSB // unsigned 16-bit samples in big-endian byte order
AUDIO_U16SYS = C.AUDIO_U16SYS // unsigned 16-bit samples in native byte order
AUDIO_U16 = C.AUDIO_U16 // AUDIO_U16LSB
AUDIO_S32LSB = C.AUDIO_S32LSB // 32-bit integer samples in little-endian byte order
AUDIO_S32MSB = C.AUDIO_S32MSB // 32-bit integer samples in big-endian byte order
AUDIO_S32SYS = C.AUDIO_S32SYS // 32-bit integer samples in native byte order
AUDIO_S32 = C.AUDIO_S32 // AUDIO_S32LSB
AUDIO_F32LSB = C.AUDIO_F32LSB // 32-bit floating point samples in little-endian byte order
AUDIO_F32MSB = C.AUDIO_F32MSB // 32-bit floating point samples in big-endian byte order
AUDIO_F32SYS = C.AUDIO_F32SYS // 32-bit floating point samples in native byte order
AUDIO_F32 = C.AUDIO_F32 // AUDIO_F32LSB
)
// AllowedChanges flags specify how SDL should behave when a device cannot offer a specific feature. If the application requests a feature that the hardware doesn't offer, SDL will always try to get the closest equivalent. Used in OpenAudioDevice().
// (https://wiki.libsdl.org/SDL_OpenAudioDevice)
const (
AUDIO_ALLOW_FREQUENCY_CHANGE = C.SDL_AUDIO_ALLOW_FREQUENCY_CHANGE
AUDIO_ALLOW_FORMAT_CHANGE = C.SDL_AUDIO_ALLOW_FORMAT_CHANGE
AUDIO_ALLOW_CHANNELS_CHANGE = C.SDL_AUDIO_ALLOW_CHANNELS_CHANGE
AUDIO_ALLOW_ANY_CHANGE = C.SDL_AUDIO_ALLOW_ANY_CHANGE
)
// An enumeration of audio device states used in GetAudioDeviceStatus() and GetAudioStatus().
// (https://wiki.libsdl.org/SDL_AudioStatus)
const (
AUDIO_STOPPED AudioStatus = C.SDL_AUDIO_STOPPED // audio device is stopped
AUDIO_PLAYING = C.SDL_AUDIO_PLAYING // audio device is playing
AUDIO_PAUSED = C.SDL_AUDIO_PAUSED // audio device is paused
)
// MIX_MAXVOLUME is the full audio volume value used in MixAudioFormat() and AudioFormat().
// (https://wiki.libsdl.org/SDL_MixAudioFormat)
const MIX_MAXVOLUME = C.SDL_MIX_MAXVOLUME // full audio volume
// AudioFormat is an enumeration of audio formats.
// (https://wiki.libsdl.org/SDL_AudioFormat)
type AudioFormat uint16
// AudioCallback is a function to call when the audio device needs more data.`
// (https://wiki.libsdl.org/SDL_AudioSpec)
type AudioCallback C.SDL_AudioCallback
// AudioFilter is the filter list used in AudioCVT() (internal use)
// (https://wiki.libsdl.org/SDL_AudioCVT)
type AudioFilter C.SDL_AudioFilter
// AudioDeviceID is ID of an audio device previously opened with OpenAudioDevice().
// (https://wiki.libsdl.org/SDL_OpenAudioDevice)
type AudioDeviceID uint32
// AudioStatus is an enumeration of audio device states.
// (https://wiki.libsdl.org/SDL_AudioStatus)
type AudioStatus uint32
type cAudioStatus C.SDL_AudioStatus
// AudioSpec contains the audio output format. It also contains a callback that is called when the audio device needs more data.
// (https://wiki.libsdl.org/SDL_AudioSpec)
type AudioSpec struct {
Freq int32 // DSP frequency (samples per second)
Format AudioFormat // audio data format
Channels uint8 // number of separate sound channels
Silence uint8 // audio buffer silence value (calculated)
Samples uint16 // audio buffer size in samples (power of 2)
_ uint16 // padding
Size uint32 // audio buffer size in bytes (calculated)
Callback AudioCallback // the function to call when the audio device needs more data
UserData unsafe.Pointer // a pointer that is passed to callback (otherwise ignored by SDL)
}
type cAudioSpec C.SDL_AudioSpec
// AudioCVT contains audio data conversion information.
// (https://wiki.libsdl.org/SDL_AudioCVT)
type AudioCVT struct {
Needed int32 // set to 1 if conversion possible
SrcFormat AudioFormat // source audio format
DstFormat AudioFormat // target audio format
RateIncr float64 // rate conversion increment
Buf unsafe.Pointer // the buffer to hold entire audio data. Use AudioCVT.BufAsSlice() for access via a Go slice
Len int32 // length of original audio buffer
LenCVT int32 // length of converted audio buffer
LenMult int32 // buf must be len*len_mult big
LenRatio float64 // given len, final size is len*len_ratio
filters [10]AudioFilter // filter list (internal use)
filterIndex int32 // current audio conversion function (internal use)
}
type cAudioCVT C.SDL_AudioCVT
func (fmt AudioFormat) c() C.SDL_AudioFormat {
return C.SDL_AudioFormat(fmt)
}
func (id AudioDeviceID) c() C.SDL_AudioDeviceID {
return C.SDL_AudioDeviceID(id)
}
func (as *AudioSpec) cptr() *C.SDL_AudioSpec {
return (*C.SDL_AudioSpec)(unsafe.Pointer(as))
}
func (cvt *AudioCVT) cptr() *C.SDL_AudioCVT {
return (*C.SDL_AudioCVT)(unsafe.Pointer(cvt))
}
// BitSize returns audio formats bit size.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) BitSize() uint8 {
return uint8(fmt & AUDIO_MASK_BITSIZE)
}
// IsFloat reports whether audio format is float.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsFloat() bool {
return (fmt & AUDIO_MASK_DATATYPE) > 0
}
// IsBigEndian reports whether audio format is big-endian.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsBigEndian() bool {
return (fmt & AUDIO_MASK_ENDIAN) > 0
}
// IsSigned reports whether audio format is signed.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsSigned() bool {
return (fmt & AUDIO_MASK_SIGNED) > 0
}
// IsInt reports whether audio format is integer.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsInt() bool {
return !fmt.IsFloat()
}
// IsLittleEndian reports whether audio format is little-endian.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsLittleEndian() bool {
return !fmt.IsBigEndian()
}
// IsUnsigned reports whether audio format is unsigned.
// (https://wiki.libsdl.org/SDL_AudioFormat)
func (fmt AudioFormat) IsUnsigned() bool {
return !fmt.IsSigned()
}
// AllocBuf allocates the requested memory for AudioCVT buffer.
func (cvt *AudioCVT) AllocBuf(size uintptr) {
cvt.Buf = C.malloc(C.size_t(size))
}
// FreeBuf deallocates the memory previously allocated from AudioCVT buffer.
func (cvt *AudioCVT) FreeBuf() {
C.free(cvt.Buf)
}
// BufAsSlice returns AudioCVT.buf as byte slice.
// NOTE: Must be used after ConvertAudio() because it uses LenCVT as slice length.
func (cvt AudioCVT) BufAsSlice() []byte {
var b []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sliceHeader.Len = int(cvt.LenCVT)
sliceHeader.Cap = int(cvt.Len * cvt.LenMult)
sliceHeader.Data = uintptr(unsafe.Pointer(cvt.Buf))
return b
}
// GetNumAudioDrivers returns the number of built-in audio drivers.
// (https://wiki.libsdl.org/SDL_GetNumAudioDrivers)
func GetNumAudioDrivers() int {
return int(C.SDL_GetNumAudioDrivers())
}
// GetAudioDriver returns the name of a built in audio driver.
// (https://wiki.libsdl.org/SDL_GetAudioDriver)
func GetAudioDriver(index int) string {
return string(C.GoString(C.SDL_GetAudioDriver(C.int(index))))
}
// AudioInit initializes a particular audio driver.
// (https://wiki.libsdl.org/SDL_AudioInit)
func AudioInit(driverName string) error {
_driverName := C.CString(driverName)
defer C.free(unsafe.Pointer(_driverName))
if C.SDL_AudioInit(_driverName) != 0 {
return GetError()
}
return nil
}
// AudioQuit shuts down audio if you initialized it with AudioInit().
// (https://wiki.libsdl.org/SDL_AudioQuit)
func AudioQuit() {
C.SDL_AudioQuit()
}
// GetCurrentAudioDriver returns the name of the current audio driver.
// (https://wiki.libsdl.org/SDL_GetCurrentAudioDriver)
func GetCurrentAudioDriver() string {
return string(C.GoString(C.SDL_GetCurrentAudioDriver()))
}
// OpenAudio opens the audio device. New programs might want to use OpenAudioDevice() instead.
// (https://wiki.libsdl.org/SDL_OpenAudio)
func OpenAudio(desired, obtained *AudioSpec) error {
if C.SDL_OpenAudio(desired.cptr(), obtained.cptr()) != 0 {
return GetError()
}
return nil
}
// GetNumAudioDevices returns the number of built-in audio devices.
// (https://wiki.libsdl.org/SDL_GetNumAudioDevices)
func GetNumAudioDevices(isCapture bool) int {
return int(C.SDL_GetNumAudioDevices(C.int(Btoi(isCapture))))
}
// GetAudioDeviceName returns the name of a specific audio device.
// (https://wiki.libsdl.org/SDL_GetAudioDeviceName)
func GetAudioDeviceName(index int, isCapture bool) string {
return string(C.GoString(C.SDL_GetAudioDeviceName(C.int(index), C.int(Btoi(isCapture)))))
}
// OpenAudioDevice opens a specific audio device.
// (https://wiki.libsdl.org/SDL_OpenAudioDevice)
func OpenAudioDevice(device string, isCapture bool, desired, obtained *AudioSpec, allowedChanges int) (AudioDeviceID, error) {
_device := C.CString(device)
if device == "" {
_device = nil
}
defer C.free(unsafe.Pointer(_device))
if id := AudioDeviceID(C.SDL_OpenAudioDevice(_device, C.int(Btoi(isCapture)), desired.cptr(), obtained.cptr(), C.int(allowedChanges))); id > 0 {
return id, nil
}
return 0, GetError()
}
// GetAudioStatus returns the current audio state of the audio device. New programs might want to use GetAudioDeviceStatus() instead.
// (https://wiki.libsdl.org/SDL_GetAudioStatus)
func GetAudioStatus() AudioStatus {
return (AudioStatus)(C.SDL_GetAudioStatus())
}
// GetAudioDeviceStatus returns the current audio state of an audio device.
// (https://wiki.libsdl.org/SDL_GetAudioDeviceStatus)
func GetAudioDeviceStatus(dev AudioDeviceID) AudioStatus {
return (AudioStatus)(C.SDL_GetAudioDeviceStatus(dev.c()))
}
// PauseAudio pauses and unpauses the audio device. New programs might want to use SDL_PauseAudioDevice() instead.
// (https://wiki.libsdl.org/SDL_PauseAudio)
func PauseAudio(pauseOn bool) {
C.SDL_PauseAudio(C.int(Btoi(pauseOn)))
}
// PauseAudioDevice pauses and unpauses audio playback on a specified device.
// (https://wiki.libsdl.org/SDL_PauseAudioDevice)
func PauseAudioDevice(dev AudioDeviceID, pauseOn bool) {
C.SDL_PauseAudioDevice(dev.c(), C.int(Btoi(pauseOn)))
}
// LoadWAVRW loads a WAVE from the data source, automatically freeing that source if freeSrc is true.
// (https://wiki.libsdl.org/SDL_LoadWAV_RW)
func LoadWAVRW(src *RWops, freeSrc bool) ([]byte, *AudioSpec) {
var _audioBuf *C.Uint8
var _audioLen C.Uint32
audioSpec := (*AudioSpec)(unsafe.Pointer(C.SDL_LoadWAV_RW(src.cptr(), C.int(Btoi(freeSrc)), (&AudioSpec{}).cptr(), &_audioBuf, &_audioLen)))
var b []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sliceHeader.Len = (int)(_audioLen)
sliceHeader.Cap = (int)(_audioLen)
sliceHeader.Data = uintptr(unsafe.Pointer(_audioBuf))
return b, audioSpec
}
// LoadWAV loads a WAVE from a file.
// (https://wiki.libsdl.org/SDL_LoadWAV)
func LoadWAV(file string) ([]byte, *AudioSpec) {
_file := C.CString(file)
_rb := C.CString("rb")
defer C.free(unsafe.Pointer(_file))
defer C.free(unsafe.Pointer(_rb))
var _audioBuf *C.Uint8
var _audioLen C.Uint32
audioSpec := (*AudioSpec)(unsafe.Pointer(C.SDL_LoadWAV_RW(C.SDL_RWFromFile(_file, _rb), 1, (&AudioSpec{}).cptr(), &_audioBuf, &_audioLen)))
var b []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sliceHeader.Len = (int)(_audioLen)
sliceHeader.Cap = (int)(_audioLen)
sliceHeader.Data = uintptr(unsafe.Pointer(_audioBuf))
return b, audioSpec
}
// FreeWAV frees data previously allocated with LoadWAV() or LoadWAVRW().
// (https://wiki.libsdl.org/SDL_FreeWAV)
func FreeWAV(audioBuf []uint8) {
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&audioBuf))
_audioBuf := (*C.Uint8)(unsafe.Pointer(sliceHeader.Data))
C.SDL_FreeWAV(_audioBuf)
}
// BuildAudioCVT initializes an AudioCVT structure for conversion.
// (https://wiki.libsdl.org/SDL_BuildAudioCVT)
func BuildAudioCVT(cvt *AudioCVT, srcFormat AudioFormat, srcChannels uint8, srcRate int, dstFormat AudioFormat, dstChannels uint8, dstRate int) (converted bool, err error) {
switch int(C.SDL_BuildAudioCVT(cvt.cptr(), srcFormat.c(), C.Uint8(srcChannels), C.int(srcRate), dstFormat.c(), C.Uint8(dstChannels), C.int(dstRate))) {
case 1:
return true, nil
case 0:
return false, nil
}
return false, GetError()
}
// ConvertAudio converts audio data to a desired audio format.
// (https://wiki.libsdl.org/SDL_ConvertAudio)
func ConvertAudio(cvt *AudioCVT) error {
_cvt := (*C.SDL_AudioCVT)(unsafe.Pointer(cvt))
if C.SDL_ConvertAudio(_cvt) != 0 {
return GetError()
}
return nil
}
// QueueAudio queues more audio on non-callback devices.
// (https://wiki.libsdl.org/SDL_QueueAudio)
func QueueAudio(dev AudioDeviceID, data []byte) error {
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&data))
_data := unsafe.Pointer(sliceHeader.Data)
_len := (C.Uint32)(sliceHeader.Len)
if C.SDL_QueueAudio(dev.c(), _data, _len) != 0 {
return GetError()
}
return nil
}
// DequeueAudio dequeues more audio on non-callback devices.
// (https://wiki.libsdl.org/SDL_DequeueAudio)
func DequeueAudio(dev AudioDeviceID, data []byte) error {
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&data))
_data := unsafe.Pointer(sliceHeader.Data)
_len := (C.Uint32)(sliceHeader.Len)
if C.SDL_DequeueAudio(dev.c(), _data, _len) != 0 {
return GetError()
}
return nil
}
// GetQueuedAudioSize returns the number of bytes of still-queued audio.
// (https://wiki.libsdl.org/SDL_GetQueuedAudioSize)
func GetQueuedAudioSize(dev AudioDeviceID) uint32 {
return uint32(C.SDL_GetQueuedAudioSize(dev.c()))
}
// ClearQueuedAudio drops any queued audio data waiting to be sent to the hardware.
// (https://wiki.libsdl.org/SDL_ClearQueuedAudio)
func ClearQueuedAudio(dev AudioDeviceID) {
C.SDL_ClearQueuedAudio(dev.c())
}
// MixAudio mixes audio data. New programs might want to use MixAudioFormat() instead.
// (https://wiki.libsdl.org/SDL_MixAudio)
func MixAudio(dst, src *uint8, len uint32, volume int) {
_dst := (*C.Uint8)(unsafe.Pointer(dst))
_src := (*C.Uint8)(unsafe.Pointer(src))
C.SDL_MixAudio(_dst, _src, C.Uint32(len), C.int(volume))
}
// MixAudioFormat mixes audio data in a specified format.
// (https://wiki.libsdl.org/SDL_MixAudioFormat)
func MixAudioFormat(dst, src *uint8, format AudioFormat, len uint32, volume int) {
_dst := (*C.Uint8)(unsafe.Pointer(dst))
_src := (*C.Uint8)(unsafe.Pointer(src))
C.SDL_MixAudioFormat(_dst, _src, format.c(), C.Uint32(len), C.int(volume))
}
// LockAudio locks the audio device. New programs might want to use LockAudioDevice() instead.
// (https://wiki.libsdl.org/SDL_LockAudio)
func LockAudio() {
C.SDL_LockAudio()
}
// LockAudioDevice locks out the audio callback function for a specified device.
// (https://wiki.libsdl.org/SDL_LockAudioDevice)
func LockAudioDevice(dev AudioDeviceID) {
C.SDL_LockAudioDevice(dev.c())
}
// UnlockAudio unlocks the audio device. New programs might want to use UnlockAudioDevice() instead.
// (https://wiki.libsdl.org/SDL_UnlockAudio)
func UnlockAudio() {
C.SDL_UnlockAudio()
}
// UnlockAudioDevice unlocks the audio callback function for a specified device.
// (https://wiki.libsdl.org/SDL_UnlockAudioDevice)
func UnlockAudioDevice(dev AudioDeviceID) {
C.SDL_UnlockAudioDevice(dev.c())
}
// CloseAudio closes the audio device. New programs might want to use CloseAudioDevice() instead.
// (https://wiki.libsdl.org/SDL_CloseAudio)
func CloseAudio() {
C.SDL_CloseAudio()
}
// CloseAudioDevice shuts down audio processing and closes the audio device.
// (https://wiki.libsdl.org/SDL_CloseAudioDevice)
func CloseAudioDevice(dev AudioDeviceID) {
C.SDL_CloseAudioDevice(dev.c())
}
/*
func AudioDeviceConnected(dev AudioDeviceID) int {
_dev := (C.SDL_AudioDeviceID) (dev)
return int (C.SDL_AudioDeviceConnected(_dev))
}
*/