-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathlinux.js
More file actions
109 lines (96 loc) · 2.31 KB
/
linux.js
File metadata and controls
109 lines (96 loc) · 2.31 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var spawn = require('child_process').spawn;
var amixer = function (args, cb) {
var ret = '';
var err = null;
var p = spawn('amixer', args);
p.stdout.on('data', function (data) {
ret += data;
});
p.stderr.on('data', function (data) {
err = new Error('Alsa Mixer Error: ' + data);
});
p.on('close', function () {
cb(err, ret.trim());
});
};
var reDefaultDevice = /Simple mixer control \'([a-z0-9 -]+)\',[0-9]+/i;
var defaultDeviceCache = null;
var defaultDevice = function(cb) {
if(defaultDeviceCache === null) {
amixer([], function (err, data) {
if(err) {
cb(err);
} else {
console.log('DEFAULT DEVICE DATA', data)
var res = reDefaultDevice.exec(data);
if(res === null) {
cb(new Error('Alsa Mixer Error: failed to parse output'));
} else {
defaultDeviceCache = res[1];
cb(null, defaultDeviceCache);
}
}
});
} else {
cb(null, defaultDeviceCache);
}
};
var reInfo = /[a-z][a-z ]*\: Playback [0-9-]+ \[([0-9]+)\%\] (?:[[0-9\.-]+dB\] )?\[(on|off)\]/i;
var getInfo = function (cb) {
defaultDevice(function (err, dev) {
if(err) {
cb(err);
} else {
amixer(['get', dev], function (err, data) {
if(err) {
cb(err);
} else {
console.log('GET INFO DATA', data)
var res = reInfo.exec(data);
if(res === null) {
cb(new Error('Alsa Mixer Error: failed to parse output'));
} else {
cb(null, {
volume: parseInt(res[1], 10),
muted: (res[2] == 'off')
});
}
}
});
}
});
};
module.exports.getVolume = function (cb) {
getInfo(function (err, obj) {
if(err) {
cb(err);
} else {
cb(null, obj.volume);
}
});
};
module.exports.setVolume = function (val, cb) {
defaultDevice(function (err, dev) {
if(err) {
cb(err);
} else {
amixer(['set', dev, val + '%'], function (err) {
cb(err);
});
}
});
};
module.exports.getMuted = function (cb) {
getInfo(function (err, obj) {
if(err) {
cb(err);
} else {
cb(null, obj.muted);
}
});
};
module.exports.setMuted = function (val, cb) {
amixer(['set', 'PCM', (val?'mute':'unmute')], function (err) {
cb(err);
});
};