|
| 1 | +// c-api-examples/kws-c-api.c |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Xiaomi Corporation |
| 4 | +// |
| 5 | +// This file demonstrates how to use keywords spotter with sherpa-onnx's C |
| 6 | +// clang-format off |
| 7 | +// |
| 8 | +// Usage |
| 9 | +// |
| 10 | +// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/kws-models/sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile.tar.bz2 |
| 11 | +// tar xvf sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile.tar.bz2 |
| 12 | +// rm sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01-mobile.tar.bz2 |
| 13 | +// |
| 14 | +// ./kws-c-api |
| 15 | +// |
| 16 | +// clang-format on |
| 17 | +#include <stdio.h> |
| 18 | +#include <stdlib.h> // exit |
| 19 | +#include <string.h> // memset |
| 20 | + |
| 21 | +#include "sherpa-onnx/c-api/c-api.h" |
| 22 | + |
| 23 | +int32_t main() { |
| 24 | + SherpaOnnxKeywordSpotterConfig config; |
| 25 | + |
| 26 | + memset(&config, 0, sizeof(config)); |
| 27 | + config.model_config.transducer.encoder = |
| 28 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/" |
| 29 | + "encoder-epoch-12-avg-2-chunk-16-left-64.onnx"; |
| 30 | + |
| 31 | + config.model_config.transducer.decoder = |
| 32 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/" |
| 33 | + "decoder-epoch-12-avg-2-chunk-16-left-64.onnx"; |
| 34 | + |
| 35 | + config.model_config.transducer.joiner = |
| 36 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/" |
| 37 | + "joiner-epoch-12-avg-2-chunk-16-left-64.onnx"; |
| 38 | + |
| 39 | + config.model_config.tokens = |
| 40 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/tokens.txt"; |
| 41 | + |
| 42 | + config.model_config.provider = "cpu"; |
| 43 | + config.model_config.num_threads = 1; |
| 44 | + config.model_config.debug = 1; |
| 45 | + |
| 46 | + config.keywords_file = |
| 47 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/" |
| 48 | + "test_keywords.txt"; |
| 49 | + |
| 50 | + const SherpaOnnxKeywordSpotter *kws = SherpaOnnxCreateKeywordSpotter(&config); |
| 51 | + if (!kws) { |
| 52 | + fprintf(stderr, "Please check your config"); |
| 53 | + exit(-1); |
| 54 | + } |
| 55 | + |
| 56 | + fprintf(stderr, |
| 57 | + "--Test pre-defined keywords from test_wavs/test_keywords.txt--\n"); |
| 58 | + |
| 59 | + const char *wav_filename = |
| 60 | + "./sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01/test_wavs/3.wav"; |
| 61 | + |
| 62 | + float tail_paddings[8000] = {0}; // 0.5 seconds |
| 63 | + |
| 64 | + const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename); |
| 65 | + if (wave == NULL) { |
| 66 | + fprintf(stderr, "Failed to read %s\n", wav_filename); |
| 67 | + exit(-1); |
| 68 | + } |
| 69 | + |
| 70 | + const SherpaOnnxOnlineStream *stream = SherpaOnnxCreateKeywordStream(kws); |
| 71 | + if (!stream) { |
| 72 | + fprintf(stderr, "Failed to create stream\n"); |
| 73 | + exit(-1); |
| 74 | + } |
| 75 | + |
| 76 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, wave->samples, |
| 77 | + wave->num_samples); |
| 78 | + |
| 79 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, tail_paddings, |
| 80 | + sizeof(tail_paddings) / sizeof(float)); |
| 81 | + SherpaOnnxOnlineStreamInputFinished(stream); |
| 82 | + while (SherpaOnnxIsKeywordStreamReady(kws, stream)) { |
| 83 | + SherpaOnnxDecodeKeywordStream(kws, stream); |
| 84 | + const SherpaOnnxKeywordResult *r = SherpaOnnxGetKeywordResult(kws, stream); |
| 85 | + if (r && r->json && strlen(r->keyword)) { |
| 86 | + fprintf(stderr, "Detected keyword: %s\n", r->json); |
| 87 | + |
| 88 | + // Remember to reset the keyword stream right after a keyword is detected |
| 89 | + SherpaOnnxResetKeywordStream(kws, stream); |
| 90 | + } |
| 91 | + SherpaOnnxDestroyKeywordResult(r); |
| 92 | + } |
| 93 | + SherpaOnnxDestroyOnlineStream(stream); |
| 94 | + |
| 95 | + // -------------------------------------------------------------------------- |
| 96 | + |
| 97 | + fprintf(stderr, "--Use pre-defined keywords + add a new keyword--\n"); |
| 98 | + |
| 99 | + stream = SherpaOnnxCreateKeywordStreamWithKeywords(kws, "y ǎn y uán @演员"); |
| 100 | + |
| 101 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, wave->samples, |
| 102 | + wave->num_samples); |
| 103 | + |
| 104 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, tail_paddings, |
| 105 | + sizeof(tail_paddings) / sizeof(float)); |
| 106 | + SherpaOnnxOnlineStreamInputFinished(stream); |
| 107 | + while (SherpaOnnxIsKeywordStreamReady(kws, stream)) { |
| 108 | + SherpaOnnxDecodeKeywordStream(kws, stream); |
| 109 | + const SherpaOnnxKeywordResult *r = SherpaOnnxGetKeywordResult(kws, stream); |
| 110 | + if (r && r->json && strlen(r->keyword)) { |
| 111 | + fprintf(stderr, "Detected keyword: %s\n", r->json); |
| 112 | + |
| 113 | + // Remember to reset the keyword stream |
| 114 | + SherpaOnnxResetKeywordStream(kws, stream); |
| 115 | + } |
| 116 | + SherpaOnnxDestroyKeywordResult(r); |
| 117 | + } |
| 118 | + SherpaOnnxDestroyOnlineStream(stream); |
| 119 | + |
| 120 | + // -------------------------------------------------------------------------- |
| 121 | + |
| 122 | + fprintf(stderr, "--Use pre-defined keywords + add two new keywords--\n"); |
| 123 | + |
| 124 | + stream = SherpaOnnxCreateKeywordStreamWithKeywords( |
| 125 | + kws, "y ǎn y uán @演员/zh ī m íng @知名"); |
| 126 | + |
| 127 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, wave->samples, |
| 128 | + wave->num_samples); |
| 129 | + |
| 130 | + SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, tail_paddings, |
| 131 | + sizeof(tail_paddings) / sizeof(float)); |
| 132 | + SherpaOnnxOnlineStreamInputFinished(stream); |
| 133 | + while (SherpaOnnxIsKeywordStreamReady(kws, stream)) { |
| 134 | + SherpaOnnxDecodeKeywordStream(kws, stream); |
| 135 | + const SherpaOnnxKeywordResult *r = SherpaOnnxGetKeywordResult(kws, stream); |
| 136 | + if (r && r->json && strlen(r->keyword)) { |
| 137 | + fprintf(stderr, "Detected keyword: %s\n", r->json); |
| 138 | + |
| 139 | + // Remember to reset the keyword stream |
| 140 | + SherpaOnnxResetKeywordStream(kws, stream); |
| 141 | + } |
| 142 | + SherpaOnnxDestroyKeywordResult(r); |
| 143 | + } |
| 144 | + SherpaOnnxDestroyOnlineStream(stream); |
| 145 | + |
| 146 | + SherpaOnnxFreeWave(wave); |
| 147 | + SherpaOnnxDestroyKeywordSpotter(kws); |
| 148 | + |
| 149 | + return 0; |
| 150 | +} |
0 commit comments