diff options
| author | Craig Jennings <c@cjennings.net> | 2026-02-26 18:01:10 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2026-02-26 18:01:10 -0600 |
| commit | ed5e3f61d00a9462801e15179fcceca65315050a (patch) | |
| tree | 48c538709ff5fc4691e24bde206f3a43dfd6048a | |
| parent | f47573e8eddb02db7f3cbf79a8c1825bf1b32323 (diff) | |
feat(recording): add mic indicators and fix misleading sink icon
Mic step now shows with green/dim coloring to indicate which mics
are actively in use (RUNNING state), with active mics sorted to top.
Changed sink inactive icon from (slashed speaker, reads as
"broken/muted") to (plain speaker, reads as "no streams right
now"). Active sinks still show .
| -rw-r--r-- | modules/video-audio-recording.el | 59 | ||||
| -rw-r--r-- | tests/test-video-audio-recording-quick-setup.el | 121 |
2 files changed, 142 insertions, 38 deletions
diff --git a/modules/video-audio-recording.el b/modules/video-audio-recording.el index b8c104de..d5c59cb7 100644 --- a/modules/video-audio-recording.el +++ b/modules/video-audio-recording.el @@ -316,6 +316,15 @@ than the raw device name." (push (cons name (or desc name)) mics)))) (nreverse mics))) +(defun cj/recording--mic-active-p (source-name) + "Return non-nil if SOURCE-NAME is actively in use (RUNNING state). +Checks `pactl list sources short' for the source's current state." + (let ((output (shell-command-to-string "pactl list sources short 2>/dev/null"))) + (cl-some (lambda (line) + (and (string-match-p (regexp-quote source-name) line) + (string-match-p "RUNNING" line))) + (split-string output "\n" t)))) + (defun cj/recording--parse-pactl-sinks-verbose (output) "Parse verbose `pactl list sinks' OUTPUT into structured list. Returns list of (name description mute state) tuples. @@ -517,11 +526,12 @@ since recording needs both to capture your voice and system audio." (defun cj/recording-quick-setup () "Quick device setup for recording — two-step mic + sink selection. -Step 1: Pick a microphone from available unmuted sources. +Step 1: Pick a microphone. Mics in use by an app (RUNNING) show +a green icon and are sorted to the top; idle mics show dim . Step 2: Pick an audio output (sink) to monitor. Sinks with active -audio streams are marked with (green) and sorted to the top; -inactive sinks show (dim). The chosen sink's .monitor source -is set as the system audio device. +audio streams show green and are sorted to the top; idle sinks +show dim . The chosen sink's .monitor source is set as the +system audio device. This approach is portable across systems — plug in a new mic, run this command, and it appears in the list. No hardware-specific configuration @@ -529,19 +539,34 @@ needed." (interactive) ;; Step 1: Mic selection (let* ((mics (cj/recording--get-available-mics)) - (mic-choices (mapcar (lambda (mic) - (cons (cdr mic) (car mic))) - mics))) - (if (null mic-choices) + (mic-labels + (mapcar + (lambda (mic) + (let* ((name (car mic)) + (desc (cdr mic)) + (active (cj/recording--mic-active-p name)) + (icon "") + (face (if active '(:foreground "#50fa7b") '(:foreground "#6272a4"))) + (label (concat (propertize icon 'face face) " " desc))) + (list label name active))) + mics)) + ;; Sort active mics to top + (sorted-mics (sort mic-labels + (lambda (a b) + (and (nth 2 a) (not (nth 2 b)))))) + (mic-alist (mapcar (lambda (entry) + (cons (nth 0 entry) (nth 1 entry))) + sorted-mics)) + (mic-alist-with-cancel (append mic-alist '(("Cancel" . nil))))) + (if (null mic-alist) (user-error "No microphones found. Is a mic plugged in and unmuted?") - (let* ((mic-choices-with-cancel (append mic-choices '(("Cancel" . nil)))) - (mic-choice (completing-read "Select microphone: " - (lambda (string pred action) - (if (eq action 'metadata) - '(metadata (display-sort-function . identity)) - (complete-with-action action mic-choices-with-cancel string pred))) - nil t)) - (mic-device (cdr (assoc mic-choice mic-choices-with-cancel)))) + (let* ((mic-choice (completing-read "Select microphone: " + (lambda (string pred action) + (if (eq action 'metadata) + '(metadata (display-sort-function . identity)) + (complete-with-action action mic-alist-with-cancel string pred))) + nil t)) + (mic-device (cdr (assoc mic-choice mic-alist-with-cancel)))) (if (null mic-device) (user-error "Device setup cancelled") ;; Step 2: Sink selection @@ -552,7 +577,7 @@ needed." (let* ((name (car sink)) (desc (cdr sink)) (active (cj/recording--sink-active-p name)) - (icon (if active "" "")) + (icon (if active "" "")) (face (if active '(:foreground "#50fa7b") '(:foreground "#6272a4"))) (label (concat (propertize icon 'face face) " " desc))) (list label name active))) diff --git a/tests/test-video-audio-recording-quick-setup.el b/tests/test-video-audio-recording-quick-setup.el index 1b2e6e53..f082f86a 100644 --- a/tests/test-video-audio-recording-quick-setup.el +++ b/tests/test-video-audio-recording-quick-setup.el @@ -3,7 +3,7 @@ ;;; Commentary: ;; Unit tests for cj/recording-quick-setup function. ;; The quick setup is a two-step flow: -;; Step 1: Pick a microphone +;; Step 1: Pick a microphone (with active/inactive indicators) ;; Step 2: Pick an audio output (sink) with active/inactive indicators ;; The chosen sink's .monitor source is set as the system audio device. @@ -36,20 +36,20 @@ "Test that selecting a mic sets cj/recording-mic-device." (test-quick-setup-setup) (unwind-protect - (let ((call-count 0)) - (cl-letf (((symbol-function 'cj/recording--get-available-mics) - (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono") - ("builtin-input" . "Built-in Analog")))) - ((symbol-function 'cj/recording--get-available-sinks) - (lambda () '(("jds-labs" . "JDS Labs Element IV")))) - ((symbol-function 'cj/recording--sink-active-p) - (lambda (_name) nil)) - ((symbol-function 'completing-read) - (lambda (_prompt table &rest _args) - (setq call-count (1+ call-count)) - (car (all-completions "" table))))) - (cj/recording-quick-setup) - (should (equal "jabra-input" cj/recording-mic-device)))) + (cl-letf (((symbol-function 'cj/recording--get-available-mics) + (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono") + ("builtin-input" . "Built-in Analog")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) + ((symbol-function 'cj/recording--get-available-sinks) + (lambda () '(("jds-labs" . "JDS Labs Element IV")))) + ((symbol-function 'cj/recording--sink-active-p) + (lambda (_name) nil)) + ((symbol-function 'completing-read) + (lambda (_prompt table &rest _args) + (car (all-completions "" table))))) + (cj/recording-quick-setup) + (should (equal "jabra-input" cj/recording-mic-device))) (test-quick-setup-teardown))) (ert-deftest test-video-audio-recording-quick-setup-normal-sets-system-to-sink-monitor () @@ -58,6 +58,8 @@ (unwind-protect (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("alsa_output.usb-JDS_Labs-00.analog-stereo" . "JDS Labs Element IV")))) ((symbol-function 'cj/recording--sink-active-p) @@ -77,6 +79,8 @@ (let ((call-count 0)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("mic-1" . "Mic One")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("sink-1" . "Sink One")))) ((symbol-function 'cj/recording--sink-active-p) @@ -89,14 +93,75 @@ (should (= 2 call-count)))) (test-quick-setup-teardown))) +(ert-deftest test-video-audio-recording-quick-setup-normal-active-mic-indicator () + "Test that active mics get the green mic icon in their label." + (test-quick-setup-setup) + (unwind-protect + (let ((mic-candidates nil) + (call-count 0)) + (cl-letf (((symbol-function 'cj/recording--get-available-mics) + (lambda () '(("active-mic" . "Active Mic") + ("idle-mic" . "Idle Mic")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (name) (equal name "active-mic"))) + ((symbol-function 'cj/recording--get-available-sinks) + (lambda () '(("sink-1" . "Sink One")))) + ((symbol-function 'cj/recording--sink-active-p) + (lambda (_name) nil)) + ((symbol-function 'completing-read) + (lambda (_prompt table &rest _args) + (setq call-count (1+ call-count)) + (let ((candidates (all-completions "" table))) + (when (= call-count 1) + (setq mic-candidates candidates)) + (car candidates))))) + (cj/recording-quick-setup) + ;; Both should have icon + (should (cl-some (lambda (c) (and (string-match-p "Active Mic" c) + (string-match-p "" c))) + mic-candidates)) + (should (cl-some (lambda (c) (and (string-match-p "Idle Mic" c) + (string-match-p "" c))) + mic-candidates)))) + (test-quick-setup-teardown))) + +(ert-deftest test-video-audio-recording-quick-setup-normal-active-mic-sorted-first () + "Test that active mics are sorted to the top of the list." + (test-quick-setup-setup) + (unwind-protect + (let ((mic-candidates nil) + (call-count 0)) + (cl-letf (((symbol-function 'cj/recording--get-available-mics) + (lambda () '(("idle-mic" . "Idle Mic") + ("active-mic" . "Active Mic")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (name) (equal name "active-mic"))) + ((symbol-function 'cj/recording--get-available-sinks) + (lambda () '(("sink-1" . "Sink One")))) + ((symbol-function 'cj/recording--sink-active-p) + (lambda (_name) nil)) + ((symbol-function 'completing-read) + (lambda (_prompt table &rest _args) + (setq call-count (1+ call-count)) + (let ((candidates (all-completions "" table))) + (when (= call-count 1) + (setq mic-candidates candidates)) + (car candidates))))) + (cj/recording-quick-setup) + ;; First candidate should be the active mic + (should (string-match-p "Active Mic" (car mic-candidates))))) + (test-quick-setup-teardown))) + (ert-deftest test-video-audio-recording-quick-setup-normal-active-sink-indicator () - "Test that active sinks get the active icon in their label." + "Test that active sinks get and inactive sinks get ." (test-quick-setup-setup) (unwind-protect (let ((sink-candidates nil) (call-count 0)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("mic-1" . "Mic One")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("active-sink" . "Active Sink") ("inactive-sink" . "Inactive Sink")))) @@ -110,13 +175,13 @@ (setq sink-candidates candidates)) (car candidates))))) (cj/recording-quick-setup) - ;; Active sink should have icon (substring-no-properties strips faces but keeps text) + ;; Active sink should have icon (should (cl-some (lambda (c) (and (string-match-p "Active Sink" c) (string-match-p "" c))) sink-candidates)) - ;; Inactive sink should have icon + ;; Inactive sink should have icon (should (cl-some (lambda (c) (and (string-match-p "Inactive Sink" c) - (string-match-p "" c))) + (string-match-p "" c))) sink-candidates)))) (test-quick-setup-teardown))) @@ -127,6 +192,8 @@ (let ((sink-candidates nil)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("mic-1" . "Mic One")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("inactive-sink" . "Inactive Sink") ("active-sink" . "Active Sink")))) @@ -151,6 +218,8 @@ (let ((message-text nil)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("jds-labs.analog-stereo" . "JDS Labs Element IV")))) ((symbol-function 'cj/recording--sink-active-p) @@ -175,6 +244,8 @@ (let ((read-called 0)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("sole-mic" . "Only Mic Available")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("sole-sink" . "Only Sink")))) ((symbol-function 'cj/recording--sink-active-p) @@ -196,6 +267,8 @@ (unwind-protect (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("sink-1" . "Sink One")))) ((symbol-function 'cj/recording--sink-active-p) @@ -215,6 +288,8 @@ (let ((call-count 0)) (cl-letf (((symbol-function 'cj/recording--get-available-mics) (lambda () '(("jabra-input" . "Jabra SPEAK 510 Mono")))) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil)) ((symbol-function 'cj/recording--get-available-sinks) (lambda () '(("sink-1" . "Sink One")))) ((symbol-function 'cj/recording--sink-active-p) @@ -236,7 +311,9 @@ (test-quick-setup-setup) (unwind-protect (cl-letf (((symbol-function 'cj/recording--get-available-mics) - (lambda () nil))) + (lambda () nil)) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil))) (should-error (cj/recording-quick-setup) :type 'user-error)) (test-quick-setup-teardown))) @@ -245,7 +322,9 @@ (test-quick-setup-setup) (unwind-protect (cl-letf (((symbol-function 'cj/recording--get-available-mics) - (lambda () nil))) + (lambda () nil)) + ((symbol-function 'cj/recording--mic-active-p) + (lambda (_name) nil))) (condition-case err (cj/recording-quick-setup) (user-error |
