使用 i3、pactl 和 bash 设置音量增加的最大限制

使用 i3、pactl 和 bash 设置音量增加的最大限制

我想配置 i3WM 以设置最大音量级别(比如说 150%),这样我就不会意外将音量调高到某个很大的值。我制作了 bash 脚本,其执行与 volume_up 键绑定。所有元素单独工作正常,但当我把所有元素放在一起时,就出现了问题。

Bash 脚本代码:

#!/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

在 i3 配置文件中绑定:

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

什么工作得很好:

  • 绑定时增加音量只需使用 pactl 增加音量的单个命令(无需任何 bash 脚本,只需在 i3 配置文件中执行 pactl 命令,与上面 if 语句中的命令相同)
  • 从终端仅执行上述 bash 脚本
  • 一起执行所有操作,但何时current_volume_pc执行则被硬编码为某个值

因此,一切都指向了通过键绑定操作执行脚本时获取当前音量值的问题,但我不知道如何解决它。如果这些信息对某人有帮助,那么从 i3 通信也没有错误,我的操作系统是 Ubuntu 20.04。我也尝试了一些其他绑定语法的方法,但结果总是一样的,上面提供的语法对我来说似乎是最合乎逻辑的。

我也在 reddit 上找到了类似的问题:https://www.reddit.com/r/i3wm/comments/dens5j/limiting_pulseaudio_max_volume/但我不明白 dikduk 的文件中发生了什么,我认为我最好寻求帮助来解决我自己的问题,而不是复制粘贴别人的解决方案

答案1

我找到了问题所在。这是由我的系统语言引起的 - 我来自波兰,所以我将波兰语设置为我的系统语言,但我将 .bashrc 中的终端语言更改为英语,因为它更方便。

就我的情况而言,当我直接从终端执行 bash 脚本时,我得到的结果是pactl list sinks英文(我猜是因为 .bashrc 语言更改),所以一切都正常。但是当我使用键绑定执行脚本时,我从上面的命令得到的结果是波兰语,所以 grep 找不到“Volume”这个词。如果有人遇到类似的问题,我会在下面放置正确的 bash 脚本,该脚本在从终端或键绑定调用时都可以工作。

#!/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

相关内容