1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var storedInputVolume = 0;
|
36 |
|
37 | var os = null;
|
38 |
|
39 | var exec = require('child_process').exec;
|
40 |
|
41 | var applescript = require("applescript");
|
42 |
|
43 | exports.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);
|
49 | } else {
|
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 |
|
63 | exports.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 |
|