Merge branch 'model' into UI_2.0

This commit is contained in:
misyaguziya
2023-10-17 04:03:49 +09:00
4 changed files with 43 additions and 37 deletions

View File

@@ -1,5 +1,4 @@
import sys import sys
from json import load, dump
import inspect import inspect
from os import path as os_path from os import path as os_path
from json import load as json_load from json import load as json_load
@@ -7,9 +6,8 @@ from json import dump as json_dump
import tkinter as tk import tkinter as tk
from tkinter import font from tkinter import font
from languages import selectable_languages from languages import selectable_languages
from models.translation.translation_languages import translatorEngine, translation_lang from models.translation.translation_languages import translatorEngine
from models.transcription.transcription_languages import transcription_lang from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice, getDefaultOutputDevice
from models.transcription.transcription_utils import getInputDevices, getOutputDevices, getDefaultInputDevice, getDefaultOutputDevice
json_serializable_vars = {} json_serializable_vars = {}
def json_serializable(var_name): def json_serializable(var_name):
@@ -20,7 +18,7 @@ def json_serializable(var_name):
def saveJson(path, key, value): def saveJson(path, key, value):
with open(path, "r", encoding="utf-8") as fp: with open(path, "r", encoding="utf-8") as fp:
json_data = load(fp) json_data = json_load(fp)
json_data[key] = value json_data[key] = value
with open(path, "w", encoding="utf-8") as fp: with open(path, "w", encoding="utf-8") as fp:
json_dump(json_data, fp, indent=4, ensure_ascii=False) json_dump(json_data, fp, indent=4, ensure_ascii=False)
@@ -346,9 +344,7 @@ class Config:
@CHOICE_SPEAKER_DEVICE.setter @CHOICE_SPEAKER_DEVICE.setter
def CHOICE_SPEAKER_DEVICE(self, value): def CHOICE_SPEAKER_DEVICE(self, value):
if value in [device["name"] for device in getOutputDevices()]: if getDefaultOutputDevice()["name"] == value:
speaker_device = [device for device in getOutputDevices() if device["name"] == value][0]
if getDefaultOutputDevice()["index"] == speaker_device["index"]:
self._CHOICE_SPEAKER_DEVICE = value self._CHOICE_SPEAKER_DEVICE = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)

View File

@@ -108,7 +108,8 @@ def receiveSpeakerMessage(message):
model.logger.info(f"[RECEIVED] {message}{translation}") model.logger.info(f"[RECEIVED] {message}{translation}")
def startTranscriptionReceiveMessage(): def startTranscriptionReceiveMessage():
model.startSpeakerTranscript(receiveSpeakerMessage) config.CHOICE_SPEAKER_DEVICE = model.getOutputDefaultDevice()
model.startSpeakerTranscript(receiveSpeakerMessage, lambda:print("[ERROR] Speaker NoDevice"))
view.setMainWindowAllWidgetsStatusToNormal() view.setMainWindowAllWidgetsStatusToNormal()
def stopTranscriptionReceiveMessage(): def stopTranscriptionReceiveMessage():
@@ -128,8 +129,10 @@ def stopThreadingTranscriptionReceiveMessage():
th_stopTranscriptionReceiveMessage.start() th_stopTranscriptionReceiveMessage.start()
def startTranscriptionReceiveMessageOnCloseConfigWindow(): def startTranscriptionReceiveMessageOnCloseConfigWindow():
config.CHOICE_SPEAKER_DEVICE = model.getOutputDefaultDevice()
model.startSpeakerTranscript(receiveSpeakerMessage) model.startSpeakerTranscript(receiveSpeakerMessage)
def stopTranscriptionReceiveMessageOnOpenConfigWindow(): def stopTranscriptionReceiveMessageOnOpenConfigWindow():
model.stopSpeakerTranscript() model.stopSpeakerTranscript()
@@ -509,7 +512,13 @@ def callbackCheckSpeakerThreshold(is_turned_on):
print("callbackCheckSpeakerThreshold", is_turned_on) print("callbackCheckSpeakerThreshold", is_turned_on)
if is_turned_on is True: if is_turned_on is True:
view.replaceSpeakerThresholdCheckButton_Disabled() 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() view.replaceSpeakerThresholdCheckButton_Active()
else: else:
view.replaceSpeakerThresholdCheckButton_Disabled() view.replaceSpeakerThresholdCheckButton_Disabled()

View File

@@ -14,7 +14,7 @@ import webbrowser
from flashtext import KeywordProcessor from flashtext import KeywordProcessor
from models.translation.translation_translator import Translator 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.osc.osc_tools import sendTyping, sendMessage, sendTestAction, receiveOscParameters
from models.transcription.transcription_recorder import SelectedMicRecorder, SelectedSpeakerRecorder from models.transcription.transcription_recorder import SelectedMicRecorder, SelectedSpeakerRecorder
from models.transcription.transcription_recorder import SelectedMicEnergyRecorder, SelectedSpeakeEnergyRecorder 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] return [device["name"] for device in getInputDevices()[config.CHOICE_MIC_HOST]][0]
@staticmethod @staticmethod
def getListOutputDevice(): def getOutputDefaultDevice():
return [device["name"] for device in getOutputDevices()] 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": if config.CHOICE_MIC_HOST == "NoHost" or config.CHOICE_MIC_DEVICE == "NoDevice":
try:
error_fnc()
except:
pass
return return
mic_audio_queue = Queue() mic_audio_queue = Queue()
@@ -306,8 +310,12 @@ class Model:
self.mic_audio_recorder.stop() self.mic_audio_recorder.stop()
self.mic_audio_recorder = None 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": if config.CHOICE_MIC_HOST == "NoHost" or config.CHOICE_MIC_DEVICE == "NoDevice":
try:
error_fnc()
except:
pass
return return
def sendMicEnergy(): def sendMicEnergy():
@@ -335,20 +343,22 @@ class Model:
self.mic_energy_recorder.stop() self.mic_energy_recorder.stop()
self.mic_energy_recorder = None self.mic_energy_recorder = None
def startSpeakerTranscript(self, fnc): def startSpeakerTranscript(self, fnc, error_fnc=None):
speaker_device = getDefaultOutputDevice()
config.CHOICE_SPEAKER_DEVICE = speaker_device["name"]
if config.CHOICE_SPEAKER_DEVICE == "NoDevice": if config.CHOICE_SPEAKER_DEVICE == "NoDevice":
try:
error_fnc()
except:
pass
return return
speaker_audio_queue = Queue()
speaker_audio_queue = Queue()
record_timeout = config.INPUT_SPEAKER_RECORD_TIMEOUT record_timeout = config.INPUT_SPEAKER_RECORD_TIMEOUT
phase_timeout = config.INPUT_SPEAKER_PHRASE_TIMEOUT phase_timeout = config.INPUT_SPEAKER_PHRASE_TIMEOUT
if record_timeout > phase_timeout: if record_timeout > phase_timeout:
record_timeout = phase_timeout record_timeout = phase_timeout
self.speaker_audio_recorder = SelectedSpeakerRecorder( self.speaker_audio_recorder = SelectedSpeakerRecorder(
device=speaker_device, device=config.CHOICE_SPEAKER_DEVICE ,
energy_threshold=config.INPUT_SPEAKER_ENERGY_THRESHOLD, energy_threshold=config.INPUT_SPEAKER_ENERGY_THRESHOLD,
dynamic_energy_threshold=config.INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD, dynamic_energy_threshold=config.INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD,
record_timeout=record_timeout, record_timeout=record_timeout,
@@ -380,10 +390,12 @@ class Model:
self.speaker_audio_recorder.stop() self.speaker_audio_recorder.stop()
self.speaker_audio_recorder = None self.speaker_audio_recorder = None
def startCheckSpeakerEnergy(self, fnc, end_fnc): def startCheckSpeakerEnergy(self, fnc, end_fnc, error_fnc=None):
speaker_device = getDefaultOutputDevice()
config.CHOICE_SPEAKER_DEVICE = speaker_device["name"]
if config.CHOICE_SPEAKER_DEVICE == "NoDevice": if config.CHOICE_SPEAKER_DEVICE == "NoDevice":
try:
error_fnc()
except:
pass
return return
def sendSpeakerEnergy(): def sendSpeakerEnergy():
@@ -396,7 +408,7 @@ class Model:
# sleep(0.01) # sleep(0.01)
speaker_energy_queue = Queue() 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_recorder.recordIntoQueue(speaker_energy_queue)
self.speaker_energy_plot_progressbar = threadFnc(sendSpeakerEnergy, end_fnc=end_fnc) self.speaker_energy_plot_progressbar = threadFnc(sendSpeakerEnergy, end_fnc=end_fnc)
self.speaker_energy_plot_progressbar.daemon = True self.speaker_energy_plot_progressbar.daemon = True

View File

@@ -16,17 +16,6 @@ def getInputDevices():
devices = {"NoHost": [{"name": "NoDevice"}]} devices = {"NoHost": [{"name": "NoDevice"}]}
return devices 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(): def getDefaultInputDevice():
with PyAudio() as p: with PyAudio() as p:
api_info = p.get_default_host_api_info() api_info = p.get_default_host_api_info()