-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalso-alsa.lisp
356 lines (284 loc) · 13.8 KB
/
also-alsa.lisp
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
;;; A very basic Advanced Linux Sound Architecture interface
(in-package #:also-alsa)
(eval-when (:compile-toplevel)
(defconstant +epipe+ 32))
(declaim (inline alsa-element-type to-alsa-format ensure-success))
(defvar *alsa-warn* nil)
(define-foreign-library libasound
(:unix "libasound.so.2")
(t (:default "libasound.so")))
(defun load-alsa ()
(use-foreign-library libasound))
;;; LW FLI can give us grief during cross builds
;;; We better invoke LOAD-ALSA as needed
#-lispworks(use-foreign-library libasound)
(defcenum snd-pcm-class
(:snd-pcm-class-generic 0)
:snd-pcm-class-multi
:snd-pcm-class-modem
:snd-pcm-class-digitizer)
(defcenum snd-pcm-stream
(:snd-pcm-stream-playback 0)
:snd-pcm-stream-capture)
(defcenum snd-pcm-access
(:snd-pcm-access-mmap-interleaved 0)
:snd-pcm-access-mmap-noninterleaved
:snd-pcm-access-mmap-complex
:snd-pcm-access-rw-interleaved
:snd-pcm-access-rw-noninterleaved)
;; incomplete list of formats
(defcenum snd-pcm-format
(:snd-pcm-format-unknown -1)
(:snd-pcm-format-s8 0)
:snd-pcm-format-u8
:snd-pcm-format-s16-le
:snd-pcm-format-s16-be
:snd-pcm-format-u16-le
:snd-pcm-format-u16-be
:snd-pcm-format-s24-le
:snd-pcm-format-s24-be
:snd-pcm-format-u24-le
:snd-pcm-format-u24-be
:snd-pcm-format-s32-le
:snd-pcm-format-s32-be
:snd-pcm-format-u32-le
:snd-pcm-format-u32-be
:snd-pcm-format-float-le
:snd-pcm-format-float-be
:snd-pcm-format-float64-le
:snd-pcm-format-float64-be
:snd-pcm-format-iec958-subframe-le
:snd-pcm-format-iec958-subframe-be
:snd-pcm-format-mu-law
:snd-pcm-format-a-law
:snd-pcm-format-ima-adpcm
:snd-pcm-format-mpeg
:snd-pcm-format-gsm
(:snd-pcm-format-special 31)
(:snd-pcm-format-s24-3le 32)
:snd-pcm-format-s24-3be
:snd-pcm-format-u24-3le
:snd-pcm-format-u24-3be)
(defcenum snd-pcm-state
:snd-pcm-state-open
:snd-pcm-state-setup
:snd-pcm-state-prepared
:snd-pcm-state-running
:snd-pcm-state-xrun
:snd-pcm-state-draining
:snd-pcm-state-paused
:snd-pcm-state-suspended
:snd-pcm-state-disconnected)
(defctype snd-pcm-uframes :ulong)
(defctype snd-pcm-sframes :long)
(defcfun "snd_pcm_open" :int (pcm :pointer) (name :string) (stream snd-pcm-stream) (mode :int))
(defcfun "snd_pcm_state" snd-pcm-state (pcm :pointer))
(defcfun "snd_pcm_hw_params_malloc" :int (dptr :pointer))
(defcfun "snd_pcm_hw_params_any" :int (pcm :pointer) (params :pointer))
(defcfun "snd_pcm_hw_params_set_access" :int (pcm :pointer) (params :pointer) (access snd-pcm-access))
(defcfun "snd_pcm_hw_params_set_format" :int (pcm :pointer) (params :pointer) (format snd-pcm-format))
(defcfun "snd_pcm_hw_params_set_rate" :int (pcm :pointer) (params :pointer) (val :int) (dir :int))
(defcfun "snd_pcm_hw_params_set_channels" :int (pcm :pointer) (params :pointer) (val :int))
(defcfun "snd_pcm_hw_params_get_period_size" :int (pcm :pointer) (valp (:pointer :ulong)) (dir :pointer))
(defcfun "snd_pcm_hw_params" :int (pcm :pointer) (params :pointer))
(defcfun "snd_pcm_hw_params_free" :int (params :pointer))
(defcfun "snd_strerror" :string (val :int))
(defcfun "snd_pcm_prepare" :int (pcm :pointer))
(defcfun "snd_pcm_start" :int (pcm :pointer))
(defcfun "snd_pcm_drain" :int (pcm :pointer))
(defcfun "snd_pcm_drop" :int (pcm :pointer))
(defcfun "snd_pcm_close" :int (pcm :pointer))
(defcfun "snd_pcm_writei" snd-pcm-sframes (pcm :pointer) (buffer :pointer) (size snd-pcm-uframes))
(defcfun "snd_pcm_readi" snd-pcm-sframes (pcm :pointer) (buffer :pointer) (size snd-pcm-uframes))
(defcfun "snd_pcm_wait" :int (pcm :pointer) (timeout :int))
(defcfun "snd_pcm_delay" :int (pcm :pointer) (delayp (:pointer :long)))
(defcfun "snd_pcm_avail_delay" :int (pcm :pointer) (availp (:pointer :long)) (delayp (:pointer :long)))
(defcfun "snd_pcm_sw_params_malloc" :int (dptr :pointer))
(defcfun "snd_pcm_sw_params_current" :int (pcm :pointer) (swparams :pointer))
(defcfun "snd_pcm_sw_params_get_start_threshold" :int (pcm :pointer) (swparams :pointer) (pval (:pointer :ulong)))
(defcfun "snd_pcm_sw_params_set_start_threshold" :int (pcm :pointer) (swparams :pointer) (val :ulong))
(defcfun "snd_pcm_sw_params_set_avail_min" :int (pcm :pointer) (swparams :pointer) (val :ulong))
(defcfun "snd_pcm_sw_params" :int (pcm :pointer) (swparams :pointer))
(declaim (inline deref snd-pcm-writei snd-pcm-readi snd-pcm-avail-delay
snd-pcm-delay snd-pcm-drain snd-pcm-start snd-pcm-wait snd-pcm-prepare))
(defun deref (var)
(mem-ref var :pointer))
(defun ensure-success (value)
(unless (zerop value)
(error "ALSA error: ~A" (snd-strerror value))))
(defun alsa-warn (string)
(when *alsa-warn*
(warn string)))
(defclass pcm-stream ()
((handle :reader handle :initform (foreign-alloc :pointer :initial-contents (list (null-pointer))))
(device :reader device :initarg :device)
(params :reader params :initform (foreign-alloc :pointer :initial-contents (list (null-pointer))))
(swparams :reader swparams :initform (foreign-alloc :pointer :initial-contents (list (null-pointer))))
(pcm-format :reader pcm-format :initarg :pcm-format :initform :snd-pcm-format-s16-le)
(buffer :reader buffer :initarg :buffer)
(buffer-size :reader buffer-size :initarg :buffer-size)
(element-type :reader element-type :initarg :element-type)
(sample-rate :reader sample-rate :initarg :sample-rate :initform 44100)
(direction :reader direction :initarg :direction)
(channels-count :reader channels-count :initarg :channels-count)
(status :accessor status :initform :initial :type (or :initial :open :closed))))
(defun alsa-element-type (type)
(cond ((equalp type '(signed-byte 16)) :int16)
((eql type 'single-float) :float)
((eql type 'double-float) :double)
((equalp type '(unsigned-byte 8)) :uint8)
((equalp type '(signed-byte 8)) :int8)
((equalp type '(unsigned-byte 16)) :uint16)
((equalp type '(unsigned-byte 32)) :uint32)
((equalp type '(signed-byte 32)) :int32)
(t (error "Invalid base type ~A" type))))
(defun to-alsa-format (element-type)
(cond ((eql element-type 'single-float) :snd-pcm-format-float-le)
((eql element-type 'double-float) :snd-pcm-format-float64-le)
((equalp element-type '(unsigned-byte 8)) :snd-pcm-format-u8)
((equalp element-type '(signed-byte 8)) :snd-pcm-format-s8)
((equalp element-type '(unsigned-byte 16)) :snd-pcm-format-u16-le)
((equalp element-type '(signed-byte 16)) :snd-pcm-format-s16-le)
((equalp element-type '(unsigned-byte 32)) :snd-pcm-format-u32-le)
((equalp element-type '(signed-byte 32)) :snd-pcm-format-s32-le)
(t (error "Invalid base type ~A" element-type))))
(defun alsa-open-2 (pcs)
(ensure-success (snd-pcm-open (handle pcs) (device pcs) (direction pcs) 0))
(setf (status pcs) :open)
(ensure-success (snd-pcm-hw-params-malloc (params pcs)))
(ensure-success (snd-pcm-hw-params-any (deref (handle pcs)) (deref (params pcs))))
(ensure-success (snd-pcm-hw-params-set-access (deref (handle pcs)) (deref (params pcs)) :snd-pcm-access-rw-interleaved))
(ensure-success (snd-pcm-hw-params-set-format (deref (handle pcs)) (deref (params pcs)) (pcm-format pcs)))
(ensure-success (snd-pcm-hw-params-set-rate (deref (handle pcs)) (deref (params pcs)) (sample-rate pcs) 0))
(ensure-success (snd-pcm-hw-params-set-channels (deref (handle pcs)) (deref (params pcs)) (channels-count pcs)))
(ensure-success (snd-pcm-hw-params (deref (handle pcs)) (deref (params pcs))))
(cffi:with-foreign-object (period :uint)
(ensure-success (snd-pcm-hw-params-get-period-size (deref (params pcs)) period (cffi:null-pointer))))
(snd-pcm-hw-params-free (deref (params pcs)))
(ensure-success (snd-pcm-prepare (deref (handle pcs))))
(ensure-success (snd-pcm-sw-params-malloc (swparams pcs)))
(ensure-success (snd-pcm-sw-params-current (deref (handle pcs)) (deref (swparams pcs))))
(ensure-success (snd-pcm-sw-params (deref (handle pcs)) (deref (swparams pcs))))
pcs)
(defun make-alsa-buffer (&key element-type size channels)
(cffi:make-shareable-byte-vector (* (cffi:foreign-type-size (alsa-element-type element-type)) size channels)))
(defun alsa-open (device buffer-size element-type &key direction (sample-rate 44100) (channels-count 2) buffer)
(when buffer
(assert (and (subtypep (type-of buffer) 'simple-array)
(subtypep (array-element-type buffer) element-type))))
(let ((pcs (make-instance 'pcm-stream
:direction (case direction
(:input :snd-pcm-stream-capture)
(:output :snd-pcm-stream-playback))
:device device
:element-type element-type
:buffer (or buffer
(make-alsa-buffer :element-type element-type :size buffer-size :channels channels-count))
:buffer-size (* buffer-size channels-count) ;number of samples really
:channels-count channels-count
:sample-rate sample-rate
:pcm-format (to-alsa-format element-type))))
(alsa-open-2 pcs)))
(defgeneric alsa-reopen (pcs device buffer-size element-type &key direction sample-rate channels-count)
(:documentation "Reopens the stream. If all parameters are the same, just keeps the exiting one."))
(defmethod alsa-reopen ((pcs pcm-stream) device buffer-size element-type &key direction (sample-rate 44100) (channels-count 2))
(if (or (eql (status pcs) :initial)
(not (and (equal device (device pcs))
(= (* buffer-size channels-count) (buffer-size pcs))
(equal element-type (element-type pcs))
(eql direction (case (direction pcs)
(:snd-pcm-stream-capture :input)
(:snd-pcm-stream-playback :output)))
(= sample-rate (sample-rate pcs))
(= channels-count (channels-count pcs)))))
(progn
(when (eql (status pcs) :open)
(alsa-close pcs))
(alsa-open-2 (reinitialize-instance pcs
:direction (case direction
(:input :snd-pcm-stream-capture)
(:output :snd-pcm-stream-playback))
:device device
:element-type element-type
:buffer (cffi:make-shareable-byte-vector
(* (cffi:foreign-type-size (alsa-element-type element-type)) buffer-size channels-count))
#+nil(foreign-alloc (alsa-element-type element-type)
:count (* (cffi:foreign-type-size (alsa-element-type element-type)) buffer-size channels-count))
:buffer-size (* buffer-size channels-count) ;number of samples really
:channels-count channels-count
:sample-rate sample-rate
:pcm-format (to-alsa-format element-type))))
(snd-pcm-drop (deref (handle pcs))))
pcs)
(defmethod ref ((pcm pcm-stream) position)
(error "Deprecated method, use AREF on the (buffer pcm) instead"))
(defmethod (setf ref) (value (pcm pcm-stream) position)
(error "Deprecated, use aref on the (buffer pcm) instead"))
(defmethod drain ((pcm pcm-stream))
(snd-pcm-drain (deref (handle pcm))))
(defmethod alsa-start ((pcm pcm-stream))
(snd-pcm-start (deref (handle pcm))))
(defmethod alsa-resume ((pcm pcm-stream))
(snd-pcm-prepare (deref (handle pcm))))
(defmethod get-delay ((pcm pcm-stream))
(snd-pcm-prepare (deref (handle pcm)))
(cffi:with-foreign-object (result :long)
(let ((error-code (snd-pcm-delay (deref (handle pcm)) result)))
(cond ((eql error-code (- +epipe+))
(alsa-warn "Pipe busted!") 0)
((minusp error-code) (error "ALSA error: ~A" error-code))
(t (mem-ref result :uint))))))
(defmethod get-avail-delay ((pcm pcm-stream))
(cffi:with-foreign-objects ((avail :long) (delay :long))
(let ((error-code (snd-pcm-avail-delay (deref (handle pcm)) avail delay)))
(cond ((eql error-code (- +epipe+)) (alsa-warn "Pipe busted!") (values 0 0))
((minusp error-code) (error "ALSA error: ~A" error-code))
(t (values (mem-ref avail :uint) (mem-ref delay :uint)))))))
(defmethod alsa-close ((pcm pcm-stream))
(when (eq (status pcm) :open)
(snd-pcm-drain (deref (handle pcm)))
(snd-pcm-close (deref (handle pcm))))
(setf (status pcm) :closed)
pcm)
(defmethod alsa-wait ((pcm pcm-stream) &optional (timeout -1))
(snd-pcm-wait (deref (handle pcm)) timeout))
(defmethod alsa-write ((pcm pcm-stream))
(assert (eql (direction pcm) :snd-pcm-stream-playback))
(let* ((expected (/ (buffer-size pcm) (channels-count pcm)))
(result (with-pointer-to-vector-data (ptr (buffer pcm))
(snd-pcm-writei (deref (handle pcm)) ptr expected))))
(cond ((= result (- +epipe+))
;; Under run, so prepare and retry
(alsa-warn "Underrun!")
(snd-pcm-prepare (deref (handle pcm)))
(alsa-write pcm))
((/= result expected)
(error "ALSA error: ~A" result)))))
(defmethod alsa-read ((pcm pcm-stream))
(assert (eql (direction pcm) :snd-pcm-stream-capture))
(let ((result (with-pointer-to-vector-data (ptr (buffer pcm))
(snd-pcm-readi (deref (handle pcm)) ptr (/ (buffer-size pcm) (channels-count pcm))))))
(unless (= result (/ (buffer-size pcm) (channels-count pcm)))
(if (eql result (- +epipe+))
(progn (alsa-warn "Underrun!") (snd-pcm-prepare (deref (handle pcm))))
(error "ALSA error: ~A" result)))
pcm))
(defmethod contents-to-lisp ((pcm pcm-stream))
(let ((result (make-array (buffer-size pcm) :element-type (element-type pcm))))
(loop for i from 0 below (buffer-size pcm) do
(setf (aref result i) (ref pcm i)))
result))
(defmacro with-alsa-device ((stream device buffer-size element-type &key direction (sample-rate 44100) (channels-count 2) buffer) &body body)
(assert direction)
`(let ((,stream (also-alsa:alsa-open ,device ,buffer-size ,element-type
:buffer ,buffer
:direction ,direction :sample-rate ,sample-rate :channels-count ,channels-count)))
(unwind-protect
(progn ,@body)
(also-alsa:drain ,stream)
(also-alsa:alsa-close ,stream))))
(defmacro with-alsa-buffer ((buffer pcm &body body))
`(cffi:with-pointer-to-vector-data (,buffer (buffer ,pcm))
,@body))
(defmethod get-state ((pcm pcm-stream))
(snd-pcm-state (deref (handle pcm))))