update spk process

This commit is contained in:
misyaguziya
2023-06-20 09:01:31 +09:00
parent 2009b613ec
commit 92e6ed9205
3 changed files with 131 additions and 92 deletions

50
VRCT.py
View File

@@ -1,6 +1,7 @@
import os
import json
import threading
import queue
import time
import customtkinter
from PIL import Image
@@ -41,7 +42,7 @@ class App(customtkinter.CTk):
self.CHOICE_SPEAKER_DEVICE = None
self.INPUT_SPEAKER_VOICE_LANGUAGE = "en-US"
self.INPUT_SPEAKER_SAMPLING_RATE = 16000
self.INPUT_SPEAKER_INTERVAL = 3
self.INPUT_SPEAKER_INTERVAL = 4
self.INPUT_SPEAKER_BUFFER_SIZE = 4096
## Parameter
@@ -302,7 +303,9 @@ class App(customtkinter.CTk):
utils.print_textbox(self.textbox_message_system_log, "Auth Key or language setting is incorrect", "ERROR")
## set transcription instance
self.vr = transcription.VoiceRecognizer()
self.mic_queue = queue.Queue()
self.spk_queue = queue.Queue()
self.vr = transcription.VoiceRecognizer(self.mic_queue, self.spk_queue)
self.CHOICE_MIC_DEVICE = self.CHOICE_MIC_DEVICE if self.CHOICE_MIC_DEVICE is not None else self.vr.search_default_device()[0]
self.CHOICE_SPEAKER_DEVICE = self.CHOICE_SPEAKER_DEVICE if self.CHOICE_SPEAKER_DEVICE is not None else self.vr.search_default_device()[1]
@@ -383,6 +386,7 @@ class App(customtkinter.CTk):
device_name=self.CHOICE_MIC_DEVICE,
threshold=int(self.INPUT_MIC_THRESHOLD),
is_dynamic=self.INPUT_MIC_IS_DYNAMIC,
language=self.INPUT_MIC_VOICE_LANGUAGE,
)
self.vr.init_mic()
self.th_vr_listen_mic = utils.thread_fnc(self.vr_listen_mic)
@@ -404,21 +408,18 @@ class App(customtkinter.CTk):
if self.ENABLE_TRANSCRIPTION_RECEIVE is True:
utils.print_textbox(self.textbox_message_log, "Start speaker2log", "INFO")
utils.print_textbox(self.textbox_message_system_log, "Start speaker2log", "INFO")
# start threading
self.vr.set_spk(
device_name=self.CHOICE_SPEAKER_DEVICE,
sample_rate=int(self.INPUT_SPEAKER_SAMPLING_RATE),
interval=int(self.INPUT_SPEAKER_INTERVAL),
buffer_size=int(self.INPUT_SPEAKER_BUFFER_SIZE),
language=self.INPUT_SPEAKER_VOICE_LANGUAGE,
)
self.vr.init_spk()
self.th_vr_listen_spk = utils.thread_fnc(self.vr_listen_spk)
self.vr.start_spk_recording()
self.th_vr_recognize_spk = utils.thread_fnc(self.vr_recognize_spk)
self.th_vr_listen_spk.start()
self.th_vr_recognize_spk.start()
else:
if isinstance(self.th_vr_listen_spk, utils.thread_fnc):
self.th_vr_listen_spk.stop()
if self.vr.spk_stream is not None:
self.vr.close_spk_stream()
if isinstance(self.th_vr_recognize_spk, utils.thread_fnc):
self.th_vr_recognize_spk.stop()
@@ -430,7 +431,7 @@ class App(customtkinter.CTk):
self.vr.listen_mic()
def vr_recognize_mic(self):
message = self.vr.recognize_mic(language=self.INPUT_MIC_VOICE_LANGUAGE)
message = self.vr.recognize_mic()
if len(message) > 0:
# translate
if self.checkbox_translation.get() is False:
@@ -457,7 +458,7 @@ class App(customtkinter.CTk):
self.vr.listen_spk()
def vr_recognize_spk(self):
message = self.vr.recognize_spk(language=self.INPUT_SPEAKER_VOICE_LANGUAGE)
message = self.vr.recognize_spk()
if len(message) > 0:
# translate
if self.checkbox_translation.get() is False:
@@ -540,12 +541,23 @@ class App(customtkinter.CTk):
self.attributes("-topmost", True)
def delete_window(self):
thread_list = threading.enumerate()
thread_list.remove(threading.main_thread())
for thread in thread_list:
thread.stop()
if isinstance(self.th_vr_listen_mic, utils.thread_fnc):
while not self.th_vr_listen_mic.stopped():
self.th_vr_listen_mic.stop()
if isinstance(self.th_vr_recognize_mic, utils.thread_fnc):
while not self.th_vr_recognize_mic.stopped():
self.th_vr_recognize_mic.stop()
if self.vr.spk_stream is not None:
self.vr.close_spk_stream()
if isinstance(self.th_vr_recognize_spk, utils.thread_fnc):
while not self.th_vr_recognize_spk.stopped():
self.th_vr_recognize_spk.stop()
self.destroy()
if __name__ == "__main__":
app = App()
app.mainloop()
try:
app = App()
app.mainloop()
except Exception as e:
print(e)