-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (51 loc) · 1.85 KB
/
index.js
File metadata and controls
64 lines (51 loc) · 1.85 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
const { spawn } = require('child_process');
const streamSplitter = require('stream-splitter');
const events = require('events');
const MAX_ROLLOFF_DIFF = 4;
let sndpeekProcess = null;
let splitter = null;
module.exports = new events.EventEmitter();
module.exports.start = c => {
const config = c || {};
if (sndpeekProcess) {
return new Error('A sndpeek process is already running.');
}
config.inputDevice = config.inputDevice || 0;
config.sndpeekPath = config.sndpeekPath || 'sndpeek';
config.maxToneCentroidOffset = config.maxToneCentroidOffset || 2;
sndpeekProcess = spawn(config.sndpeekPath, ['--nodisplay', '--print', `--inputDevice:${config.inputDevice}`]);
sndpeekProcess.on('exit', code => {
module.exports.emit('error', new Error(`sndpeek process exited with code ${code}`));
});
sndpeekProcess.on('error', e => {
module.exports.emit('error', e);
});
splitter = sndpeekProcess.stdout.pipe(streamSplitter('\n'));
splitter.on('token', data => {
const tokens = (`${data}`).split(' ');
const value = {
centroid: parseFloat(tokens[0]),
rolloffLow: parseFloat(tokens[3]),
rolloffHigh: parseFloat(tokens[4]),
};
// Simple check to see if the program initializes. Very specific numbers in that case.
if (value.centroid === 256 || value.rolloffHigh === 511 || value.rolloffLow === 511) {
return;
}
if (Math.abs(value.rolloffHigh - value.rolloffLow) > MAX_ROLLOFF_DIFF) {
return;
}
if (config.targetToneCentroid &&
Math.abs(value.centroid - config.targetToneCentroid) < config.maxToneCentroidOffset) {
module.exports.emit('event', value);
} else {
module.exports.emit('event', value);
}
});
};
module.exports.stop = () => {
if (!sndpeekProcess) {
return new Error('There is no sndpeek process running to kill.');
}
sndpeekProcess.kill();
};