C API Usage example¶
Examples are from native_client/client.cc.
Creating a model instance and loading model¶
Transcribing audio with the loaded model¶
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | if (extended_output) {
Metadata *result = STT_SpeechToTextWithMetadata(aCtx, aBuffer, aBufferSize, 1);
res.string = CandidateTranscriptToString(&result->transcripts[0]);
STT_FreeMetadata(result);
} else if (json_output) {
Metadata *result = STT_SpeechToTextWithMetadata(aCtx, aBuffer, aBufferSize, json_candidate_transcripts);
res.string = MetadataToJSON(result);
STT_FreeMetadata(result);
} else if (stream_size > 0) {
StreamingState* ctx;
int status = STT_CreateStream(aCtx, &ctx);
if (status != STT_ERR_OK) {
res.string = strdup("");
return res;
}
size_t off = 0;
const char *last = nullptr;
const char *prev = nullptr;
while (off < aBufferSize) {
size_t cur = aBufferSize - off > stream_size ? stream_size : aBufferSize - off;
STT_FeedAudioContent(ctx, aBuffer + off, cur);
off += cur;
prev = last;
const char* partial = STT_IntermediateDecode(ctx);
if (last == nullptr || strcmp(last, partial)) {
printf("%s\n", partial);
last = partial;
} else {
STT_FreeString((char *) partial);
}
if (prev != nullptr && prev != last) {
STT_FreeString((char *) prev);
}
}
if (last != nullptr) {
STT_FreeString((char *) last);
}
res.string = STT_FinishStream(ctx);
} else if (extended_stream_size > 0) {
StreamingState* ctx;
int status = STT_CreateStream(aCtx, &ctx);
if (status != STT_ERR_OK) {
res.string = strdup("");
return res;
}
size_t off = 0;
const char *last = nullptr;
const char *prev = nullptr;
while (off < aBufferSize) {
size_t cur = aBufferSize - off > extended_stream_size ? extended_stream_size : aBufferSize - off;
STT_FeedAudioContent(ctx, aBuffer + off, cur);
off += cur;
prev = last;
const Metadata* result = STT_IntermediateDecodeWithMetadata(ctx, 1);
const char* partial = CandidateTranscriptToString(&result->transcripts[0]);
if (last == nullptr || strcmp(last, partial)) {
printf("%s\n", partial);
last = partial;
} else {
free((char *) partial);
}
if (prev != nullptr && prev != last) {
free((char *) prev);
}
STT_FreeMetadata((Metadata *)result);
}
const Metadata* result = STT_FinishStreamWithMetadata(ctx, 1);
res.string = CandidateTranscriptToString(&result->transcripts[0]);
STT_FreeMetadata((Metadata *)result);
free((char *) last);
} else {
res.string = STT_SpeechToText(aCtx, aBuffer, aBufferSize);
}
|
Full source code¶
See Full source code
.