Skip to content

Instantly share code, notes, and snippets.

@Ropid
Created August 6, 2018 15:09
Show Gist options
  • Save Ropid/3a08d70a807e35c7e8c688d54b725e8e to your computer and use it in GitHub Desktop.
Save Ropid/3a08d70a807e35c7e8c688d54b725e8e to your computer and use it in GitHub Desktop.
PulseAudio volume control script (which will not go above 100%)
#!/bin/perl
use warnings;
use strict;
use v5.010;
$_ = qx/pacmd list-sinks/;
my ($sink, $vol) = /(?:^|\n)\h*\* index: (\S+).*?\n\h*volume:[^\n]*?(\d+)%/s;
if (scalar @ARGV == 1 and $ARGV[0] =~ /^ (-|\+)? (\d+)% | (toggle) $/x) {
if ($3) {
system "pactl set-sink-mute $sink toggle";
} else {
if (not $1) { $vol = $2; }
elsif ($1 eq "-") { $vol -= $2; }
elsif ($1 eq "+") { $vol += $2; }
$vol = 0 if $vol < 0;
$vol = 100 if $vol > 100;
system "pactl set-sink-volume $sink $vol%";
}
} else {
my $name = $0 =~ s|.*/||r;
print <<END;
Usage: $name [-|+]VOL% | toggle
Ex.: $name 100%
$name -5%
$name +5%
$name toggle # mute/unmute
END
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment