Skip to content

Commit

Permalink
Reduce vad-moonshine-c-api example code. (#1742)
Browse files Browse the repository at this point in the history
  • Loading branch information
whyb authored Jan 21, 2025
1 parent e764fa6 commit 5bcd7e1
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions c-api-examples/vad-moonshine-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,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 =
SherpaOnnxVoiceActivityDetectorFront(vad);
Expand Down Expand Up @@ -131,36 +135,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 5bcd7e1

Please sign in to comment.