-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord2.js
44 lines (39 loc) · 1.34 KB
/
record2.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
// Imports modules.
const fs = require(`fs`),
path = require(`path`);
const AudioRecorder = require('node-audiorecorder');
// Constants.
const DIRECTORY = `examples-recordings`;
// Initialize recorder and file stream.
const audioRecorder = new AudioRecorder({
program: process.platform === `win32` ? `sox` : `rec`,
silence: 0
}, console);
// Create path to write recordings to.
if (!fs.existsSync(DIRECTORY)){
fs.mkdirSync(DIRECTORY);
}
// Create file path with random name.
const fileName = path.join(DIRECTORY, Math.random().toString(36).replace(/[^a-z]+/g, ``).substr(0, 4).concat(`.wav`));
console.log(`Writing new recording file at: `, fileName);
// Create write stream.
const fileStream = fs.createWriteStream(fileName, { encoding: `binary` });
// Start and write to the file.
audioRecorder.start().stream().pipe(fileStream);
// Log information on the following events
audioRecorder.stream().on(`close`, function(code) {
console.warn(`Recording closed. Exit code: `, code);
});
audioRecorder.stream().on(`end`, function() {
console.warn(`Recording ended.`);
});
audioRecorder.stream().on(`error`, function() {
console.warn(`Recording error.`);
});
// Write incoming data out the console.
/*audioRecorder.stream().on(`data`, function(chunk) {
console.log(chunk);
});*/
// Keep process alive.
process.stdin.resume();
console.warn(`Press ctrl+c to exit.`);