Skip to content

Commit

Permalink
Reduce vad-sense-voice example code. (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
whyb authored Nov 5, 2024
1 parent 4eeb336 commit 86b1856
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions c-api-examples/vad-sense-voice-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 86b1856

Please sign in to comment.