-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (44 loc) · 1.27 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
49
50
51
52
53
54
55
56
57
58
59
/*!
* ear-pipe
* Pipe audio streams to your ears
* Dan Motzenbecker <[email protected]>
* MIT Licensed
*/
"use strict";
var spawn = require('child_process').spawn,
util = require('util'),
Duplex = require('stream').Duplex,
apply = function(obj, method, args) {
return obj[method].apply(obj, args);
}
var EarPipe = function(type, bits, transcode) {
var params;
if (!(this instanceof EarPipe)) {
return new EarPipe(type, bits, transcode);
}
Duplex.apply(this);
params = ['-q', '-b', bits || 16, '-t', type || 'mp3', '-'];
if (transcode) {
this.process = spawn('sox',
params.concat(['-t', typeof transcode === 'string' ? transcode : 'wav', '-']));
} else {
this.process = spawn('play', params);
}
}
util.inherits(EarPipe, Duplex);
EarPipe.prototype._read = function() {
return apply(this.process.stdout, 'read', arguments);
}
EarPipe.prototype._write = function() {
return apply(this.process.stdin, 'write', arguments);
}
EarPipe.prototype.pipe = function() {
return apply(this.process.stdout, 'pipe', arguments);
}
EarPipe.prototype.end = function() {
return apply(this.process.stdin, 'end', arguments);
}
EarPipe.prototype.kill = function() {
return apply(this.process, 'kill', arguments);
}
module.exports = EarPipe;