diff --git a/config.py b/config.py index b0fdacc5..030da6bb 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,4 @@ import sys -from json import load, dump import inspect from os import path as os_path from json import load as json_load @@ -7,9 +6,8 @@ from json import dump as json_dump import tkinter as tk from tkinter import font from languages import selectable_languages -from models.translation.translation_languages import translatorEngine, translation_lang -from models.transcription.transcription_languages import transcription_lang -from models.transcription.transcription_utils import getInputDevices, getOutputDevices, getDefaultInputDevice, getDefaultOutputDevice +from models.translation.translation_languages import translatorEngine +from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice, getDefaultOutputDevice json_serializable_vars = {} def json_serializable(var_name): @@ -20,7 +18,7 @@ def json_serializable(var_name): def saveJson(path, key, value): with open(path, "r", encoding="utf-8") as fp: - json_data = load(fp) + json_data = json_load(fp) json_data[key] = value with open(path, "w", encoding="utf-8") as fp: json_dump(json_data, fp, indent=4, ensure_ascii=False) @@ -346,11 +344,9 @@ class Config: @CHOICE_SPEAKER_DEVICE.setter def CHOICE_SPEAKER_DEVICE(self, value): - if value in [device["name"] for device in getOutputDevices()]: - speaker_device = [device for device in getOutputDevices() if device["name"] == value][0] - if getDefaultOutputDevice()["index"] == speaker_device["index"]: - self._CHOICE_SPEAKER_DEVICE = value - saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) + if getDefaultOutputDevice()["name"] == value: + self._CHOICE_SPEAKER_DEVICE = value + saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) @property @json_serializable('INPUT_SPEAKER_ENERGY_THRESHOLD') diff --git a/controller.py b/controller.py index bc0f0f68..52c6af9c 100644 --- a/controller.py +++ b/controller.py @@ -108,7 +108,8 @@ def receiveSpeakerMessage(message): model.logger.info(f"[RECEIVED] {message}{translation}") def startTranscriptionReceiveMessage(): - model.startSpeakerTranscript(receiveSpeakerMessage) + config.CHOICE_SPEAKER_DEVICE = model.getOutputDefaultDevice() + model.startSpeakerTranscript(receiveSpeakerMessage, lambda:print("[ERROR] Speaker NoDevice")) view.setMainWindowAllWidgetsStatusToNormal() def stopTranscriptionReceiveMessage(): @@ -128,8 +129,10 @@ def stopThreadingTranscriptionReceiveMessage(): th_stopTranscriptionReceiveMessage.start() def startTranscriptionReceiveMessageOnCloseConfigWindow(): + config.CHOICE_SPEAKER_DEVICE = model.getOutputDefaultDevice() model.startSpeakerTranscript(receiveSpeakerMessage) + def stopTranscriptionReceiveMessageOnOpenConfigWindow(): model.stopSpeakerTranscript() @@ -509,7 +512,13 @@ def callbackCheckSpeakerThreshold(is_turned_on): print("callbackCheckSpeakerThreshold", is_turned_on) if is_turned_on is True: view.replaceSpeakerThresholdCheckButton_Disabled() - model.startCheckSpeakerEnergy(setProgressBarSpeakerEnergy, view.initProgressBar_SpeakerEnergy) + config.CHOICE_SPEAKER_DEVICE = model.getOutputDefaultDevice() + model.startCheckSpeakerEnergy( + setProgressBarSpeakerEnergy, + view.initProgressBar_SpeakerEnergy, + lambda:print("[ERROR] Speaker NoDevice") + ) + view.replaceSpeakerThresholdCheckButton_Active() else: view.replaceSpeakerThresholdCheckButton_Disabled() diff --git a/model.py b/model.py index 6b8781cc..08e0a528 100644 --- a/model.py +++ b/model.py @@ -14,7 +14,7 @@ import webbrowser from flashtext import KeywordProcessor from models.translation.translation_translator import Translator -from models.transcription.transcription_utils import getInputDevices, getOutputDevices, getDefaultInputDevice, getDefaultOutputDevice +from models.transcription.transcription_utils import getInputDevices, getDefaultOutputDevice from models.osc.osc_tools import sendTyping, sendMessage, sendTestAction, receiveOscParameters from models.transcription.transcription_recorder import SelectedMicRecorder, SelectedSpeakerRecorder from models.transcription.transcription_recorder import SelectedMicEnergyRecorder, SelectedSpeakeEnergyRecorder @@ -259,11 +259,15 @@ class Model: return [device["name"] for device in getInputDevices()[config.CHOICE_MIC_HOST]][0] @staticmethod - def getListOutputDevice(): - return [device["name"] for device in getOutputDevices()] + def getOutputDefaultDevice(): + return getDefaultOutputDevice()["name"] - def startMicTranscript(self, fnc): + def startMicTranscript(self, fnc, error_fnc=None): if config.CHOICE_MIC_HOST == "NoHost" or config.CHOICE_MIC_DEVICE == "NoDevice": + try: + error_fnc() + except: + pass return mic_audio_queue = Queue() @@ -306,8 +310,12 @@ class Model: self.mic_audio_recorder.stop() self.mic_audio_recorder = None - def startCheckMicEnergy(self, fnc, end_fnc): + def startCheckMicEnergy(self, fnc, end_fnc, error_fnc=None): if config.CHOICE_MIC_HOST == "NoHost" or config.CHOICE_MIC_DEVICE == "NoDevice": + try: + error_fnc() + except: + pass return def sendMicEnergy(): @@ -335,20 +343,22 @@ class Model: self.mic_energy_recorder.stop() self.mic_energy_recorder = None - def startSpeakerTranscript(self, fnc): - speaker_device = getDefaultOutputDevice() - config.CHOICE_SPEAKER_DEVICE = speaker_device["name"] + def startSpeakerTranscript(self, fnc, error_fnc=None): if config.CHOICE_SPEAKER_DEVICE == "NoDevice": + try: + error_fnc() + except: + pass return - speaker_audio_queue = Queue() + speaker_audio_queue = Queue() record_timeout = config.INPUT_SPEAKER_RECORD_TIMEOUT phase_timeout = config.INPUT_SPEAKER_PHRASE_TIMEOUT if record_timeout > phase_timeout: record_timeout = phase_timeout self.speaker_audio_recorder = SelectedSpeakerRecorder( - device=speaker_device, + device=config.CHOICE_SPEAKER_DEVICE , energy_threshold=config.INPUT_SPEAKER_ENERGY_THRESHOLD, dynamic_energy_threshold=config.INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD, record_timeout=record_timeout, @@ -380,10 +390,12 @@ class Model: self.speaker_audio_recorder.stop() self.speaker_audio_recorder = None - def startCheckSpeakerEnergy(self, fnc, end_fnc): - speaker_device = getDefaultOutputDevice() - config.CHOICE_SPEAKER_DEVICE = speaker_device["name"] + def startCheckSpeakerEnergy(self, fnc, end_fnc, error_fnc=None): if config.CHOICE_SPEAKER_DEVICE == "NoDevice": + try: + error_fnc() + except: + pass return def sendSpeakerEnergy(): @@ -396,7 +408,7 @@ class Model: # sleep(0.01) speaker_energy_queue = Queue() - self.speaker_energy_recorder = SelectedSpeakeEnergyRecorder(speaker_device) + self.speaker_energy_recorder = SelectedSpeakeEnergyRecorder(config.CHOICE_SPEAKER_DEVICE) self.speaker_energy_recorder.recordIntoQueue(speaker_energy_queue) self.speaker_energy_plot_progressbar = threadFnc(sendSpeakerEnergy, end_fnc=end_fnc) self.speaker_energy_plot_progressbar.daemon = True diff --git a/models/transcription/transcription_utils.py b/models/transcription/transcription_utils.py index fead4fc4..f40defeb 100644 --- a/models/transcription/transcription_utils.py +++ b/models/transcription/transcription_utils.py @@ -16,17 +16,6 @@ def getInputDevices(): devices = {"NoHost": [{"name": "NoDevice"}]} return devices -def getOutputDevices(): - devices =[] - with PyAudio() as p: - wasapi_info = p.get_host_api_info_by_type(paWASAPI) - for device in p.get_loopback_device_info_generator(): - if device["hostApi"] == wasapi_info["index"] and device["isLoopbackDevice"] is True: - devices.append(device) - if len(devices) == 0: - devices = [{'name':"NoDevice"}] - return devices - def getDefaultInputDevice(): with PyAudio() as p: api_info = p.get_default_host_api_info()