Hello,

On my system the sink keeps changing it's number when I plug and unplug
it from my computer so I wanted to use the special sink
"@DEFAULT_SINK@".  This sink can be passed to pactl as an argument but
doesn't show up when running "pactl list sinks" so I had to modify
emms-volume--pulse-get-volume.

>From a3c6fc729618dacb93aba0bcef09ccc8cd39c3d7 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email protected]>
Date: Wed, 21 Sep 2022 11:13:31 -0400
Subject: [PATCH 1/2] Ask pactl for sink by name

This allows us to ask for special names like @DEFAULT_SINK@
---
 emms-volume-pulse.el | 41 +++++++++++++----------------------------
 1 file changed, 13 insertions(+), 28 deletions(-)

diff --git a/emms-volume-pulse.el b/emms-volume-pulse.el
index 5211e69..504bb83 100644
--- a/emms-volume-pulse.el
+++ b/emms-volume-pulse.el
@@ -42,8 +42,6 @@
 
 ;;; Code:
 
-(require 'cl-lib)
-
 ;; TODO: it would be great if custom could have
 ;; choices based on pactl list short sinks | cut -f1-2
 
@@ -61,34 +59,21 @@ See full list of devices on your system by running
   :type 'integer
   :group 'emms-volume)
 
-
 (defun emms-volume--pulse-get-volume ()
   "Return `emms-volume-pulse-sink' volume."
-  (let ((sink-number-p (numberp emms-volume-pulse-sink))
-        (output
-         (shell-command-to-string
-          (concat "pactl list sinks" "|"
-                  "grep -E -e 'Sink' -e 'Name' -e '^[^a-zA-Z]*Volume'"))))
-    (string-to-number
-     (car
-      (reverse
-       (funcall
-        (if sink-number-p #'assq #'assoc)
-        emms-volume-pulse-sink
-        (mapcar (if sink-number-p 'identity 'cdr)
-                (cl-loop while
-			 (string-match
-			  (mapconcat #'identity
-				     '(".*Sink[ \t]+\\#\\([0-9]+\\)"
-				       ".*Name:[ \t]\\([^\n]+\\)"
-				       ".*Volume:.*?\\([0-9]+\\)%.*\n?")
-				     "\n")
-			  output)
-			 collect (list (string-to-number (match-string 1 output))
-				       (match-string 2 output)
-				       (match-string 3 output))
-			 do (setq output (replace-match "" nil nil output))))))))))
-
+  (let* ((sink-string (if (numberp emms-volume-pulse-sink)
+                         (number-to-string emms-volume-pulse-sink)
+                       emms-volume-pulse-sink))
+         (output
+          (shell-command-to-string
+           (concat "pactl get-sink-volume " sink-string))))
+	(string-match
+	 ".*Volume:.*?\\([0-9]+\\)%.*\n?"
+	 output)
+    (let ((volume (match-string 1 output)))
+      (if volume
+          (string-to-number volume)
+        (error "Could not find pulse sink %s" sink-string)))))
 
 ;;;###autoload
 (defun emms-volume-pulse-change (amount)
-- 
2.37.3

>From d848df05a6b39448ac5a9f6ce58874df28977d20 Mon Sep 17 00:00:00 2001
From: Morgan Smith <[email protected]>
Date: Wed, 21 Sep 2022 11:18:00 -0400
Subject: [PATCH 2/2] Change default of emms-volume-pulse-sink to
 "@DEFAULT_SINK@"

---
 emms-volume-pulse.el | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/emms-volume-pulse.el b/emms-volume-pulse.el
index 504bb83..e17564c 100644
--- a/emms-volume-pulse.el
+++ b/emms-volume-pulse.el
@@ -45,7 +45,7 @@
 ;; TODO: it would be great if custom could have
 ;; choices based on pactl list short sinks | cut -f1-2
 
-(defcustom emms-volume-pulse-sink 0
+(defcustom emms-volume-pulse-sink "@DEFAULT_SINK@"
   "The sink to use for volume adjustment.
 
 See full list of devices on your system by running
-- 
2.37.3

Thanks,

Morgan

Reply via email to