Permalink
Browse files

added volume set/get in linux

  • Loading branch information...
1 parent 8440f3a commit 62a7f6f3ff68b02afcc8d45e7c3912bc7f556e56 @msoap committed Feb 9, 2013
Showing with 38 additions and 0 deletions.
  1. +19 −0 mojolicious-radio-box.pl
  2. +16 −0 src/cmus-client.pm
  3. +3 −0 src/util.pm
View
@@ -47,6 +47,9 @@ sub init {
}
$OPTIONS{is_mac} = 1 if $^O eq 'darwin';
+ $OPTIONS{is_linux} = 1 if $^O eq 'linux';
+ $OPTIONS{is_pulseaudio} = 1 if $OPTIONS{is_linux} && `pacmd --version` =~ m/^pacmd\s+\d+/;
+ $OPTIONS{is_alsa} = 1 if $OPTIONS{is_linux} && `amixer --version` =~ m/^amixer\s+version\s+\d+/;
}
# ------------------------------------------------------------------------------
@@ -134,7 +137,19 @@ sub cmus_get_info {
if ($OPTIONS{is_mac}) {
$info->{volume} = int(`osascript -e "output volume of (get volume settings)"`);
+ } elsif ($OPTIONS{is_pulseaudio}) {
+ my ($pa_info) = grep {/set-sink-volume/} `pacmd dump`;
+ $pa_info =~ /\s+ ([0-9a-fx]+) \s* $/xi;
+ if (defined $1 && hex($1) >= 0) {
+ $info->{volume} = int(sprintf("%0.0f", hex($1) / 65536 * 100));
+ }
+ } elsif ($OPTIONS{is_alsa}) {
+ my $alsa_info = join "#", grep {/Front\s+(Left|Right):\s+Playback/} `amixer get Master`;
+ if ($alsa_info =~ /\d+ \s+ \[(\d{1,3})%\] .+ \d+ \s+ \[(\d{1,3})%\]/sx) {
+ $info->{volume} = int((int($1) + int($2)) / 2);
+ }
}
+
$info->{server_version} = $VERSION;
return $info;
@@ -276,6 +291,10 @@ sub cmus_set_volume {
if ($OPTIONS{is_mac}) {
system("osascript", "-e", "set volume output volume $volume");
+ } elsif ($OPTIONS{is_pulseaudio}) {
+ system("pactl", "set-sink-volume", "0", "${volume}%");
+ } elsif ($OPTIONS{is_alsa}) {
+ system("amixer", "-q", "set", "Master", "${volume}%");
}
return;
View
@@ -34,7 +34,19 @@ sub cmus_get_info {
if ($OPTIONS{is_mac}) {
$info->{volume} = int(`osascript -e "output volume of (get volume settings)"`);
+ } elsif ($OPTIONS{is_pulseaudio}) {
+ my ($pa_info) = grep {/set-sink-volume/} `pacmd dump`;
+ $pa_info =~ /\s+ ([0-9a-fx]+) \s* $/xi;
+ if (defined $1 && hex($1) >= 0) {
+ $info->{volume} = int(sprintf("%0.0f", hex($1) / 65536 * 100));
+ }
+ } elsif ($OPTIONS{is_alsa}) {
+ my $alsa_info = join "#", grep {/Front\s+(Left|Right):\s+Playback/} `amixer get Master`;
+ if ($alsa_info =~ /\d+ \s+ \[(\d{1,3})%\] .+ \d+ \s+ \[(\d{1,3})%\]/sx) {
+ $info->{volume} = int((int($1) + int($2)) / 2);
+ }
}
+
$info->{server_version} = $VERSION;
return $info;
@@ -176,6 +188,10 @@ sub cmus_set_volume {
if ($OPTIONS{is_mac}) {
system("osascript", "-e", "set volume output volume $volume");
+ } elsif ($OPTIONS{is_pulseaudio}) {
+ system("pactl", "set-sink-volume", "0", "${volume}%");
+ } elsif ($OPTIONS{is_alsa}) {
+ system("amixer", "-q", "set", "Master", "${volume}%");
}
return;
View
@@ -14,6 +14,9 @@ sub init {
}
$OPTIONS{is_mac} = 1 if $^O eq 'darwin';
+ $OPTIONS{is_linux} = 1 if $^O eq 'linux';
+ $OPTIONS{is_pulseaudio} = 1 if $OPTIONS{is_linux} && `pacmd --version` =~ m/^pacmd\s+\d+/;
+ $OPTIONS{is_alsa} = 1 if $OPTIONS{is_linux} && `amixer --version` =~ m/^amixer\s+version\s+\d+/;
}
# ------------------------------------------------------------------------------

0 comments on commit 62a7f6f

Please sign in to comment.