vocoder.cc
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// sherpa-onnx/csrc/vocoder.cc
//
// Copyright (c) 2025 Xiaomi Corporation
#include "sherpa-onnx/csrc/vocoder.h"
#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/file-utils.h"
#include "sherpa-onnx/csrc/hifigan-vocoder.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/vocos-vocoder.h"
namespace sherpa_onnx {
namespace {
enum class ModelType : std::uint8_t {
kHifigan,
kVocoos,
kUnknown,
};
} // namespace
static ModelType GetModelType(char *model_data, size_t model_data_length,
bool debug) {
Ort::Env env(ORT_LOGGING_LEVEL_ERROR);
Ort::SessionOptions sess_opts;
sess_opts.SetIntraOpNumThreads(1);
sess_opts.SetInterOpNumThreads(1);
auto sess = std::make_unique<Ort::Session>(env, model_data, model_data_length,
sess_opts);
Ort::ModelMetadata meta_data = sess->GetModelMetadata();
if (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;
auto model_type =
LookupCustomModelMetaData(meta_data, "model_type", allocator);
if (model_type.empty()) {
SHERPA_ONNX_LOGE(
"No model_type in the metadata!\n"
"Please make sure you are using the vocoder from "
"https://github.com/k2-fsa/sherpa-onnx/releases/tag/vocoder-models");
return ModelType::kUnknown;
}
if (model_type == "hifigan") {
return ModelType::kHifigan;
} else if (model_type == "vocos") {
return ModelType::kVocoos;
} else {
SHERPA_ONNX_LOGE("Unsupported model_type: %s", model_type.c_str());
return ModelType::kUnknown;
}
}
std::unique_ptr<Vocoder> Vocoder::Create(const OfflineTtsModelConfig &config) {
auto buffer = ReadFile(config.matcha.vocoder);
auto model_type = GetModelType(buffer.data(), buffer.size(), config.debug);
switch (model_type) {
case ModelType::kHifigan:
return std::make_unique<HifiganVocoder>(
config.num_threads, config.provider, config.matcha.vocoder);
case ModelType::kVocoos:
return std::make_unique<VocosVocoder>(config);
case ModelType::kUnknown:
SHERPA_ONNX_LOGE("Unknown model type in vocoder!");
return nullptr;
}
return nullptr;
}
template <typename Manager>
std::unique_ptr<Vocoder> Vocoder::Create(Manager *mgr,
const OfflineTtsModelConfig &config) {
auto buffer = ReadFile(mgr, config.matcha.vocoder);
auto model_type = GetModelType(buffer.data(), buffer.size(), config.debug);
switch (model_type) {
case ModelType::kHifigan:
return std::make_unique<HifiganVocoder>(
config.num_threads, config.provider, config.matcha.vocoder);
case ModelType::kVocoos:
return std::make_unique<VocosVocoder>(config);
case ModelType::kUnknown:
SHERPA_ONNX_LOGE("Unknown model type in vocoder!");
return nullptr;
}
}
#if __ANDROID_API__ >= 9
template std::unique_ptr<Vocoder> Vocoder::Create(
AAssetManager *mgr, const OfflineTtsModelConfig &config);
#endif
#if __OHOS__
template std::unique_ptr<Vocoder> Vocoder::Create(
NativeResourceManager *mgr, const OfflineTtsModelConfig &config);
#endif
} // namespace sherpa_onnx