hifigan-vocoder.cc
3.6 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
// sherpa-onnx/csrc/hifigan-vocoder.cc
//
// Copyright (c) 2024 Xiaomi Corporation
#include "sherpa-onnx/csrc/hifigan-vocoder.h"
#include <string>
#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/file-utils.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/onnx-utils.h"
#include "sherpa-onnx/csrc/session.h"
namespace sherpa_onnx {
class HifiganVocoder::Impl {
public:
explicit Impl(int32_t num_threads, const std::string &provider,
const std::string &model)
: env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(num_threads, provider)),
allocator_{} {
auto buf = ReadFile(model);
Init(buf.data(), buf.size());
}
template <typename Manager>
explicit Impl(Manager *mgr, int32_t num_threads, const std::string &provider,
const std::string &model)
: env_(ORT_LOGGING_LEVEL_ERROR),
sess_opts_(GetSessionOptions(num_threads, provider)),
allocator_{} {
auto buf = ReadFile(mgr, model);
Init(buf.data(), buf.size());
}
std::vector<float> Run(Ort::Value mel) const {
auto out = sess_->Run({}, input_names_ptr_.data(), &mel, 1,
output_names_ptr_.data(), output_names_ptr_.size());
std::vector<int64_t> audio_shape =
out[0].GetTensorTypeAndShapeInfo().GetShape();
int64_t total = 1;
// The output shape may be (1, 1, total) or (1, total) or (total,)
for (auto i : audio_shape) {
total *= i;
}
const float *p = out[0].GetTensorData<float>();
return {p, p + total};
}
private:
void Init(void *model_data, size_t model_data_length) {
sess_ = std::make_unique<Ort::Session>(env_, model_data, model_data_length,
sess_opts_);
GetInputNames(sess_.get(), &input_names_, &input_names_ptr_);
GetOutputNames(sess_.get(), &output_names_, &output_names_ptr_);
}
private:
Ort::Env env_;
Ort::SessionOptions sess_opts_;
Ort::AllocatorWithDefaultOptions allocator_;
std::unique_ptr<Ort::Session> sess_;
std::vector<std::string> input_names_;
std::vector<const char *> input_names_ptr_;
std::vector<std::string> output_names_;
std::vector<const char *> output_names_ptr_;
};
HifiganVocoder::HifiganVocoder(int32_t num_threads, const std::string &provider,
const std::string &model)
: impl_(std::make_unique<Impl>(num_threads, provider, model)) {}
template <typename Manager>
HifiganVocoder::HifiganVocoder(Manager *mgr, int32_t num_threads,
const std::string &provider,
const std::string &model)
: impl_(std::make_unique<Impl>(mgr, num_threads, provider, model)) {}
HifiganVocoder::~HifiganVocoder() = default;
std::vector<float> HifiganVocoder::Run(Ort::Value mel) const {
return impl_->Run(std::move(mel));
}
#if __ANDROID_API__ >= 9
template HifiganVocoder::HifiganVocoder(AAssetManager *mgr, int32_t num_threads,
const std::string &provider,
const std::string &model);
#endif
#if __OHOS__
template HifiganVocoder::HifiganVocoder(NativeResourceManager *mgr,
int32_t num_threads,
const std::string &provider,
const std::string &model);
#endif
} // namespace sherpa_onnx