Skip to content

Commit

Permalink
also-alsa: Optimize for the usb8-typed buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
aartaka committed Apr 9, 2022
1 parent 18032b7 commit b2e8ee6
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions also-alsa.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@
pcs)

(defun make-alsa-buffer (&key element-type size channels)
(make-array (* size channels) :element-type element-type))
(if (equalp element-type '(unsigned-byte 8))
(cffi:make-shareable-byte-vector (* size channels))
(make-array (* size channels) :element-type element-type)))

(defun alsa-open (device buffer-size element-type &key direction (sample-rate 44100) (channels-count 2) buffer)
(when buffer
Expand Down Expand Up @@ -310,10 +312,14 @@
(assert (eql (direction pcm) :snd-pcm-stream-playback))
(let* ((expected (/ (buffer-size pcm) (channels-count pcm)))
(element-type (array-element-type (buffer pcm)))
(cffi-type (cffi::ensure-parsed-base-type
(list :array (alsa-element-type element-type) expected)))
(result (with-foreign-array (ptr (buffer pcm) cffi-type)
(snd-pcm-writei (deref (handle pcm)) ptr expected))))
(result (if (equalp element-type '(unsigned-byte 8))
(with-pointer-to-vector-data (ptr (buffer pcm))
(snd-pcm-writei (deref (handle pcm)) ptr expected))
(with-foreign-array
(ptr (buffer pcm)
(cffi::ensure-parsed-base-type
(list :array (alsa-element-type element-type) expected)))
(snd-pcm-writei (deref (handle pcm)) ptr expected)))))
(cond ((= result (- +epipe+))
;; Under run, so prepare and retry
(alsa-warn "Underrun!")
Expand All @@ -326,13 +332,18 @@
(assert (eql (direction pcm) :snd-pcm-stream-capture))
(let* ((expected (/ (buffer-size pcm) (channels-count pcm)))
(element-type (array-element-type (buffer pcm)))
(cffi-type (cffi::ensure-parsed-base-type
(list :array (alsa-element-type element-type) expected)))
(result (with-foreign-array (ptr (buffer pcm) cffi-type)
(prog1
(snd-pcm-readi (deref (handle pcm)) ptr expected)
(setf (slot-value pcm 'buffer)
(foreign-array-to-lisp ptr cffi-type :element-type element-type))))))
(result (if (equalp element-type '(unsigned-byte 8))
(with-pointer-to-vector-data (ptr (buffer pcm))
(snd-pcm-readi (deref (handle pcm)) ptr
(/ (buffer-size pcm) (channels-count pcm))))
(with-foreign-array
(ptr (buffer pcm)
(cffi::ensure-parsed-base-type
(list :array (alsa-element-type element-type) expected)))
(prog1
(snd-pcm-readi (deref (handle pcm)) ptr expected)
(setf (slot-value pcm 'buffer)
(foreign-array-to-lisp ptr cffi-type :element-type element-type)))))))
(unless (= result (/ (buffer-size pcm) (channels-count pcm)))
(if (eql result (- +epipe+))
(progn (alsa-warn "Underrun!") (snd-pcm-prepare (deref (handle pcm))))
Expand Down

0 comments on commit b2e8ee6

Please sign in to comment.