Publish sherpa_onnx.har for HarmonyOS (#1572)

This commit is contained in:
Fangjun Kuang
2024-11-28 17:30:16 +08:00
committed by GitHub
parent 109fb799ca
commit 315d8e2a47
104 changed files with 6257 additions and 4378 deletions

View File

@@ -8,6 +8,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <strstream>
#include <utility>
#include <vector>
@@ -1201,6 +1202,28 @@ const SherpaOnnxWave *SherpaOnnxReadWave(const char *filename) {
return wave;
}
const SherpaOnnxWave *SherpaOnnxReadWaveFromBinaryData(const char *data,
int32_t n) {
int32_t sample_rate = -1;
bool is_ok = false;
std::istrstream is(data, n);
std::vector<float> samples = sherpa_onnx::ReadWave(is, &sample_rate, &is_ok);
if (!is_ok) {
return nullptr;
}
float *c_samples = new float[samples.size()];
std::copy(samples.begin(), samples.end(), c_samples);
SherpaOnnxWave *wave = new SherpaOnnxWave;
wave->samples = c_samples;
wave->sample_rate = sample_rate;
wave->num_samples = samples.size();
return wave;
}
void SherpaOnnxFreeWave(const SherpaOnnxWave *wave) {
if (wave) {
delete[] wave->samples;

View File

@@ -1001,6 +1001,14 @@ SHERPA_ONNX_API typedef struct SherpaOnnxWave {
// SherpaOnnxFreeWave() to free the returned pointer to avoid memory leak.
SHERPA_ONNX_API const SherpaOnnxWave *SherpaOnnxReadWave(const char *filename);
// Similar to SherpaOnnxReadWave(), it has read the content of `filename`
// into the array `data`.
//
// If the returned pointer is not NULL, the user has to invoke
// SherpaOnnxFreeWave() to free the returned pointer to avoid memory leak.
SHERPA_ONNX_API const SherpaOnnxWave *SherpaOnnxReadWaveFromBinaryData(
const char *data, int32_t n);
SHERPA_ONNX_API void SherpaOnnxFreeWave(const SherpaOnnxWave *wave);
// ============================================================