forked from vincentsaluzzo/node-microphone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (46 loc) · 1.6 KB
/
index.js
File metadata and controls
56 lines (46 loc) · 1.6 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
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 || {};
var alsa_format = options.alsa_format || 'dat',
alsa_device = options.alsa_device || 'plughw:1,0',
alsa_addn_args = options.alsa_addn_args || [],
sox_format = options.sox_format || 'dat',
sox_addn_args = options.sox_addn_args || [],
mp3_channels = options.mp3_channels || 2,
mp3_bitDepth = options.mp3_bitDepth || 16,
mp3_sampleRate = options.mp3_sampleRate || 44100;
if(ps == null) {
ps = isMacOrWin
? spawn('sox', ['-d', '-t', sox_format, '-p'].concat(sox_addn_args))
: spawn('arecord', ['-D', alsa_device, '-f', alsa_format].concat(alsa_addn_args));
if(options.mp3output === true) {
var encoder = new lame.Encoder( {
channels: mp3_channels,
bitDepth: mp3_bitDepth,
sampleRate: mp3_sampleRate
});
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;