Skip to content

Instantly share code, notes, and snippets.

@fufexan
Created April 30, 2025 10:19
Show Gist options
  • Save fufexan/9984008f9a68acd4a9ff32ae6eee55e9 to your computer and use it in GitHub Desktop.
Save fufexan/9984008f9a68acd4a9ff32ae6eee55e9 to your computer and use it in GitHub Desktop.
Quadratic volume increase/decrease for PipeWire using Wireplumber
#!/usr/bin/env bash
# Quadratic volume setter
# USAGE:
# ./quadvol.sh [+/-]VOL
# NOTE: Does not support setting absolute volume
# Optionally change the volume cap
VOL_CAP=1.0
# Checks
CMD="$1"
if [[ ! "$CMD" =~ ^[+\-][01](.[0-9])?$ ]]; then
echo "Command should have the format +/-[0.0 - $VOL_CAP]"
exit 1
fi
# Parse command
AMOUNT=${CMD//[+\-]/}
ACTION=${CMD//[0-9.]/}
if awk "BEGIN {exit !($AMOUNT < 0.0 || $AMOUNT > $VOL_CAP)}"; then
echo "Amount should be in the range 0.0 - $VOL_CAP"
exit 1
fi
# Get volume and compute square root
CRT_VOL_LIN=$(wpctl get-volume @DEFAULT_SINK@ | cut -d' ' -f2)
CRT_VOL_QUAD=$(echo "$CRT_VOL_LIN" | awk '{print sqrt($0)}')
# Compute new volume
NEW_CRT_VOL_QUAD=$(awk "BEGIN {print ($CRT_VOL_QUAD $ACTION $AMOUNT)}")
NEW_CRT_VOL_LIN=$(awk "BEGIN {print ($NEW_CRT_VOL_QUAD ^ 2)}")
# New volume should be bound between 0.0 and $VOL_CAP
if awk "BEGIN {exit !($NEW_CRT_VOL_LIN < 0.0)}"; then
NEW_CRT_VOL_LIN=0
elif awk "BEGIN {exit !($NEW_CRT_VOL_LIN > $VOL_CAP)}"; then
NEW_CRT_VOL_LIN=1
fi
wpctl set-volume @DEFAULT_SINK@ "$NEW_CRT_VOL_LIN" -l "$VOL_CAP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment