張小凡
Committed by GitHub

Reduce vad-whisper-c-api example code. (#1891)

@@ -96,12 +96,17 @@ int32_t main() { @@ -96,12 +96,17 @@ int32_t main() {
96 96
97 int32_t window_size = vadConfig.silero_vad.window_size; 97 int32_t window_size = vadConfig.silero_vad.window_size;
98 int32_t i = 0; 98 int32_t i = 0;
  99 + int is_eof = 0;
99 100
100 - while (i + window_size < wave->num_samples) {  
101 - SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad, wave->samples + i,  
102 - window_size);  
103 - i += window_size;  
104 - 101 + while (!is_eof) {
  102 + if (i + window_size < wave->num_samples) {
  103 + SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad, wave->samples + i,
  104 + window_size);
  105 + }
  106 + else {
  107 + SherpaOnnxVoiceActivityDetectorFlush(vad);
  108 + is_eof = 1;
  109 + }
105 while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) { 110 while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) {
106 const SherpaOnnxSpeechSegment *segment = 111 const SherpaOnnxSpeechSegment *segment =
107 SherpaOnnxVoiceActivityDetectorFront(vad); 112 SherpaOnnxVoiceActivityDetectorFront(vad);
@@ -129,36 +134,7 @@ int32_t main() { @@ -129,36 +134,7 @@ int32_t main() {
129 SherpaOnnxDestroySpeechSegment(segment); 134 SherpaOnnxDestroySpeechSegment(segment);
130 SherpaOnnxVoiceActivityDetectorPop(vad); 135 SherpaOnnxVoiceActivityDetectorPop(vad);
131 } 136 }
132 - }  
133 -  
134 - SherpaOnnxVoiceActivityDetectorFlush(vad);  
135 -  
136 - while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) {  
137 - const SherpaOnnxSpeechSegment *segment =  
138 - SherpaOnnxVoiceActivityDetectorFront(vad);  
139 -  
140 - const SherpaOnnxOfflineStream *stream =  
141 - SherpaOnnxCreateOfflineStream(recognizer);  
142 -  
143 - SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, segment->samples,  
144 - segment->n);  
145 -  
146 - SherpaOnnxDecodeOfflineStream(recognizer, stream);  
147 -  
148 - const SherpaOnnxOfflineRecognizerResult *result =  
149 - SherpaOnnxGetOfflineStreamResult(stream);  
150 -  
151 - float start = segment->start / 16000.0f;  
152 - float duration = segment->n / 16000.0f;  
153 - float stop = start + duration;  
154 -  
155 - fprintf(stderr, "%.3f -- %.3f: %s\n", start, stop, result->text);  
156 -  
157 - SherpaOnnxDestroyOfflineRecognizerResult(result);  
158 - SherpaOnnxDestroyOfflineStream(stream);  
159 -  
160 - SherpaOnnxDestroySpeechSegment(segment);  
161 - SherpaOnnxVoiceActivityDetectorPop(vad); 137 + i += window_size;
162 } 138 }
163 139
164 SherpaOnnxDestroyOfflineRecognizer(recognizer); 140 SherpaOnnxDestroyOfflineRecognizer(recognizer);