|
| 1 | +{ Copyright (c) 2025 Xiaomi Corporation } |
| 2 | + |
| 3 | +{ |
| 4 | +This file shows how to use a non-streaming Dolphin model |
| 5 | +with silero VAD to decode files. |
| 6 | +
|
| 7 | +You can download the model files from |
| 8 | +https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models |
| 9 | +} |
| 10 | + |
| 11 | +program vad_with_dolphin; |
| 12 | + |
| 13 | +{$mode objfpc} |
| 14 | + |
| 15 | +uses |
| 16 | + sherpa_onnx, |
| 17 | + SysUtils; |
| 18 | + |
| 19 | +function CreateVad(): TSherpaOnnxVoiceActivityDetector; |
| 20 | +var |
| 21 | + Config: TSherpaOnnxVadModelConfig; |
| 22 | + |
| 23 | + SampleRate: Integer; |
| 24 | + WindowSize: Integer; |
| 25 | +begin |
| 26 | + Initialize(Config); |
| 27 | + |
| 28 | + SampleRate := 16000; {Please don't change it unless you know the details} |
| 29 | + WindowSize := 512; {Please don't change it unless you know the details} |
| 30 | + |
| 31 | + Config.SileroVad.Model := './silero_vad.onnx'; |
| 32 | + Config.SileroVad.MinSpeechDuration := 0.5; |
| 33 | + Config.SileroVad.MinSilenceDuration := 0.5; |
| 34 | + Config.SileroVad.Threshold := 0.5; |
| 35 | + Config.SileroVad.WindowSize := WindowSize; |
| 36 | + Config.NumThreads:= 1; |
| 37 | + Config.Debug:= True; |
| 38 | + Config.Provider:= 'cpu'; |
| 39 | + Config.SampleRate := SampleRate; |
| 40 | + |
| 41 | + Result := TSherpaOnnxVoiceActivityDetector.Create(Config, 30); |
| 42 | +end; |
| 43 | + |
| 44 | +function CreateOfflineRecognizer(): TSherpaOnnxOfflineRecognizer; |
| 45 | +var |
| 46 | + Config: TSherpaOnnxOfflineRecognizerConfig; |
| 47 | +begin |
| 48 | + Initialize(Config); |
| 49 | + |
| 50 | + Config.ModelConfig.Dolphin.Model := './sherpa-onnx-dolphin-base-ctc-multi-lang-int8-2025-04-02/model.int8.onnx'; |
| 51 | + Config.ModelConfig.Tokens := './sherpa-onnx-dolphin-base-ctc-multi-lang-int8-2025-04-02/tokens.txt'; |
| 52 | + Config.ModelConfig.Provider := 'cpu'; |
| 53 | + Config.ModelConfig.NumThreads := 1; |
| 54 | + Config.ModelConfig.Debug := False; |
| 55 | + |
| 56 | + Result := TSherpaOnnxOfflineRecognizer.Create(Config); |
| 57 | +end; |
| 58 | + |
| 59 | +var |
| 60 | + Wave: TSherpaOnnxWave; |
| 61 | + |
| 62 | + Recognizer: TSherpaOnnxOfflineRecognizer; |
| 63 | + Vad: TSherpaOnnxVoiceActivityDetector; |
| 64 | + |
| 65 | + Offset: Integer; |
| 66 | + WindowSize: Integer; |
| 67 | + SpeechSegment: TSherpaOnnxSpeechSegment; |
| 68 | + |
| 69 | + Start: Single; |
| 70 | + Duration: Single; |
| 71 | + |
| 72 | + Stream: TSherpaOnnxOfflineStream; |
| 73 | + RecognitionResult: TSherpaOnnxOfflineRecognizerResult; |
| 74 | +begin |
| 75 | + Vad := CreateVad(); |
| 76 | + Recognizer := CreateOfflineRecognizer(); |
| 77 | + |
| 78 | + Wave := SherpaOnnxReadWave('./lei-jun-test.wav'); |
| 79 | + if Wave.SampleRate <> Vad.Config.SampleRate then |
| 80 | + begin |
| 81 | + WriteLn(Format('Expected sample rate: %d. Given: %d', |
| 82 | + [Vad.Config.SampleRate, Wave.SampleRate])); |
| 83 | + |
| 84 | + Exit; |
| 85 | + end; |
| 86 | + |
| 87 | + WindowSize := Vad.Config.SileroVad.WindowSize; |
| 88 | + Offset := 0; |
| 89 | + while Offset + WindowSize <= Length(Wave.Samples) do |
| 90 | + begin |
| 91 | + Vad.AcceptWaveform(Wave.Samples, Offset, WindowSize); |
| 92 | + Offset += WindowSize; |
| 93 | + |
| 94 | + while not Vad.IsEmpty do |
| 95 | + begin |
| 96 | + SpeechSegment := Vad.Front(); |
| 97 | + Vad.Pop(); |
| 98 | + Stream := Recognizer.CreateStream(); |
| 99 | + |
| 100 | + Stream.AcceptWaveform(SpeechSegment.Samples, Wave.SampleRate); |
| 101 | + Recognizer.Decode(Stream); |
| 102 | + RecognitionResult := Recognizer.GetResult(Stream); |
| 103 | + |
| 104 | + Start := SpeechSegment.Start / Wave.SampleRate; |
| 105 | + Duration := Length(SpeechSegment.Samples) / Wave.SampleRate; |
| 106 | + WriteLn(Format('%.3f -- %.3f %s', |
| 107 | + [Start, Start + Duration, RecognitionResult.Text])); |
| 108 | + |
| 109 | + FreeAndNil(Stream); |
| 110 | + end; |
| 111 | + end; |
| 112 | + |
| 113 | + Vad.Flush; |
| 114 | + |
| 115 | + while not Vad.IsEmpty do |
| 116 | + begin |
| 117 | + SpeechSegment := Vad.Front(); |
| 118 | + Vad.Pop(); |
| 119 | + Stream := Recognizer.CreateStream(); |
| 120 | + |
| 121 | + Stream.AcceptWaveform(SpeechSegment.Samples, Wave.SampleRate); |
| 122 | + Recognizer.Decode(Stream); |
| 123 | + RecognitionResult := Recognizer.GetResult(Stream); |
| 124 | + |
| 125 | + Start := SpeechSegment.Start / Wave.SampleRate; |
| 126 | + Duration := Length(SpeechSegment.Samples) / Wave.SampleRate; |
| 127 | + WriteLn(Format('%.3f -- %.3f %s', |
| 128 | + [Start, Start + Duration, RecognitionResult.Text])); |
| 129 | + |
| 130 | + FreeAndNil(Stream); |
| 131 | + end; |
| 132 | + |
| 133 | + FreeAndNil(Recognizer); |
| 134 | + FreeAndNil(Vad); |
| 135 | +end. |
0 commit comments