-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34d70a2
commit f204e62
Showing
9 changed files
with
285 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// c-api-examples/audio-tagging-c-api.c | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
|
||
// We assume you have pre-downloaded the model files for testing | ||
// from https://github.com/k2-fsa/sherpa-onnx/releases/tag/audio-tagging-models | ||
// | ||
// An example is given below: | ||
// | ||
// clang-format off | ||
// | ||
// wget https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
// tar xvf sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
// rm sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
// | ||
// clang-format on | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "sherpa-onnx/c-api/c-api.h" | ||
|
||
int32_t main() { | ||
SherpaOnnxAudioTaggingConfig config; | ||
memset(&config, 0, sizeof(config)); | ||
|
||
config.model.zipformer.model = | ||
"./sherpa-onnx-zipformer-audio-tagging-2024-04-09/model.int8.onnx"; | ||
config.model.num_threads = 1; | ||
config.model.debug = 1; | ||
config.model.provider = "cpu"; | ||
// clang-format off | ||
config.labels = "./sherpa-onnx-zipformer-audio-tagging-2024-04-09/class_labels_indices.csv"; | ||
// clang-format on | ||
|
||
const SherpaOnnxAudioTagging *tagger = SherpaOnnxCreateAudioTagging(&config); | ||
if (!tagger) { | ||
fprintf(stderr, "Failed to create audio tagger. Please check your config"); | ||
return -1; | ||
} | ||
|
||
// You can find more test waves from | ||
// https://github.com/k2-fsa/sherpa-onnx/releases/download/audio-tagging-models/sherpa-onnx-zipformer-audio-tagging-2024-04-09.tar.bz2 | ||
const char *wav_filename = | ||
"./sherpa-onnx-zipformer-audio-tagging-2024-04-09/test_wavs/1.wav"; | ||
|
||
const SherpaOnnxWave *wave = SherpaOnnxReadWave(wav_filename); | ||
if (wave == NULL) { | ||
fprintf(stderr, "Failed to read %s\n", wav_filename); | ||
return -1; | ||
} | ||
|
||
const SherpaOnnxOfflineStream *stream = | ||
SherpaOnnxAudioTaggingCreateOfflineStream(tagger); | ||
|
||
AcceptWaveformOffline(stream, wave->sample_rate, wave->samples, | ||
wave->num_samples); | ||
|
||
int32_t top_k = 5; | ||
const SherpaOnnxAudioEvent *const *results = | ||
SherpaOnnxAudioTaggingCompute(tagger, stream, top_k); | ||
|
||
fprintf(stderr, "--------------------------------------------------\n"); | ||
fprintf(stderr, "Index\t\tProbability\t\tEvent name\n"); | ||
fprintf(stderr, "--------------------------------------------------\n"); | ||
for (int32_t i = 0; i != top_k; ++i) { | ||
fprintf(stderr, "%d\t\t%.3f\t\t\t%s\n", i, results[i]->prob, | ||
results[i]->name); | ||
} | ||
fprintf(stderr, "--------------------------------------------------\n"); | ||
|
||
SherpaOnnxAudioTaggingFreeResults(results); | ||
DestroyOfflineStream(stream); | ||
SherpaOnnxFreeWave(wave); | ||
SherpaOnnxDestroyAudioTagging(tagger); | ||
|
||
return 0; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.