diff --git a/src/transcription-filter-data.h b/src/transcription-filter-data.h index d238d6a..3df3e94 100644 --- a/src/transcription-filter-data.h +++ b/src/transcription-filter-data.h @@ -73,4 +73,6 @@ struct transcription_filter_audio_info { uint64_t timestamp; }; +void set_text_callback(struct transcription_filter_data *gf, const std::string &str); + #endif /* TRANSCRIPTION_FILTER_DATA_H */ diff --git a/src/transcription-filter.cpp b/src/transcription-filter.cpp index 7a63a4e..5770352 100644 --- a/src/transcription-filter.cpp +++ b/src/transcription-filter.cpp @@ -174,6 +174,35 @@ void acquire_weak_text_source_ref(struct transcription_filter_data *gf) } } +void set_text_callback(struct transcription_filter_data *gf, const std::string &str) +{ + if (!gf->text_source_mutex) { + obs_log(LOG_ERROR, "text_source_mutex is null"); + return; + } + + if (!gf->text_source) { + // attempt to acquire a weak ref to the text source if it's yet available + acquire_weak_text_source_ref(gf); + } + + std::lock_guard lock(*gf->text_source_mutex); + + if (!gf->text_source) { + obs_log(LOG_ERROR, "text_source is null"); + return; + } + auto target = obs_weak_source_get_source(gf->text_source); + if (!target) { + obs_log(LOG_ERROR, "text_source target is null"); + return; + } + auto text_settings = obs_source_get_settings(target); + obs_data_set_string(text_settings, "text", str.c_str()); + obs_source_update(target, text_settings); + obs_source_release(target); +}; + void transcription_filter_update(void *data, obs_data_t *s) { struct transcription_filter_data *gf = @@ -364,36 +393,6 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter) gf->text_source = nullptr; gf->text_source_name = nullptr; - obs_log(LOG_INFO, "transcription_filter: setup callback"); - // set the callback to set the text in the output text source (subtitles) - gf->setTextCallback = [gf](const std::string &str) { - if (!gf->text_source_mutex) { - obs_log(LOG_ERROR, "text_source_mutex is null"); - return; - } - - if (!gf->text_source) { - // attempt to acquire a weak ref to the text source if it's yet available - acquire_weak_text_source_ref(gf); - } - - std::lock_guard lock(*gf->text_source_mutex); - - if (!gf->text_source) { - obs_log(LOG_ERROR, "text_source is null"); - return; - } - auto target = obs_weak_source_get_source(gf->text_source); - if (!target) { - obs_log(LOG_ERROR, "text_source target is null"); - return; - } - auto text_settings = obs_source_get_settings(target); - obs_data_set_string(text_settings, "text", str.c_str()); - obs_source_update(target, text_settings); - obs_source_release(target); - }; - obs_log(LOG_INFO, "transcription_filter: run update"); // get the settings updated on the filter data struct transcription_filter_update(gf, settings); diff --git a/src/whisper-processing.cpp b/src/whisper-processing.cpp index bc3cda5..69bbfb8 100644 --- a/src/whisper-processing.cpp +++ b/src/whisper-processing.cpp @@ -254,16 +254,16 @@ void process_audio_from_buffer(struct transcription_filter_data *gf) if (inference_result.result == DETECTION_RESULT_SPEECH) { // output inference result to a text source - gf->setTextCallback(inference_result.text); + set_text_callback(gf, inference_result.text); } else if (inference_result.result == DETECTION_RESULT_SILENCE) { // output inference result to a text source - gf->setTextCallback("[silence]"); + set_text_callback(gf, "[silence]"); } } else { if (gf->log_words) { obs_log(LOG_INFO, "skipping inference"); } - gf->setTextCallback(""); + set_text_callback(gf, ""); } // end of timer