Download reference/cpp/onnx_wrapper.h from hzeng8/vad_cpp: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/hzeng8/vad_cpp/resolve/main/reference/cpp/onnx_wrapper.h
- Command line
-
hf download hf://hzeng8/vad_cpp/reference/cpp/onnx_wrapper.h
-
curl -L -o onnx_wrapper.h https://huggingface.co/hzeng8/vad_cpp/resolve/main/reference/cpp/onnx_wrapper.h
1.82 kB
| 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"); | |
| } | |
| 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); | |
| }; |