Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.

Commit

Permalink
show and allow set sound volume
Browse files Browse the repository at this point in the history
  • Loading branch information
msoap committed Feb 2, 2013
1 parent 8609803 commit f030dfd
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 6 deletions.
113 changes: 109 additions & 4 deletions mojolicious-radio-box.pl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ sub init {
close $FH;
$OPTIONS{radio_playlist_dir} =~ s/^~/$ENV{HOME}/ if defined $OPTIONS{radio_playlist_dir};
}

$OPTIONS{is_mac} = 1 if $^O eq 'darwin';
}

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -116,6 +118,10 @@ sub cmus_get_info {
close $FH;
}

if ($OPTIONS{is_mac}) {
$info->{volume} = int(`osascript -e "output volume of (get volume settings)"`);
}

return $info;
}

Expand Down Expand Up @@ -238,6 +244,30 @@ sub cmus_get_music {

# ------------------------------------------------------------------------------

=head2 cmus_set_volume
Set sound volume
=cut

sub cmus_set_volume {
my $volume = shift;

die "cmus_set_volume: volume is invalid"
unless defined $volume
&& $volume =~ /^\d+$/
&& $volume >= 0
&& $volume <= 100;

if ($OPTIONS{is_mac}) {
system("osascript", "-e", "set volume output volume $volume");
}

return;
}

# ------------------------------------------------------------------------------

=head1 _cmus_parse_info
Parse lines from cmus-remote -Q
Expand Down Expand Up @@ -313,6 +343,14 @@ sub _cmus_parse_info {
return $self->render_json({status => 'ok', info => cmus_get_music()});
};

any '/set_volume' => sub {
my $self = shift;

my $volume = $self->param("volume");
cmus_set_volume($volume);
return $self->render_json({status => 'ok'});
};

# go ---------------------------------------------------------------------------
init();
app
Expand Down Expand Up @@ -367,6 +405,19 @@ sub _cmus_parse_info {
width: 170px;
display: none;
}
input#volume_slider {
margin: 7px 10px;
width: 150px;
}
.volume-buttons {
width: 50px;
position: relative;
top: -3px;
font-size: 10pt;
border: 1px solid #888;
border-radius: 5px;
background-color: #eee;
}
</style>
<link rel="stylesheet" href="/font-awesome.css">
</head>
Expand All @@ -377,10 +428,17 @@ sub _cmus_parse_info {
<button class="nav_buttons" id="bt_pause"><i class="icon-play"></i>&nbsp;&nbsp;play</button>
<button class="nav_buttons" id="bt_next">next&nbsp;&nbsp;<i class="icon-forward"></i></button>
</div>
<div id="div_info"></div>
<button class="nav_buttons" id="bt_get_radio"><i class="icon-volume-up"></i>&nbsp;&nbsp;get radio&hellip;</button>
<button class="nav_buttons" id="bt_get_radio"><i class="icon-tasks"></i>&nbsp;&nbsp;get radio&hellip;</button>
<select id="radio_stations"></select><br>
<button class="nav_buttons" id="bt_get_music"><i class="icon-music"></i>&nbsp;&nbsp;get music&hellip;</button>
<button class="nav_buttons" id="bt_get_music"><i class="icon-music"></i>&nbsp;&nbsp;get music&hellip;</button><br><br>
<button id="volume_down" class="volume-buttons"><i class="icon-volume-down"></i></button>
<input id="volume_slider" type="range" min="0" max="100" step="1">
<button id="volume_up" class="volume-buttons"><i class="icon-volume-up" id="volume_up"></i></button>
<div id="div_error">Server unavailable&hellip;</div>
</body>
</html>
Expand All @@ -405,6 +463,18 @@ sub _cmus_parse_info {
$("#bt_get_radio").on('click', App.do_get_radio);
$("#bt_get_music").on('click', App.do_get_music);
$("#radio_stations").on('change', App.do_select_radio);
$("#volume_slider").on('change', {
absolute: true
}, App.do_change_volume);
$("#volume_slider").on('blur', {
absolute: true
}, App.do_change_volume);
$("#volume_down").on('click', {
down: 10
}, App.do_change_volume);
$("#volume_up").on('click', {
up: 10
}, App.do_change_volume);
$(document).ajaxError(function() {
return $("#div_error").show().fadeOut(1500, function() {
return $("button.nav_buttons").removeAttr('disabled');
Expand Down Expand Up @@ -445,11 +515,14 @@ sub _cmus_parse_info {
if (App.info.radio_title && App.info.file.match(/http:\/\//)) {
$("#radio_stations").show();
if (App.radio_stations.length) {
return App.render_select_radio();
App.render_select_radio();
} else {
return App.do_get_radio();
App.do_get_radio();
}
}
if (App.info.volume !== void 0) {
return $('input#volume_slider').val(App.info.volume);
}
},
render_select_radio: function() {
var item, new_option, select_input, _i, _len, _ref, _results;
Expand Down Expand Up @@ -523,6 +596,38 @@ sub _cmus_parse_info {
return App.render_info();
});
}
},
do_change_volume: function(event) {
var new_volume;
if (App._change_valume_tid) {
window.clearTimeout(App._change_valume_tid);
App._change_valume_tid = void 0;
}
new_volume = 0;
if (event.data.up) {
new_volume = App.info.volume + event.data.up;
if (new_volume > 100) {
new_volume = 100;
}
} else if (event.data.down) {
new_volume = App.info.volume - event.data.down;
if (new_volume < 0) {
new_volume = 0;
}
} else if (event.data.absolute) {
new_volume = parseInt($("#volume_slider").val());
} else {
return;
}
return App._change_valume_tid = window.setTimeout(function() {
if (new_volume !== void 0 && new_volume !== App.info.volume) {
App.info.volume = new_volume;
$("#volume_slider").val(new_volume);
return $.get('/set_volume', {
volume: new_volume
});
}
}, 200);
}
};
Expand Down
28 changes: 28 additions & 0 deletions src/cmus-client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ sub cmus_get_info {
close $FH;
}

if ($OPTIONS{is_mac}) {
$info->{volume} = int(`osascript -e "output volume of (get volume settings)"`);
}

return $info;
}

Expand Down Expand Up @@ -154,6 +158,30 @@ sub cmus_get_music {

# ------------------------------------------------------------------------------

=head2 cmus_set_volume
Set sound volume
=cut

sub cmus_set_volume {
my $volume = shift;

die "cmus_set_volume: volume is invalid"
unless defined $volume
&& $volume =~ /^\d+$/
&& $volume >= 0
&& $volume <= 100;

if ($OPTIONS{is_mac}) {
system("osascript", "-e", "set volume output volume $volume");
}

return;
}

# ------------------------------------------------------------------------------

=head1 _cmus_parse_info
Parse lines from cmus-remote -Q
Expand Down
24 changes: 22 additions & 2 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@
width: 170px;
display: none;
}
input#volume_slider {
margin: 7px 10px;
width: 150px;
}
.volume-buttons {
width: 50px;
position: relative;
top: -3px;
font-size: 10pt;
border: 1px solid #888;
border-radius: 5px;
background-color: #eee;
}
</style>
<link rel="stylesheet" href="/font-awesome.css">
</head>
Expand All @@ -54,10 +67,17 @@ <h1>♫♬ Mojolicious radio box</h1>
<button class="nav_buttons" id="bt_pause"><i class="icon-play"></i>&nbsp;&nbsp;play</button>
<button class="nav_buttons" id="bt_next">next&nbsp;&nbsp;<i class="icon-forward"></i></button>
</div>

<div id="div_info"></div>
<button class="nav_buttons" id="bt_get_radio"><i class="icon-volume-up"></i>&nbsp;&nbsp;get radio&hellip;</button>

<button class="nav_buttons" id="bt_get_radio"><i class="icon-tasks"></i>&nbsp;&nbsp;get radio&hellip;</button>
<select id="radio_stations"></select><br>
<button class="nav_buttons" id="bt_get_music"><i class="icon-music"></i>&nbsp;&nbsp;get music&hellip;</button>
<button class="nav_buttons" id="bt_get_music"><i class="icon-music"></i>&nbsp;&nbsp;get music&hellip;</button><br><br>

<button id="volume_down" class="volume-buttons"><i class="icon-volume-down"></i></button>
<input id="volume_slider" type="range" min="0" max="100" step="1">
<button id="volume_up" class="volume-buttons"><i class="icon-volume-up" id="volume_up"></i></button>

<div id="div_error">Server unavailable&hellip;</div>
</body>
</html>
34 changes: 34 additions & 0 deletions src/radio-box-client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ window.App =
$("#bt_get_radio").on('click', App.do_get_radio)
$("#bt_get_music").on('click', App.do_get_music)
$("#radio_stations").on('change', App.do_select_radio)
$("#volume_slider").on('change', {absolute: true}, App.do_change_volume)
$("#volume_slider").on('blur', {absolute: true}, App.do_change_volume)
$("#volume_down").on('click', {down: 10}, App.do_change_volume)
$("#volume_up").on('click', {up: 10}, App.do_change_volume)

$(document).ajaxError () ->
$("#div_error").show()
Expand Down Expand Up @@ -68,6 +72,9 @@ window.App =
else
App.do_get_radio()

if App.info.volume != undefined
$('input#volume_slider').val(App.info.volume)

# ...........................................
render_select_radio: ->
select_input = $('#radio_stations')[0]
Expand Down Expand Up @@ -132,5 +139,32 @@ window.App =
App.info = info_data.info
App.render_info()

# ...........................................
do_change_volume: (event) ->
if App._change_valume_tid
window.clearTimeout App._change_valume_tid
App._change_valume_tid = undefined

new_volume = 0
if event.data.up
new_volume = App.info.volume + event.data.up
new_volume = 100 if new_volume > 100
else if event.data.down
new_volume = App.info.volume - event.data.down
new_volume = 0 if new_volume < 0
else if event.data.absolute
new_volume = parseInt($("#volume_slider").val())
else
return

App._change_valume_tid = window.setTimeout(
() ->
if new_volume != undefined && new_volume != App.info.volume
App.info.volume = new_volume
$("#volume_slider").val(new_volume)
$.get '/set_volume'
volume: new_volume
200
)
# .............................................................................
$ () -> App.init()
8 changes: 8 additions & 0 deletions src/routers.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@ any '/get_music' => sub {
my $self = shift;
return $self->render_json({status => 'ok', info => cmus_get_music()});
};

any '/set_volume' => sub {
my $self = shift;

my $volume = $self->param("volume");
cmus_set_volume($volume);
return $self->render_json({status => 'ok'});
};
2 changes: 2 additions & 0 deletions src/util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ sub init {
close $FH;
$OPTIONS{radio_playlist_dir} =~ s/^~/$ENV{HOME}/ if defined $OPTIONS{radio_playlist_dir};
}

$OPTIONS{is_mac} = 1 if $^O eq 'darwin';
}

# ------------------------------------------------------------------------------
Expand Down

0 comments on commit f030dfd

Please sign in to comment.