Merge branch 'change_device_with_energy' into for_webui

This commit is contained in:
misyaguziya
2024-09-15 17:25:18 +09:00
3 changed files with 132 additions and 47 deletions

View File

@@ -69,6 +69,7 @@ class Model:
def init(self): def init(self):
self.logger = None self.logger = None
self.th_check_device = None
self.mic_print_transcript = None self.mic_print_transcript = None
self.mic_audio_recorder = None self.mic_audio_recorder = None
self.mic_energy_recorder = None self.mic_energy_recorder = None
@@ -422,6 +423,33 @@ class Model:
def getListOutputDevice(): def getListOutputDevice():
return [device["name"] for device in getOutputDevices()] return [device["name"] for device in getOutputDevices()]
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"]
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 = 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): def startMicTranscript(self, fnc):
if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True:
default_device = getDefaultInputDevice() default_device = getDefaultInputDevice()
@@ -553,7 +581,10 @@ class Model:
# self.mic_get_energy.stop() # self.mic_get_energy.stop()
# self.mic_get_energy = None # 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: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True:
default_device = getDefaultInputDevice() default_device = getDefaultInputDevice()
mic_host_name = default_device["host"]["name"] mic_host_name = default_device["host"]["name"]
@@ -572,7 +603,7 @@ class Model:
if mic_energy_queue.empty() is False: if mic_energy_queue.empty() is False:
energy = mic_energy_queue.get() energy = mic_energy_queue.get()
try: try:
fnc(energy) self.check_mic_energy_fnc(energy)
except Exception: except Exception:
pass pass
sleep(0.01) sleep(0.01)
@@ -684,7 +715,10 @@ class Model:
# self.speaker_get_energy.stop() # self.speaker_get_energy.stop()
# self.speaker_get_energy = None # 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: if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
default_device = getDefaultOutputDevice() default_device = getDefaultOutputDevice()
speaker_device_name = default_device["device"]["name"] speaker_device_name = default_device["device"]["name"]
@@ -701,7 +735,7 @@ class Model:
if speaker_energy_queue.empty() is False: if speaker_energy_queue.empty() is False:
energy = speaker_energy_queue.get() energy = speaker_energy_queue.get()
try: try:
fnc(energy) self.check_speaker_energy_fnc(energy)
except Exception: except Exception:
pass pass
sleep(0.01) sleep(0.01)

View File

@@ -5,7 +5,7 @@ from threading import Thread
import re import re
from config import config from config import config
from model import model from model import model
from utils import isUniqueStrings, printLog, printResponse from utils import isUniqueStrings, printLog
# Common # Common
class DownloadSoftwareProgressBar: class DownloadSoftwareProgressBar:
@@ -778,22 +778,60 @@ def callbackClearDeeplAuthKey(*args, **kwargs) -> dict:
# Transcription Tab # Transcription Tab
# Transcription (Mic) # Transcription (Mic)
class UpdateSelectedDevice:
def __init__(self, action):
self.action = action
def callbackEnableMicAutomaticSelection(*args, **kwargs) -> dict: 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":{"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,
"result":device
})
def callbackEnableMicAutomaticSelection(data, action, *args, **kwargs) -> dict:
printLog("Enable Mic Automatic Selection") printLog("Enable Mic Automatic Selection")
update_device = UpdateSelectedDevice(action)
model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker)
config.ENABLE_MIC_AUTOMATIC_SELECTION = True config.ENABLE_MIC_AUTOMATIC_SELECTION = True
return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION} return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION}
def callbackDisableMicAutomaticSelection(*args, **kwargs) -> dict: def callbackDisableMicAutomaticSelection(*args, **kwargs) -> dict:
printLog("Disable Mic Automatic Selection") printLog("Disable Mic Automatic Selection")
model.stopAutomaticDeviceSelection()
config.ENABLE_MIC_AUTOMATIC_SELECTION = False config.ENABLE_MIC_AUTOMATIC_SELECTION = False
return {"status":200, "result":config.ENABLE_MIC_AUTOMATIC_SELECTION} 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: def callbackSetMicHost(data, *args, **kwargs) -> dict:
printLog("Set Mic Host", data) printLog("Set Mic Host", data)
config.CHOICE_MIC_HOST = data config.CHOICE_MIC_HOST = data
config.CHOICE_MIC_DEVICE = model.getInputDefaultDevice() config.CHOICE_MIC_DEVICE = model.getInputDefaultDevice()
model.stopCheckMicEnergy() if config.ENABLE_CHECK_ENERGY_SEND is True:
model.stopCheckMicEnergy()
model.startCheckMicEnergy()
return {"status":200, return {"status":200,
"result":{ "result":{
"host":config.CHOICE_MIC_HOST, "host":config.CHOICE_MIC_HOST,
@@ -804,8 +842,10 @@ def callbackSetMicHost(data, *args, **kwargs) -> dict:
def callbackSetMicDevice(data, *args, **kwargs) -> dict: def callbackSetMicDevice(data, *args, **kwargs) -> dict:
printLog("Set Mic Device", data) printLog("Set Mic Device", data)
config.CHOICE_MIC_DEVICE = data config.CHOICE_MIC_DEVICE = data
model.stopCheckMicEnergy() if config.ENABLE_CHECK_ENERGY_SEND is True:
return {"status":200, "result":config.CHOICE_MIC_DEVICE} model.stopCheckMicEnergy()
model.startCheckMicEnergy()
return {"status":200, "result":{"host":config.CHOICE_MIC_HOST, "device":config.CHOICE_MIC_DEVICE}}
def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict: def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict:
printLog("Set Mic Energy Threshold", data) printLog("Set Mic Energy Threshold", data)
@@ -926,21 +966,12 @@ def callbackDeleteMicWordFilter(data, *args, **kwargs) -> dict:
return {"status":200, "result":config.INPUT_MIC_WORD_FILTER} return {"status":200, "result":config.INPUT_MIC_WORD_FILTER}
# Transcription (Speaker) # Transcription (Speaker)
def callbackEnableSpeakerAutomaticSelection(*args, **kwargs) -> dict:
printLog("Enable Speaker Automatic Selection")
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")
config.ENABLE_SPEAKER_AUTOMATIC_SELECTION = False
return {"status":200, "result":config.ENABLE_SPEAKER_AUTOMATIC_SELECTION}
def callbackSetSpeakerDevice(data, *args, **kwargs) -> dict: def callbackSetSpeakerDevice(data, *args, **kwargs) -> dict:
printLog("Set Speaker Device", data) printLog("Set Speaker Device", data)
config.CHOICE_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} return {"status":200, "result":config.CHOICE_SPEAKER_DEVICE}
def callbackSetSpeakerEnergyThreshold(data, *args, **kwargs) -> dict: def callbackSetSpeakerEnergyThreshold(data, *args, **kwargs) -> dict:
@@ -1325,7 +1356,7 @@ def getListInputDevice(*args, **kwargs) -> dict:
def getListOutputDevice(*args, **kwargs) -> dict: def getListOutputDevice(*args, **kwargs) -> dict:
return {"status":200, "result": model.getListOutputDevice()} 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 Initialization")
printLog("Start check DeepL API Key") printLog("Start check DeepL API Key")
@@ -1343,9 +1374,8 @@ def init(endpoints:dict, *args, **kwargs) -> None:
# check Downloaded CTranslate2 Model Weight # check Downloaded CTranslate2 Model Weight
printLog("Check Downloaded CTranslate2 Model Weight") printLog("Check Downloaded CTranslate2 Model Weight")
if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False: if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False:
def callback(progress): download = DownloadCTranslate2ProgressBar(actions["download_ctranslate2"])
printResponse(200, endpoints["ctranslate2"], {"progress":progress}) startThreadingDownloadCtranslate2Weight(download.set)
startThreadingDownloadCtranslate2Weight(callback)
# set Transcription Engine # set Transcription Engine
printLog("Set Transcription Engine") printLog("Set Transcription Engine")
@@ -1357,9 +1387,8 @@ def init(endpoints:dict, *args, **kwargs) -> None:
# check Downloaded Whisper Model Weight # check Downloaded Whisper Model Weight
printLog("Check Downloaded Whisper Model Weight") printLog("Check Downloaded Whisper Model Weight")
if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False: if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False:
def callback(progress): download = DownloadWhisperProgressBar(actions["download_whisper"])
printResponse(200, endpoints["whisper"], {"progress":progress}) startThreadingDownloadWhisperWeight(download.set)
startThreadingDownloadWhisperWeight(callback)
# set word filter # set word filter
printLog("Set Word Filter") printLog("Set Word Filter")
@@ -1380,4 +1409,10 @@ def init(endpoints:dict, *args, **kwargs) -> None:
model.startReceiveOSC() model.startReceiveOSC()
if config.ENABLE_VRC_MIC_MUTE_SYNC is True: if config.ENABLE_VRC_MIC_MUTE_SYNC is True:
model.startCheckMuteSelfStatus() model.startCheckMuteSelfStatus()
printLog("End Initialization") printLog("End Initialization")
# 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)

View File

@@ -240,6 +240,14 @@ action_mapping = {
"/controller/callback_download_whisper_weight": { "/controller/callback_download_whisper_weight": {
"download":"/action/download_whisper_weight" "download":"/action/download_whisper_weight"
}, },
"/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",
}
} }
def handleConfigRequest(endpoint): def handleConfigRequest(endpoint):
@@ -259,12 +267,16 @@ def handleControllerRequest(endpoint, data=None):
status = 404 status = 404
else: else:
action_endpoint = action_mapping.get(endpoint, None) action_endpoint = action_mapping.get(endpoint, None)
if action_endpoint is not None: try:
response = handler(data, Action(action_endpoint).transmit) if action_endpoint is not None:
else: response = handler(data, Action(action_endpoint).transmit)
response = handler(data) else:
status = response.get("status", None) response = handler(data)
result = response.get("result", None) status = response.get("status", None)
result = response.get("result", None)
except Exception as e:
result = str(e)
status = 500
return result, status return result, status
class Action: class Action:
@@ -272,9 +284,12 @@ class Action:
self.endpoints = endpoints self.endpoints = endpoints
def transmit(self, key:str, data:dict) -> None: def transmit(self, key:str, data:dict) -> None:
status = data.get("status", None) if key not in self.endpoints:
result = data.get("result", None) printLog("Invalid endpoint", key)
printResponse(status, self.endpoints[key], result) else:
status = data.get("status", None)
result = data.get("result", None)
printResponse(status, self.endpoints[key], result)
def main(): def main():
received_data = sys.stdin.readline().strip() received_data = sys.stdin.readline().strip()
@@ -284,7 +299,7 @@ def main():
endpoint = received_data.get("endpoint", None) endpoint = received_data.get("endpoint", None)
data = received_data.get("data", None) data = received_data.get("data", None)
data = encodeBase64(data) if data is not None else None data = encodeBase64(data) if data is not None else None
printLog(endpoint, data) printLog(endpoint, {"receive_data":data})
try: try:
match endpoint.split("/")[1]: match endpoint.split("/")[1]:
@@ -297,12 +312,14 @@ def main():
except Exception as e: except Exception as e:
result = str(e) result = str(e)
status = 500 status = 500
printLog(endpoint, {"send_data":result})
printResponse(status, endpoint, result) printResponse(status, endpoint, result)
if __name__ == "__main__": if __name__ == "__main__":
controller.init({ controller.init({
"ctranslate2": action_mapping["/controller/callback_download_ctranslate2_weight"]["download"], "download_ctranslate2": Action(action_mapping["/controller/callback_download_ctranslate2_weight"]).transmit,
"whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], "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" process = "main"
@@ -317,12 +334,11 @@ if __name__ == "__main__":
traceback.print_exc(file=f) traceback.print_exc(file=f)
case "test": case "test":
endpoint = "/controller/callback_download_ctranslate2_weight" for _ in range(100):
result, status = handleControllerRequest(endpoint) time.sleep(0.5)
printResponse(status, endpoint, result) endpoint = "/controller/list_mic_host"
endpoint = "/controller/callback_download_whisper_weight" result, status = handleControllerRequest(endpoint)
result, status = handleControllerRequest(endpoint) printResponse(status, endpoint, result)
printResponse(status, endpoint, result)
case "test_all": case "test_all":
import time import time