From 030bb819c348dd8381b1bbcaf0e385e17bddbc4d Mon Sep 17 00:00:00 2001 From: Roy Shilkrot Date: Sun, 29 Oct 2023 01:41:58 -0400 Subject: [PATCH] fix windown unicode model path --- src/whisper-processing.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/whisper-processing.cpp b/src/whisper-processing.cpp index 63eb357..283712b 100644 --- a/src/whisper-processing.cpp +++ b/src/whisper-processing.cpp @@ -8,6 +8,8 @@ #include #include +#include +#include #define VAD_THOLD 0.0001f #define FREQ_THOLD 100.0f @@ -73,7 +75,32 @@ bool vad_simple(float *pcmf32, size_t pcm32f_size, uint32_t sample_rate, float v struct whisper_context *init_whisper_context(const std::string &model_path) { obs_log(LOG_INFO, "Loading whisper model from %s", model_path.c_str()); + +#ifdef _WIN32 + // convert model path UTF8 to wstring (wchar_t) for whisper + int count = MultiByteToWideChar(CP_UTF8, 0, model_path.c_str(), (int)model_path.length(), NULL, 0); + std::wstring model_path_ws(count, 0); + MultiByteToWideChar(CP_UTF8, 0, model_path.c_str(), (int)model_path.length(), &model_path_ws[0], count); + + // Read model into buffer + std::ifstream modelFile(model_path_ws, std::ios::binary); + if (!modelFile.is_open()) { + obs_log(LOG_ERROR, "Failed to open whisper model file %s", model_path.c_str()); + return nullptr; + } + modelFile.seekg(0, std::ios::end); + const size_t modelFileSize = modelFile.tellg(); + modelFile.seekg(0, std::ios::beg); + std::vector modelBuffer(modelFileSize); + modelFile.read(modelBuffer.data(), modelFileSize); + modelFile.close(); + + // Initialize whisper + struct whisper_context *ctx = whisper_init_from_buffer(modelBuffer.data(), modelFileSize); +#else struct whisper_context *ctx = whisper_init_from_file(model_path.c_str()); +#endif + if (ctx == nullptr) { obs_log(LOG_ERROR, "Failed to load whisper model"); return nullptr;