|
| 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('model', help: 'Path to the paraformer model') |
| 15 | + ..addOption('tokens', help: 'Path to tokens.txt') |
| 16 | + ..addOption('language', |
| 17 | + help: 'auto, zh, en, ja, ko, yue, or leave it empty to use auto', |
| 18 | + defaultsTo: '') |
| 19 | + ..addOption('use-itn', |
| 20 | + help: 'true to use inverse text normalization', defaultsTo: 'false') |
| 21 | + ..addOption('input-wav', help: 'Path to input.wav to transcribe'); |
| 22 | + |
| 23 | + final res = parser.parse(arguments); |
| 24 | + if (res['model'] == null || |
| 25 | + res['tokens'] == null || |
| 26 | + res['input-wav'] == null) { |
| 27 | + print(parser.usage); |
| 28 | + exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + final model = res['model'] as String; |
| 32 | + final tokens = res['tokens'] as String; |
| 33 | + final inputWav = res['input-wav'] as String; |
| 34 | + final language = res['language'] as String; |
| 35 | + final useItn = (res['use-itn'] as String).toLowerCase() == 'true'; |
| 36 | + |
| 37 | + final senseVoice = sherpa_onnx.OfflineSenseVoiceModelConfig( |
| 38 | + model: model, language: language, useInverseTextNormalization: useItn); |
| 39 | + |
| 40 | + final modelConfig = sherpa_onnx.OfflineModelConfig( |
| 41 | + senseVoice: senseVoice, |
| 42 | + tokens: tokens, |
| 43 | + debug: true, |
| 44 | + numThreads: 1, |
| 45 | + ); |
| 46 | + final config = sherpa_onnx.OfflineRecognizerConfig(model: modelConfig); |
| 47 | + final recognizer = sherpa_onnx.OfflineRecognizer(config); |
| 48 | + |
| 49 | + final waveData = sherpa_onnx.readWave(inputWav); |
| 50 | + final stream = recognizer.createStream(); |
| 51 | + |
| 52 | + stream.acceptWaveform( |
| 53 | + samples: waveData.samples, sampleRate: waveData.sampleRate); |
| 54 | + recognizer.decode(stream); |
| 55 | + |
| 56 | + final result = recognizer.getResult(stream); |
| 57 | + print(result.text); |
| 58 | + |
| 59 | + stream.free(); |
| 60 | + recognizer.free(); |
| 61 | +} |
0 commit comments