-
Notifications
You must be signed in to change notification settings - Fork 9
/
voskjs.js
executable file
·774 lines (606 loc) · 22.2 KB
/
voskjs.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#!/usr/bin/env node
/**
* @module voskjs
*
* @public
* @function logLevel
* @function loadModel
* @function transcript
* @function freeModel
*
-* @see VoskAPI https://github.com/alphacep/vosk-api/blob/master/nodejs/index.js
* @see https://github.com/alphacep/vosk-api/blob/master/nodejs/index.js#L207
* @see https://www.tutorialsteacher.com/nodejs/nodejs-eventemitter
* util.inherits(vosk.Recognizer, emitter)
*/
const fs = require('fs')
const util = require('util')
const emitter = require('events').EventEmitter
const { Readable } = require('stream')
const wav = require('wav')
const vosk = require('vosk')
const { info } = require('./lib/info')
const { getArgs } = require('./lib/getArgs')
const { setTimer, getTimer, unixTimeMsecs } = require('./lib/chronos')
/**
* @constant
*/
const SAMPLE_RATE = 16000
const PARTIAL_RESULT_EVENT = 'partial'
const END_OF_SPEECH_EVENT = 'endOfSpeech'
const FINAL_RESULT_EVENT = 'final'
/**
* @function logLevel
* @public
* Set log level for Vosk/Kaldi log messages
*
* @param {number} level The higher, the more verbose. 0 for infos and errors. Less than 0 for silence.
*/
function logLevel(level=0) {
// set vosk log level
vosk.setLogLevel(level)
}
/**
* @function loadModel
* Create a run time model from the specified directory
*
* @public
*
* @param {String} modelDirectory directory name of the Vosk model
*
* @typedef ModelObject
* @property {VoskModel} model run time model object returned by Vosk engine.
* @property {Number} latency elpased time in msecs
*
* @return {promise<ModelObject>}
*
*/
function loadModel(modelDirectory) {
// check if model directory exists, async
fs.access(modelDirectory, (err) => {
if (err)
throw `${err}: file ${modelDirectory} not found.`
})
// create new run time model from the specified directory
// TODO try/catch?
const model = new vosk.Model(modelDirectory)
return model
}
/**
* @function createRecognizer
*
* Create a run time Vosk recognizer
*
* @typedef VoskRecognizerArgsObject
* @property {Boolean} multiThreads if true, an external (Vosk engine) thread is spawned on the fly
* that need in server (concurrent requests) architecture.
* @property {Number} sampleRate Default value: 16000
* @property {String[]} grammar array of words, or sentences
* @property {Number} alternatives maximum alternatives to return from recognition results
* @property {Boolean} words if true, recognizer result will include word by word details
* @example
* {
* result: [
* { conf: 1, end: 1.02, start: 0.36, word: 'experience' },
* { conf: 1, end: 1.35, start: 1.02, word: 'proves' },
* { conf: 1, end: 1.74, start: 1.35, word: 'this' }
* ],
* text: 'experience proves this'
* }
*
* @param {ModelObject} model the Vosk model returned by InitModel()
* @param {VoskRecognizerArgsObject} [options] Vosk Recognizer arguments setting. Optional
* @return {VoskRecognizerObject}
*
*/
function createRecognizer(model, { sampleRate=SAMPLE_RATE, grammar=null, alternatives=0, words=true } = {}) {
// if a grammar is specified, pass it to the Vosk Recognizer
const voskRecognizerArgs = grammar ?
{model, sampleRate, grammar} :
{model, sampleRate}
// create Vosk recognizer
// TODO try/catch?
const recognizer = new vosk.Recognizer(voskRecognizerArgs)
if ( alternatives )
recognizer.setMaxAlternatives(alternatives)
recognizer.setWords(words)
return recognizer
}
/**
* @function transcriptFromFile
* speech recognition into a text, from an audio file, given a specified Vosk model
*
* @alias transcript
* @public
* @async
*
* @param {String} fileName the name of speech file, in WAV format
* @param {ModelObject} model the Vosk model returned by InitModel()
* @param {VoskRecognizerArgsObject} [options] Vosk Recognizer arguments setting. Optional.
*
* @return {Promise<VoskResultObject>} transcript object returned by Vosk engine
*
*/
async function transcriptFromFile(fileName, model, { multiThreads=true, sampleRate=SAMPLE_RATE, grammar=null, alternatives=0, words=true } = {}) {
const DEBUG = false
return new Promise( (resolve, reject) => {
// validate audiofile existence, async
fs.access(fileName, (err) => {
if (err)
return reject(`${err}: file ${fileName} not found.`)
})
if (DEBUG)
setTimer('createRecognizer')
const recognizer = createRecognizer( model, {sampleRate, grammar, alternatives, words} )
if (DEBUG)
console.log(`recognizer latency : ${getTimer('createRecognizer')}ms`)
const wfStream = fs.createReadStream(fileName, {'highWaterMark': 4096})
const wfReader = new wav.Reader()
wfStream.pipe(wfReader)
const pcmChunks = new Readable().wrap(wfReader)
wfReader.on('format', async ( { audioFormat, sampleRate, channels } ) => {
if (audioFormat != 1 || channels != 1)
return reject(`${fileName}: audio file (sample rate: ${sampleRate}) must be WAV format mono PCM.`)
for await (const data of pcmChunks) {
//
// WARNING
// From vosk version 0.3.25
// the acceptWaveformAsync function runs in a dedicated thread.
// That wold improve performances in case of concurrent requests
// from the caller (server) program
//
// Previous vosk version 0.3.25
// const end_of_speech = recognizer.acceptWaveform(data)
//
const end_of_speech = multiThreads ?
await recognizer.acceptWaveformAsync(data) :
recognizer.acceptWaveform(data)
//
// WARNING
// 1. AcceptWaveform returns true when silence is detected and you can retrieve the result with Result().
// 2. If silence is not detected you can retrieve PartialResult() only.
// 3. FinalResult means the stream is ended, you flush the buffers and retrieve remaining result.
// By Nicolay Shmirev. See: https://github.com/alphacep/vosk-api/issues/590#issuecomment-863065813
//
if (end_of_speech) {
// End of speech means silence detected.
// We want to transcript all the audio so the processing continue until the end.
// debug
//console.log('DEBUG', END_OF_SPEECH_EVENT, recognizer.result())
continue
}
//else
// console.log('partialResult', recognizer.partialResult())
}
// copy final Vosk engine result object
const result = {...recognizer.finalResult(recognizer)}
recognizer.free()
return resolve(result)
})
})
}
/**
* @function transcriptEventsFromFile
*
* speech recognition into a text, from an audio file, given a specified Vosk model,
* return an events emitter
*
* @public
*
* @param {String} fileName the name of speech file, in WAV format
* @param {ModelObject} model the Vosk model returned by InitModel()
* @param {VoskRecognizerArgsObject} [options] Vosk Recognizer arguments setting. Optional.
*
* @return {Emitter} emit events
*
*/
function transcriptEventsFromFile(fileName, model, { multiThreads=true, sampleRate=SAMPLE_RATE, grammar=null, alternatives=0, words=true } = {}) {
const DEBUG = false
// validate audiofile existence, async
fs.access(fileName, (err) => {
if (err)
throw (`${err}: file ${fileName} not found.`)
})
if (DEBUG)
setTimer('createRecognizer')
// the function is enabled to emit events
const event = new emitter()
const recognizer = createRecognizer( model, {sampleRate, grammar, alternatives, words} )
if (DEBUG)
console.log(`recognizer latency : ${getTimer('createRecognizer')}ms`)
const wfStream = fs.createReadStream(fileName, {'highWaterMark': 4096})
const wfReader = new wav.Reader()
wfStream.pipe(wfReader)
const pcmChunks = new Readable().wrap(wfReader)
wfReader.on('format', async ( { audioFormat, sampleRate, channels } ) => {
if (audioFormat != 1 || channels != 1)
throw (`${fileName}: audio file (sample rate: ${sampleRate}) must be WAV format mono PCM.`)
const lastPartialResult = {}
for await (const data of pcmChunks) {
//
// WARNING
// From vosk version 0.3.25
// the acceptWaveformAsync function runs in a dedicated thread.
// That wold improve performances in case of concurrent requests
// from the caller (server) program
//
// Previous vosk version 0.3.25
// const end_of_speech = recognizer.acceptWaveform(data)
//
const end_of_speech = multiThreads ?
await recognizer.acceptWaveformAsync(data) :
recognizer.acceptWaveform(data)
//
// WARNING
// Emit partial result events
// 1. AcceptWaveform returns true when silence is detected and you can retrieve the result with Result().
// 2. If silence is not detected you can retrieve PartialResult() only.
// 3. FinalResult means the stream is ended, you flush the buffers and retrieve remaining result.
// By Nicolay Shmirev. See: https://github.com/alphacep/vosk-api/issues/590#issuecomment-863065813
//
if (end_of_speech)
event.emit(END_OF_SPEECH_EVENT, recognizer.result())
else {
const partialResult = recognizer.partialResult()
// Doesn't emit duplicated events:
// Is the current partialResult different form the last one?
// If true a new event is emiitted.
// This avoid "duplicated" events to be emitted
if (partialResult.partial !== lastPartialResult.partial) {
event.emit(PARTIAL_RESULT_EVENT, partialResult)
lastPartialResult.partial = partialResult.partial
}
}
}
//
// WARNING
// Emit partial result events
// 1. AcceptWaveform returns true when silence is detected and you can retrieve the result with Result().
// 2. If silence is not detected you can retrieve PartialResult() only.
// 3. FinalResult means the stream is ended, you flush the buffers and retrieve remaining result.
// By Nicolay Shmirev. See: https://github.com/alphacep/vosk-api/issues/590#issuecomment-863065813
//
event.emit(FINAL_RESULT_EVENT, recognizer.finalResult(recognizer))
recognizer.free()
})
return event
}
/**
* @function transcriptFromBuffer
* speech recognition into a text, from an audio file, given a specified Vosk model
*
* @alias transcript
* @public
* @async
*
* @param {Buffer} buffer input buffer, in PCM format
* @param {ModelObject} model the Vosk model returned by InitModel()
* @param {VoskRecognizerArgsObject} [options] Vosk Recognizer arguments setting. Optional.
*
* @return {Promise<VoskResultObject>} transcript object returned by Vosk engine
*
*/
async function transcriptFromBuffer(buffer, model, { multiThreads=true, sampleRate=SAMPLE_RATE, grammar=null, alternatives=0, words=true } = {}) {
const recognizer = createRecognizer( model, {sampleRate, grammar, alternatives, words} )
// https://gist.github.com/wpscholar/270005d42b860b1c33cf5ab25b37928a
// https://stackoverflow.com/questions/47089230/how-to-convert-buffer-to-stream-in-nodejs
//
// WARNING
// From vosk version 0.3.25
// the acceptWaveformAsync function runs in a dedicated thread.
// That wold improve performances in case of cocurrent requests
// from the caller (server) program
//
// Previous vosk version 0.3.25
// const end_of_speech = recognizer.acceptWaveform(data)
//
if ( multiThreads )
await recognizer.acceptWaveformAsync(buffer)
else
recognizer.acceptWaveform(buffer)
// copy final Vosk engine result object
const result = {...recognizer.finalResult(recognizer)}
recognizer.free()
return Promise.resolve(result)
}
/**
* @function transcriptEventsFromBuffer
* speech recognition into a text, from an audio file, given a specified Vosk model
*
* @alias transcript
* @public
* @async
*
* @param {Buffer} buffer input buffer, in PCM format
* @param {ModelObject} model the Vosk model returned by InitModel()
* @param {VoskRecognizerArgsObject} [options] Vosk Recognizer arguments setting. Optional.
*
* @return {Emitter}
*
*/
async function transcriptEventsFromBuffer(buffer, model, { multiThreads=true, sampleRate=SAMPLE_RATE, grammar=null, alternatives=0, words=true } = {}) {
// the function is enabled to emit events
const event = new emitter()
const recognizer = createRecognizer( model, {sampleRate, grammar, alternatives, words} )
// https://gist.github.com/wpscholar/270005d42b860b1c33cf5ab25b37928a
// https://stackoverflow.com/questions/47089230/how-to-convert-buffer-to-stream-in-nodejs
const lastPartialResult = {}
for(;;) {
//
// WARNING
// From vosk version 0.3.25
// the acceptWaveformAsync function runs in a dedicated thread.
// That wold improve performances in case of cocurrent requests
// from the caller (server) program
//
// Previous vosk version 0.3.25
// const end_of_speech = recognizer.acceptWaveform(data)
//
const end_of_speech = multiThreads ?
await recognizer.acceptWaveformAsync(buffer) :
recognizer.acceptWaveform(buffer)
//
// WARNING
// Emit partial result events
// 1. AcceptWaveform returns true when silence is detected and you can retrieve the result with Result().
// 2. If silence is not detected you can retrieve PartialResult() only.
// 3. FinalResult means the stream is ended, you flush the buffers and retrieve remaining result.
// By Nicolay Shmirev. See: https://github.com/alphacep/vosk-api/issues/590#issuecomment-863065813
//
if (end_of_speech) {
event.emit(END_OF_SPEECH_EVENT, recognizer.result())
continue
}
else {
const partialResult = recognizer.partialResult()
// Doesn't emit duplicated events:
// Is the current partialResult different form the last one?
// If true a new event is emiitted.
// This avoid "duplicated" events to be emitted
if (partialResult.partial !== lastPartialResult.partial) {
event.emit(PARTIAL_RESULT_EVENT, partialResult)
lastPartialResult.partial = partialResult.partial
}
break
}
}
//
// WARNING
// if end_Of_Speech is detected (the buffere contains a sentence followed by a silence)
// and the result() function is called, so
// the finalResult() contains just the remaining (last) part of the sentence before the end of the audio.
// It's up to user to collect events data assempling the final textual (multisentence) result.
//
event.emit(FINAL_RESULT_EVENT, recognizer.finalResult())
recognizer.free()
return event
}
/**
* @function freeModel
* @public
*
* @param {ModelObject} model
*
*/
function freeModel(model) {
model.free()
}
/**
* test section
*/
function helpAndExit() {
console.log('voskjs is a CLI utility to test Vosk-api features')
console.log (info())
console.log()
console.log('Usage')
console.log()
console.log(' voskjs \\ ')
console.log(' --model=<model directory> \\ ')
console.log(' --audio=<audio file name> \\ ')
console.log(' [--grammar=<list of comma-separated words or sentences>] \\ ')
console.log(' [--samplerate=<Number, usually 16000 or 8000>] \\ ')
console.log(' [--alternatives=<number of max alternatives in text result>] \\ ')
console.log(' [--textonly] \\ ')
console.log(' [--tableevents] \\ ')
console.log(' [--objectevents] \\ ')
console.log(' [--debug=<Vosk debug level>] ')
console.log()
console.log('Examples')
console.log()
console.log(' 1. Recognize a speech file using a specific model directory:')
console.log()
console.log(' voskjs --audio=audio/2830-3980-0043.wav --model=models/vosk-model-en-us-aspire-0.2')
console.log()
console.log(' 2. Recognize a speech file setting a grammar (with a dynamic graph model) and a number of alternative:')
console.log()
console.log(' voskjs \\ ')
console.log(' --audio=audio/2830-3980-0043.wav \\ ')
console.log(' --model=models/vosk-model-small-en-us-0.15 \\ ')
console.log(' --grammar="experience proves this, bla bla bla"')
console.log(' --alternatives=3')
console.log()
process.exit(1)
}
/**
* @function checkArgs
* command line parsing
*
* @param {String} args
*
* @typedef {Object} SentenceAndAttributes
* @property {String} language
* @returns {SentenceAndAttributes}
*
*/
function checkArgs(args) {
// mandatory arguments
const modelDirectory = args.model
const audioFile = args.audio
// optional arguments
const grammar = args.grammar
const sampleRate = args.samplerate
const alternatives = args.alternatives
const textOnly = args.textonly
const tableevents = args.tableevents
const objectevents = args.objectevents
// if not specified, set default Vosk debug level to -1 (silent mode)
const debug = args.debug ? args.debug : -1
if ( !modelDirectory )
helpAndExit()
if ( !audioFile )
helpAndExit()
return {
modelDirectory,
audioFile,
// if grammar args is present, as comma separated sentences,
// convert it in an array of strings
grammar: grammar ? grammar.split(',').map(sentence => sentence.trim()) : undefined,
// convert to Number
sampleRate: sampleRate ? +sampleRate : undefined,
alternatives,
textOnly,
tableevents,
objectevents,
debug
}
}
function printObject(object, args) {
const defaultArgs = {showHidden:false, breakLength:Infinity, depth:null, colors:true}
return util.inspect(object, {...defaultArgs, ...args } )
}
function printResultsAsTable(results) {
console.log ('Events table:')
console.log ()
console.log('| %s | %s | %s |',
'time'.padEnd(6),
'event'.padEnd(12),
'text'.padEnd(40)
)
console.log('| %s | %s | %s |',
'-'.repeat(6),
'-'.repeat(12),
'-'.repeat(40)
)
for (const result of results) {
if ( result.event === PARTIAL_RESULT_EVENT )
console.log('| %s | %s | %s',
result.time.toString().padStart(6),
result.event.padEnd(12),
result.data.partial //.padStart(11+6+2+1)
)
if ( result.event === END_OF_SPEECH_EVENT )
console.log('| %s | %s | %s',
result.time.toString().padStart(6),
result.event.padEnd(12),
result.data.text //.padStart(11+6+2+1)
)
if ( result.event === FINAL_RESULT_EVENT )
console.log('| %s | %s | %s',
result.time.toString().padStart(6),
result.event.padEnd(12),
result.data.text //.padStart(11+6+2+1)
)
}
console.log()
}
function statistics(modelDirectory, audioFile, grammar, sampleRate, alternatives, textOnly, tableevents, objectevents, debug, sentences) {
console.log('voskjs is a CLI utility to test Vosk-api features')
console.log (info())
console.log()
console.log('Statistics:')
console.log()
console.log(`model directory : ${modelDirectory}`)
console.log(`speech file name : ${audioFile}`)
console.log(`grammar : ${grammar ? grammar : 'not specified. Default: NO'}`)
console.log(`sample rate : ${sampleRate ? sampleRate : 'not specified. Default: 16000'}`)
console.log(`max alternatives : ${alternatives}`)
console.log(`text only / JSON : ${textOnly ? 'text' : 'JSON'}`)
console.log(`Vosk debug level : ${debug}`)
console.log()
console.log(`load model latency : ${getTimer('loadModel')}ms`)
console.log(`transcript latency : ${getTimer('transcript')}ms`)
console.log(`transcript text : ${sentences.join(' ')}`)
console.log()
}
/**
* @function main
* unit test
*/
async function main() {
// get command line arguments
const { args } = getArgs()
const { modelDirectory, audioFile, grammar, sampleRate, alternatives, textOnly, tableevents, objectevents, debug } = checkArgs(args)
const words = ! textOnly
// set the vosk log level to silence
logLevel(debug)
setTimer('loadModel')
// load in memory a Vosk directory model
const model = loadModel(modelDirectory)
setTimer('transcript')
const startSentenceTimer = unixTimeMsecs()
let transcriptEvents
const sentences = []
const results = []
// speech recognition from an audio file
try {
transcriptEvents = transcriptEventsFromFile(audioFile, model, {grammar, sampleRate, alternatives, words})
}
catch(error) {
console.error(error)
}
transcriptEvents.on(PARTIAL_RESULT_EVENT, data => {
if ( !textOnly ) {
const dataItem = {
time: unixTimeMsecs() - startSentenceTimer,
event: PARTIAL_RESULT_EVENT,
data
}
results.push(dataItem)
}
})
transcriptEvents.on(END_OF_SPEECH_EVENT, data => {
sentences.push(data.text)
if ( ! textOnly ) {
const dataItem = {
time: unixTimeMsecs() - startSentenceTimer,
event: END_OF_SPEECH_EVENT,
data
}
results.push(dataItem)
}
})
transcriptEvents.on(FINAL_RESULT_EVENT, data => {
sentences.push(data.text)
if ( textOnly ) {
console.log(sentences.join(' '))
}
else {
const dataItem = {
time: unixTimeMsecs() - startSentenceTimer,
event: FINAL_RESULT_EVENT,
data
}
results.push(dataItem)
if ( !textOnly )
statistics(modelDirectory, audioFile, grammar, sampleRate, alternatives, textOnly, tableevents, objectevents, debug, sentences)
if ( !textOnly && tableevents)
printResultsAsTable(results)
if ( !textOnly && objectevents)
console.log(printObject(results, {breakLength:80}))
}
})
// free the runtime model
freeModel(model)
}
if (require.main === module)
main()
module.exports = {
logLevel,
loadModel,
transcriptFromBuffer,
transcriptEventsFromBuffer,
transcriptFromFile,
transcriptEventsFromFile,
//transcript: transcriptFromFile, // alias
freeModel
}