| 1 | ;;; emms-volume-amixer.el --- a mode for changing volume using amixer -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Copyright (C) 2006, 2007, 2008, 2009, 2023 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Martin Schoenmakers <aiviru@diamond-age.net> |
| 6 | |
| 7 | ;; This file is part of EMMS. |
| 8 | |
| 9 | ;; EMMS is free software; you can redistribute it and/or modify |
| 10 | ;; it under the terms of the GNU General Public License as published by |
| 11 | ;; the Free Software Foundation; either version 3, or (at your option) |
| 12 | ;; any later version. |
| 13 | ;; |
| 14 | ;; EMMS is distributed in the hope that it will be useful, |
| 15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | ;; GNU General Public License for more details. |
| 18 | ;; |
| 19 | ;; You should have received a copy of the GNU General Public License |
| 20 | ;; along with EMMS; see the file COPYING. If not, write to the |
| 21 | ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 22 | ;; Boston, MA 02110-1301, USA. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; This file defines a few simple functions to raise or lower the volume |
| 27 | ;; using amixer. It can be used stand-alone, though it's meant for usage |
| 28 | ;; with EMMS, particularly with emms-volume.el |
| 29 | |
| 30 | ;;; History: |
| 31 | |
| 32 | ;; May 30 2006: First cleanup and collation of amixer functions into a |
| 33 | ;; separate file for releasability. |
| 34 | |
| 35 | |
| 36 | ;;; Code: |
| 37 | (defcustom emms-volume-amixer-control "Master" |
| 38 | "The control to change the volume with. |
| 39 | Controls includes \"Master\", \"PCM\", etc. For a full list of available |
| 40 | controls, run `amixer controls' in a shell." |
| 41 | :type '(choice (const :tag "Master" "Master") |
| 42 | (const :tag "PCM" "PCM") |
| 43 | (string :tag "Something else: ")) |
| 44 | :group 'emms-volume) |
| 45 | |
| 46 | (defcustom emms-volume-amixer-card 0 |
| 47 | "The card number to change volume. |
| 48 | The card is identified by a number. For a full list run `cat |
| 49 | /proc/asound/cards' in a shell." |
| 50 | :type 'integer |
| 51 | :group 'emms-volume) |
| 52 | |
| 53 | (defvar emms-volume-amixer-volume-regexp |
| 54 | "\\[\\([0-9]+\\)%\\]" |
| 55 | "Regexp to capture the volume from amixer output.") |
| 56 | |
| 57 | ;;;###autoload |
| 58 | (defun emms-volume-amixer-change (amount) |
| 59 | "Change amixer master volume by AMOUNT." |
| 60 | (message "Playback channels: %s" |
| 61 | (with-temp-buffer |
| 62 | (when (zerop |
| 63 | (call-process "amixer" nil (current-buffer) nil |
| 64 | "-c" |
| 65 | (format "%d" emms-volume-amixer-card) |
| 66 | "sset" emms-volume-amixer-control |
| 67 | (format "%d%%%s" (abs amount) |
| 68 | (if (< amount 0) "-" "+")))) |
| 69 | (if (re-search-backward emms-volume-amixer-volume-regexp nil t) |
| 70 | (match-string 1)))))) |
| 71 | |
| 72 | (defun emms-volume-amixer-get () |
| 73 | "Return the amixer volume. |
| 74 | |
| 75 | Number is limited to the range [0-100]." |
| 76 | (let ((v (with-temp-buffer |
| 77 | (when (zerop |
| 78 | (call-process "amixer" nil (current-buffer) nil |
| 79 | "-c" |
| 80 | (format "%d" emms-volume-amixer-card) |
| 81 | "sget" emms-volume-amixer-control)) |
| 82 | (if (re-search-backward |
| 83 | emms-volume-amixer-volume-regexp nil t) |
| 84 | (match-string 1) |
| 85 | nil))))) |
| 86 | (if v |
| 87 | (max (min (string-to-number v) 100) 0) |
| 88 | (error "could not get volume from amixer backend")))) |
| 89 | |
| 90 | |
| 91 | |
| 92 | (provide 'emms-volume-amixer) |
| 93 | |
| 94 | ;;; emms-volume-amixer.el ends here |