UNPKG

4.97 kBJavaScriptView Raw
1/*
2
3BSD LICENSED
4
5Copyright (c) 2011, Janne Julkunen
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without modification,
9are permitted provided that the following conditions are met:
10
11* Redistributions of source code must retain the above copyright notice, this
12list of conditions and the following disclaimer.
13
14* Redistributions in binary form must reproduce the above copyright notice,
15this list of conditions and the following disclaimer in the documentation
16and/or other materials provided with the distribution.
17
18*Neither the name of the Enlightened Linux Solutions nor the names of its
19contributors may be used to endorse or promote products derived from this
20software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31OF THE POSSIBILITY OF SUCH DAMAGE.
32
33*/
34
35var storedInputVolume = 0; // Go around OS X having no mute for input
36
37var os = null;
38
39var exec = require('child_process').exec;
40
41var applescript = require("applescript");
42
43exports.setup = function(cb) {
44 var child = exec("pactl --help", function(error, stdout, stderr) {
45 if(error) {
46 var child = exec("osascript -e 'help'", function(error, stdout, stderr) {
47 if(error) {
48 cb(false);
49else {
50 os = "OSX";
51
52 cb("sound", "Mac OS X", "System Sound");
53 }
54 });
55 } else {
56 os = "Linux";
57
58 cb("sound", "PulseAudio", "System Sound");
59 }
60 });
61};
62
63exports.execute = function(req, res) {
64 console.log("Executing sound command: " + req.params[0]);
65
66 if(os == "Linux") {
67 var execute_string = "";
68
69 if(req.params[0] == "input") {
70 if(req.param("mute"))
71 execute_string += './data/bin/pulseaudio-control.sh input mute ' + req.param("mute") + ';';
72
73 if(req.param("volume"))
74 execute_string += './data/bin/pulseaudio-control.sh input volume ' + req.param("volume") + ';';
75 } else if(req.params[0] == "output") {
76 if(req.param("mute"))
77 execute_string += './data/bin/pulseaudio-control.sh output mute ' + req.param("mute") + ';';
78
79 if(req.param("volume"))
80 execute_string += './data/bin/pulseaudio-control.sh output volume ' + req.param("volume") + ';';
81 }
82
83 execute_string += './data/bin/pulseaudio-control.sh status';
84
85 var child = exec(execute_string, function(error, stdout, stderr) {
86 res.header('Content-Type', 'text/javascript');
87
88 if(error !== null) {
89 res.send({"state": "unknown", "input": {"volume": 0, "mute": true},
90 "output": {"volume": 0, "mute": true}});
91 } else {
92 var status = stdout.replace("\n", "").split(",");
93
94 res.send({"state": "online", "input": {"volume": parseInt(status[0]), "mute": (status[1] == "true")},
95 "output": {"volume": parseInt(status[2]), "mute": (status[3] == "true")}});
96 }
97 });
98 } else if(os == "OSX") {
99 var script_string = "";
100
101 if(req.params[0] == "input") {
102 if(req.param("mute") == "true")
103 var script_string = 'set volume input volume 0\n';
104 else if(req.param("volume"))
105 var script_string = 'set volume input volume ' + req.param("volume") + '\n';
106 else
107 var script_string = 'set volume input volume ' + storedInputVolume + '\n';
108 } else if(req.params[0] == "output") {
109 var script_string = 'set volume';
110
111 if(req.param("volume"))
112 script_string = 'set volume output volume ' + req.param("volume");
113
114 if(req.param("mute") == "true")
115 script_string += ' with output muted\n';
116 else
117 script_string += ' without output muted\n';
118 }
119
120 script_string += 'get volume settings\n';
121
122 applescript.execString(script_string, function(error, result) {
123 if(error) {
124 res.send({"state": "unknown", "input": {"volume": 0, "mute": true},
125 "output": {"volume": 0, "mute": true}});
126 } else {
127 var inputMuted = false;
128
129 var inputVolume = result[1].split(":")[1];
130
131 if(inputVolume != 0)
132 storedInputVolume = inputVolume;
133 else {
134 inputMuted = true;
135
136 inputVolume = storedInputVolume;
137 }
138
139 var state = "online";
140
141 if(result[3].split(":")[1] == "true")
142 state = "muted";
143
144 res.send({"state": "online", "input": {"volume": inputVolume, "mute": inputMuted},
145 "output": {"volume": result[0].split(":")[1], "mute": (result[3].split(":")[1] == "true")}});
146 }
147 });
148 }
149};
150