#!/bin/bash # # volume-helper.sh — Unified volume control script for Hyprland using pactl # # Purpose: # Many systems have multiple audio output devices (sinks), such as onboard # audio and an external device (e.g. M-Audio M-Track). This script adjusts the # volume of both: # # - The default sink (`@DEFAULT_SINK@`) # - A second sink (identified by partial name, e.g., "M-Track") # # Supports: # - up → Increase volume by 5% (defined by STEP, capped at MAX_VOLUME) # - down → Decrease volume by 5% # - mute → Mute all sinks # - unmute → Unmute all sinks # - toggle → Toggle mute state of all sinks # # Do NOT run this with sudo! pactl must run in your user session. # MAX_VOLUME=140 STEP=5 # Secondary sink (e.g. M-Track 2X2) SECOND_SINK=$(pactl list short sinks | grep "M-Track" | awk '{print $1}') DEFAULT_SINK="@DEFAULT_SINK@" adjust_volume() { local sink=$1 local direction=$2 if [[ "$direction" == "up" ]]; then pactl set-sink-volume "$sink" +${STEP}% VOL=$(pactl get-sink-volume "$sink" | grep -oP '\d+(?=%)' | sort -nr | head -1) if [ "$VOL" -gt "$MAX_VOLUME" ]; then pactl set-sink-volume "$sink" "$MAX_VOLUME"% fi elif [[ "$direction" == "down" ]]; then pactl set-sink-volume "$sink" -${STEP}% fi } toggle_mute() { local sink=$1 pactl set-sink-mute "$sink" "$2" } # Process action case "$1" in up) adjust_volume "$DEFAULT_SINK" up [[ -n "$SECOND_SINK" ]] && adjust_volume "$SECOND_SINK" up ;; down) adjust_volume "$DEFAULT_SINK" down [[ -n "$SECOND_SINK" ]] && adjust_volume "$SECOND_SINK" down ;; mute) toggle_mute "$DEFAULT_SINK" 1 [[ -n "$SECOND_SINK" ]] && toggle_mute "$SECOND_SINK" 1 ;; unmute) toggle_mute "$DEFAULT_SINK" 0 [[ -n "$SECOND_SINK" ]] && toggle_mute "$SECOND_SINK" 0 ;; toggle) CURRENT_STATE=$(pactl get-sink-mute "$DEFAULT_SINK" | grep -oE "yes|no") if [[ "$CURRENT_STATE" == "yes" ]]; then "$0" unmute else "$0" mute fi ;; *) echo "Usage: $0 up|down|mute|unmute|toggle" exit 1 ;; esac