forked from tsibley/soundbridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundbridge-volume
executable file
·44 lines (37 loc) · 1011 Bytes
/
soundbridge-volume
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env perl
use strict;
use warnings;
use open qw/ :utf8 :std /;
use utf8;
use feature 'say';
use experimental 'switch';
use Soundbridge;
my $sb = Soundbridge->new( timeout => 0.2 );
my $volume = shift @ARGV;
my $attempt = 1;
my $current;
while (not defined $current or $attempt <= 3) {
($current) = $sb->get("Volume");
$attempt++;
}
if (defined $volume and $volume =~ /^([+-])(\d+)$/) {
die "😞 I couldn’t get the current volume to adjust!\n"
if not defined $current;
my ($op, $amount) = ($1, $2);
$volume = $current;
given ($op) {
$volume -= $amount when "-";
$volume += $amount when "+";
}
}
$current //= 0;
if (defined $volume and $volume =~ /^\d+$/ and $volume != $current) {
say "$current → $volume";
my ($result) = $sb->set("Volume", $volume);
unless ($result and $result eq "OK") {
warn "😞 Something didn't work, try again?\n";
warn " result = $result\n";
}
} else {
say $current;
}