1 ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020 Omar Polo
5 ;; Author: Omar Polo <op@omarpolo.com>
7 ;; Keywords: multimedia
8 ;; URL: https://git.omarpolo.com/sndio.el
12 ;; This package provides the sndio major mode to interact with
17 (eval-when-compile (require 'subr-x))
19 (defvar sndio-sndioctl-cmd "sndioctl"
20 "Path to the sndioctl executable.")
22 (defvar sndio-step 0.02
23 "Step for `sndio-increase' and `sndio-decrease'.")
25 (defvar sndio-mode-map
26 (let ((m (make-sparse-keymap)))
27 (define-key m (kbd "n") #'forward-line)
28 (define-key m (kbd "p") #'previous-line)
29 (define-key m (kbd "i") #'sndio-increase)
30 (define-key m (kbd "d") #'sndio-decrease)
31 (define-key m (kbd "m") #'sndio-mute)
32 (define-key m (kbd "t") #'sndio-toggle)
33 (define-key m (kbd "g") #'sndio-update)
37 (define-derived-mode sndio-mode special-mode "sndio"
38 "Major mode for sndio interaction."
42 (defun sndio-update ()
43 "Update the current sndio buffer."
45 (with-current-buffer "*sndio*"
46 (let ((inhibit-read-only t))
48 (process-file sndio-sndioctl-cmd nil (current-buffer) nil))))
50 (defun sndio--run (&rest args)
51 "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
53 (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
56 (defun sndio--current-io ()
57 "Yield the input/poutput at point as string."
58 (when-let (end (save-excursion
60 (ignore-errors (search-forward "="))))
61 (buffer-substring-no-properties (line-beginning-position)
64 (defun sndio--update-value (x)
65 "Update the value for the input/output at point setting it to X."
69 (let ((inhibit-read-only t))
70 (delete-region (point) (line-end-position))
71 (insert (string-trim-right x)))))
73 (defun sndio-increase ()
74 "Increase the volume for the input/output at point."
76 (when-let (x (sndio--current-io))
77 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
78 (sndio--update-value val))))
80 (defun sndio-decrease ()
81 "Decrease the volume for the input/output at point."
83 (when-let (x (sndio--current-io))
84 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
85 (sndio--update-value val))))
88 "Mute the input/output at point."
90 (when-let (x (sndio--current-io))
91 (when-let (val (sndio--run "-n" (concat x "=0")))
92 (sndio--update-value val))))
94 (defun sndio-toggle ()
95 "Toggle input/output at point."
97 (when-let (x (sndio--current-io))
98 (when-let (val (sndio--run "-n" (concat x "=!")))
99 (sndio--update-value val))))
105 (switch-to-buffer "*sndio*")
109 ;;; sndio.el ends here