-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
117 lines (104 loc) · 3.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict'
var spawn = require('child_process').spawn
var cp // Recording process
// returns a Readable stream
exports.start = function (options) {
cp = null // Empty out possibly dead recording process
var defaults = {
sampleRate: 16000,
channels: 1,
compress: false,
threshold: 0.5,
thresholdStart: null,
thresholdEnd: null,
silence: '1.0',
verbose: false,
recordProgram: 'sox'
}
options = Object.assign(defaults, options)
// Capture audio stream
var cmd, cmdArgs, cmdOptions, audioType
switch (options.recordProgram) {
// On some Windows machines, sox is installed using the "sox" binary
// instead of "rec"
case 'sox':
cmd = "sox"
audioType = "wav"
if (options.audioType) audioType = options.audioType
if (options.asRaw) audioType = "raw"
cmdArgs = [
'-q', // show no progress
'-t', 'waveaudio',
'-d',
'-r', options.sampleRate.toString(), // sample rate
'-c', '1', // channels
'-e', 'signed-integer', // sample encoding
'-b', '16', // precision (bits)
'-t', audioType, // audio type
'-'
]
break
case 'rec':
default:
cmd = "rec"
audioType = "wav"
if (options.audioType) audioType = options.audioType
cmdArgs = [
'-q', // show no progress
'-r', options.sampleRate, // sample rate
'-c', options.channels, // channels
'-e', 'signed-integer', // sample encoding
'-b', '16', // precision (bits)
'-t', audioType, // audio type
'-', // pipe
// end on silence
'silence', '1', '0.1', options.thresholdStart || options.threshold + '%',
'1', options.silence, options.thresholdEnd || options.threshold + '%'
]
break
// On some systems (RasPi), arecord is the prefered recording binary
case 'arecord':
cmd = 'arecord'
audioType = "wav"
if (options.audioType) audioType = options.audioType
cmdArgs = [
'-q', // show no progress
'-r', options.sampleRate, // sample rate
'-c', options.channels, // channels
'-t', audioType, // audio type
'-f', 'S16_LE', // Sample format
'-' // pipe
]
if (options.device) {
cmdArgs.unshift('-D', options.device)
}
break
}
// Spawn audio capture command
cmdOptions = { encoding: 'binary' }
if (options.device) {
cmdOptions.env = Object.assign({}, process.env, { AUDIODEV: options.device })
}
cp = spawn(cmd, cmdArgs, cmdOptions)
var rec = cp.stdout
if (options.verbose) {
console.log('Recording', options.channels, 'channels with sample rate',
options.sampleRate + '...')
console.time('End Recording')
rec.on('data', function (data) {
console.log('Recording %d bytes', data.length)
})
rec.on('end', function () {
console.timeEnd('End Recording')
})
}
return rec
}
exports.stop = function () {
if (!cp) {
console.log('Please start a recording first')
return false
}
cp.kill() // Exit the spawned process, exit gracefully
return cp
}