Browse Source

Added volume control support

Brendan Abolivier 8 years ago
parent
commit
9017343ec0
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 changed files with 30 additions and 1 deletions
  1. 27
    1
      welcomehome
  2. 3
    0
      welcomehome.example.conf

+ 27
- 1
welcomehome View File

23
     return $?
23
     return $?
24
 }
24
 }
25
 
25
 
26
+check_volume() {
27
+    # Check if the volume value is valid
28
+    if [ $1 -gt 100 ] || [ $1 -lt 0 ]; then
29
+        echo "Invalid volume value"
30
+        exit 1
31
+    fi
32
+}
33
+
34
+set_volume() {
35
+    # Set the volume to the right value
36
+    volume=$1
37
+    check_volume $volume
38
+    log "Set volume to $volume"
39
+
40
+    data='{"jsonrpc": "2.0", "id": 1, "method": "core.mixer.set_volume", "params": [VOLUME]}'
41
+    # Using sed cuz simple quotes
42
+    data=`echo $data | sed "s/VOLUME/${volume}/"`
43
+
44
+    # Send the load
45
+    curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
46
+}
47
+
26
 play_stream() {
48
 play_stream() {
27
     # Reset playback state
49
     # Reset playback state
28
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
50
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
39
     # Play dat sound
61
     # Play dat sound
40
     curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
62
     curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
41
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
63
     curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
64
+
65
+    # The right music at the righ volume. Nothing's more perfect.
66
+    set_volume $2
42
 }
67
 }
43
 
68
 
44
 stop_stream() {
69
 stop_stream() {
59
 log "Parsing the configuration file"
84
 log "Parsing the configuration file"
60
 
85
 
61
 # Get parameters from the given configuration file
86
 # Get parameters from the given configuration file
87
+volume=$(parse_arg $config_file "volume")
62
 stream=$(parse_arg $config_file "stream")
88
 stream=$(parse_arg $config_file "stream")
63
 threshold=$(parse_arg $config_file "device_threshold")
89
 threshold=$(parse_arg $config_file "device_threshold")
64
 address=$(parse_arg $config_file "device_address")
90
 address=$(parse_arg $config_file "device_address")
80
         # Lock file present = music already playing
106
         # Lock file present = music already playing
81
         if [ ! -f $lock_file ]; then
107
         if [ ! -f $lock_file ]; then
82
             log "Device detected"
108
             log "Device detected"
83
-            play_stream $stream
109
+            play_stream $stream $volume
84
             touch $lock_file
110
             touch $lock_file
85
         fi
111
         fi
86
     else
112
     else

+ 3
- 0
welcomehome.example.conf View File

8
 
8
 
9
 # URL of the stream to be played when the device is detected
9
 # URL of the stream to be played when the device is detected
10
 stream = http://fr5.ah.fm:443
10
 stream = http://fr5.ah.fm:443
11
+
12
+# Volume to play the stream at. Goes from 0 to 100
13
+volume = 80