From c6b7e9c331375bcd28040ea048959fdcfd639dbe Mon Sep 17 00:00:00 2001 From: abb128 <65567823+abb128@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:47:30 +0300 Subject: [PATCH] Clear final lines after 6 seconds of silence --- src/asrproc.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/asrproc.c b/src/asrproc.c index 5bbd476..5548f38 100644 --- a/src/asrproc.c +++ b/src/asrproc.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -59,13 +60,35 @@ struct asr_thread_i { volatile bool text_stream_active; volatile bool pause; + volatile time_t last_silence_time; + + volatile bool ending; + bool errored; }; -static void *run_asr_thread(void *userdata) { +static gboolean main_thread_update_label(void *userdata); + +static void *run_asr_thread(void *userdata){ asr_thread data = (asr_thread)userdata; + while(!data->ending){ + sleep(1); + + if(data->last_silence_time == 0) continue; + + time_t current_time = time(NULL); + + if(difftime(current_time, data->last_silence_time) >= 6.0) { + g_mutex_lock(&data->text_mutex); + data->last_silence_time = 0; + for(int i=1; iline); + g_mutex_unlock(&data->text_mutex); + g_idle_add(main_thread_update_label, data); + } + } + //sleep(40); //if(data->sound_counter < 512) { @@ -102,6 +125,7 @@ static void april_result_handler(void* userdata, AprilResultType result, size_t case APRIL_RESULT_RECOGNITION_FINAL: { g_mutex_lock(&data->text_mutex); + data->last_silence_time = 0; if((data->layout_counter != data->window->font_layout_counter) || (data->line.layout == NULL)) { if(data->line.layout != NULL) g_object_unref(data->line.layout); @@ -130,6 +154,7 @@ static void april_result_handler(void* userdata, AprilResultType result, size_t case APRIL_RESULT_SILENCE: { g_mutex_lock(&data->text_mutex); + data->last_silence_time = time(NULL); line_generator_break(&data->line); save_silence_to_history(); @@ -273,6 +298,7 @@ bool asr_thread_update_model(asr_thread data, const char *model_path) { data->session = new_session; data->errored = false; + data->ending = false; data->pause = false; line_generator_finalize(&data->line); @@ -296,6 +322,8 @@ void asr_thread_flush(asr_thread thread) { } void free_asr_thread(asr_thread thread) { + thread->ending = true; + g_mutex_lock(&thread->text_mutex); g_thread_join(thread->thread_id);