-
Notifications
You must be signed in to change notification settings - Fork 15
/
alsa.go
340 lines (312 loc) · 9.68 KB
/
alsa.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
// Copyright 2015-2016 Cocoon Labs Ltd.
//
// See LICENSE file for terms and conditions.
// Package alsa provides Go bindings to the ALSA library.
package alsa
import (
"errors"
"fmt"
"reflect"
"runtime"
"unsafe"
)
/*
#cgo LDFLAGS: -lasound
#include "reader_thread.h"
#include <alsa/asoundlib.h>
#include <stdint.h>
*/
import "C"
// Format is the type used for specifying sample formats.
type Format C.snd_pcm_format_t
// The range of sample formats supported by ALSA.
const (
FormatS8 = C.SND_PCM_FORMAT_S8
FormatU8 = C.SND_PCM_FORMAT_U8
FormatS16LE = C.SND_PCM_FORMAT_S16_LE
FormatS16BE = C.SND_PCM_FORMAT_S16_BE
FormatU16LE = C.SND_PCM_FORMAT_U16_LE
FormatU16BE = C.SND_PCM_FORMAT_U16_BE
FormatS24LE = C.SND_PCM_FORMAT_S24_LE
FormatS24BE = C.SND_PCM_FORMAT_S24_BE
FormatU24LE = C.SND_PCM_FORMAT_U24_LE
FormatU24BE = C.SND_PCM_FORMAT_U24_BE
FormatS32LE = C.SND_PCM_FORMAT_S32_LE
FormatS32BE = C.SND_PCM_FORMAT_S32_BE
FormatU32LE = C.SND_PCM_FORMAT_U32_LE
FormatU32BE = C.SND_PCM_FORMAT_U32_BE
FormatFloatLE = C.SND_PCM_FORMAT_FLOAT_LE
FormatFloatBE = C.SND_PCM_FORMAT_FLOAT_BE
FormatFloat64LE = C.SND_PCM_FORMAT_FLOAT64_LE
FormatFloat64BE = C.SND_PCM_FORMAT_FLOAT64_BE
)
var (
// ErrOverrun signals an overrun error
ErrOverrun = errors.New("overrun")
// ErrUnderrun signals an underrun error
ErrUnderrun = errors.New("underrun")
)
// BufferParams specifies the buffer parameters of a device.
type BufferParams struct {
BufferFrames int
PeriodFrames int
Periods int
}
type device struct {
h *C.snd_pcm_t
Channels int
Format Format
Rate int
BufferParams BufferParams
frames int
readerThread *C.reader_thread_state
}
func createError(errorMsg string, errorCode C.int) (err error) {
strError := C.GoString(C.snd_strerror(errorCode))
err = fmt.Errorf("%s: %s", errorMsg, strError)
return
}
func (d *device) createDevice(deviceName string, channels int, format Format, rate int, playback bool, bufferParams BufferParams) (err error) {
deviceCString := C.CString(deviceName)
defer C.free(unsafe.Pointer(deviceCString))
var ret C.int
if playback {
ret = C.snd_pcm_open(&d.h, deviceCString, C.SND_PCM_STREAM_PLAYBACK, 0)
} else {
ret = C.snd_pcm_open(&d.h, deviceCString, C.SND_PCM_STREAM_CAPTURE, 0)
}
if ret < 0 {
return fmt.Errorf("could not open ALSA device %s", deviceName)
}
runtime.SetFinalizer(d, (*device).Close)
var hwParams *C.snd_pcm_hw_params_t
ret = C.snd_pcm_hw_params_malloc(&hwParams)
if ret < 0 {
return createError("could not alloc hw params", ret)
}
defer C.snd_pcm_hw_params_free(hwParams)
ret = C.snd_pcm_hw_params_any(d.h, hwParams)
if ret < 0 {
return createError("could not set default hw params", ret)
}
ret = C.snd_pcm_hw_params_set_access(d.h, hwParams, C.SND_PCM_ACCESS_RW_INTERLEAVED)
if ret < 0 {
return createError("could not set access params", ret)
}
ret = C.snd_pcm_hw_params_set_format(d.h, hwParams, C.snd_pcm_format_t(format))
if ret < 0 {
return createError("could not set format params", ret)
}
ret = C.snd_pcm_hw_params_set_channels(d.h, hwParams, C.uint(channels))
if ret < 0 {
return createError("could not set channels params", ret)
}
ret = C.snd_pcm_hw_params_set_rate(d.h, hwParams, C.uint(rate), 0)
if ret < 0 {
return createError("could not set rate params", ret)
}
var bufferSize = C.snd_pcm_uframes_t(bufferParams.BufferFrames)
if bufferParams.BufferFrames == 0 {
// Default buffer size: max buffer size
ret = C.snd_pcm_hw_params_get_buffer_size_max(hwParams, &bufferSize)
if ret < 0 {
return createError("could not get buffer size", ret)
}
}
ret = C.snd_pcm_hw_params_set_buffer_size_near(d.h, hwParams, &bufferSize)
if ret < 0 {
return createError("could not set buffer size", ret)
}
// Default period size: 1/8 of a second
var periodFrames = C.snd_pcm_uframes_t(rate / 8)
if bufferParams.PeriodFrames > 0 {
periodFrames = C.snd_pcm_uframes_t(bufferParams.PeriodFrames)
} else if bufferParams.Periods > 0 {
periodFrames = C.snd_pcm_uframes_t(int(bufferSize) / bufferParams.Periods)
}
ret = C.snd_pcm_hw_params_set_period_size_near(d.h, hwParams, &periodFrames, nil)
if ret < 0 {
return createError("could not set period size", ret)
}
var periods = C.uint(0)
ret = C.snd_pcm_hw_params_get_periods(hwParams, &periods, nil)
if ret < 0 {
return createError("could not get periods", ret)
}
ret = C.snd_pcm_hw_params(d.h, hwParams)
if ret < 0 {
return createError("could not set hw params", ret)
}
d.frames = int(periodFrames)
d.Channels = channels
d.Format = format
d.Rate = rate
d.BufferParams.BufferFrames = int(bufferSize)
d.BufferParams.PeriodFrames = int(periodFrames)
d.BufferParams.Periods = int(periods)
return
}
// Close closes a device and frees the resources associated with it.
func (d *device) Close() {
if d.h != nil {
C.snd_pcm_drain(d.h)
C.snd_pcm_close(d.h)
d.h = nil
}
if d.readerThread != nil {
C.reader_thread_stop(d.readerThread)
d.readerThread = nil
}
runtime.SetFinalizer(d, nil)
}
func (d device) formatSampleSize() (s int) {
switch d.Format {
case FormatS8, FormatU8:
return 1
case FormatS16LE, FormatS16BE, FormatU16LE, FormatU16BE:
return 2
case FormatS24LE, FormatS24BE, FormatU24LE, FormatU24BE, FormatS32LE, FormatS32BE, FormatU32LE, FormatU32BE, FormatFloatLE, FormatFloatBE:
return 4
case FormatFloat64LE, FormatFloat64BE:
return 8
}
panic("unsupported format")
}
// CaptureDevice is an ALSA device configured to record audio.
type CaptureDevice struct {
device
}
// NewCaptureDevice creates a new CaptureDevice object.
func NewCaptureDevice(deviceName string, channels int, format Format, rate int, bufferParams BufferParams) (c *CaptureDevice, err error) {
c = new(CaptureDevice)
err = c.createDevice(deviceName, channels, format, rate, false, bufferParams)
if err != nil {
return nil, err
}
return c, nil
}
func (c *CaptureDevice) StartReadThread() error {
if c.readerThread != nil {
return errors.New("Reader thread already running")
}
periodBytes := C.int(c.formatSampleSize() * c.Channels * c.BufferParams.PeriodFrames)
// Alocate a 1 second buffer
nbuf := C.int(c.Rate / c.BufferParams.PeriodFrames)
c.readerThread = C.reader_thread_start(c.h, periodBytes, C.int(c.BufferParams.PeriodFrames), nbuf)
if c.readerThread == nil {
return fmt.Errorf("C.reader_thread_start: %s", C.GoString(C.reader_thread_error))
}
return nil
}
// Read reads samples into a buffer and returns the amount read.
func (c *CaptureDevice) Read(buffer interface{}) (samples int, err error) {
bufferType := reflect.TypeOf(buffer)
if !(bufferType.Kind() == reflect.Array ||
bufferType.Kind() == reflect.Slice) {
return 0, errors.New("Read requires an array type")
}
sizeError := errors.New("Read requires a matching sample size")
switch bufferType.Elem().Kind() {
case reflect.Int8:
if c.formatSampleSize() != 1 {
return 0, sizeError
}
case reflect.Int16:
if c.formatSampleSize() != 2 {
return 0, sizeError
}
case reflect.Int32, reflect.Float32:
if c.formatSampleSize() != 4 {
return 0, sizeError
}
case reflect.Float64:
if c.formatSampleSize() != 8 {
return 0, sizeError
}
default:
return 0, errors.New("Read does not support this format")
}
val := reflect.ValueOf(buffer)
length := val.Len()
sliceData := val.Slice(0, length)
frames := length / c.Channels
bufPtr := unsafe.Pointer(sliceData.Index(0).Addr().Pointer())
if c.readerThread != nil {
if frames != c.BufferParams.PeriodFrames {
return 0, errors.New("buffer size must match period")
}
rc := C.reader_thread_poll(c.readerThread, bufPtr)
if rc == 1 {
return 0, ErrOverrun
} else if rc != 0 {
return 0, fmt.Errorf("read error: %s", C.GoString(C.reader_thread_error))
}
samples = frames * c.Channels
} else {
ret := C.snd_pcm_readi(c.h, bufPtr, C.snd_pcm_uframes_t(frames))
if ret == -C.EPIPE {
C.snd_pcm_prepare(c.h)
return 0, ErrOverrun
} else if ret < 0 {
return 0, createError("read error", C.int(ret))
}
samples = int(ret) * c.Channels
}
return
}
// PlaybackDevice is an ALSA device configured to playback audio.
type PlaybackDevice struct {
device
}
// NewPlaybackDevice creates a new PlaybackDevice object.
func NewPlaybackDevice(deviceName string, channels int, format Format, rate int, bufferParams BufferParams) (p *PlaybackDevice, err error) {
p = new(PlaybackDevice)
err = p.createDevice(deviceName, channels, format, rate, true, bufferParams)
if err != nil {
return nil, err
}
return p, nil
}
// Write writes a buffer of data to a playback device.
func (p *PlaybackDevice) Write(buffer interface{}) (samples int, err error) {
bufferType := reflect.TypeOf(buffer)
if !(bufferType.Kind() == reflect.Array ||
bufferType.Kind() == reflect.Slice) {
return 0, errors.New("Write requires an array type")
}
sizeError := errors.New("Write requires a matching sample size")
switch bufferType.Elem().Kind() {
case reflect.Int8:
if p.formatSampleSize() != 1 {
return 0, sizeError
}
case reflect.Int16:
if p.formatSampleSize() != 2 {
return 0, sizeError
}
case reflect.Int32, reflect.Float32:
if p.formatSampleSize() != 4 {
return 0, sizeError
}
case reflect.Float64:
if p.formatSampleSize() != 8 {
return 0, sizeError
}
default:
return 0, errors.New("Write does not support this format")
}
val := reflect.ValueOf(buffer)
length := val.Len()
sliceData := val.Slice(0, length)
var frames = C.snd_pcm_uframes_t(length / p.Channels)
bufPtr := unsafe.Pointer(sliceData.Index(0).Addr().Pointer())
ret := C.snd_pcm_writei(p.h, bufPtr, frames)
if ret == -C.EPIPE {
C.snd_pcm_prepare(p.h)
return 0, ErrUnderrun
} else if ret < 0 {
return 0, createError("write error", C.int(ret))
}
samples = int(ret) * p.Channels
return
}