From 134b682e1b549f6abb454a042fe07c84f220355d Mon Sep 17 00:00:00 2001 From: Ralph Ronnquist Date: Fri, 28 Apr 2023 02:02:27 +1000 Subject: [PATCH 1/1] cleanup/simplify --- alsa-dispatcher.lsp | 48 ++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) mode change 100644 => 100755 alsa-dispatcher.lsp diff --git a/alsa-dispatcher.lsp b/alsa-dispatcher.lsp old mode 100644 new mode 100755 index 8468a12..2948d05 --- a/alsa-dispatcher.lsp +++ b/alsa-dispatcher.lsp @@ -1,3 +1,5 @@ +#!/usr/bin/newlisp + ;; This is the main script for the pcm-dispatch tool ;; last main-arg nominates the configuration file @@ -66,8 +68,7 @@ ) ;; (snd_pcm_close PCM) - Close PCM handle. Closes the given PCM handle -;; and frees all associated resources. (The PCM reference is probably -;; "invalid" after closing) +;; and frees all associated resources. (import libasound.so "snd_pcm_close" "int" "void*" ; snd_pcm_t *pcm ) @@ -105,15 +106,6 @@ "unsigned int" ; unsigned int latency ) -;; (snd_pcm_wait PCM TIMEOUT) - Wait for a PCM to become ready. -;; Returns a positive value on success otherwise a negative error code -;; (-EPIPE [-32] for the xrun and -ESTRPIPE [-86] for the suspended -;; status, others for general errors) -(import libasound.so "snd_pcm_wait" "int" - "void*" ; snd_pcm_t *pcm - "int" ; int timeout - ) - ;; (snd_pcm_writei PCM BUFFER FRAMES) - Write interleaved frames to a ;; PCM. If the blocking behaviour is selected and the PCM is running, ;; then routine waits until all requested frames are played or put to @@ -130,56 +122,50 @@ (snd_pcm_open NAME SND_PCM_STREAM_PLAYBACK SND_PCM_MODE_BLOCK)) ;; Setup PCM -(define (setup-pcm PCM) +(define (setup-pcm PCM NAME) (snd_pcm_set_params PCM - (cfg-lookup PCM "format" SND_PCM_FORMAT_S16_LE) + (cfg-lookup NAME "format" SND_PCM_FORMAT_S16_LE) SND_PCM_ACCESS_RW_INTERLEAVED - (cfg-lookup PCM "channels" 2) - (cfg-lookup PCM "rate" 48000) + (cfg-lookup NAME "channels" 2) + (cfg-lookup NAME "rate" 48000) 1 ; soft resample (0/1) - (cfg-lookup PCM "latency" 100000) ; (microseconds) + (cfg-lookup NAME "latency" 100000) ; (microseconds) )) ;; ############################################################ ; The main program ;; redirect stdout/err to /dev/null -(when nil +(when true (let ((REDIR (open "/dev/null" "append"))) (when (< REDIR) (exit 1)) - (when (< (libc:dup2 REDIR 2)) (exit 1)) - (when (< (libc:dup2 REDIR 1)) (exit 1)) + (when (< (dup2 REDIR 2)) (exit 1)) + (when (< (dup2 REDIR 1)) (exit 1)) (close REDIR) )) ; find the first usable PCM -(setf PCM (unless (dolist (NAME PCM-LIST (open-pcm NAME))) +(setf PCM (unless (dolist (N PCM-LIST (open-pcm (setf NAME N)))) (die 1 "No PCM available"))) ; configure the PCM -(when (!= (setf E (setup-pcm PCM))) +(when (!= (setf E (setup-pcm PCM NAME))) (snd_pcm_close PCM) - (die 1 "set params" E)) + (die 1 "setup pcm" E)) (setf N 0 x 0 E 0 i 0) -(setf CACHE nil) -(while (> (or (setf N (read 0 BUFFER 20000000)) 0)) - ;(println "reading " N) - (setf CACHE BUFFER) +(while (> (or (setf N (read 0 BUFFER 1000000)) 0)) (while (> N) - ;;(ready PCM) - (when (< (setf E (snd_pcm_writei PCM CACHE (/ N 4)))) + (when (< (setf E (snd_pcm_writei PCM BUFFER (/ N 4)))) (snd_pcm_close PCM) (die 1 "writing" E N)) (setf E (* E 4)) - (setf CACHE (E CACHE)) - ;(println "wrote " E) + (setf BUFFER (E BUFFER)) (dec N E) ) ) -(snd_pcm_wait PCM -1) (snd_pcm_drain PCM) (snd_pcm_close PCM) (exit 0) -- 2.39.2