👍️[Update] Model : Add Select Speaker Devices

This commit is contained in:
misyaguziya
2024-04-03 18:01:27 +09:00
parent 3916144723
commit 0d7741a427
3 changed files with 50 additions and 18 deletions

View File

@@ -6,7 +6,7 @@ from json import dump as json_dump
import tkinter as tk
from tkinter import font
from models.translation.translation_languages import translation_lang
from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice
from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice, getOutputDevices, getDefaultOutputDevice
from models.transcription.transcription_languages import transcription_lang
from utils import generatePercentageStringsList, isUniqueStrings
@@ -537,6 +537,17 @@ class Config:
self._INPUT_MIC_WORD_FILTER = sorted(set(value), key=value.index)
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)
@property
@json_serializable('CHOICE_SPEAKER_DEVICE')
def CHOICE_SPEAKER_DEVICE(self):
return self._CHOICE_SPEAKER_DEVICE
@CHOICE_SPEAKER_DEVICE.setter
def CHOICE_SPEAKER_DEVICE(self, value):
if value in [device["name"] for device in getOutputDevices()]:
self._CHOICE_SPEAKER_DEVICE = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)
@property
@json_serializable('INPUT_SPEAKER_ENERGY_THRESHOLD')
def INPUT_SPEAKER_ENERGY_THRESHOLD(self):
@@ -957,6 +968,7 @@ class Config:
self._INPUT_MIC_PHRASE_TIMEOUT = 3
self._INPUT_MIC_MAX_PHRASES = 10
self._INPUT_MIC_WORD_FILTER = []
self._CHOICE_SPEAKER_DEVICE = getDefaultOutputDevice()["device"]["name"]
self._INPUT_SPEAKER_ENERGY_THRESHOLD = 300
self._INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD = False
self._INPUT_SPEAKER_RECORD_TIMEOUT = 3

View File

@@ -16,7 +16,7 @@ import webbrowser
from typing import Callable
from flashtext import KeywordProcessor
from models.translation.translation_translator import Translator
from models.transcription.transcription_utils import getInputDevices, getDefaultOutputDevice
from models.transcription.transcription_utils import getInputDevices, getOutputDevices
from models.osc.osc_tools import sendTyping, sendMessage, sendTestAction, receiveOscParameters
from models.transcription.transcription_recorder import SelectedMicEnergyAndAudioRecorder, SelectedSpeakerEnergyAndAudioRecorder
from models.transcription.transcription_recorder import SelectedMicEnergyRecorder, SelectedSpeakerEnergyRecorder
@@ -305,12 +305,8 @@ class Model:
return [device["name"] for device in getInputDevices()[config.CHOICE_MIC_HOST]]
@staticmethod
def getInputDefaultDevice():
return [device["name"] for device in getInputDevices()[config.CHOICE_MIC_HOST]][0]
@staticmethod
def getOutputDefaultDevice():
return getDefaultOutputDevice()["name"]
def getListOutputDevice():
return [device["name"] for device in getOutputDevices()]
def startMicTranscript(self, fnc, error_fnc=None):
if config.CHOICE_MIC_HOST == "NoHost" or config.CHOICE_MIC_DEVICE == "NoDevice":
@@ -322,14 +318,14 @@ class Model:
mic_audio_queue = Queue()
# mic_energy_queue = Queue()
device = [device for device in getInputDevices()[config.CHOICE_MIC_HOST] if device["name"] == config.CHOICE_MIC_DEVICE][0]
mic_device = [device for device in getInputDevices()[config.CHOICE_MIC_HOST] if device["name"] == config.CHOICE_MIC_DEVICE][0]
record_timeout = config.INPUT_MIC_RECORD_TIMEOUT
phase_timeout = config.INPUT_MIC_PHRASE_TIMEOUT
if record_timeout > phase_timeout:
record_timeout = phase_timeout
self.mic_audio_recorder = SelectedMicEnergyAndAudioRecorder(
device=device,
device=mic_device,
energy_threshold=config.INPUT_MIC_ENERGY_THRESHOLD,
dynamic_energy_threshold=config.INPUT_MIC_DYNAMIC_ENERGY_THRESHOLD,
record_timeout=record_timeout,
@@ -422,8 +418,7 @@ class Model:
self.mic_energy_recorder = None
def startSpeakerTranscript(self, fnc, error_fnc=None):
speaker_device = getDefaultOutputDevice()
if speaker_device["name"] == "NoDevice":
if config.CHOICE_SPEAKER_DEVICE == "NoDevice":
try:
error_fnc()
except Exception:
@@ -432,6 +427,8 @@ class Model:
speaker_audio_queue = Queue()
# speaker_energy_queue = Queue()
print([device for device in getOutputDevices() if device["name"] == config.CHOICE_SPEAKER_DEVICE])
speaker_device = [device for device in getOutputDevices() if device["name"] == config.CHOICE_SPEAKER_DEVICE][0]
record_timeout = config.INPUT_SPEAKER_RECORD_TIMEOUT
phase_timeout = config.INPUT_SPEAKER_PHRASE_TIMEOUT
if record_timeout > phase_timeout:
@@ -498,8 +495,7 @@ class Model:
# self.speaker_get_energy = None
def startCheckSpeakerEnergy(self, fnc, end_fnc, error_fnc=None):
speaker_device = getDefaultOutputDevice()
if speaker_device["name"] == "NoDevice":
if config.CHOICE_SPEAKER_DEVICE == "NoDevice":
try:
error_fnc()
except Exception:
@@ -516,6 +512,7 @@ class Model:
sleep(0.01)
speaker_energy_queue = Queue()
speaker_device = [device for device in getOutputDevices() if device["name"] == config.CHOICE_SPEAKER_DEVICE][0]
self.speaker_energy_recorder = SelectedSpeakerEnergyRecorder(speaker_device)
self.speaker_energy_recorder.recordIntoQueue(speaker_energy_queue)
self.speaker_energy_plot_progressbar = threadFnc(sendSpeakerEnergy, end_fnc=end_fnc)

View File

@@ -23,12 +23,32 @@ def getDefaultInputDevice():
for host_index in range(0, p.get_host_api_count()):
host = p.get_host_api_info_by_index(host_index)
for device_index in range(0, p. get_host_api_info_by_index(host_index)['deviceCount']):
for device_index in range(0, p.get_host_api_info_by_index(host_index)['deviceCount']):
device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if device["index"] == defaultInputDevice:
return {"host": host, "device": device}
return {"host": {"name": "NoHost"}, "device": {"name": "NoDevice"}}
def getOutputDevices():
devices = []
with PyAudio() as p:
wasapi_info = p.get_host_api_info_by_type(paWASAPI)
defaultOutputDevice = wasapi_info["defaultOutputDevice"]
for host_index in range(0, p.get_host_api_count()):
for device_index in range(0, p.get_host_api_info_by_index(host_index)['deviceCount']):
device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if device["index"] == defaultOutputDevice:
default_speakers = device
if not default_speakers["isLoopbackDevice"]:
for loopback in p.get_loopback_device_info_generator():
if default_speakers["name"] in loopback["name"]:
devices.append(loopback)
if len(devices) == 0:
devices = [{"name": "NoDevice"}]
return devices
def getDefaultOutputDevice():
with PyAudio() as p:
wasapi_info = p.get_host_api_info_by_type(paWASAPI)
@@ -42,6 +62,9 @@ def getDefaultOutputDevice():
if not default_speakers["isLoopbackDevice"]:
for loopback in p.get_loopback_device_info_generator():
if default_speakers["name"] in loopback["name"]:
default_device = loopback
return default_device
return {"name":"NoDevice"}
return {"device": loopback}
return {"device": {"name": "NoDevice"}}
if __name__ == "__main__":
print("getOutputDevices():", getOutputDevices())
print("getDefaultOutputDevice()", getDefaultOutputDevice())