From 86b1856c207761d999228c2f14f013908b5047c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=B5=E5=B0=8F=E5=87=A1?= <2672931+whyb@users.noreply.github.com> Date: Tue, 5 Nov 2024 20:34:12 +0800 Subject: [PATCH] Reduce vad-sense-voice example code. (#1510) --- c-api-examples/vad-sense-voice-c-api.c | 46 ++++++-------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/c-api-examples/vad-sense-voice-c-api.c b/c-api-examples/vad-sense-voice-c-api.c index ee9504d1a..eeddfce88 100644 --- a/c-api-examples/vad-sense-voice-c-api.c +++ b/c-api-examples/vad-sense-voice-c-api.c @@ -99,11 +99,16 @@ int32_t main() { int32_t window_size = vadConfig.silero_vad.window_size; int32_t i = 0; - - while (i + window_size < wave->num_samples) { - SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad, wave->samples + i, - window_size); - i += window_size; + int is_eof = 0; + + while (!is_eof) { + if (i + window_size < wave->num_samples) { + SherpaOnnxVoiceActivityDetectorAcceptWaveform(vad, wave->samples + i, + window_size); + } else { + SherpaOnnxVoiceActivityDetectorFlush(vad); + is_eof = 1; + } while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) { const SherpaOnnxSpeechSegment *segment = @@ -132,36 +137,7 @@ int32_t main() { SherpaOnnxDestroySpeechSegment(segment); SherpaOnnxVoiceActivityDetectorPop(vad); } - } - - SherpaOnnxVoiceActivityDetectorFlush(vad); - - while (!SherpaOnnxVoiceActivityDetectorEmpty(vad)) { - const SherpaOnnxSpeechSegment *segment = - SherpaOnnxVoiceActivityDetectorFront(vad); - - const SherpaOnnxOfflineStream *stream = - SherpaOnnxCreateOfflineStream(recognizer); - - SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, segment->samples, - segment->n); - - SherpaOnnxDecodeOfflineStream(recognizer, stream); - - const SherpaOnnxOfflineRecognizerResult *result = - SherpaOnnxGetOfflineStreamResult(stream); - - float start = segment->start / 16000.0f; - float duration = segment->n / 16000.0f; - float stop = start + duration; - - fprintf(stderr, "%.3f -- %.3f: %s\n", start, stop, result->text); - - SherpaOnnxDestroyOfflineRecognizerResult(result); - SherpaOnnxDestroyOfflineStream(stream); - - SherpaOnnxDestroySpeechSegment(segment); - SherpaOnnxVoiceActivityDetectorPop(vad); + i += window_size; } SherpaOnnxDestroyOfflineRecognizer(recognizer);