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