|
| 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('encoder', help: 'Path to the encoder model') |
| 15 | + ..addOption('decoder', help: 'Path to decoder model') |
| 16 | + ..addOption('tokens', help: 'Path to tokens.txt') |
| 17 | + ..addOption('input-wav', help: 'Path to input.wav to transcribe'); |
| 18 | + |
| 19 | + final res = parser.parse(arguments); |
| 20 | + if (res['encoder'] == null || |
| 21 | + res['decoder'] == null || |
| 22 | + res['tokens'] == null || |
| 23 | + res['input-wav'] == null) { |
| 24 | + print(parser.usage); |
| 25 | + exit(1); |
| 26 | + } |
| 27 | + |
| 28 | + final encoder = res['encoder'] as String; |
| 29 | + final decoder = res['decoder'] as String; |
| 30 | + final tokens = res['tokens'] as String; |
| 31 | + final inputWav = res['input-wav'] as String; |
| 32 | + |
| 33 | + final paraformer = sherpa_onnx.OnlineParaformerModelConfig( |
| 34 | + encoder: encoder, |
| 35 | + decoder: decoder, |
| 36 | + ); |
| 37 | + |
| 38 | + final modelConfig = sherpa_onnx.OnlineModelConfig( |
| 39 | + paraformer: paraformer, |
| 40 | + tokens: tokens, |
| 41 | + debug: true, |
| 42 | + numThreads: 1, |
| 43 | + ); |
| 44 | + final config = sherpa_onnx.OnlineRecognizerConfig(model: modelConfig); |
| 45 | + final recognizer = sherpa_onnx.OnlineRecognizer(config); |
| 46 | + |
| 47 | + final waveData = sherpa_onnx.readWave(inputWav); |
| 48 | + final stream = recognizer.createStream(); |
| 49 | + |
| 50 | + // simulate streaming. You can choose an arbitrary chunk size. |
| 51 | + // chunkSize of a single sample is also ok, i.e, chunkSize = 1 |
| 52 | + final chunkSize = 1600; // 0.1 second for 16kHz |
| 53 | + final numChunks = waveData.samples.length ~/ chunkSize; |
| 54 | + |
| 55 | + var last = ''; |
| 56 | + for (int i = 0; i != numChunks; ++i) { |
| 57 | + int start = i * chunkSize; |
| 58 | + stream.acceptWaveform( |
| 59 | + samples: |
| 60 | + Float32List.sublistView(waveData.samples, start, start + chunkSize), |
| 61 | + sampleRate: waveData.sampleRate, |
| 62 | + ); |
| 63 | + while (recognizer.isReady(stream)) { |
| 64 | + recognizer.decode(stream); |
| 65 | + } |
| 66 | + final result = recognizer.getResult(stream); |
| 67 | + if (result.text != last && result.text != '') { |
| 68 | + last = result.text; |
| 69 | + print(last); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // 0.5 seconds, assume sampleRate is 16kHz |
| 74 | + final tailPaddings = Float32List(8000); |
| 75 | + stream.acceptWaveform( |
| 76 | + samples: tailPaddings, |
| 77 | + sampleRate: waveData.sampleRate, |
| 78 | + ); |
| 79 | + |
| 80 | + while (recognizer.isReady(stream)) { |
| 81 | + recognizer.decode(stream); |
| 82 | + } |
| 83 | + |
| 84 | + final result = recognizer.getResult(stream); |
| 85 | + |
| 86 | + if (result.text != '') { |
| 87 | + print(result.text); |
| 88 | + } |
| 89 | + |
| 90 | + stream.free(); |
| 91 | + recognizer.free(); |
| 92 | +} |
0 commit comments