Skip to content

Commit

Permalink
Add non-streaming ASR support for HarmonyOS.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Nov 26, 2024
1 parent a4b79f0 commit e2d4fe3
Show file tree
Hide file tree
Showing 50 changed files with 648 additions and 454 deletions.
103 changes: 62 additions & 41 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,27 +352,7 @@ struct SherpaOnnxOfflineStream {
: impl(std::move(p)) {}
};

static sherpa_onnx::OfflineRecognizerConfig convertConfig(
const SherpaOnnxOfflineRecognizerConfig *config);

const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(
const SherpaOnnxOfflineRecognizerConfig *config) {
sherpa_onnx::OfflineRecognizerConfig recognizer_config =
convertConfig(config);

if (!recognizer_config.Validate()) {
SHERPA_ONNX_LOGE("Errors in config");
return nullptr;
}

SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;

recognizer->impl =
std::make_unique<sherpa_onnx::OfflineRecognizer>(recognizer_config);

return recognizer;
}
sherpa_onnx::OfflineRecognizerConfig convertConfig(
static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(
const SherpaOnnxOfflineRecognizerConfig *config) {
sherpa_onnx::OfflineRecognizerConfig recognizer_config;

Expand Down Expand Up @@ -491,17 +471,39 @@ sherpa_onnx::OfflineRecognizerConfig convertConfig(
recognizer_config.rule_fars = SHERPA_ONNX_OR(config->rule_fars, "");

if (config->model_config.debug) {
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s", recognizer_config.ToString().c_str());
#else
SHERPA_ONNX_LOGE("%s", recognizer_config.ToString().c_str());
#endif
}

return recognizer_config;
}

const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizer(
const SherpaOnnxOfflineRecognizerConfig *config) {
sherpa_onnx::OfflineRecognizerConfig recognizer_config =
GetOfflineRecognizerConfig(config);

if (!recognizer_config.Validate()) {
SHERPA_ONNX_LOGE("Errors in config");
return nullptr;
}

SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;

recognizer->impl =
std::make_unique<sherpa_onnx::OfflineRecognizer>(recognizer_config);

return recognizer;
}

void SherpaOnnxOfflineRecognizerSetConfig(
const SherpaOnnxOfflineRecognizer *recognizer,
const SherpaOnnxOfflineRecognizerConfig *config) {
sherpa_onnx::OfflineRecognizerConfig recognizer_config =
convertConfig(config);
GetOfflineRecognizerConfig(config);
recognizer->impl->SetConfig(recognizer_config);
}

Expand Down Expand Up @@ -977,25 +979,6 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetector(
return p;
}

#ifdef __OHOS__
SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
NativeResourceManager *mgr) {
if (mgr == nullptr) {
return SherpaOnnxCreateVoiceActivityDetector(config,
buffer_size_in_seconds);
}

auto vad_config = GetVadModelConfig(config);

SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;
p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(
mgr, vad_config, buffer_size_in_seconds);

return p;
}
#endif

void SherpaOnnxDestroyVoiceActivityDetector(
SherpaOnnxVoiceActivityDetector *p) {
delete p;
Expand Down Expand Up @@ -1891,4 +1874,42 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
return ans;
}

#ifdef __OHOS__

const SherpaOnnxOfflineRecognizer *SherpaOnnxCreateOfflineRecognizerOHOS(
const SherpaOnnxOfflineRecognizerConfig *config,
NativeResourceManager *mgr) {
if (mgr == nullptr) {
return SherpaOnnxCreateOfflineRecognizer(config);
}

sherpa_onnx::OfflineRecognizerConfig recognizer_config =
GetOfflineRecognizerConfig(config);

SherpaOnnxOfflineRecognizer *recognizer = new SherpaOnnxOfflineRecognizer;

recognizer->impl =
std::make_unique<sherpa_onnx::OfflineRecognizer>(mgr, recognizer_config);

return recognizer;
}

SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
NativeResourceManager *mgr) {
if (mgr == nullptr) {
return SherpaOnnxCreateVoiceActivityDetector(config,
buffer_size_in_seconds);
}

auto vad_config = GetVadModelConfig(config);

SherpaOnnxVoiceActivityDetector *p = new SherpaOnnxVoiceActivityDetector;
p->impl = std::make_unique<sherpa_onnx::VoiceActivityDetector>(
mgr, vad_config, buffer_size_in_seconds);

return p;
}
#endif

#endif
38 changes: 23 additions & 15 deletions sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -841,21 +841,6 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
SherpaOnnxCreateVoiceActivityDetector(const SherpaOnnxVadModelConfig *config,
float buffer_size_in_seconds);

#ifdef __OHOS__

// Return an instance of VoiceActivityDetector.
// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free
// the returned pointer to avoid memory leak.
//
// It is for HarmonyOS
typedef struct NativeResourceManager NativeResourceManager;

SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
SherpaOnnxCreateVoiceActivityDetectorOHOS(
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
NativeResourceManager *mgr);
#endif

SHERPA_ONNX_API void SherpaOnnxDestroyVoiceActivityDetector(
SherpaOnnxVoiceActivityDetector *p);

Expand Down Expand Up @@ -1537,6 +1522,29 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(
SHERPA_ONNX_API void SherpaOnnxOfflineSpeakerDiarizationDestroyResult(
const SherpaOnnxOfflineSpeakerDiarizationResult *r);

#ifdef __OHOS__

// It is for HarmonyOS
typedef struct NativeResourceManager NativeResourceManager;

/// @param config Config for the recognizer.
/// @return Return a pointer to the recognizer. The user has to invoke
// SherpaOnnxDestroyOfflineRecognizer() to free it to avoid memory
// leak.
SHERPA_ONNX_API const SherpaOnnxOfflineRecognizer *
SherpaOnnxCreateOfflineRecognizerOHOS(
const SherpaOnnxOfflineRecognizerConfig *config,
NativeResourceManager *mgr);

// Return an instance of VoiceActivityDetector.
// The user has to use SherpaOnnxDestroyVoiceActivityDetector() to free
// the returned pointer to avoid memory leak.
SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
SherpaOnnxCreateVoiceActivityDetectorOHOS(
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
NativeResourceManager *mgr);
#endif

#if defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
Expand Down
5 changes: 0 additions & 5 deletions sherpa-onnx/c-api/cxx-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ struct SHERPA_ONNX_API OfflineTdnnModelConfig {
std::string model;
};

struct SHERPA_ONNX_API SherpaOnnxOfflineLMConfig {
std::string model;
float scale = 1.0;
};

struct SHERPA_ONNX_API OfflineSenseVoiceModelConfig {
std::string model;
std::string language;
Expand Down
29 changes: 25 additions & 4 deletions sherpa-onnx/csrc/offline-ctc-model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#include <sstream>
#include <string>

#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/offline-nemo-enc-dec-ctc-model.h"
#include "sherpa-onnx/csrc/offline-tdnn-ctc-model.h"
Expand Down Expand Up @@ -48,7 +57,11 @@ static ModelType GetModelType(char *model_data, size_t model_data_length,
if (debug) {
std::ostringstream os;
PrintModelMetadata(os, meta_data);
SHERPA_ONNX_LOGE("%s", os.str().c_str());
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s\n", os.str().c_str());
#else
SHERPA_ONNX_LOGE("%s\n", os.str().c_str());
#endif
}

Ort::AllocatorWithDefaultOptions allocator;
Expand Down Expand Up @@ -144,10 +157,9 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
return nullptr;
}

#if __ANDROID_API__ >= 9

template <typename Manager>
std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
AAssetManager *mgr, const OfflineModelConfig &config) {
Manager *mgr, const OfflineModelConfig &config) {
// TODO(fangjun): Refactor it. We don't need to use model_type here
ModelType model_type = ModelType::kUnknown;

Expand Down Expand Up @@ -196,6 +208,15 @@ std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(

return nullptr;
}

#if __ANDROID_API__ >= 9
template std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
AAssetManager *mgr, const OfflineModelConfig &config);
#endif

#if __OHOS__
template std::unique_ptr<OfflineCtcModel> OfflineCtcModel::Create(
NativeResourceManager *mgr, const OfflineModelConfig &config);
#endif

} // namespace sherpa_onnx
10 changes: 2 additions & 8 deletions sherpa-onnx/csrc/offline-ctc-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include <string>
#include <vector>

#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif

#include "onnxruntime_cxx_api.h" // NOLINT
#include "sherpa-onnx/csrc/offline-model-config.h"

Expand All @@ -25,10 +20,9 @@ class OfflineCtcModel {
static std::unique_ptr<OfflineCtcModel> Create(
const OfflineModelConfig &config);

#if __ANDROID_API__ >= 9
template <typename Manager>
static std::unique_ptr<OfflineCtcModel> Create(
AAssetManager *mgr, const OfflineModelConfig &config);
#endif
Manager *mgr, const OfflineModelConfig &config);

/** Run the forward method of the model.
*
Expand Down
24 changes: 21 additions & 3 deletions sherpa-onnx/csrc/offline-lm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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/offline-rnn-lm.h"

namespace sherpa_onnx {
Expand All @@ -16,12 +25,11 @@ std::unique_ptr<OfflineLM> OfflineLM::Create(const OfflineLMConfig &config) {
return std::make_unique<OfflineRnnLM>(config);
}

#if __ANDROID_API__ >= 9
std::unique_ptr<OfflineLM> OfflineLM::Create(AAssetManager *mgr,
template <typename Manager>
std::unique_ptr<OfflineLM> OfflineLM::Create(Manager *mgr,
const OfflineLMConfig &config) {
return std::make_unique<OfflineRnnLM>(mgr, config);
}
#endif

void OfflineLM::ComputeLMScore(float scale, int32_t context_size,
std::vector<Hypotheses> *hyps) {
Expand Down Expand Up @@ -75,4 +83,14 @@ void OfflineLM::ComputeLMScore(float scale, int32_t context_size,
}
}

#if __ANDROID_API__ >= 9
template std::unique_ptr<OfflineLM> OfflineLM::Create(
AAssetManager *mgr, const OfflineLMConfig &config);
#endif

#if __OHOS__
template std::unique_ptr<OfflineLM> OfflineLM::Create(
NativeResourceManager *mgr, const OfflineLMConfig &config);
#endif

} // namespace sherpa_onnx
10 changes: 2 additions & 8 deletions sherpa-onnx/csrc/offline-lm.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
#include <memory>
#include <vector>

#if __ANDROID_API__ >= 9
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif

#include "onnxruntime_cxx_api.h" // NOLINT
#include "sherpa-onnx/csrc/hypothesis.h"
#include "sherpa-onnx/csrc/offline-lm-config.h"
Expand All @@ -25,10 +20,9 @@ class OfflineLM {

static std::unique_ptr<OfflineLM> Create(const OfflineLMConfig &config);

#if __ANDROID_API__ >= 9
static std::unique_ptr<OfflineLM> Create(AAssetManager *mgr,
template <typename Manager>
static std::unique_ptr<OfflineLM> Create(Manager *mgr,
const OfflineLMConfig &config);
#endif

/** Rescore a batch of sentences.
*
Expand Down
Loading

0 comments on commit e2d4fe3

Please sign in to comment.