From 551bc424b8ee4153f88d4d297238b2f674a9fe7e Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Sat, 14 Sep 2024 03:09:19 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:=20Dev?= =?UTF-8?q?ice=E3=81=AE=E5=A4=89=E6=9B=B4=E7=9B=A3=E8=A6=96=E5=87=A6?= =?UTF-8?q?=E7=90=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit マイク/スピーカーの別々のスレッドの処理を一つにまとめたら動いた --- src-python/model.py | 39 ++++++++------------ src-python/webui_controller.py | 65 +++++++++++++++------------------- src-python/webui_mainloop.py | 6 ++-- 3 files changed, 48 insertions(+), 62 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index d66da83e..90ca6be0 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -69,6 +69,7 @@ class Model: def init(self): self.logger = None + self.th_check_device = None self.mic_print_transcript = None self.mic_audio_recorder = None self.mic_energy_recorder = None @@ -422,8 +423,8 @@ class Model: def getListOutputDevice(): return [device["name"] for device in getOutputDevices()] - def startAutomaticMicSelection(self, fnc): - def checkMicDevice(fnc): + def startAutomaticDeviceSelection(self, fnc_mic, fnc_speaker): + def checkDevice(fnc_mic, fnc_speaker): if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: default_device = getDefaultInputDevice() mic_host_name = default_device["host"]["name"] @@ -431,36 +432,26 @@ class Model: if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE: config.CHOICE_MIC_HOST = mic_host_name config.CHOICE_MIC_DEVICE = mic_device_name - fnc(config.CHOICE_MIC_DEVICE) - sleep(2) + fnc_mic(config.CHOICE_MIC_DEVICE) - self.th_check_mic_device = threadFnc(checkMicDevice, args=(fnc,)) - self.th_check_mic_device.daemon = True - self.th_check_mic_device.start() - - def stopAutomaticMicSelection(self): - if isinstance(self.th_check_mic_device, threadFnc): - self.th_check_mic_device.stop() - self.th_check_mic_device = None - - def startAutomaticSpeakerSelection(self, fnc): - def checkSpeakerDevice(fnc): if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: default_device = getDefaultOutputDevice() speaker_device_name = default_device["device"]["name"] if speaker_device_name != config.CHOICE_SPEAKER_DEVICE: config.CHOICE_SPEAKER_DEVICE = speaker_device_name - fnc(config.CHOICE_SPEAKER_DEVICE) - sleep(2) + fnc_speaker(config.CHOICE_SPEAKER_DEVICE) + sleep(1) - self.th_check_speaker_device = threadFnc(checkSpeakerDevice, args=(fnc,)) - self.th_check_speaker_device.daemon = True - self.th_check_speaker_device.start() + if isinstance(self.th_check_device, threadFnc) is False: + self.th_check_device = threadFnc(checkDevice, args=(fnc_mic, fnc_speaker,)) + self.th_check_device.daemon = True + self.th_check_device.start() - def stopAutomaticSpeakerSelection(self): - if isinstance(self.th_check_speaker_device, threadFnc): - self.th_check_speaker_device.stop() - self.th_check_speaker_device = None + def stopAutomaticDeviceSelection(self): + if config.ENABLE_MIC_AUTOMATIC_SELECTION is False and config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is False: + if isinstance(self.th_check_device, threadFnc): + self.th_check_device.stop() + self.th_check_device = None def startMicTranscript(self, fnc): if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 673fb06c..46ef5a91 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -778,30 +778,50 @@ def callbackClearDeeplAuthKey(*args, **kwargs) -> dict: # Transcription Tab # Transcription (Mic) -class UpdateMicDevice: +class UpdateSelectedDevice: def __init__(self, action): self.action = action - def set(self, device) -> None: + def set_mic(self, device) -> None: printLog("Update Mic Device", device) self.action("mic", { "status":200, "result":device }) + def set_speaker(self, device) -> None: + printLog("Update Speaker Device", device) + self.action("speaker", { + "status":200, + "result":device + }) + def callbackEnableMicAutomaticSelection(data, action, *args, **kwargs) -> dict: printLog("Enable Mic Automatic Selection") - update_mic_device = UpdateMicDevice(action) - model.startAutomaticMicSelection(update_mic_device.set) + update_device = UpdateSelectedDevice(action) + model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) config.ENABLE_MIC_AUTOMATIC_SELECTION = True return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION} def callbackDisableMicAutomaticSelection(*args, **kwargs) -> dict: printLog("Disable Mic Automatic Selection") - model.stopAutomaticMicSelection() + model.stopAutomaticDeviceSelection() config.ENABLE_MIC_AUTOMATIC_SELECTION = False return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION} +def callbackEnableSpeakerAutomaticSelection(data, action, *args, **kwargs) -> dict: + printLog("Enable Speaker Automatic Selection") + update_device = UpdateSelectedDevice(action) + model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) + config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = True + return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION} + +def callbackDisableSpeakerAutomaticSelection(*args, **kwargs) -> dict: + printLog("Disable Speaker Automatic Selection") + model.stopAutomaticDeviceSelection() + config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = False + return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION} + def callbackSetMicHost(data, *args, **kwargs) -> dict: printLog("Set Mic Host", data) config.CHOICE_MIC_HOST = data @@ -943,30 +963,6 @@ def callbackDeleteMicWordFilter(data, *args, **kwargs) -> dict: return {"status":200, "result":config.INPUT_MIC_WORD_FILTER} # Transcription (Speaker) -class UpdateSpeakerDevice: - def __init__(self, action): - self.action = action - - def set(self, device) -> None: - printLog("Update Speaker Device", device) - self.action("speaker", { - "status":200, - "result":device - }) - -def callbackEnableSpeakerAutomaticSelection(data, action, *args, **kwargs) -> dict: - printLog("Enable Speaker Automatic Selection") - update_speaker_device = UpdateSpeakerDevice(action) - model.startAutomaticSpeakerSelection(update_speaker_device.set) - config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = True - return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION} - -def callbackDisableSpeakerAutomaticSelection(*args, **kwargs) -> dict: - printLog("Disable Speaker Automatic Selection") - model.stopAutomaticSpeakerSelection() - config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = False - return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION} - def callbackSetSpeakerDevice(data, *args, **kwargs) -> dict: printLog("Set Speaker Device", data) config.CHOICE_SPEAKER_DEVICE = data @@ -1416,12 +1412,9 @@ def init(endpoints:dict, *args, **kwargs) -> None: # init Auto device selection printLog("Init Auto Device Selection") - if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: - def callback(device): + if config.ENABLE_MIC_AUTOMATIC_SELECTION is True or config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: + def mic_callback(device): printResponse(200, endpoints["check_mic_device"], device) - model.startAutomaticMicSelection(callback) - - if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: - def callback(device): + def speaker_callback(device): printResponse(200, endpoints["check_speaker_device"], device) - model.startAutomaticSpeakerSelection(callback) \ No newline at end of file + model.startAutomaticDeviceSelection(mic_callback, speaker_callback) \ No newline at end of file diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index a0d473a7..59a4e4a5 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -241,10 +241,12 @@ action_mapping = { "download":"/action/download_whisper_weight" }, "/controller/callback_enable_mic_automatic_selection": { - "mic":"/controller/callback_set_mic_device" + "mic":"/controller/callback_set_mic_device", + "speaker":"/controller/callback_set_speaker_device", }, "/controller/callback_enable_speaker_automatic_selection": { - "speaker":"/controller/callback_set_speaker_device" + "mic":"/controller/callback_set_mic_device", + "speaker":"/controller/callback_set_speaker_device", } }