From b448b710da4d08b22da737dbb33b3632637b5d43 Mon Sep 17 00:00:00 2001 From: Papoteur Date: Sun, 24 Oct 2021 12:58:10 +0200 Subject: [PATCH] Avoid waiting state when data are present. Reduce size of chunks from recorder to 1024. This improves responsiveness and allows to have smaller chunks from recorder --- nerd-dictation | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/nerd-dictation b/nerd-dictation index 62d64ac..8b0ee2e 100755 --- a/nerd-dictation +++ b/nerd-dictation @@ -96,7 +96,7 @@ def file_remove_if_exists(filepath: str) -> bool: def enqueue_output(vosk_out, queue): - for block in iter(partial(vosk_out.read, 16384), b""): + for block in iter(partial(vosk_out.read, 1024), b""): queue.put(block) @@ -644,22 +644,21 @@ def text_from_vosk_pipe( # -1=cancel, 0=continue, 1=finish. code = exit_fn(handled_any) - if idle_time > 0.0: - # Subtract processing time from the previous loop. - # Skip idling in the event dictation can't keep up with the recording. - idle_time_curr = time.time() - idle_time_test = idle_time - (idle_time_curr - idle_time_prev) - if idle_time_test > 0.0: - # Prevents excessive processor load. - time.sleep(idle_time_test) - idle_time_prev = time.time() - else: - idle_time_prev = idle_time_curr try: data = vosk_queue.get_nowait() except Empty: - pass + if idle_time > 0.0: + # Subtract processing time from the previous loop. + # Skip idling in the event dictation can't keep up with the recording. + idle_time_curr = time.time() + idle_time_test = idle_time - (idle_time_curr - idle_time_prev) + if idle_time_test > 0.0: + # Prevents excessive processor load. + time.sleep(idle_time_test) + idle_time_prev = time.time() + else: + idle_time_prev = idle_time_curr else: ok = rec.AcceptWaveform(data) if ok: