From 6254a93b20285f6010de09432a7c0ac991f02e62 Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Fri, 13 Sep 2024 18:15:49 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20device?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E6=99=82=E3=81=ABenergy=E3=82=82=E5=90=8C?= =?UTF-8?q?=E6=9C=9F=E3=81=97=E3=81=A6=E5=A4=89=E6=9B=B4=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/model.py | 14 ++++++++++---- src-python/webui_controller.py | 12 +++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index b0363f25..7e37d14c 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -553,7 +553,10 @@ class Model: # self.mic_get_energy.stop() # self.mic_get_energy = None - def startCheckMicEnergy(self, fnc): + def startCheckMicEnergy(self, fnc:Callable[[float], None]=None) -> None: + if isinstance(fnc, Callable): + self.check_mic_energy_fnc = fnc + if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: default_device = getDefaultInputDevice() mic_host_name = default_device["host"]["name"] @@ -572,7 +575,7 @@ class Model: if mic_energy_queue.empty() is False: energy = mic_energy_queue.get() try: - fnc(energy) + self.check_mic_energy_fnc(energy) except Exception: pass sleep(0.01) @@ -684,7 +687,10 @@ class Model: # self.speaker_get_energy.stop() # self.speaker_get_energy = None - def startCheckSpeakerEnergy(self, fnc): + def startCheckSpeakerEnergy(self, fnc:Callable[[float], None]=None) -> None: + if isinstance(fnc, Callable): + self.check_speaker_energy_fnc = fnc + if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: default_device = getDefaultOutputDevice() speaker_device_name = default_device["device"]["name"] @@ -701,7 +707,7 @@ class Model: if speaker_energy_queue.empty() is False: energy = speaker_energy_queue.get() try: - fnc(energy) + self.check_speaker_energy_fnc(energy) except Exception: pass sleep(0.01) diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index f7c94c69..f2cfb2e4 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -793,7 +793,9 @@ def callbackSetMicHost(data, *args, **kwargs) -> dict: printLog("Set Mic Host", data) config.CHOICE_MIC_HOST = data config.CHOICE_MIC_DEVICE = model.getInputDefaultDevice() - model.stopCheckMicEnergy() + if config.ENABLE_CHECK_ENERGY_SEND is True: + model.stopCheckMicEnergy() + model.startCheckMicEnergy() return {"status":200, "result":{ "host":config.CHOICE_MIC_HOST, @@ -804,7 +806,9 @@ def callbackSetMicHost(data, *args, **kwargs) -> dict: def callbackSetMicDevice(data, *args, **kwargs) -> dict: printLog("Set Mic Device", data) config.CHOICE_MIC_DEVICE = data - model.stopCheckMicEnergy() + if config.ENABLE_CHECK_ENERGY_SEND is True: + model.stopCheckMicEnergy() + model.startCheckMicEnergy() return {"status":200, "result":config.CHOICE_MIC_DEVICE} def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict: @@ -940,7 +944,9 @@ def callbackDisableSpeakerAutomaticSelection(*args, **kwargs) -> dict: def callbackSetSpeakerDevice(data, *args, **kwargs) -> dict: printLog("Set Speaker Device", data) config.CHOICE_SPEAKER_DEVICE = data - model.stopCheckSpeakerEnergy() + if config.ENABLE_CHECK_ENERGY_RECEIVE is True: + model.stopCheckSpeakerEnergy() + model.startCheckSpeakerEnergy() return {"status":200, "result":config.CHOICE_SPEAKER_DEVICE} def callbackSetSpeakerEnergyThreshold(data, *args, **kwargs) -> dict: From a40529aaa525444c579996ea3f772cf4e36d9fde Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Sat, 14 Sep 2024 00:22:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=9A=A7[WIP/TEST]=20Model=20:=20Device?= =?UTF-8?q?=E3=81=AE=E5=A4=89=E6=9B=B4=E7=9B=A3=E8=A6=96=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit マイク/スピーカー両方とも自動選択ONの場合にUIが固まる --- src-python/model.py | 40 +++++++++++++++++++++++++++++++ src-python/webui_controller.py | 44 +++++++++++++++++++++++++++++++--- src-python/webui_mainloop.py | 8 +++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index 7e37d14c..d66da83e 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -422,6 +422,46 @@ class Model: def getListOutputDevice(): return [device["name"] for device in getOutputDevices()] + def startAutomaticMicSelection(self, fnc): + def checkMicDevice(fnc): + if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: + default_device = getDefaultInputDevice() + mic_host_name = default_device["host"]["name"] + mic_device_name = default_device["device"]["name"] + 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) + + 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) + + self.th_check_speaker_device = threadFnc(checkSpeakerDevice, args=(fnc,)) + self.th_check_speaker_device.daemon = True + self.th_check_speaker_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 startMicTranscript(self, fnc): if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: default_device = getDefaultInputDevice() diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index f2cfb2e4..673fb06c 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -778,14 +778,27 @@ def callbackClearDeeplAuthKey(*args, **kwargs) -> dict: # Transcription Tab # Transcription (Mic) +class UpdateMicDevice: + def __init__(self, action): + self.action = action -def callbackEnableMicAutomaticSelection(*args, **kwargs) -> dict: + def set(self, device) -> None: + printLog("Update Mic Device", device) + self.action("mic", { + "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) 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() config.ENABLE_MIC_AUTOMATIC_SELECTION = False return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION} @@ -930,14 +943,27 @@ 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 callbackEnableSpeakerAutomaticSelection(*args, **kwargs) -> dict: + 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} @@ -1386,4 +1412,16 @@ def init(endpoints:dict, *args, **kwargs) -> None: model.startReceiveOSC() if config.ENABLE_VRC_MIC_MUTE_SYNC is True: model.startCheckMuteSelfStatus() - printLog("End Initialization") \ No newline at end of file + printLog("End Initialization") + + # init Auto device selection + printLog("Init Auto Device Selection") + if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: + def callback(device): + printResponse(200, endpoints["check_mic_device"], device) + model.startAutomaticMicSelection(callback) + + if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: + def callback(device): + printResponse(200, endpoints["check_speaker_device"], device) + model.startAutomaticSpeakerSelection(callback) \ No newline at end of file diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 1e96b257..a0d473a7 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -240,6 +240,12 @@ action_mapping = { "/controller/callback_download_whisper_weight": { "download":"/action/download_whisper_weight" }, + "/controller/callback_enable_mic_automatic_selection": { + "mic":"/controller/callback_set_mic_device" + }, + "/controller/callback_enable_speaker_automatic_selection": { + "speaker":"/controller/callback_set_speaker_device" + } } def handleConfigRequest(endpoint): @@ -303,6 +309,8 @@ if __name__ == "__main__": controller.init({ "ctranslate2": action_mapping["/controller/callback_download_ctranslate2_weight"]["download"], "whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], + "check_mic_device": action_mapping["/controller/callback_enable_mic_automatic_selection"]["mic"], + "check_speaker_device": action_mapping["/controller/callback_enable_speaker_automatic_selection"]["speaker"], }) process = "main" From 551bc424b8ee4153f88d4d297238b2f674a9fe7e Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Sat, 14 Sep 2024 03:09:19 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Device=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", } } From 5deeb4da2d0aa074e3a62707bbd72a61a0255a08 Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Sun, 15 Sep 2024 17:25:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:?= =?UTF-8?q?=20Device=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=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - デバイス監視処理実行時にデバイス選択を行うとアクセス干渉のために動作停止の可能性あり --- src-python/model.py | 7 ++--- src-python/webui_controller.py | 32 ++++++++++------------ src-python/webui_mainloop.py | 50 +++++++++++++++++++--------------- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index 90ca6be0..7c5ebb16 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -430,16 +430,13 @@ class Model: mic_host_name = default_device["host"]["name"] mic_device_name = default_device["device"]["name"] 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_mic(config.CHOICE_MIC_DEVICE) + fnc_mic(mic_host_name, mic_device_name) 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_speaker(config.CHOICE_SPEAKER_DEVICE) + fnc_speaker(speaker_device_name) sleep(1) if isinstance(self.th_check_device, threadFnc) is False: diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 46ef5a91..9d766593 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -5,7 +5,7 @@ from threading import Thread import re from config import config from model import model -from utils import isUniqueStrings, printLog, printResponse +from utils import isUniqueStrings, printLog # Common class DownloadSoftwareProgressBar: @@ -782,14 +782,17 @@ class UpdateSelectedDevice: def __init__(self, action): self.action = action - def set_mic(self, device) -> None: - printLog("Update Mic Device", device) + def set_mic(self, host, device) -> None: + config.CHOICE_MIC_HOST = host + config.CHOICE_MIC_DEVICE = device + printLog("Update Host/Mic Device", f"{host}/{device}") self.action("mic", { "status":200, - "result":device + "result":{"host":host, "device":device} }) def set_speaker(self, device) -> None: + config.CHOICE_SPEAKER_DEVICE = device printLog("Update Speaker Device", device) self.action("speaker", { "status":200, @@ -842,7 +845,7 @@ def callbackSetMicDevice(data, *args, **kwargs) -> dict: if config.ENABLE_CHECK_ENERGY_SEND is True: model.stopCheckMicEnergy() model.startCheckMicEnergy() - return {"status":200, "result":config.CHOICE_MIC_DEVICE} + return {"status":200, "result":{"host":config.CHOICE_MIC_HOST, "device":config.CHOICE_MIC_DEVICE}} def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict: printLog("Set Mic Energy Threshold", data) @@ -1353,7 +1356,7 @@ def getListInputDevice(*args, **kwargs) -> dict: def getListOutputDevice(*args, **kwargs) -> dict: return {"status":200, "result": model.getListOutputDevice()} -def init(endpoints:dict, *args, **kwargs) -> None: +def init(actions:dict, *args, **kwargs) -> None: printLog("Start Initialization") printLog("Start check DeepL API Key") @@ -1371,9 +1374,8 @@ def init(endpoints:dict, *args, **kwargs) -> None: # check Downloaded CTranslate2 Model Weight printLog("Check Downloaded CTranslate2 Model Weight") if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False: - def callback(progress): - printResponse(200, endpoints["ctranslate2"], {"progress":progress}) - startThreadingDownloadCtranslate2Weight(callback) + download = DownloadCTranslate2ProgressBar(actions["download_ctranslate2"]) + startThreadingDownloadCtranslate2Weight(download.set) # set Transcription Engine printLog("Set Transcription Engine") @@ -1385,9 +1387,8 @@ def init(endpoints:dict, *args, **kwargs) -> None: # check Downloaded Whisper Model Weight printLog("Check Downloaded Whisper Model Weight") if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False: - def callback(progress): - printResponse(200, endpoints["whisper"], {"progress":progress}) - startThreadingDownloadWhisperWeight(callback) + download = DownloadWhisperProgressBar(actions["download_whisper"]) + startThreadingDownloadWhisperWeight(download.set) # set word filter printLog("Set Word Filter") @@ -1413,8 +1414,5 @@ def init(endpoints:dict, *args, **kwargs) -> None: # init Auto device selection printLog("Init Auto Device Selection") 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) - def speaker_callback(device): - printResponse(200, endpoints["check_speaker_device"], device) - model.startAutomaticDeviceSelection(mic_callback, speaker_callback) \ No newline at end of file + update_device = UpdateSelectedDevice(actions["update_selected_device"]) + model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) \ No newline at end of file diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 59a4e4a5..f4c4a4cd 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -241,11 +241,11 @@ action_mapping = { "download":"/action/download_whisper_weight" }, "/controller/callback_enable_mic_automatic_selection": { - "mic":"/controller/callback_set_mic_device", + "mic":"/controller/callback_set_mic_host", "speaker":"/controller/callback_set_speaker_device", }, "/controller/callback_enable_speaker_automatic_selection": { - "mic":"/controller/callback_set_mic_device", + "mic":"/controller/callback_set_mic_host", "speaker":"/controller/callback_set_speaker_device", } } @@ -267,12 +267,16 @@ def handleControllerRequest(endpoint, data=None): status = 404 else: action_endpoint = action_mapping.get(endpoint, None) - if action_endpoint is not None: - response = handler(data, Action(action_endpoint).transmit) - else: - response = handler(data) - status = response.get("status", None) - result = response.get("result", None) + try: + if action_endpoint is not None: + response = handler(data, Action(action_endpoint).transmit) + else: + response = handler(data) + status = response.get("status", None) + result = response.get("result", None) + except Exception as e: + result = str(e) + status = 500 return result, status class Action: @@ -280,9 +284,12 @@ class Action: self.endpoints = endpoints def transmit(self, key:str, data:dict) -> None: - status = data.get("status", None) - result = data.get("result", None) - printResponse(status, self.endpoints[key], result) + if key not in self.endpoints: + printLog("Invalid endpoint", key) + else: + status = data.get("status", None) + result = data.get("result", None) + printResponse(status, self.endpoints[key], result) def main(): received_data = sys.stdin.readline().strip() @@ -292,7 +299,7 @@ def main(): endpoint = received_data.get("endpoint", None) data = received_data.get("data", None) data = encodeBase64(data) if data is not None else None - printLog(endpoint, data) + printLog(endpoint, {"receive_data":data}) try: match endpoint.split("/")[1]: @@ -305,14 +312,14 @@ def main(): except Exception as e: result = str(e) status = 500 + printLog(endpoint, {"send_data":result}) printResponse(status, endpoint, result) if __name__ == "__main__": controller.init({ - "ctranslate2": action_mapping["/controller/callback_download_ctranslate2_weight"]["download"], - "whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], - "check_mic_device": action_mapping["/controller/callback_enable_mic_automatic_selection"]["mic"], - "check_speaker_device": action_mapping["/controller/callback_enable_speaker_automatic_selection"]["speaker"], + "download_ctranslate2": Action(action_mapping["/controller/callback_download_ctranslate2_weight"]).transmit, + "download_whisper": Action(action_mapping["/controller/callback_download_whisper_weight"]).transmit, + "update_selected_device": Action(action_mapping["/controller/callback_enable_mic_automatic_selection"]).transmit, }) process = "main" @@ -327,12 +334,11 @@ if __name__ == "__main__": traceback.print_exc(file=f) case "test": - endpoint = "/controller/callback_download_ctranslate2_weight" - result, status = handleControllerRequest(endpoint) - printResponse(status, endpoint, result) - endpoint = "/controller/callback_download_whisper_weight" - result, status = handleControllerRequest(endpoint) - printResponse(status, endpoint, result) + for _ in range(100): + time.sleep(0.5) + endpoint = "/controller/list_mic_host" + result, status = handleControllerRequest(endpoint) + printResponse(status, endpoint, result) case "test_all": import time