add a module for FreeBSD's audio mixer

This commit is contained in:
Nyx 2023-10-16 15:43:12 -07:00
parent 36f43af312
commit 0e776696d7
4 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,15 @@
# stumpwm-mixer
This is a StumpWM interface to FreeBSD's built-in `mixer` command so
that you can control the volume from StumpWM and get some visual
feedback when the volume changes. It's deliberately designed to have a
similar interface to the `stumpwm-sndioctl` module.
Add something like this to your config to bind its commands to the
media buttons:
```
(define-key *top-map* (kbd "XF86AudioMute") "toggle-mute")
(define-key *top-map* (kbd "XF86AudioLowerVolume") "volume-down")
(define-key *top-map* (kbd "XF86AudioRaiseVolume") "volume-up")
```

View file

@ -0,0 +1,51 @@
(in-package #:stumpwm-mixer)
(defun mixer-output ()
(let ((output (run-shell-command "mixer" t)))
(loop for line in (uiop:split-string output :separator '(#\newline))
collect (uiop:split-string line :separator '(#\space)))))
(defun parse-mixer ()
(let ((output (mixer-output))
(rem '("" "Mixer" "is" "currently" "set" "to")))
(loop for line in output
collect (remove-if
(lambda (x)
(member x rem :test #'equalp))
line))))
(defun print-audio ()
(let* ((output (parse-mixer))
(volume (car (cdr (assoc "pcm" output :test #'equalp))))
(muted (car (cdr (assoc "vol" output :test #'equalp)))))
(message
(format nil "Volume: ~a~:[~; (muted)~]" volume
(string= "0:0" muted)))))
(defcommand volume-up () ()
(run-shell-command
"mixer pcm +5")
(print-audio))
(defcommand volume-down () ()
(run-shell-command
"mixer pcm -5")
(print-audio))
(defcommand toggle-mute () ()
(let ((vol (assoc "vol" (parse-mixer)
:test #'equalp)))
(if (equalp (cadr vol) "0:0")
(run-shell-command
"mixer vol 100")
(run-shell-command
"mixer vol 0"))
(print-audio)))
(defcommand set-mute () ()
(run-shell-command
"mixer vol 0"))
(defcommand unset-mute () ()
(run-shell-command
"mixer vol 100"))

View file

@ -0,0 +1,7 @@
(defpackage #:stumpwm-mixer
(:use :cl :stumpwm)
(:export :volume-up
:volume-down
:toggle-mute
:set-mute
:unset-mute))

View file

@ -0,0 +1,8 @@
(defsystem "stumpwm-mixer"
:description "Interface to FreeBSD's built-in sound mixer"
:author "Nyx <n1x@riseup.net>"
:license "ISC"
:serial t
:depends-on ("stumpwm")
:components ((:file "package")
(:file "mixer")))