Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there any command to check the PulseAudio sinks volume. Means I want display PulseAudio volume of particular sinks in %

i.e. 50%

I already set the volume using pactl set-sinks-volume 1 50% command. But now i want to check is it 50% or not.

So how can i do this?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You can use pactl list sinks to get info on the current state of your sinks. This returns a lot of information, including the volume. So, to get only the volume of sink 1, you can use:

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

This will return something like:

Volume: 0:  50% 1:  50%

The above perl command will print the details of sink 1. To use a different sink, change the #1 to another number, for example #0.

I am not sure what the two 50% mean, I assume they are the left and right speaker volumes. So, in order to check if they are above or below a specific value, (assuming that the balance is set to "center", that both left and right volumes are identical so only one needs to be checked), you can do:

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

The above will print a y if the volume is greater than or equal to 50 and an n otherwise. This is all getting a bit complex though, so I would simplify by creating a script:

#!/usr/bin/env perl 

## The sink we are interested in should be given as the 
## 1st argument to the script.
my $sink=$ARGV[0] || die("Need a sink number as the first argument\n");

## 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";
        }
    }

Save the script above as check_volume.pl in a directory in your $PATH (for example, /usr/local/bin), make it executable, chmod +x check_volume.pl and then run it giving the sink you are interested as the first argument:

$ check_volume.pl 1
50%

To check whether the volume is above a given threshold, give the threshold as a second argument:

$ check_volume.pl 1 50
y
$ check_volume.pl 1 70
n
share|improve this answer
    
Thanks it's working great for me –  Prakash V Holkar Apr 28 at 9:00
    
How can I check the volume is >= 50% in if condition –  Prakash V Holkar Apr 28 at 9:31
    
@PrakashVHolkar see updated answer, I made a more complex script. –  terdon Apr 28 at 12:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.