Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

pulsemixer-notification (Source)

#!/bin/sh
# File: pulsemixer-notification
# Location: /usr/bin
# Author: bgstack15
# Startdate: 2023-10-04-4 09:06
# SPDX-License-Identifier: GPL-3.0
# Title: pulsemixer wrapper script that includes notifications
# Package: bgscripts
# Purpose: wrap around pulsemixer hotkey invocations to include status notifications
# History:
# Usage:
#    In ~/.fluxbox/keys:
#       121 :Exec pulsemixer-notification toggle
#       122 :Exec pulsemixer-notification down
#       123 :Exec pulsemixer-notification up
# Reference:
# Improve:
# Dependencies:
#    devuan-dep: pulsemixer, pulseaudio, notify-send
# Documentation:
# Load configuration
test -z "${devtty}" && devtty=/dev/null
test -z "${PN_TEMPFILE}" && PN_TEMPFILE=~/.cache/audio.temp
test -z "${PN_STEP}" && PN_STEP=3
test -z "${PN_EXPIRE_MS}" && PN_EXPIRE_MS=1250
test -f "${HOME}/.config/pulsemixer-notification" && . "${HOME}/.config/pulsemixer-notification"
test -f "/etc/pulsemixer-notification.conf" && . "/etc/pulsemixer-notification.conf"
# runtime
exec 1>>"${devtty}"
action="${1}"
unset icon vol relative_level message replacestring
case "${action}" in
   toggle)
      pulsemixer --toggle-mute
      case "$( pulsemixer --get-mute )" in
         0)
            action="unmute"
            ;;
         1)
            icon="audio-volume-muted"
            message="muted"
            ;;
      esac
      ;;
   up) pulsemixer --unmute ; pulsemixer --change-volume +"${PN_STEP}" ;;
   down) pulsemixer --unmute ; pulsemixer --change-volume -"${PN_STEP}" ;;
   *)
      echo "unknown: ${action}"
      ;;
esac
get_vol_level() {
   vol="$( pulsemixer --get-volume | awk '{print $1}' )"
   is_low="$( printf '%s\n' "${vol}<=33" | bc )"
   is_med="$( printf '%s\n' "(${vol}<=66)*(${vol}>33)" | bc )"
   is_hih="$( printf '%s\n' "${vol}>66" | bc )"
   is_off="$( printf '%s\n' "${vol}==0" | bc )"
   test "${is_low}" = "1" && printf '%s' "low" && return 0
   test "${is_med}" = "1" && printf '%s' "medium" && return 0
   test "${is_hih}" = "1" && printf '%s' "high" && return 0
   test "${is_off}" = "1" && printf '%s' "muted" && return 0
}
# awk: cheat and just use left-side volume of stereo volume.
vol="$( pulsemixer --get-volume | awk '{print $1}' )"
relative_level="$( get_vol_level )"
case "${action}" in
   up|down)
      icon="audio-volume-${relative_level}"
      message="${vol}"
      ;;
   unmute)
      icon="audio-volume-${relative_level}"
      message="unmuted"
      ;;
esac
test -f "${PN_TEMPFILE}" && replacestring="--replace-id=$( cat "${PN_TEMPFILE}" 2>/dev/null )"
# leave replacestring unquoted in case it is empty
notify-send ${replacestring} --icon "${icon}" --transient "${message}" --urgency low --expire-time "${PN_EXPIRE_MS}" --print-id > "${PN_TEMPFILE}"