Library loader locks mpg123 process while running.
authorTim Vaughan <timv@ughan.xyz>
Thu, 12 Dec 2019 10:36:40 +0000 (11:36 +0100)
committerTim Vaughan <timv@ughan.xyz>
Thu, 12 Dec 2019 10:36:40 +0000 (11:36 +0100)
emus.el

diff --git a/emus.el b/emus.el
index 71c6bd2..75b92e0 100644 (file)
--- a/emus.el
+++ b/emus.el
 (require 'seq)
 
 
-;;; Global constants
-;;
-
-(defconst emus-version "1.0.0"
-  "Current version of emus.")
-
-
 ;;; Customizations
 ;;
 
   '((t :inherit bold))
   "Face used for current track cursor")
 
+;;; mpg123 process
+;;
+
+(defvar emus--proc-in-use nil)
+
+(defun emus-get-process ()
+  "Return current or new mpg123 process."
+  (let* ((emus-process-raw (get-process "emus-process"))
+         (emus-process (if emus-process-raw
+                           (if (process-live-p emus-process-raw)
+                               emus-process-raw
+                             (kill-process emus-process-raw)
+                             nil))))
+    (if emus-process
+        emus-process
+      (let ((proc
+             (make-process :name "emus-process"
+                           ;; :buffer (get-buffer-create "*emus-process*")
+                           :command `(,emus-mpg123-program "-R"))))
+        (set-process-query-on-exit-flag proc nil)
+        (process-send-string proc "silence\n")
+        proc))))
+
+(defun emus--send-cmd-raw (cmd &rest args)
+    (process-send-string (emus-get-process)
+                         (concat
+                          (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
+                          "\n")))
+
+(defun emus-send-cmd (cmd &rest args)
+  (unless emus--proc-in-use
+    (apply #'emus--send-cmd-raw cmd args)))
+
+
 ;;; Library
 ;;
 
   (aset track 4 pos))
 
 (defun emus--load-library (then)
-  (emus--suspend-cp)
-  (setq emus-state 'stopped)
-  (let ((proc (emus-get-process))
-        (tagstr "")
-        (filenames (emus-get-audio-files)))
-    (setq emus-tracks nil)
-    (set-process-filter proc (lambda (proc string)
-                               (setq tagstr (concat tagstr string))
-                               (when (string-suffix-p "@P 1\n" string)
-                                 (add-to-list 'emus-tracks
-                                              (emus--make-track-from-tagstr (car filenames)
-                                                                            tagstr))
-                                 (setq tagstr "")
-                                 (setq filenames (cdr filenames))
-                                 (if filenames
-                                     (emus-send-cmd "lp" (car filenames))
-                                   (set-process-filter proc nil)
-                                   (setq emus-tracks (reverse emus-tracks))
-                                   (emus--sort-tracks)
-                                   (unless emus-current-track
-                                     (setq emus-current-track (car emus-tracks)))
-                                   (funcall then)
-                                   ;; (emus-render-tracks)
-                                   (emus--resume-cp)))))
-    (emus-send-cmd "lp" (car filenames))))
+  (unless emus--proc-in-use
+    (setq emus--proc-in-use t)
+    (emus--suspend-cp)
+    (setq emus-state 'stopped)
+    (let ((proc (emus-get-process))
+          (tagstr "")
+          (filenames (emus-get-audio-files)))
+      (setq emus-tracks nil)
+      (set-process-filter proc (lambda (proc string)
+                                 (setq tagstr (concat tagstr string))
+                                 (when (string-suffix-p "@P 1\n" string)
+                                   (add-to-list 'emus-tracks
+                                                (emus--make-track-from-tagstr (car filenames)
+                                                                              tagstr))
+                                   (setq tagstr "")
+                                   (setq filenames (cdr filenames))
+                                   (if filenames
+                                       (emus--send-cmd-raw "lp" (car filenames))
+                                     (set-process-filter proc nil)
+                                     (setq emus-tracks (reverse emus-tracks))
+                                     (emus--sort-tracks)
+                                     (unless emus-current-track
+                                       (setq emus-current-track (car emus-tracks)))
+                                     (funcall then)
+                                     (emus--resume-cp)
+                                     (setq emus--proc-in-use nil)))))
+      (emus--send-cmd-raw "lp" (car filenames)))))
 
 (defun emus--make-track-from-tagstr (filename tagstr)
   (let ((artist "")
      (emus--load-library
       (lambda () ,@args))))
 
-;;; mpg123 process
-;;
-
-(defvar emus-proc-in-use nil)
-
-(defun emus-get-process ()
-  "Return current or new mpg123 process."
-  (let* ((emus-process-raw (get-process "emus-process"))
-         (emus-process (if emus-process-raw
-                           (if (process-live-p emus-process-raw)
-                               emus-process-raw
-                             (kill-process emus-process-raw)
-                             nil))))
-    (if emus-process
-        emus-process
-      (let ((proc
-             (make-process :name "emus-process"
-                           ;; :buffer (get-buffer-create "*emus-process*")
-                           :command `(,emus-mpg123-program "-R"))))
-        (set-process-query-on-exit-flag proc nil)
-        (process-send-string proc "silence\n")
-        proc))))
-
-
-(defun emus-send-cmd (cmd &rest args)
-  (process-send-string (emus-get-process)
-                       (concat
-                        (seq-reduce (lambda (s1 s2) (concat s1 " " s2)) args cmd)
-                        "\n")))
-
-(defun emus-send-and-process (respfun predfun cmd &rest args)
-  (let ((respstr ""))
-    (set-process-filter (emus-get-process)
-                        (lambda (proc string)
-                          (setq respstr (concat respstr string))
-                          (when (funcall predfun respstr)
-                            (set-process-filter proc nil)
-                            (funcall respfun respstr))))
-    (apply #'emus-send-cmd cmd args)))
-
 
 ;;; Playback
 ;;