remove lambda callback

This commit is contained in:
Roy Shilkrot 2023-08-23 08:34:34 +03:00
parent 60a3cf0364
commit de346a2897
3 changed files with 34 additions and 33 deletions

View File

@ -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 */

View File

@ -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<std::mutex> 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<std::mutex> 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);

View File

@ -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