;;;; sym2voice

;;;; This is the worst and most kludgy util I have ever posted.
;;;; Kinda cool though. Uses libsndfile and openbsd aucat and ffmpeg.
;;;; No particular reason it's not just libsndfile.

(defpackage sym2voice (:use cl cl-user)(:nicknames s2v))
(in-package sym2voice)

(defvar *test* '(1 (2) (3 (4)) 5))
(defvar *workfile* "work.wav")

(defun rec ()
(ext:run-program "aucat" `("-o" ,*workfile*)))

(defun sav ()
(when *missing*
 (ext:run-program "mv"
  `(,*workfile* ,(sound-of (pop *missing*))))
 (check-missing)
 (when *missing*
  (format t "Next do: ~s~%" (car *missing*)))))

(defun ply ()
(ext:run-program "aucat" `("-i" ,*workfile*)))

(defvar *out* (list))
(defun deflate (list)
(loop for l in list do
 (if (listp l) (deflate l)
  (push l *out*))
 initially (push '|(| *out*)
 finally (push '|)| *out*)))

(defun sound-of (sym)
(format nil "voice/~a.wav"
       (pathname-name (pathname (format nil "~s" sym)))))

(defun sound-p (sym)
(directory (sound-of sym)))

(defvar *missing* (list))
(defun check-missing ()
(setq *missing*
 (mapcan (lambda (x) (and (not (sound-p x)) (list x)))
        *out*)))

(defun cat ()
(ext:run-program "sndfile-concat"
 (append (mapcar 'sound-of (reverse *out*))
        (list *workfile*)))
(ext:system "ffmpeg -i work.wav -af \"highpass=f=300\" -y high.wav")
(ext:system "ffmpeg -i high.wav -af \"atempo=1.5\" -y fast.wav")
(ext:system "ffmpeg -i fast.wav -af \"adeclick\" -y clickless.wav")
(ext:system "ffmpeg -i clickless.wav -af \"volume=1.7\" -y loud.opus"))

(defun file-test (path)
(setq *test*
 (with-open-file (in path :direction :input)
  (loop for read = (read in nil nil)
   while read collecting read))))