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