Skip to content

Commit

Permalink
Add HarmonyOS support for text-to-speech.
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj committed Dec 1, 2024
1 parent a3d6e1a commit 2c99013
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 102 deletions.
43 changes: 36 additions & 7 deletions sherpa-onnx/c-api/c-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,9 @@ static sherpa_onnx::OfflineRecognizerConfig GetOfflineRecognizerConfig(

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

Expand Down Expand Up @@ -967,9 +967,9 @@ sherpa_onnx::VadModelConfig GetVadModelConfig(

if (vad_config.debug) {
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s", vad_config.ToString().c_str());
SHERPA_ONNX_LOGE("%{public}s\n", vad_config.ToString().c_str());
#else
SHERPA_ONNX_LOGE("%s", vad_config.ToString().c_str());
SHERPA_ONNX_LOGE("%s\n", vad_config.ToString().c_str());
#endif
}

Expand Down Expand Up @@ -1053,7 +1053,7 @@ struct SherpaOnnxOfflineTts {
std::unique_ptr<sherpa_onnx::OfflineTts> impl;
};

SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts(
static sherpa_onnx::OfflineTtsConfig GetOfflineTtsConfig(
const SherpaOnnxOfflineTtsConfig *config) {
sherpa_onnx::OfflineTtsConfig tts_config;

Expand Down Expand Up @@ -1084,9 +1084,20 @@ SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts(
tts_config.max_num_sentences = SHERPA_ONNX_OR(config->max_num_sentences, 2);

if (tts_config.model.debug) {
#if __OHOS__
SHERPA_ONNX_LOGE("%{public}s\n", tts_config.ToString().c_str());
#else
SHERPA_ONNX_LOGE("%s\n", tts_config.ToString().c_str());
#endif
}

return tts_config;
}

SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTts(
const SherpaOnnxOfflineTtsConfig *config) {
auto tts_config = GetOfflineTtsConfig(config);

if (!tts_config.Validate()) {
SHERPA_ONNX_LOGE("Errors in config");
return nullptr;
Expand Down Expand Up @@ -1908,6 +1919,7 @@ SherpaOnnxOfflineSpeakerDiarizationProcessWithCallbackNoArg(

return ans;
}
#endif

#ifdef __OHOS__

Expand Down Expand Up @@ -1959,6 +1971,23 @@ SherpaOnnxVoiceActivityDetector *SherpaOnnxCreateVoiceActivityDetectorOHOS(

return p;
}
#endif

#endif
#if SHERPA_ONNX_ENABLE_TTS == 1
SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTtsOHOS(
const SherpaOnnxOfflineTtsConfig *config, NativeResourceManager *mgr) {
if (!mgr) {
return SherpaOnnxCreateOfflineTts(config);
}

auto tts_config = GetOfflineTtsConfig(config);

SherpaOnnxOfflineTts *tts = new SherpaOnnxOfflineTts;

tts->impl = std::make_unique<sherpa_onnx::OfflineTts>(mgr, tts_config);

return tts;
}

#endif // #if SHERPA_ONNX_ENABLE_TTS == 1

#endif // #ifdef __OHOS__
3 changes: 3 additions & 0 deletions sherpa-onnx/c-api/c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,9 @@ SHERPA_ONNX_API SherpaOnnxVoiceActivityDetector *
SherpaOnnxCreateVoiceActivityDetectorOHOS(
const SherpaOnnxVadModelConfig *config, float buffer_size_in_seconds,
NativeResourceManager *mgr);

SHERPA_ONNX_API SherpaOnnxOfflineTts *SherpaOnnxCreateOfflineTtsOHOS(
const SherpaOnnxOfflineTtsConfig *config, NativeResourceManager *mgr);
#endif

#if defined(__GNUC__)
Expand Down
27 changes: 21 additions & 6 deletions sherpa-onnx/csrc/lexicon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
#include <algorithm>
#include <cctype>
#include <fstream>
#include <memory>
#include <sstream>
#include <strstream>
#include <utility>

#if __ANDROID_API__ >= 9
#include <strstream>

#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif

#include <memory>
#if __OHOS__
#include "rawfile/raw_file_manager.h"
#endif

#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
Expand Down Expand Up @@ -110,8 +112,8 @@ Lexicon::Lexicon(const std::string &lexicon, const std::string &tokens,
InitPunctuations(punctuations);
}

#if __ANDROID_API__ >= 9
Lexicon::Lexicon(AAssetManager *mgr, const std::string &lexicon,
template <typename Manager>
Lexicon::Lexicon(Manager *mgr, const std::string &lexicon,
const std::string &tokens, const std::string &punctuations,
const std::string &language, bool debug /*= false*/
)
Expand All @@ -132,7 +134,6 @@ Lexicon::Lexicon(AAssetManager *mgr, const std::string &lexicon,

InitPunctuations(punctuations);
}
#endif

std::vector<TokenIDs> Lexicon::ConvertTextToTokenIds(
const std::string &text, const std::string & /*voice*/ /*= ""*/) const {
Expand Down Expand Up @@ -371,4 +372,18 @@ void Lexicon::InitPunctuations(const std::string &punctuations) {
}
}

#if __ANDROID_API__ >= 9
template Lexicon::Lexicon(AAssetManager *mgr, const std::string &lexicon,
const std::string &tokens,
const std::string &punctuations,
const std::string &language, bool debug = false);
#endif

#if __OHOS__
template Lexicon::Lexicon(NativeResourceManager *mgr,
const std::string &lexicon, const std::string &tokens,
const std::string &punctuations,
const std::string &language, bool debug = false);
#endif

} // namespace sherpa_onnx
14 changes: 4 additions & 10 deletions sherpa-onnx/csrc/lexicon.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
#include <unordered_set>
#include <vector>

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

#include "sherpa-onnx/csrc/offline-tts-frontend.h"

namespace sherpa_onnx {
Expand All @@ -31,11 +26,10 @@ class Lexicon : public OfflineTtsFrontend {
const std::string &punctuations, const std::string &language,
bool debug = false);

#if __ANDROID_API__ >= 9
Lexicon(AAssetManager *mgr, const std::string &lexicon,
const std::string &tokens, const std::string &punctuations,
const std::string &language, bool debug = false);
#endif
template <typename Manager>
Lexicon(Manager *mgr, const std::string &lexicon, const std::string &tokens,
const std::string &punctuations, const std::string &language,
bool debug = false);

std::vector<TokenIDs> ConvertTextToTokenIds(
const std::string &text, const std::string &voice = "") const override;
Expand Down
36 changes: 26 additions & 10 deletions sherpa-onnx/csrc/offline-tts-character-frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
//
// Copyright (c) 2023 Xiaomi Corporation

#if __ANDROID_API__ >= 9
#include <strstream>

#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#endif
#include <algorithm>
#include <cctype>
#include <codecvt>
#include <fstream>
#include <locale>
#include <sstream>
#include <strstream>
#include <utility>

#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-tts-character-frontend.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
Expand Down Expand Up @@ -82,18 +86,16 @@ OfflineTtsCharacterFrontend::OfflineTtsCharacterFrontend(
token2id_ = ReadTokens(is);
}

#if __ANDROID_API__ >= 9
template <typename Manager>
OfflineTtsCharacterFrontend::OfflineTtsCharacterFrontend(
AAssetManager *mgr, const std::string &tokens,
Manager *mgr, const std::string &tokens,
const OfflineTtsVitsModelMetaData &meta_data)
: meta_data_(meta_data) {
auto buf = ReadFile(mgr, tokens);
std::istrstream is(buf.data(), buf.size());
token2id_ = ReadTokens(is);
}

#endif

std::vector<TokenIDs> OfflineTtsCharacterFrontend::ConvertTextToTokenIds(
const std::string &_text, const std::string & /*voice = ""*/) const {
// see
Expand Down Expand Up @@ -189,4 +191,18 @@ std::vector<TokenIDs> OfflineTtsCharacterFrontend::ConvertTextToTokenIds(
return ans;
}

#if __ANDROID_API__ >= 9
template OfflineTtsCharacterFrontend::OfflineTtsCharacterFrontend(
AAssetManager *mgr, const std::string &tokens,
const OfflineTtsVitsModelMetaData &meta_data);

#endif

#if __OHOS__
template OfflineTtsCharacterFrontend::OfflineTtsCharacterFrontend(
NativeResourceManager *mgr, const std::string &tokens,
const OfflineTtsVitsModelMetaData &meta_data);

#endif

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

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

#include "sherpa-onnx/csrc/offline-tts-frontend.h"
#include "sherpa-onnx/csrc/offline-tts-vits-model-metadata.h"

Expand All @@ -24,11 +19,10 @@ class OfflineTtsCharacterFrontend : public OfflineTtsFrontend {
OfflineTtsCharacterFrontend(const std::string &tokens,
const OfflineTtsVitsModelMetaData &meta_data);

#if __ANDROID_API__ >= 9
OfflineTtsCharacterFrontend(AAssetManager *mgr, const std::string &tokens,
template <typename Manager>
OfflineTtsCharacterFrontend(Manager *mgr, const std::string &tokens,
const OfflineTtsVitsModelMetaData &meta_data);

#endif
/** Convert a string to token IDs.
*
* @param text The input text.
Expand Down
22 changes: 20 additions & 2 deletions sherpa-onnx/csrc/offline-tts-impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

#include <memory>

#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-tts-vits-impl.h"

namespace sherpa_onnx {
Expand All @@ -16,12 +25,21 @@ std::unique_ptr<OfflineTtsImpl> OfflineTtsImpl::Create(
return std::make_unique<OfflineTtsVitsImpl>(config);
}

#if __ANDROID_API__ >= 9
template <typename Manager>
std::unique_ptr<OfflineTtsImpl> OfflineTtsImpl::Create(
AAssetManager *mgr, const OfflineTtsConfig &config) {
Manager *mgr, const OfflineTtsConfig &config) {
// TODO(fangjun): Support other types
return std::make_unique<OfflineTtsVitsImpl>(mgr, config);
}

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

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

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

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

#include "sherpa-onnx/csrc/offline-tts.h"

namespace sherpa_onnx {
Expand All @@ -23,10 +18,9 @@ class OfflineTtsImpl {

static std::unique_ptr<OfflineTtsImpl> Create(const OfflineTtsConfig &config);

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

virtual GeneratedAudio Generate(
const std::string &text, int64_t sid = 0, float speed = 1.0,
Expand Down
Loading

0 comments on commit 2c99013

Please sign in to comment.