From f84e48fb6c9fd05db5a94743e2ba6df93146acd3 Mon Sep 17 00:00:00 2001 From: Roy Shilkrot Date: Sun, 20 Aug 2023 07:13:37 +0300 Subject: [PATCH] minor fixes --- src/transcription-filter-data.h | 18 +++++++++--------- src/transcription-filter.cpp | 19 +++++++++---------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/transcription-filter-data.h b/src/transcription-filter-data.h index 4457368..00a8c31 100644 --- a/src/transcription-filter-data.h +++ b/src/transcription-filter-data.h @@ -37,11 +37,11 @@ struct transcription_filter_data { struct circlebuf input_buffers[MAX_PREPROC_CHANNELS]; /* Resampler */ - audio_resampler_t *resampler; + audio_resampler_t *resampler = nullptr; /* whisper */ std::string whisper_model_path = "models/ggml-tiny.en.bin"; - struct whisper_context *whisper_context; + struct whisper_context *whisper_context = nullptr; whisper_full_params whisper_params; float filler_p_threshold; @@ -50,21 +50,21 @@ struct transcription_filter_data { bool vad_enabled; int log_level; bool log_words; - bool active; + bool active = false; // Text source to output the subtitles - obs_weak_source_t *text_source; - char *text_source_name; - std::unique_ptr text_source_mutex; + obs_weak_source_t *text_source = nullptr; + char *text_source_name = nullptr; + std::unique_ptr text_source_mutex = nullptr; // Callback to set the text in the output text source (subtitles) std::function setTextCallback; // Use std for thread and mutex std::thread whisper_thread; - std::unique_ptr whisper_buf_mutex; - std::unique_ptr whisper_ctx_mutex; - std::unique_ptr wshiper_thread_cv; + std::unique_ptr whisper_buf_mutex = nullptr; + std::unique_ptr whisper_ctx_mutex = nullptr; + std::unique_ptr wshiper_thread_cv = nullptr; }; // Audio packet info diff --git a/src/transcription-filter.cpp b/src/transcription-filter.cpp index 15079eb..fd69441 100644 --- a/src/transcription-filter.cpp +++ b/src/transcription-filter.cpp @@ -206,8 +206,9 @@ void transcription_filter_update(void *data, obs_data_t *s) obs_weak_source_release(old_weak_text_source); } - const char *new_model_path = obs_data_get_string(s, "whisper_model_path"); - if (strcmp(new_model_path, gf->whisper_model_path.c_str()) != 0) { + std::string new_model_path = obs_data_get_string(s, "whisper_model_path"); + + if (new_model_path != gf->whisper_model_path) { // model path changed, reload the model obs_log(LOG_INFO, "model path changed, reloading model"); if (gf->whisper_context != nullptr) { @@ -220,7 +221,7 @@ void transcription_filter_update(void *data, obs_data_t *s) if (gf->whisper_thread.joinable()) { gf->whisper_thread.join(); } - gf->whisper_model_path = bstrdup(new_model_path); + gf->whisper_model_path = new_model_path; // check if the model exists, if not, download it if (!check_if_model_exists(gf->whisper_model_path)) { @@ -229,8 +230,7 @@ void transcription_filter_update(void *data, obs_data_t *s) gf->whisper_model_path, [gf](int download_status) { if (download_status == 0) { obs_log(LOG_INFO, "Model download complete"); - gf->whisper_context = init_whisper_context( - gf->whisper_model_path); + gf->whisper_context = init_whisper_context(gf->whisper_model_path); gf->whisper_thread = std::thread(whisper_loop, gf); } else { obs_log(LOG_ERROR, "Model download failed"); @@ -321,8 +321,6 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter) gf->resampler = audio_resampler_create(&dst, &src); - gf->active = true; - gf->whisper_buf_mutex = std::unique_ptr(new std::mutex()); gf->whisper_ctx_mutex = std::unique_ptr(new std::mutex()); gf->wshiper_thread_cv = @@ -340,12 +338,11 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter) std::lock_guard lock(*gf->text_source_mutex); - obs_weak_source_t *text_source = gf->text_source; - if (!text_source) { + if (!gf->text_source) { obs_log(LOG_ERROR, "text_source is null"); return; } - auto target = obs_weak_source_get_source(text_source); + auto target = obs_weak_source_get_source(gf->text_source); if (!target) { obs_log(LOG_ERROR, "text_source target is null"); return; @@ -362,6 +359,8 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter) // start the thread gf->whisper_thread = std::thread(whisper_loop, gf); + gf->active = true; + return gf; }