-
Notifications
You must be signed in to change notification settings - Fork 79
/
index.js
206 lines (170 loc) · 5.28 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
'use strict'
const record = require('node-record-lpcm16')
const stream = require('stream')
const { Detector, Models } = require('snowboy')
const ERROR = {
NOT_STARTED: "NOT_STARTED",
INVALID_INDEX: "INVALID_INDEX"
}
const ARECORD_FILE_LIMIT = 1500000000 // 1.5 GB
const CloudSpeechRecognizer = {}
CloudSpeechRecognizer.init = recognizer => {
const csr = new stream.Writable()
csr.listening = false
csr.recognizer = recognizer
return csr
}
CloudSpeechRecognizer.startStreaming = (options, audioStream, cloudSpeechRecognizer) => {
if (cloudSpeechRecognizer.listening) {
return
}
let hasResults = false
cloudSpeechRecognizer.listening = true
const request = {
config: {
encoding: 'LINEAR16',
sampleRateHertz: 16000,
languageCode: options.language,
speechContexts: options.speechContexts || null
},
singleUtterance: true,
interimResults: true,
}
const recognitionStream = cloudSpeechRecognizer.recognizer
.streamingRecognize(request)
.on('error', err => {
cloudSpeechRecognizer.emit('error', err)
stopStream()
})
.on('data', data => {
if (data.results[0] && data.results[0].alternatives[0]) {
hasResults = true;
// Emit partial or final results and end the stream
if (data.error) {
cloudSpeechRecognizer.emit('error', data.error)
stopStream()
} else if (data.results[0].isFinal) {
cloudSpeechRecognizer.emit('final-result', data.results[0].alternatives[0].transcript)
stopStream()
} else {
cloudSpeechRecognizer.emit('partial-result', data.results[0].alternatives[0].transcript)
}
} else {
// Reached transcription time limit
if(!hasResults){
cloudSpeechRecognizer.emit('final-result', '')
}
stopStream()
}
})
const stopStream = () => {
cloudSpeechRecognizer.listening = false
audioStream.unpipe(recognitionStream)
recognitionStream.end()
}
audioStream.pipe(recognitionStream)
}
const Sonus = {}
Sonus.annyang = require('./lib/annyang-core.js')
Sonus.init = (options, recognizer) => {
// don't mutate options
const opts = Object.assign({}, options),
models = new Models(),
sonus = new stream.Writable(),
csr = CloudSpeechRecognizer.init(recognizer)
sonus.mic = {}
sonus.recordProgram = opts.recordProgram
sonus.device = opts.device
sonus.started = false
// If we don't have any hotwords passed in, add the default global model
opts.hotwords = opts.hotwords || [1]
opts.hotwords.forEach(model => {
models.add({
file: model.file || 'node_modules/snowboy/resources/snowboy.umdl',
sensitivity: model.sensitivity || '0.5',
hotwords: model.hotword || 'default'
})
})
// defaults
opts.models = models
opts.resource = opts.resource || 'node_modules/snowboy/resources/common.res'
opts.audioGain = opts.audioGain || 2.0
opts.language = opts.language || 'en-US' //https://cloud.google.com/speech/docs/languages
const detector = sonus.detector = new Detector(opts)
detector.on('silence', () => sonus.emit('silence'))
detector.on('sound', () => sonus.emit('sound'))
// When a hotword is detected pipe the audio stream to speech detection
detector.on('hotword', (index, hotword) => {
sonus.trigger(index, hotword)
})
// Handel speech recognition requests
csr.on('error', error => sonus.emit('error', { streamingError: error }))
csr.on('partial-result', transcript => sonus.emit('partial-result', transcript))
csr.on('final-result', transcript => {
sonus.emit('final-result', transcript)
Sonus.annyang.trigger(transcript)
})
sonus.trigger = (index, hotword) => {
if (sonus.started) {
try {
let triggerHotword = (index == 0) ? hotword : models.lookup(index)
sonus.emit('hotword', index, triggerHotword)
CloudSpeechRecognizer.startStreaming(opts, sonus.mic, csr)
} catch (e) {
throw ERROR.INVALID_INDEX
}
} else {
throw ERROR.NOT_STARTED
}
}
sonus.pause = () => {
record.pause()
}
sonus.resume = () => {
record.resume()
}
return sonus
}
Sonus.start = sonus => {
sonus.mic = Recorder(sonus)
if(sonus.recordProgram === "arecord"){
ArecordHelper.init(sonus)
}
sonus.mic.pipe(sonus.detector)
sonus.started = true
}
const Recorder = (sonus) => {
return record.start({
threshold: 0,
device: sonus.device || null,
recordProgram: sonus.recordProgram || "rec",
verbose: false
})
}
const ArecordHelper = {byteCount: 0}
ArecordHelper.init = (sonus) => {
ArecordHelper.track(sonus)
}
ArecordHelper.track = (sonus) => {
sonus.mic.on('data', data => {
ArecordHelper.byteCount += data.length
// When we get to arecord wav file limit, reset
if(ArecordHelper.byteCount > ARECORD_FILE_LIMIT){
ArecordHelper.restart(sonus)
}
})
}
ArecordHelper.restart = (sonus) => {
sonus.mic.unpipe(sonus.detector)
record.stop()
// Restart the audio recording
sonus.mic = Recorder(sonus)
ArecordHelper.byteCount = 0
ArecordHelper.track(sonus)
sonus.mic.pipe(sonus.detector)
}
Sonus.trigger = (sonus, index, hotword) => sonus.trigger(index, hotword)
Sonus.pause = () => record.pause()
Sonus.resume = () => record.resume()
Sonus.stop = () => record.stop()
module.exports = Sonus