-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (55 loc) · 1.52 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
var fs = require('fs');
var path = require('path');
var proc = require('child_process');
var CronJob = require('cron').CronJob;
var moment = require('moment');
var config = require('./config');
var acron = require('./');
var prev;
// Periodically record 1 second wav files
var job = new CronJob({
cronTime: config.period
, onTick: onTick
, start: config.immediate
});
onTick();
// As wav files come into the directory, convert them to
// mp3 and then delete the old wav file
//fs.watch( config.wavDir, onWavDirChange );
function onComplete(){
console.log("complete!");
process.exit(0);
}
function onError( error ){
console.error( error );
};
function onSuccess( filename ){
console.log( "Processed", filenamed );
}
function onTick(){
console.log('tick');
if ( prev ) prev.kill();
prev = acron.record( function( error ){
if ( error ) return onError( error );
console.log('Recording complete!');
});
}
function onWavDirChange( e, filename ){
return;
return console.log('onWavDirChange', filename);
var lame = proc.spawn(
'lame'
, config.lame.concat( path.join( config.mp3Dir, filename ) )
);
fs.createReadStream( filename ).pipe(
lame.stdin
).on( 'close', onLameEncodingComplete.bind( null, filename ) );
}
function onLameEncodingComplete( filename ){
fs.unlink( path.join( config.mp3Dir, filename ), function( error ){
if ( error ) return onError( error );
onSuccess( filename );
});
}
module.exports.onTick = onTick;
module.exports.onWavDirChange = onWavDirChange;