// ignore_for_file: avoid_print import 'dart:io'; import 'package:flutter_test/flutter_test.dart'; import 'package:kouyu_english/core/sherpa_stt_service.dart'; import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx; void main() { TestWidgetsFlutterBinding.ensureInitialized(); final home = Platform.environment['HOME'] ?? ''; final pubCache = Platform.environment['PUB_CACHE'] ?? '$home/.pub-cache'; final candidateDirs = [ '$pubCache/hosted/pub.dev/sherpa_onnx_macos-1.13.8/macos', '$home/.pub-cache/hosted/pub.dev/sherpa_onnx_macos-1.13.8/macos', '/Users/shenlei/.pub-cache/hosted/pub.dev/sherpa_onnx_macos-1.13.8/macos', ]; final macosDir = candidateDirs.firstWhere( (d) => Directory(d).existsSync(), orElse: () => candidateDirs.first, ); test('SenseVoice-Small ONNX transcribes WAV audio file accurately', () async { // Test direct Sherpa ASR initialization and decoding with SenseVoice sherpa_onnx.initBindings(macosDir); final modelConfig = sherpa_onnx.OfflineModelConfig( senseVoice: const sherpa_onnx.OfflineSenseVoiceModelConfig( model: 'assets/models/sense_voice/model.int8.onnx', language: 'en', useInverseTextNormalization: true, ), tokens: 'assets/models/sense_voice/tokens.txt', numThreads: 2, debug: false, ); final recognizerConfig = sherpa_onnx.OfflineRecognizerConfig( model: modelConfig, feat: const sherpa_onnx.FeatureConfig(sampleRate: 16000, featureDim: 80), ); final recognizer = sherpa_onnx.OfflineRecognizer(recognizerConfig); const testWave = '/tmp/sherpa_test/sherpa-onnx-zipformer-small-en-2023-06-26/test_wavs/0.wav'; if (File(testWave).existsSync()) { final wave = sherpa_onnx.readWave(testWave); expect(wave.samples.isNotEmpty, isTrue); final stream = recognizer.createStream(); stream.acceptWaveform(samples: wave.samples, sampleRate: wave.sampleRate); recognizer.decode(stream); final result = recognizer.getResult(stream); print('SenseVoice transcribed result: ${result.text}'); expect(result.text.toLowerCase().contains('nightfall'), isTrue); stream.free(); } recognizer.free(); }); test('SherpaSttService singleton initializes and transcribes cleanly', () async { final ready = await SherpaSttService.instance.initialize(nativeLibDir: macosDir); expect(ready, isTrue); expect(SherpaSttService.instance.isReady, isTrue); const testWave = '/tmp/sherpa_test/sherpa-onnx-zipformer-small-en-2023-06-26/test_wavs/0.wav'; if (File(testWave).existsSync()) { final transcribed = await SherpaSttService.instance.transcribeWav(testWave); expect(transcribed, isNotNull); print('SherpaSttService transcribed: $transcribed'); expect(transcribed!.toLowerCase().contains('nightfall'), isTrue); // SenseVoice tags should be stripped expect(transcribed.contains('<|'), isFalse); } }); }