检查 PulseAudio 接收器音量

Pra*_*kar 9 command-line pulseaudio 12.04

是否有任何命令可以检查 PulseAudio 接收器的音量。意味着我想以 % 显示特定接收器的 PulseAudio 音量

即 50%

我已经使用pactl set-sinks-volume 1 50%命令设置了音量。但现在我想检查它是否为 50%。

那么我该怎么做呢?

ter*_*don 9

您可以使用pactl list sinks获取有关接收器当前状态的信息。这将返回大量信息,包括体积。因此,要仅获取水槽 1 的体积,您可以使用:

pactl list sinks | perl -000ne 'if(/#1/){/(Volume:.*)/; print "$1\n"}'
Run Code Online (Sandbox Code Playgroud)

这将返回如下内容:

Volume: 0:  50% 1:  50%
Run Code Online (Sandbox Code Playgroud)

上面的perl命令将打印sink 1. 要使用不同的接收器,请将 更改#1为另一个数字,例如#0

我不确定这两个50%是什么意思,我假设它们是左右扬声器音量。因此,为了检查它们是否高于或低于特定值,(假设余额设置为“中心”,左右音量相同,因此只需要检查一个),您可以执行以下操作:

pactl list sinks | perl -000lne 'if(/#1/){/Volume:.*?(\d+)%/; $1 >= 50 ? (print "y\n") : (print "n\n")}'
Run Code Online (Sandbox Code Playgroud)

y如果体积大于或等于 50,则上面将打印 a,否则将打印n。不过这一切都变得有点复杂,所以我会通过创建一个脚本来简化:



    #!/usr/bin/env perl 
    
    ## The sink we are interested in should be given as the 
    ## 1st argument to the script.
    die("Need a sink number as the first argument\n") if @ARGV < 1;
    my $sink=$ARGV[0];
    
    ## If the script has been run with a second argument,
    ## that argument will be the volume threshold we are checking
    my $volume_limit=$ARGV[1]||undef;
    
    ## Run the pactl command and save the output in 
    ## ther filehandle $fh
    open(my $fh, '-|', 'pactl list sinks');
    
    ## Set the record separator to consecutive newlines (same as -000)
    ## this means we read the info for each sink as a single "line".
    $/="\n\n";
    
    ## Go through the pactl output
    while (<$fh>) {
        ## If this is the sink we are interested in
        if (/#$sink/) {
            ## Extract the current colume of this sink
            /Volume:.*?(\d+)%/;
            my $volume=$1;
            ## If the script has been run with a second argument,
            ## check whether the volume is above or below that
            if ($volume_limit) {
                ## If the volume os greater than or equal to the
                ## value passed, print "y"
                if ($volume >= $volume_limit) {
                   print "y\n";
                    exit 0;
                }
                else {
                    print "n\n";
                    exit 1;
                }
            }   
            ## Else, if the script has been run with just one argument,
            ## print the current volume.
            else {
                print "$volume%\n";
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

将上面的脚本保存check_volume.pl在您的目录中$PATH(例如,/usr/local/bin),使其可执行,chmod +x check_volume.pl然后运行它,将您感兴趣的接收器作为第一个参数:

$ check_volume.pl 1
50%
Run Code Online (Sandbox Code Playgroud)

要检查音量是否高于给定阈值,请将阈值作为第二个参数:

$ check_volume.pl 1 50
y
$ check_volume.pl 1 70
n
Run Code Online (Sandbox Code Playgroud)

请注意,这假设您的系统语言是英语。如果它没有使用更改的语言环境运行脚本:

LC_ALL=C check_volume.pl 1 50
Run Code Online (Sandbox Code Playgroud)

  • `pactl` 输出依赖于语言环境的字符串,导致此答案中的命令在非英语系统上失败。始终使用`LC_ALL=C`作为前缀,或者输入一次`export LC_ALL=C`以将shell(以及从中运行的所有命令)设置为默认语言环境。然后`pactl | perl ...` 有效。 (3认同)