Skip to content

Commit

Permalink
Refactor C API to prefix each API with SherpaOnnx. (#1171)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jul 26, 2024
1 parent 994c3e7 commit 4e6aeff
Show file tree
Hide file tree
Showing 47 changed files with 667 additions and 618 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.10.19

* Prefix all C API functions with SherpaOnnx

## 1.10.18

* Fix the case when recognition results contain the symbol `"`. It caused
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ project(sherpa-onnx)
# ./nodejs-addon-examples
# ./dart-api-examples/
# ./CHANGELOG.md
set(SHERPA_ONNX_VERSION "1.10.18")
set(SHERPA_ONNX_VERSION "1.10.19")

# Disable warning about
#
Expand Down
29 changes: 15 additions & 14 deletions c-api-examples/asr-microphone-example/c-api-alsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,11 @@ int32_t main(int32_t argc, char *argv[]) {
}

const SherpaOnnxOnlineRecognizer *recognizer =
CreateOnlineRecognizer(&config);
const SherpaOnnxOnlineStream *stream = CreateOnlineStream(recognizer);
SherpaOnnxCreateOnlineRecognizer(&config);
const SherpaOnnxOnlineStream *stream =
SherpaOnnxCreateOnlineStream(recognizer);

const SherpaOnnxDisplay *display = CreateDisplay(50);
const SherpaOnnxDisplay *display = SherpaOnnxCreateDisplay(50);
int32_t segment_id = 0;

const char *device_name = argv[context.index];
Expand All @@ -218,17 +219,17 @@ int32_t main(int32_t argc, char *argv[]) {

while (!stop) {
const std::vector<float> &samples = alsa.Read(chunk);
AcceptWaveform(stream, expected_sample_rate, samples.data(),
samples.size());
while (IsOnlineStreamReady(recognizer, stream)) {
DecodeOnlineStream(recognizer, stream);
SherpaOnnxOnlineStreamAcceptWaveform(stream, expected_sample_rate,
samples.data(), samples.size());
while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
SherpaOnnxDecodeOnlineStream(recognizer, stream);
}

const SherpaOnnxOnlineRecognizerResult *r =
GetOnlineStreamResult(recognizer, stream);
SherpaOnnxGetOnlineStreamResult(recognizer, stream);

std::string text = r->text;
DestroyOnlineRecognizerResult(r);
SherpaOnnxDestroyOnlineRecognizerResult(r);

if (!text.empty() && last_text != text) {
last_text = text;
Expand All @@ -240,18 +241,18 @@ int32_t main(int32_t argc, char *argv[]) {
fflush(stderr);
}

if (IsEndpoint(recognizer, stream)) {
if (SherpaOnnxOnlineStreamIsEndpoint(recognizer, stream)) {
if (!text.empty()) {
++segment_index;
}
Reset(recognizer, stream);
SherpaOnnxOnlineStreamReset(recognizer, stream);
}
}

// free allocated resources
DestroyDisplay(display);
DestroyOnlineStream(stream);
DestroyOnlineRecognizer(recognizer);
SherpaOnnxDestroyDisplay(display);
SherpaOnnxDestroyOnlineStream(stream);
SherpaOnnxDestroyOnlineRecognizer(recognizer);
fprintf(stderr, "\n");

return 0;
Expand Down
6 changes: 3 additions & 3 deletions c-api-examples/audio-tagging-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ int32_t main() {
const SherpaOnnxOfflineStream *stream =
SherpaOnnxAudioTaggingCreateOfflineStream(tagger);

AcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);

int32_t top_k = 5;
const SherpaOnnxAudioEvent *const *results =
Expand All @@ -71,7 +71,7 @@ int32_t main() {
fprintf(stderr, "--------------------------------------------------\n");

SherpaOnnxAudioTaggingFreeResults(results);
DestroyOfflineStream(stream);
SherpaOnnxDestroyOfflineStream(stream);
SherpaOnnxFreeWave(wave);
SherpaOnnxDestroyAudioTagging(tagger);

Expand Down
42 changes: 22 additions & 20 deletions c-api-examples/decode-file-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ int32_t main(int32_t argc, char *argv[]) {
}

const SherpaOnnxOnlineRecognizer *recognizer =
CreateOnlineRecognizer(&config);
const SherpaOnnxOnlineStream *stream = CreateOnlineStream(recognizer);
SherpaOnnxCreateOnlineRecognizer(&config);
const SherpaOnnxOnlineStream *stream =
SherpaOnnxCreateOnlineStream(recognizer);

const SherpaOnnxDisplay *display = CreateDisplay(50);
const SherpaOnnxDisplay *display = SherpaOnnxCreateDisplay(50);
int32_t segment_id = 0;

const char *wav_filename = argv[context.index];
Expand All @@ -190,52 +191,53 @@ int32_t main(int32_t argc, char *argv[]) {
(start + N > wave->num_samples) ? wave->num_samples : (start + N);
k += N;

AcceptWaveform(stream, wave->sample_rate, wave->samples + start,
end - start);
while (IsOnlineStreamReady(recognizer, stream)) {
DecodeOnlineStream(recognizer, stream);
SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate,
wave->samples + start, end - start);
while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
SherpaOnnxDecodeOnlineStream(recognizer, stream);
}

const SherpaOnnxOnlineRecognizerResult *r =
GetOnlineStreamResult(recognizer, stream);
SherpaOnnxGetOnlineStreamResult(recognizer, stream);

if (strlen(r->text)) {
SherpaOnnxPrint(display, segment_id, r->text);
}

if (IsEndpoint(recognizer, stream)) {
if (SherpaOnnxOnlineStreamIsEndpoint(recognizer, stream)) {
if (strlen(r->text)) {
++segment_id;
}
Reset(recognizer, stream);
SherpaOnnxOnlineStreamReset(recognizer, stream);
}

DestroyOnlineRecognizerResult(r);
SherpaOnnxDestroyOnlineRecognizerResult(r);
}

// add some tail padding
float tail_paddings[4800] = {0}; // 0.3 seconds at 16 kHz sample rate
AcceptWaveform(stream, wave->sample_rate, tail_paddings, 4800);
SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, tail_paddings,
4800);

SherpaOnnxFreeWave(wave);

InputFinished(stream);
while (IsOnlineStreamReady(recognizer, stream)) {
DecodeOnlineStream(recognizer, stream);
SherpaOnnxOnlineStreamInputFinished(stream);
while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
SherpaOnnxDecodeOnlineStream(recognizer, stream);
}

const SherpaOnnxOnlineRecognizerResult *r =
GetOnlineStreamResult(recognizer, stream);
SherpaOnnxGetOnlineStreamResult(recognizer, stream);

if (strlen(r->text)) {
SherpaOnnxPrint(display, segment_id, r->text);
}

DestroyOnlineRecognizerResult(r);
SherpaOnnxDestroyOnlineRecognizerResult(r);

DestroyDisplay(display);
DestroyOnlineStream(stream);
DestroyOnlineRecognizer(recognizer);
SherpaOnnxDestroyDisplay(display);
SherpaOnnxDestroyOnlineStream(stream);
SherpaOnnxDestroyOnlineRecognizer(recognizer);
fprintf(stderr, "\n");

return 0;
Expand Down
18 changes: 9 additions & 9 deletions c-api-examples/sense-voice-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,27 @@ int32_t main() {
recognizer_config.model_config = offline_model_config;

SherpaOnnxOfflineRecognizer *recognizer =
CreateOfflineRecognizer(&recognizer_config);
SherpaOnnxCreateOfflineRecognizer(&recognizer_config);

if (recognizer == NULL) {
fprintf(stderr, "Please check your config!\n");
SherpaOnnxFreeWave(wave);
return -1;
}

SherpaOnnxOfflineStream *stream = CreateOfflineStream(recognizer);
SherpaOnnxOfflineStream *stream = SherpaOnnxCreateOfflineStream(recognizer);

AcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
DecodeOfflineStream(recognizer, stream);
SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxDecodeOfflineStream(recognizer, stream);
const SherpaOnnxOfflineRecognizerResult *result =
GetOfflineStreamResult(stream);
SherpaOnnxGetOfflineStreamResult(stream);

fprintf(stderr, "Decoded text: %s\n", result->text);

DestroyOfflineRecognizerResult(result);
DestroyOfflineStream(stream);
DestroyOfflineRecognizer(recognizer);
SherpaOnnxDestroyOfflineRecognizerResult(result);
SherpaOnnxDestroyOfflineStream(stream);
SherpaOnnxDestroyOfflineRecognizer(recognizer);
SherpaOnnxFreeWave(wave);

return 0;
Expand Down
7 changes: 4 additions & 3 deletions c-api-examples/speaker-identification-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ static const float *ComputeEmbedding(
const SherpaOnnxOnlineStream *stream =
SherpaOnnxSpeakerEmbeddingExtractorCreateStream(ex);

AcceptWaveform(stream, wave->sample_rate, wave->samples, wave->num_samples);
InputFinished(stream);
SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxOnlineStreamInputFinished(stream);

if (!SherpaOnnxSpeakerEmbeddingExtractorIsReady(ex, stream)) {
fprintf(stderr, "The input wave file %s is too short!\n", wav_filename);
Expand All @@ -49,7 +50,7 @@ static const float *ComputeEmbedding(
const float *v =
SherpaOnnxSpeakerEmbeddingExtractorComputeEmbedding(ex, stream);

DestroyOnlineStream(stream);
SherpaOnnxDestroyOnlineStream(stream);
SherpaOnnxFreeWave(wave);

// Remeber to free v to avoid memory leak
Expand Down
6 changes: 3 additions & 3 deletions c-api-examples/spoken-language-identification-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ int32_t main() {
SherpaOnnxOfflineStream *stream =
SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(slid);

AcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);

const SherpaOnnxSpokenLanguageIdentificationResult *result =
SherpaOnnxSpokenLanguageIdentificationCompute(slid, stream);
Expand All @@ -60,7 +60,7 @@ int32_t main() {
fprintf(stderr, "Detected language: %s\n", result->lang);

SherpaOnnxDestroySpokenLanguageIdentificationResult(result);
DestroyOfflineStream(stream);
SherpaOnnxDestroyOfflineStream(stream);
SherpaOnnxFreeWave(wave);
SherpaOnnxDestroySpokenLanguageIdentification(slid);

Expand Down
42 changes: 22 additions & 20 deletions c-api-examples/streaming-hlg-decode-file-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ int32_t main() {
config.model_config.debug = 0;
config.ctc_fst_decoder_config.graph = graph;
const SherpaOnnxOnlineRecognizer *recognizer =
CreateOnlineRecognizer(&config);
SherpaOnnxCreateOnlineRecognizer(&config);
if (!recognizer) {
fprintf(stderr, "Failed to create recognizer");
exit(-1);
}

const SherpaOnnxOnlineStream *stream = CreateOnlineStream(recognizer);
const SherpaOnnxOnlineStream *stream =
SherpaOnnxCreateOnlineStream(recognizer);

const SherpaOnnxDisplay *display = CreateDisplay(50);
const SherpaOnnxDisplay *display = SherpaOnnxCreateDisplay(50);
int32_t segment_id = 0;

const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename);
Expand All @@ -76,52 +77,53 @@ int32_t main() {
(start + N > wave->num_samples) ? wave->num_samples : (start + N);
k += N;

AcceptWaveform(stream, wave->sample_rate, wave->samples + start,
end - start);
while (IsOnlineStreamReady(recognizer, stream)) {
DecodeOnlineStream(recognizer, stream);
SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate,
wave->samples + start, end - start);
while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
SherpaOnnxDecodeOnlineStream(recognizer, stream);
}

const SherpaOnnxOnlineRecognizerResult *r =
GetOnlineStreamResult(recognizer, stream);
SherpaOnnxGetOnlineStreamResult(recognizer, stream);

if (strlen(r->text)) {
SherpaOnnxPrint(display, segment_id, r->text);
}

if (IsEndpoint(recognizer, stream)) {
if (SherpaOnnxOnlineStreamIsEndpoint(recognizer, stream)) {
if (strlen(r->text)) {
++segment_id;
}
Reset(recognizer, stream);
SherpaOnnxOnlineStreamReset(recognizer, stream);
}

DestroyOnlineRecognizerResult(r);
SherpaOnnxDestroyOnlineRecognizerResult(r);
}

// add some tail padding
float tail_paddings[4800] = {0}; // 0.3 seconds at 16 kHz sample rate
AcceptWaveform(stream, wave->sample_rate, tail_paddings, 4800);
SherpaOnnxOnlineStreamAcceptWaveform(stream, wave->sample_rate, tail_paddings,
4800);

SherpaOnnxFreeWave(wave);

InputFinished(stream);
while (IsOnlineStreamReady(recognizer, stream)) {
DecodeOnlineStream(recognizer, stream);
SherpaOnnxOnlineStreamInputFinished(stream);
while (SherpaOnnxIsOnlineStreamReady(recognizer, stream)) {
SherpaOnnxDecodeOnlineStream(recognizer, stream);
}

const SherpaOnnxOnlineRecognizerResult *r =
GetOnlineStreamResult(recognizer, stream);
SherpaOnnxGetOnlineStreamResult(recognizer, stream);

if (strlen(r->text)) {
SherpaOnnxPrint(display, segment_id, r->text);
}

DestroyOnlineRecognizerResult(r);
SherpaOnnxDestroyOnlineRecognizerResult(r);

DestroyDisplay(display);
DestroyOnlineStream(stream);
DestroyOnlineRecognizer(recognizer);
SherpaOnnxDestroyDisplay(display);
SherpaOnnxDestroyOnlineStream(stream);
SherpaOnnxDestroyOnlineRecognizer(recognizer);
fprintf(stderr, "\n");

return 0;
Expand Down
18 changes: 9 additions & 9 deletions c-api-examples/whisper-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int32_t main() {
recognizer_config.model_config = offline_model_config;

SherpaOnnxOfflineRecognizer *recognizer =
CreateOfflineRecognizer(&recognizer_config);
SherpaOnnxCreateOfflineRecognizer(&recognizer_config);

if (recognizer == NULL) {
fprintf(stderr, "Please check your config!\n");
Expand All @@ -69,19 +69,19 @@ int32_t main() {
return -1;
}

SherpaOnnxOfflineStream *stream = CreateOfflineStream(recognizer);
SherpaOnnxOfflineStream *stream = SherpaOnnxCreateOfflineStream(recognizer);

AcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
DecodeOfflineStream(recognizer, stream);
SherpaOnnxAcceptWaveformOffline(stream, wave->sample_rate, wave->samples,
wave->num_samples);
SherpaOnnxDecodeOfflineStream(recognizer, stream);
const SherpaOnnxOfflineRecognizerResult *result =
GetOfflineStreamResult(stream);
SherpaOnnxGetOfflineStreamResult(stream);

fprintf(stderr, "Decoded text: %s\n", result->text);

DestroyOfflineRecognizerResult(result);
DestroyOfflineStream(stream);
DestroyOfflineRecognizer(recognizer);
SherpaOnnxDestroyOfflineRecognizerResult(result);
SherpaOnnxDestroyOfflineStream(stream);
SherpaOnnxDestroyOfflineRecognizer(recognizer);
SherpaOnnxFreeWave(wave);

return 0;
Expand Down
Loading

0 comments on commit 4e6aeff

Please sign in to comment.