-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4666f37
commit 6508ecd
Showing
13 changed files
with
222 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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/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()); | ||
} | ||
|
||
Ort::Value Run(Ort::Value mel) const { | ||
auto out = sess_->Run({}, input_names_ptr_.data(), &mel, 1, | ||
output_names_ptr_.data(), output_names_ptr_.size()); | ||
|
||
return std::move(out[0]); | ||
} | ||
|
||
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; | ||
|
||
Ort::Value 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// sherpa-onnx/csrc/hifigan-vocoder.h | ||
// | ||
// Copyright (c) 2024 Xiaomi Corporation | ||
|
||
#ifndef SHERPA_ONNX_CSRC_HIFIGAN_VOCODER_H_ | ||
#define SHERPA_ONNX_CSRC_HIFIGAN_VOCODER_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "onnxruntime_cxx_api.h" // NOLINT | ||
|
||
namespace sherpa_onnx { | ||
|
||
class HifiganVocoder { | ||
public: | ||
~HifiganVocoder(); | ||
|
||
HifiganVocoder(int32_t num_threads, const std::string &provider, | ||
const std::string &model); | ||
|
||
template <typename Manager> | ||
HifiganVocoder(Manager *mgr, int32_t num_threads, const std::string &provider, | ||
const std::string &model); | ||
|
||
/** @param mel A float32 tensor of shape (batch_size, feat_dim, num_frames). | ||
* @return Return a float32 tensor of shape (batch_size, num_samples). | ||
*/ | ||
Ort::Value Run(Ort::Value mel) const; | ||
|
||
private: | ||
class Impl; | ||
std::unique_ptr<Impl> impl_; | ||
}; | ||
|
||
} // namespace sherpa_onnx | ||
|
||
#endif // SHERPA_ONNX_CSRC_HIFIGAN_VOCODER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters