From 5566ebe401d8aa73fb4c39df631d6f68457fbed9 Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Mon, 6 May 2024 01:29:44 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B[bugfix]=20Model:=20mic/speaker?= =?UTF-8?q?=E3=81=AE=E9=9F=B3=E5=A3=B0=E5=85=A5=E5=8A=9B=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=AEThread=E3=81=AE=E5=87=A6=E7=90=86=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3(energy=E3=81=AF=E3=81=BE=E3=81=A0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model.py | 38 ++++++++++--------- .../transcription_transcriber.py | 5 +++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/model.py b/model.py index f30dd0bb..71c3d92f 100644 --- a/model.py +++ b/model.py @@ -36,19 +36,17 @@ class threadFnc(Thread): super(threadFnc, self).__init__(daemon=daemon, *args, **kwargs) self.fnc = fnc self.end_fnc = end_fnc - self._stop = Event() - def stop(self): - self._stop.set() - def stopped(self): - return self._stop.is_set() - def run(self): - while True: - if self.stopped(): - if callable(self.end_fnc): - self.end_fnc() - return - self.fnc(*self._args, **self._kwargs) + self.loop = True + def stop(self): + self.loop = False + + def run(self): + while self.loop: + self.fnc(*self._args, **self._kwargs) + if callable(self.end_fnc): + self.end_fnc() + return class Model: _instance = None @@ -392,9 +390,10 @@ class Model: ) def sendMicTranscript(): try: - self.mic_transcriber.transcribeAudioQueue(mic_audio_queue, config.SOURCE_LANGUAGE, config.SOURCE_COUNTRY) - message = self.mic_transcriber.getTranscript() - fnc(message) + res = self.mic_transcriber.transcribeAudioQueue(mic_audio_queue, config.SOURCE_LANGUAGE, config.SOURCE_COUNTRY) + if res: + message = self.mic_transcriber.getTranscript() + fnc(message) except Exception: pass @@ -425,6 +424,7 @@ class Model: def stopMicTranscript(self): if isinstance(self.mic_print_transcript, threadFnc): self.mic_print_transcript.stop() + self.mic_print_transcript.join() self.mic_print_transcript = None if isinstance(self.mic_audio_recorder, SelectedMicEnergyAndAudioRecorder): self.mic_audio_recorder.stop() @@ -505,9 +505,10 @@ class Model: ) def sendSpeakerTranscript(): try: - self.speaker_transcriber.transcribeAudioQueue(speaker_audio_queue, config.TARGET_LANGUAGE, config.TARGET_COUNTRY) - message = self.speaker_transcriber.getTranscript() - fnc(message) + res = self.speaker_transcriber.transcribeAudioQueue(speaker_audio_queue, config.TARGET_LANGUAGE, config.TARGET_COUNTRY) + if res: + message = self.speaker_transcriber.getTranscript() + fnc(message) except Exception: pass @@ -538,6 +539,7 @@ class Model: def stopSpeakerTranscript(self): if isinstance(self.speaker_print_transcript, threadFnc): self.speaker_print_transcript.stop() + self.speaker_print_transcript.join() self.speaker_print_transcript = None if isinstance(self.speaker_audio_recorder, SelectedSpeakerEnergyAndAudioRecorder): self.speaker_audio_recorder.stop() diff --git a/models/transcription/transcription_transcriber.py b/models/transcription/transcription_transcriber.py index c5a6cbff..aaaca210 100644 --- a/models/transcription/transcription_transcriber.py +++ b/models/transcription/transcription_transcriber.py @@ -1,3 +1,4 @@ +import time from io import BytesIO from threading import Event import wave @@ -38,6 +39,9 @@ class AudioTranscriber: self.transcription_engine = "Whisper" def transcribeAudioQueue(self, audio_queue, language, country): + if audio_queue.empty(): + time.sleep(0.1) + return False audio, time_spoken = audio_queue.get() self.updateLastSampleAndPhraseStatus(audio, time_spoken) @@ -75,6 +79,7 @@ class AudioTranscriber: if text != '': self.updateTranscript(text) + return True def updateLastSampleAndPhraseStatus(self, data, time_spoken): source_info = self.audio_sources