|
| 1 | +// Copyright (c) 2024 Xiaomi Corporation |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:typed_data'; |
| 4 | + |
| 5 | +import 'package:args/args.dart'; |
| 6 | +import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx; |
| 7 | + |
| 8 | +import './init.dart'; |
| 9 | + |
| 10 | +void main(List<String> arguments) async { |
| 11 | + await initSherpaOnnx(); |
| 12 | + |
| 13 | + final parser = ArgParser() |
| 14 | + ..addOption('silero-vad', help: 'Path to silero_vad.onnx') |
| 15 | + ..addOption('preprocessor', |
| 16 | + help: 'Path to the moonshine preprocessor model') |
| 17 | + ..addOption('encoder', help: 'Path to the moonshine encoder model') |
| 18 | + ..addOption('uncached-decoder', |
| 19 | + help: 'Path to moonshine uncached decoder model') |
| 20 | + ..addOption('cached-decoder', |
| 21 | + help: 'Path to moonshine cached decoder model') |
| 22 | + ..addOption('tokens', help: 'Path to tokens.txt') |
| 23 | + ..addOption('input-wav', help: 'Path to input.wav to transcribe'); |
| 24 | + |
| 25 | + final res = parser.parse(arguments); |
| 26 | + if (res['silero-vad'] == null || |
| 27 | + res['preprocessor'] == null || |
| 28 | + res['encoder'] == null || |
| 29 | + res['uncached-decoder'] == null || |
| 30 | + res['cached-decoder'] == null || |
| 31 | + res['tokens'] == null || |
| 32 | + res['input-wav'] == null) { |
| 33 | + print(parser.usage); |
| 34 | + exit(1); |
| 35 | + } |
| 36 | + |
| 37 | + // create VAD |
| 38 | + final sileroVad = res['silero-vad'] as String; |
| 39 | + |
| 40 | + final sileroVadConfig = sherpa_onnx.SileroVadModelConfig( |
| 41 | + model: sileroVad, |
| 42 | + minSilenceDuration: 0.25, |
| 43 | + minSpeechDuration: 0.5, |
| 44 | + maxSpeechDuration: 5.0, |
| 45 | + ); |
| 46 | + |
| 47 | + final vadConfig = sherpa_onnx.VadModelConfig( |
| 48 | + sileroVad: sileroVadConfig, |
| 49 | + numThreads: 1, |
| 50 | + debug: true, |
| 51 | + ); |
| 52 | + |
| 53 | + final vad = sherpa_onnx.VoiceActivityDetector( |
| 54 | + config: vadConfig, bufferSizeInSeconds: 10); |
| 55 | + |
| 56 | + // create whisper recognizer |
| 57 | + final preprocessor = res['preprocessor'] as String; |
| 58 | + final encoder = res['encoder'] as String; |
| 59 | + final uncachedDecoder = res['uncached-decoder'] as String; |
| 60 | + final cachedDecoder = res['cached-decoder'] as String; |
| 61 | + final tokens = res['tokens'] as String; |
| 62 | + final inputWav = res['input-wav'] as String; |
| 63 | + |
| 64 | + final moonshine = sherpa_onnx.OfflineMoonshineModelConfig( |
| 65 | + preprocessor: preprocessor, |
| 66 | + encoder: encoder, |
| 67 | + uncachedDecoder: uncachedDecoder, |
| 68 | + cachedDecoder: cachedDecoder, |
| 69 | + ); |
| 70 | + final modelConfig = sherpa_onnx.OfflineModelConfig( |
| 71 | + moonshine: moonshine, |
| 72 | + tokens: tokens, |
| 73 | + debug: false, |
| 74 | + numThreads: 1, |
| 75 | + ); |
| 76 | + final config = sherpa_onnx.OfflineRecognizerConfig(model: modelConfig); |
| 77 | + final recognizer = sherpa_onnx.OfflineRecognizer(config); |
| 78 | + |
| 79 | + final waveData = sherpa_onnx.readWave(inputWav); |
| 80 | + if (waveData.sampleRate != 16000) { |
| 81 | + print('Only 16000 Hz is supported. Given: ${waveData.sampleRate}'); |
| 82 | + exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + int numSamples = waveData.samples.length; |
| 86 | + int numIter = numSamples ~/ vadConfig.sileroVad.windowSize; |
| 87 | + |
| 88 | + for (int i = 0; i != numIter; ++i) { |
| 89 | + int start = i * vadConfig.sileroVad.windowSize; |
| 90 | + vad.acceptWaveform(Float32List.sublistView( |
| 91 | + waveData.samples, start, start + vadConfig.sileroVad.windowSize)); |
| 92 | + |
| 93 | + while (!vad.isEmpty()) { |
| 94 | + final samples = vad.front().samples; |
| 95 | + final startTime = vad.front().start.toDouble() / waveData.sampleRate; |
| 96 | + final endTime = |
| 97 | + startTime + samples.length.toDouble() / waveData.sampleRate; |
| 98 | + |
| 99 | + final stream = recognizer.createStream(); |
| 100 | + stream.acceptWaveform(samples: samples, sampleRate: waveData.sampleRate); |
| 101 | + recognizer.decode(stream); |
| 102 | + |
| 103 | + final result = recognizer.getResult(stream); |
| 104 | + stream.free(); |
| 105 | + print( |
| 106 | + '${startTime.toStringAsPrecision(5)} -- ${endTime.toStringAsPrecision(5)} : ${result.text}'); |
| 107 | + |
| 108 | + vad.pop(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + vad.flush(); |
| 113 | + |
| 114 | + while (!vad.isEmpty()) { |
| 115 | + final samples = vad.front().samples; |
| 116 | + final startTime = vad.front().start.toDouble() / waveData.sampleRate; |
| 117 | + final endTime = startTime + samples.length.toDouble() / waveData.sampleRate; |
| 118 | + |
| 119 | + final stream = recognizer.createStream(); |
| 120 | + stream.acceptWaveform(samples: samples, sampleRate: waveData.sampleRate); |
| 121 | + recognizer.decode(stream); |
| 122 | + |
| 123 | + final result = recognizer.getResult(stream); |
| 124 | + stream.free(); |
| 125 | + print( |
| 126 | + '${startTime.toStringAsPrecision(5)} -- ${endTime.toStringAsPrecision(5)} : ${result.text}'); |
| 127 | + |
| 128 | + vad.pop(); |
| 129 | + } |
| 130 | + |
| 131 | + vad.free(); |
| 132 | + |
| 133 | + recognizer.free(); |
| 134 | +} |
0 commit comments