-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
48 lines (38 loc) · 1.1 KB
/
index.js
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
var isMacOrWin = require('os').type() == 'Darwin' || require('os').type().indexOf('Windows') > -1;
var spawn = require('child_process').spawn
var PassThrough = require('stream').PassThrough;
var lame = require('lame');
var ps = null;
var audio = new PassThrough;
var info = new PassThrough;
var start = function(options) {
options = options || {};
if(ps == null) {
ps = isMacOrWin
? spawn('sox', ['-d', '-t', 'dat', '-p'])
: spawn('arecord', ['-D', 'plughw:1,0', '-f', 'dat']);
if(options.mp3output === true) {
var encoder = new lame.Encoder( {
channels: 2,
bitDepth: 16,
sampleRate: 44100
});
ps.stdout.pipe(encoder);
encoder.pipe(audio);
ps.stderr.pipe(info);
} else {
ps.stdout.pipe(audio);
ps.stderr.pipe(info);
}
}
};
var stop = function() {
if(ps) {
ps.kill();
ps = null;
}
};
exports.audioStream = audio;
exports.infoStream = info;
exports.startCapture = start;
exports.stopCapture = stop;