Add speaker identification APIs for HarmonyOS (#1607)

* Add speaker embedding extractor API for HarmonyOS

* Add ArkTS API for speaker identification
This commit is contained in:
Fangjun Kuang
2024-12-09 19:23:18 +08:00
committed by GitHub
parent a743a4400f
commit 314545f938
19 changed files with 374 additions and 60 deletions

View File

@@ -8,6 +8,15 @@
#include <utility>
#include <vector>
#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#if __OHOS__
#include "rawfile/raw_file_manager.h"
#endif
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/session.h"
@@ -28,8 +37,8 @@ class SpeakerEmbeddingExtractorModel::Impl {
}
}
#if __ANDROID_API__ >= 9
Impl(AAssetManager *mgr, const SpeakerEmbeddingExtractorConfig &config)
template <typename Manager>
Impl(Manager *mgr, const SpeakerEmbeddingExtractorConfig &config)
: config_(config),
env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(config)),
@@ -39,7 +48,6 @@ class SpeakerEmbeddingExtractorModel::Impl {
Init(buf.data(), buf.size());
}
}
#endif
Ort::Value Compute(Ort::Value x) const {
std::array<Ort::Value, 1> inputs = {std::move(x)};
@@ -68,7 +76,11 @@ class SpeakerEmbeddingExtractorModel::Impl {
if (config_.debug) {
std::ostringstream os;
PrintModelMetadata(os, meta_data);
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s", os.str().c_str());
#else
SHERPA_ONNX_LOGE("%s", os.str().c_str());
#endif
}
Ort::AllocatorWithDefaultOptions allocator; // used in the macro below
@@ -84,8 +96,14 @@ class SpeakerEmbeddingExtractorModel::Impl {
std::string framework;
SHERPA_ONNX_READ_META_DATA_STR(framework, "framework");
if (framework != "wespeaker" && framework != "3d-speaker") {
#if __OHOS__
SHERPA_ONNX_LOGE(
"Expect a wespeaker or a 3d-speaker model, given: %{public}s",
framework.c_str());
#else
SHERPA_ONNX_LOGE("Expect a wespeaker or a 3d-speaker model, given: %s",
framework.c_str());
#endif
exit(-1);
}
}
@@ -111,11 +129,10 @@ SpeakerEmbeddingExtractorModel::SpeakerEmbeddingExtractorModel(
const SpeakerEmbeddingExtractorConfig &config)
: impl_(std::make_unique<Impl>(config)) {}
#if __ANDROID_API__ >= 9
template <typename Manager>
SpeakerEmbeddingExtractorModel::SpeakerEmbeddingExtractorModel(
AAssetManager *mgr, const SpeakerEmbeddingExtractorConfig &config)
Manager *mgr, const SpeakerEmbeddingExtractorConfig &config)
: impl_(std::make_unique<Impl>(mgr, config)) {}
#endif
SpeakerEmbeddingExtractorModel::~SpeakerEmbeddingExtractorModel() = default;
@@ -128,4 +145,14 @@ Ort::Value SpeakerEmbeddingExtractorModel::Compute(Ort::Value x) const {
return impl_->Compute(std::move(x));
}
#if __ANDROID_API__ >= 9
template SpeakerEmbeddingExtractorModel::SpeakerEmbeddingExtractorModel(
AAssetManager *mgr, const SpeakerEmbeddingExtractorConfig &config);
#endif
#if __OHOS__
template SpeakerEmbeddingExtractorModel::SpeakerEmbeddingExtractorModel(
NativeResourceManager *mgr, const SpeakerEmbeddingExtractorConfig &config);
#endif
} // namespace sherpa_onnx