File size: 1,819 Bytes
d21d362 | 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 | #include <vector>
#include <string>
#if defined(__APPLE__)
#include <onnxruntime/onnxruntime_cxx_api.h>
#else
#include "onnxruntime_run_options_config_keys.h"
#include "onnxruntime_cxx_api.h"
#endif
#ifdef _WIN32
#define ORTSTRING(str) StrToWstr(str)
#define ORTCHAR(str) StrToWstr(str).c_str()
inline std::wstring String2wstring(const std::string& str, const std::string& locale)
{
typedef std::codecvt_byname<wchar_t, char, std::mbstate_t> F;
std::wstring_convert<F> strCnv(new F(locale));
return strCnv.from_bytes(str);
}
inline std::wstring StrToWstr(std::string str) {
if (str.length() == 0)
return L"";
return String2wstring(str, "zh-CN");
}
#else
#define ORTSTRING(str) str
#define ORTCHAR(str) str
#endif
class OnnxVadWrapper {
public:
explicit OnnxVadWrapper(const std::string& model_path, bool force_cpu = false, int thread_num = 1);
~OnnxVadWrapper();
// 重载 operator(),使得对象可以像函数一样调用
std::pair<std::vector<float>, std::vector<float>> operator()(const std::vector<float>& x, int sr);
// 批量处理整个音频
std::vector<float> audio_forward(const std::vector<float>& audio, int sr);
// 重置 RNN 状态
void reset_states(int batch_size = 1);
private:
Ort::Env env_;
std::unique_ptr<Ort::Session> session_;
std::vector<std::string> input_names_, output_names_;
std::vector<const char *> vad_in_names_;
std::vector<const char *> vad_out_names_;
std::vector<int> sample_rates_;
std::string model_path_;
std::vector<float> state_; // RNN State
std::vector<float> context_; // Context buffer
int last_sr_ = 0;
int last_batch_size_ = 0;
void read_model();
bool supports_cpu();
void validate_input(const std::vector<float>& x, int sr);
}; |