👍️[Update] Model : Deviceの変更監視処理を追加

マイク/スピーカーの別々のスレッドの処理を一つにまとめたら動いた
This commit is contained in:
misyaguziya
2024-09-14 03:09:19 +09:00
parent a40529aaa5
commit 551bc424b8
3 changed files with 48 additions and 62 deletions

View File

@@ -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:

View File

@@ -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)
model.startAutomaticDeviceSelection(mic_callback, speaker_callback)

View File

@@ -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",
}
}