Advertisement
Toumo

pactl_change_volume.sh

Apr 6th, 2023 (edited)
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.92 KB | None | 0 0
  1. #!/bin/bash
  2. # Increments or decrements current output volume based on uint input parameter $1 (% volume)
  3. # Each % increase in volume is a 655 units increase in the unitless volume value informed by pactl get-sink-volume
  4. change_vol() {
  5.     local rel_inc=$1 # Relative increment (input, can be negative)
  6.     local abs_inc=$((rel_inc * 655)) # Absolute increment
  7.     local max_vol=65536 # Exact 100%
  8.     local min_vol=0
  9.     local cur_vol=$(pactl get-sink-volume @DEFAULT_SINK@)
  10.     cur_vol=${cur_vol##*left: } #
  11.     cur_vol=${cur_vol%% /*}     # Pattern-matching to get the current volume value
  12.     local new_vol=$((cur_vol + abs_inc))
  13.  
  14.     if   [ "$new_vol" -gt "$max_vol" ]; then
  15.         pactl set-sink-volume @DEFAULT_SINK@ $max_vol
  16.     elif [ "$new_vol" -lt "$min_vol" ]; then
  17.         pactl set-sink-volume @DEFAULT_SINK@ $min_vol
  18.     else
  19.         pactl set-sink-volume @DEFAULT_SINK@ $new_vol
  20.     fi
  21. }
  22.  
  23. change_vol $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement