-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.js
More file actions
55 lines (43 loc) · 1.25 KB
/
encoder.js
File metadata and controls
55 lines (43 loc) · 1.25 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
'use strict';
var child_process = require('child_process');
var path = require('path');
var fs = require('fs');
// path is expected to be something like /home/pi/PiFmRds/src/pi_fm_rds (on a tinyfm vanilla install)
var pifmPath = process.env.PIFM_PATH || path.join('/', 'home', 'pi', 'PiFmRds', 'src', 'pi_fm_rds');
var activeStream;
module.exports = function(options){
var resolvedOptions = options || {};
var pipeline = resolveStreamer(resolvedOptions.env || process.env.NODE_ENV || null);
return function streamFrom(filepath){
clearStream(function(){
var resolvedFilepath = path.resolve(resolvedOptions.baseDir, filepath);
var stream = fs.createReadStream(resolvedFilepath);
stream.pipe(pipeline.stdin);
return stream;
});
};
};
function clearStream(fn){
if (activeStream){
activeStream.unpipe();
activeStream.close(function(){
activeStream = fn();
});
}
else {
process.nextTick(function(){
activeStream = fn();
});
}
}
function resolveStreamer(env){
var args = [];
if (env !== 'production'){
args = ['play', ['-']];
}
else {
var pifmPath = require.resolve(pifmPath);
args = [pifmPath, ["-audio", "-"]];
}
return child_process.spawn.apply(child_process, args);
}