Score:1

Set max limit for volume increase with i3, pactl and bash

in flag

I would like to configure i3WM to set maximum volume level (let's say 150%) so I won't accidentally increase volume to some huge value. I made bash script which execution is binded to volume_up key. All elements work fine separatedly, but when I put everything together, there is something wrong.

Bash script code:

#!/bin/bash
max_volume_pc=$1
current_volume_pc=$(pactl list sinks | grep '^[[:space:]]Volume:' | head -n $(( $SINK + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,')
if (($current_volume_pc < $max_volume_pc-10)) ; then
    pactl set-sink-volume @DEFAULT_SINK@ +10% && $refresh_i3status
else
    a=$(($max_volume_pc - $current_volume_pc))
    pactl set-sink-volume @DEFAULT_SINK@ +$a% && $refresh_i3status
fi

bind in i3 config file:

bindsym XF86AudioRaiseVolume exec ~/.config/i3/custom_configs/volume_up.sh 150

What works just fine:

  • voluming up when binded is just a single command for increasing volume with pactl (without any bash script, just exec pactl command in i3 config file same as command in if statement above)
  • executing only the above bash script from terminal
  • executing everything together, but when current_volume_pc is hard coded to some value

So everything points to issue with getting current volume value when script is executed through key-binded action, but I have no idea how to fix it. There is also no error comunicate from i3 and my OS is Ubuntu 20.04, if these informations would be helpful to someone. I also tried some other ways of binding syntax, but the result is always the same and syntax provided above seems most logic to me.

I also found reddit with similar question here: https://www.reddit.com/r/i3wm/comments/dens5j/limiting_pulseaudio_max_volume/, but I don't understand what is happening in dikduk's file and I think it's better for me to find help with fixing my own issue rather than copy-pasting someone's solution

Score:1
in flag

I found the issue. It was caused by my system language - I'm from Poland, so I set Polish to my system language, but I changed terminal language in .bashrc to English because it's more convinient.

In my case, when I executed my bash script directly from terminal, I got result of pactl list sinks in English (I assume due to .bashrc language change), so everything worked just fine. But when I executed script using key binding, the result I got from above command was in Polish, so grep couldn't find 'Volume' word. I'm putting correct bash script that works both when calling from terminal or key binding below if anyone would had similar problem.

#!/bin/bash
max_volume_pc=$1
current_volume_pc=$(pactl list sinks | grep '<Your system language word that means "volume">' | head -n $(( $SINK + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,')

if (($(echo -n $current_volume_pc | wc -m) == 0)); then
    current_volume_pc=$(pactl list sinks | grep '^[[:space:]]Volume:' | head -n $(( $SINK + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,')
fi

if (($current_volume_pc < $max_volume_pc-10)) ; then
    pactl set-sink-volume @DEFAULT_SINK@ +10% && $refresh_i3status
else
    a=$(($max_volume_pc - $current_volume_pc))
    pactl set-sink-volume @DEFAULT_SINK@ +$a% && $refresh_i3status
fi
pa4080 avatar
cn flag
You could set the locale by adding [`export LC_ALL=C`](https://unix.stackexchange.com/q/87745/201297) in the beginning of the script (right after the shebang *#!/bin/bash*) in order to make it language independent.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.