|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const VAD = require("node-vad"); |
| 4 | +const Ds = require('deepspeech'); |
| 5 | +const argparse = require('argparse'); |
| 6 | +const util = require('util'); |
| 7 | + |
| 8 | +// These constants control the beam search decoder |
| 9 | + |
| 10 | +// Beam width used in the CTC decoder when building candidate transcriptions |
| 11 | +const BEAM_WIDTH = 1024; |
| 12 | + |
| 13 | +// The alpha hyperparameter of the CTC decoder. Language Model weight |
| 14 | +const LM_WEIGHT = 1.50; |
| 15 | + |
| 16 | +// Valid word insertion weight. This is used to lessen the word insertion penalty |
| 17 | +// when the inserted word is part of the vocabulary |
| 18 | +const VALID_WORD_COUNT_WEIGHT = 2.25; |
| 19 | + |
| 20 | +// These constants are tied to the shape of the graph used (changing them changes |
| 21 | +// the geometry of the first layer), so make sure you use the same constants that |
| 22 | +// were used during training |
| 23 | + |
| 24 | +// Number of MFCC features to use |
| 25 | +const N_FEATURES = 26; |
| 26 | + |
| 27 | +// Size of the context window used for producing timesteps in the input vector |
| 28 | +const N_CONTEXT = 9; |
| 29 | + |
| 30 | +let VersionAction = function VersionAction(options) { |
| 31 | + options = options || {}; |
| 32 | + options.nargs = 0; |
| 33 | + argparse.Action.call(this, options); |
| 34 | +}; |
| 35 | + |
| 36 | +util.inherits(VersionAction, argparse.Action); |
| 37 | + |
| 38 | +VersionAction.prototype.call = function(parser) { |
| 39 | + Ds.printVersions(); |
| 40 | + process.exit(0); |
| 41 | +}; |
| 42 | + |
| 43 | +let parser = new argparse.ArgumentParser({addHelp: true, description: 'Running DeepSpeech inference.'}); |
| 44 | +parser.addArgument(['--model'], {required: true, help: 'Path to the model (protocol buffer binary file)'}); |
| 45 | +parser.addArgument(['--alphabet'], {required: true, help: 'Path to the configuration file specifying the alphabet used by the network'}); |
| 46 | +parser.addArgument(['--lm'], {help: 'Path to the language model binary file', nargs: '?'}); |
| 47 | +parser.addArgument(['--trie'], {help: 'Path to the language model trie file created with native_client/generate_trie', nargs: '?'}); |
| 48 | +parser.addArgument(['--audio'], {required: true, help: 'Path to the audio file to run (WAV format)'}); |
| 49 | +parser.addArgument(['--version'], {action: VersionAction, help: 'Print version and exits'}); |
| 50 | +let args = parser.parseArgs(); |
| 51 | + |
| 52 | +function totalTime(hrtimeValue) { |
| 53 | + return (hrtimeValue[0] + hrtimeValue[1] / 1000000000).toPrecision(4); |
| 54 | +} |
| 55 | + |
| 56 | +console.error('Loading model from file %s', args['model']); |
| 57 | +const model_load_start = process.hrtime(); |
| 58 | +let model = new Ds.Model(args['model'], N_FEATURES, N_CONTEXT, args['alphabet'], BEAM_WIDTH); |
| 59 | +const model_load_end = process.hrtime(model_load_start); |
| 60 | +console.error('Loaded model in %ds.', totalTime(model_load_end)); |
| 61 | + |
| 62 | +if (args['lm'] && args['trie']) { |
| 63 | + console.error('Loading language model from files %s %s', args['lm'], args['trie']); |
| 64 | + const lm_load_start = process.hrtime(); |
| 65 | + model.enableDecoderWithLM(args['alphabet'], args['lm'], args['trie'], |
| 66 | + LM_WEIGHT, VALID_WORD_COUNT_WEIGHT); |
| 67 | + const lm_load_end = process.hrtime(lm_load_start); |
| 68 | + console.error('Loaded language model in %ds.', totalTime(lm_load_end)); |
| 69 | +} |
| 70 | + |
| 71 | +const vad = new VAD(VAD.Mode.NORMAL); |
| 72 | +const voice = {START: true, STOP: false}; |
| 73 | +let sctx = model.setupStream(150, 16000); |
| 74 | +let state = voice.STOP; |
| 75 | + |
| 76 | +function finishStream() { |
| 77 | + const model_load_start = process.hrtime(); |
| 78 | + console.error('Running inference.'); |
| 79 | + console.log('Transcription: ', model.finishStream(sctx)); |
| 80 | + const model_load_end = process.hrtime(model_load_start); |
| 81 | + console.error('Inference took %ds.', totalTime(model_load_end)); |
| 82 | +} |
| 83 | + |
| 84 | +let ffmpeg = require('child_process').spawn('ffmpeg', [ |
| 85 | + '-hide_banner', |
| 86 | + '-nostats', |
| 87 | + '-loglevel', 'fatal', |
| 88 | + '-i', args['audio'], |
| 89 | + '-af', 'highpass=f=200,lowpass=f=3000', |
| 90 | + '-vn', |
| 91 | + '-acodec', 'pcm_s16le', |
| 92 | + '-ac', 1, |
| 93 | + '-ar', 16000, |
| 94 | + '-f', 's16le', |
| 95 | + 'pipe:' |
| 96 | +]); |
| 97 | + |
| 98 | +ffmpeg.stdout.on('data', chunk => { |
| 99 | + vad.processAudio(chunk, 16000).then(res => { |
| 100 | + switch (res) { |
| 101 | + case VAD.Event.SILENCE: |
| 102 | + if (state === voice.START) { |
| 103 | + state = voice.STOP; |
| 104 | + finishStream(); |
| 105 | + sctx = model.setupStream(150,16000); |
| 106 | + } |
| 107 | + break; |
| 108 | + case VAD.Event.VOICE: |
| 109 | + state = voice.START; |
| 110 | + model.feedAudioContent(sctx, chunk.slice(0, chunk.length / 2)); |
| 111 | + break; |
| 112 | + } |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +ffmpeg.stdout.on('close', code => { |
| 117 | + finishStream(); |
| 118 | +}); |
0 commit comments