// sherpa-onnx/csrc/hifigan-vocoder.cc // // Copyright (c) 2024 Xiaomi Corporation #include "sherpa-onnx/csrc/hifigan-vocoder.h" #include #include #include #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 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 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 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(); return {p, p + total}; } private: void Init(void *model_data, size_t model_data_length) { sess_ = std::make_unique(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 sess_; std::vector input_names_; std::vector input_names_ptr_; std::vector output_names_; std::vector output_names_ptr_; }; HifiganVocoder::HifiganVocoder(int32_t num_threads, const std::string &provider, const std::string &model) : impl_(std::make_unique(num_threads, provider, model)) {} template HifiganVocoder::HifiganVocoder(Manager *mgr, int32_t num_threads, const std::string &provider, const std::string &model) : impl_(std::make_unique(mgr, num_threads, provider, model)) {} HifiganVocoder::~HifiganVocoder() = default; std::vector 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