Permalink
Browse files

show and allow set sound volume

  • Loading branch information...
1 parent 8609803 commit f030dfdfb0f1e9b45b87295ca527b3dc1e8295f8 @msoap committed Feb 2, 2013
Showing with 203 additions and 6 deletions.
  1. +109 −4 mojolicious-radio-box.pl
  2. +28 −0 src/cmus-client.pm
  3. +22 −2 src/index.html
  4. +34 −0 src/radio-box-client.coffee
  5. +8 −0 src/routers.pm
  6. +2 −0 src/util.pm
@@ -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';
}
# ------------------------------------------------------------------------------
@@ -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;
}
@@ -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
@@ -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
@@ -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>
@@ -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>
@@ -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');
@@ -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;
@@ -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);
}
};
View
@@ -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;
}
@@ -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
View
@@ -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>
@@ -54,10 +67,17 @@
<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>
@@ -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()
@@ -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]
@@ -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()
View
@@ -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'});
+};
View
@@ -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';
}
# ------------------------------------------------------------------------------

0 comments on commit f030dfd

Please sign in to comment.