From aa30c248d5326c6416f158f571b01d5aaf4b9383 Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Wed, 18 Sep 2024 17:38:32 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model:=20ENABLE?= =?UTF-8?q?=5FMIC=5FAUTOMATIC=5FSELECTION/ENABLE=5FSPEAKER=5FAUTOMATIC=5FS?= =?UTF-8?q?ELECTION=E3=81=AE=E5=87=A6=E7=90=86=E3=82=92device=5Fmanager?= =?UTF-8?q?=E3=81=AEcallback=E3=81=A7=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=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 | 27 --------------- .../transcription/transcription_utils.py | 20 +++++++++++ src-python/webui_controller.py | 33 ++++++++++++------- src-python/webui_mainloop.py | 5 ++- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src-python/model.py b/src-python/model.py index 02aeddc8..73f11a52 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -423,33 +423,6 @@ class Model: result = [device["name"] for device in device_manager.getOutputDevices()] return result - def startAutomaticDeviceSelection(self, fnc_mic, fnc_speaker): - def checkDevice(fnc_mic, fnc_speaker): - if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: - default_device = device_manager.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: - fnc_mic(mic_host_name, mic_device_name) - - if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: - default_device = device_manager.getDefaultOutputDevice() - speaker_device_name = default_device["device"]["name"] - if speaker_device_name != config.CHOICE_SPEAKER_DEVICE: - fnc_speaker(speaker_device_name) - sleep(1) - - 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 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: default_device = device_manager.getDefaultInputDevice() diff --git a/src-python/models/transcription/transcription_utils.py b/src-python/models/transcription/transcription_utils.py index 0d0854e5..2b51ec31 100644 --- a/src-python/models/transcription/transcription_utils.py +++ b/src-python/models/transcription/transcription_utils.py @@ -35,6 +35,9 @@ class DeviceManager: self.default_output_device = {"device": {"name": "NoDevice"}} self.update() + self.callback_default_input_device = None + self.callback_default_output_device = None + self.monitoring_flag = False self.startMonitoring() @@ -123,6 +126,11 @@ class DeviceManager: enumerator.UnregisterEndpointNotificationCallback(cb) self.update() + if self.callback_default_input_device is not None: + self.callback_default_input_device(self.default_input_device["host"]["name"], self.default_input_device["device"]["name"]) + if self.callback_default_output_device is not None: + self.callback_default_output_device(self.default_output_device["device"]["name"]) + cb = Client() enumerator = AudioUtilities.GetDeviceEnumerator() enumerator.RegisterEndpointNotificationCallback(cb) @@ -140,6 +148,18 @@ class DeviceManager: self.monitoring_flag = False self.th_monitoring.join() + def setCallbackDefaultInputDevice(self, callback): + self.callback_default_input_device = callback + + def clearCallbackDefaultInputDevice(self): + self.callback_default_input_device = None + + def setCallbackDefaultOutputDevice(self, callback): + self.callback_default_output_device = callback + + def clearCallbackDefaultOutputDevice(self): + self.callback_default_output_device = None + def getInputDevices(self): return self.input_devices diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 722130e9..b78b92e5 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -6,6 +6,7 @@ import re from config import config from model import model from utils import isUniqueStrings, printLog +from models.transcription.transcription_utils import device_manager # Common class DownloadSoftwareProgressBar: @@ -778,11 +779,11 @@ def callbackClearDeeplAuthKey(*args, **kwargs) -> dict: # Transcription Tab # Transcription (Mic) -class UpdateSelectedDevice: +class UpdateSelectedMicDevice: def __init__(self, action): self.action = action - def set_mic(self, host, device) -> None: + def set(self, host, device) -> None: config.CHOICE_MIC_HOST = host config.CHOICE_MIC_DEVICE = device printLog("Update Host/Mic Device", f"{host}/{device}") @@ -791,7 +792,11 @@ class UpdateSelectedDevice: "result":{"host":host, "device":device} }) - def set_speaker(self, device) -> None: +class UpdateSelectedSpeakerDevice: + def __init__(self, action): + self.action = action + + def set(self, device) -> None: config.CHOICE_SPEAKER_DEVICE = device printLog("Update Speaker Device", device) self.action("speaker", { @@ -801,27 +806,27 @@ class UpdateSelectedDevice: def callbackEnableMicAutomaticSelection(data, action, *args, **kwargs) -> dict: printLog("Enable Mic Automatic Selection") - update_device = UpdateSelectedDevice(action) - model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) + update_device = UpdateSelectedMicDevice(action) + device_manager.setCallbackDefaultInputDevice(update_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.stopAutomaticDeviceSelection() + device_manager.clearCallbackDefaultInputDevice() 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) + update_device = UpdateSelectedSpeakerDevice(action) + device_manager.setCallbackDefaultOutputDevice(update_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.stopAutomaticDeviceSelection() + device_manager.clearCallbackDefaultInputDevice() config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = False return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION} @@ -1412,8 +1417,12 @@ def init(actions: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: - update_device = UpdateSelectedDevice(actions["update_selected_device"]) - model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) + if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: + update_mic_device = UpdateSelectedMicDevice(actions["update_selected_mic_device"]) + device_manager.setCallbackDefaultInputDevice(update_mic_device.set) + + if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: + update_speaker_device = UpdateSelectedSpeakerDevice(actions["update_selected_speaker_device"]) + device_manager.setCallbackDefaultOutputDevice(update_speaker_device.set) printLog("End Initialization") \ No newline at end of file diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index aac62345..0e4b11f3 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -244,10 +244,8 @@ action_mapping = { }, "/controller/callback_enable_mic_automatic_selection": { "mic":"/controller/callback_set_mic_host", - "speaker":"/controller/callback_set_speaker_device", }, "/controller/callback_enable_speaker_automatic_selection": { - "mic":"/controller/callback_set_mic_host", "speaker":"/controller/callback_set_speaker_device", } } @@ -391,7 +389,8 @@ if __name__ == "__main__": controller.init({ "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, + "update_selected_mic_device": Action(action_mapping["/controller/callback_enable_mic_automatic_selection"]).transmit, + "update_selected_speaker_device": Action(action_mapping["/controller/callback_enable_speaker_automatic_selection"]).transmit, }) # mappingのすべてのstatusをTrueにする