👍️[Update] Model: Deviceのアクセス干渉の回避処理をするためにDeviceの検出を常に行うように変更

This commit is contained in:
misyaguziya
2024-09-16 23:05:30 +09:00
parent e751f5ddbd
commit d21e0a09fc
3 changed files with 138 additions and 118 deletions

View File

@@ -5,7 +5,7 @@ from json import load as json_load
from json import dump as json_dump from json import dump as json_dump
import tkinter as tk import tkinter as tk
from tkinter import font from tkinter import font
from models.transcription.transcription_utils import getInputDevices, getDefaultInputDevice, getOutputDevices, getDefaultOutputDevice from models.transcription.transcription_utils import device_manager
from models.transcription.transcription_languages import transcription_lang from models.transcription.transcription_languages import transcription_lang
from utils import generatePercentageStringsList, isUniqueStrings from utils import generatePercentageStringsList, isUniqueStrings
@@ -470,7 +470,7 @@ class Config:
@CHOICE_MIC_HOST.setter @CHOICE_MIC_HOST.setter
def CHOICE_MIC_HOST(self, value): def CHOICE_MIC_HOST(self, value):
if value in [host for host in getInputDevices().keys()]: if value in [host for host in device_manager.getInputDevices().keys()]:
self._CHOICE_MIC_HOST = value self._CHOICE_MIC_HOST = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)
@@ -481,7 +481,7 @@ class Config:
@CHOICE_MIC_DEVICE.setter @CHOICE_MIC_DEVICE.setter
def CHOICE_MIC_DEVICE(self, value): def CHOICE_MIC_DEVICE(self, value):
if value in [device["name"] for device in getInputDevices()[self.CHOICE_MIC_HOST]]: if value in [device["name"] for device in device_manager.getInputDevices()[self.CHOICE_MIC_HOST]]:
self._CHOICE_MIC_DEVICE = value self._CHOICE_MIC_DEVICE = value
saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value) saveJson(self.PATH_CONFIG, inspect.currentframe().f_code.co_name, value)
@@ -591,7 +591,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 value in [device["name"] for device in device_manager.getOutputDevices()]:
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)
@@ -1117,8 +1117,8 @@ class Config:
"height": "654", "height": "654",
} }
self._ENABLE_MIC_AUTOMATIC_SELECTION = True self._ENABLE_MIC_AUTOMATIC_SELECTION = True
self._CHOICE_MIC_HOST = getDefaultInputDevice()["host"]["name"] self._CHOICE_MIC_HOST = device_manager.getDefaultInputDevice()["host"]["name"]
self._CHOICE_MIC_DEVICE = getDefaultInputDevice()["device"]["name"] self._CHOICE_MIC_DEVICE = device_manager.getDefaultInputDevice()["device"]["name"]
self._INPUT_MIC_ENERGY_THRESHOLD = 300 self._INPUT_MIC_ENERGY_THRESHOLD = 300
self._INPUT_MIC_DYNAMIC_ENERGY_THRESHOLD = False self._INPUT_MIC_DYNAMIC_ENERGY_THRESHOLD = False
self._INPUT_MIC_RECORD_TIMEOUT = 3 self._INPUT_MIC_RECORD_TIMEOUT = 3
@@ -1128,7 +1128,7 @@ class Config:
self._INPUT_MIC_AVG_LOGPROB=-0.8 self._INPUT_MIC_AVG_LOGPROB=-0.8
self._INPUT_MIC_NO_SPEECH_PROB=0.6 self._INPUT_MIC_NO_SPEECH_PROB=0.6
self._ENABLE_SPEAKER_AUTOMATIC_SELECTION = True self._ENABLE_SPEAKER_AUTOMATIC_SELECTION = True
self._CHOICE_SPEAKER_DEVICE = getDefaultOutputDevice()["device"]["name"] self._CHOICE_SPEAKER_DEVICE = device_manager.getDefaultOutputDevice()["device"]["name"]
self._INPUT_SPEAKER_ENERGY_THRESHOLD = 300 self._INPUT_SPEAKER_ENERGY_THRESHOLD = 300
self._INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD = False self._INPUT_SPEAKER_DYNAMIC_ENERGY_THRESHOLD = False
self._INPUT_SPEAKER_RECORD_TIMEOUT = 3 self._INPUT_SPEAKER_RECORD_TIMEOUT = 3

View File

@@ -17,7 +17,7 @@ from typing import Callable
from flashtext import KeywordProcessor from flashtext import KeywordProcessor
from pykakasi import kakasi from pykakasi import kakasi
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 device_manager
from models.osc.osc_tools import sendTyping, sendMessage, receiveOscParameters, getOSCParameterValue from models.osc.osc_tools import sendTyping, sendMessage, receiveOscParameters, getOSCParameterValue
from models.transcription.transcription_recorder import SelectedMicEnergyAndAudioRecorder, SelectedSpeakerEnergyAndAudioRecorder from models.transcription.transcription_recorder import SelectedMicEnergyAndAudioRecorder, SelectedSpeakerEnergyAndAudioRecorder
from models.transcription.transcription_recorder import SelectedMicEnergyRecorder, SelectedSpeakerEnergyRecorder from models.transcription.transcription_recorder import SelectedMicEnergyRecorder, SelectedSpeakerEnergyRecorder
@@ -69,7 +69,6 @@ class Model:
def init(self): def init(self):
self.logger = None self.logger = None
self.device_access = False
self.th_check_device = 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
@@ -408,53 +407,33 @@ class Model:
command = [os_path.join(current_directory, batch_name), program_name] command = [os_path.join(current_directory, batch_name), program_name]
Popen(command, cwd=current_directory) Popen(command, cwd=current_directory)
def startAccessDevice(self):
while self.device_access is True:
sleep(0.1)
self.device_access = True
def stopAccessDevice(self):
self.device_access = False
def getListInputHost(self): def getListInputHost(self):
self.startAccessDevice() result = [host for host in device_manager.getInputDevices().keys()]
result = [host for host in getInputDevices().keys()]
self.stopAccessDevice()
return result return result
def getInputDefaultDevice(self): def getInputDefaultDevice(self):
self.startAccessDevice() result = device_manager.getInputDevices().get(config.CHOICE_MIC_HOST, [{"name": "NoDevice"}])[0]["name"]
result = getInputDevices().get(config.CHOICE_MIC_HOST, [{"name": "NoDevice"}])[0]["name"]
self.stopAccessDevice()
return result return result
def getListInputDevice(self): def getListInputDevice(self):
self.startAccessDevice() result = [device["name"] for device in device_manager.getInputDevices().get(config.CHOICE_MIC_HOST, [{"name": "NoDevice"}])]
result = [device["name"] for device in getInputDevices().get(config.CHOICE_MIC_HOST, [{"name": "NoDevice"}])]
self.stopAccessDevice()
return result return result
def getListOutputDevice(self): def getListOutputDevice(self):
self.startAccessDevice() result = [device["name"] for device in device_manager.getOutputDevices()]
result = [device["name"] for device in getOutputDevices()]
self.stopAccessDevice()
return result return result
def startAutomaticDeviceSelection(self, fnc_mic, fnc_speaker): def startAutomaticDeviceSelection(self, fnc_mic, fnc_speaker):
def checkDevice(fnc_mic, fnc_speaker): def checkDevice(fnc_mic, fnc_speaker):
if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultInputDevice()
default_device = getDefaultInputDevice()
self.stopAccessDevice()
mic_host_name = default_device["host"]["name"] mic_host_name = default_device["host"]["name"]
mic_device_name = default_device["device"]["name"] mic_device_name = default_device["device"]["name"]
if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE: if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE:
fnc_mic(mic_host_name, mic_device_name) fnc_mic(mic_host_name, mic_device_name)
if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultOutputDevice()
default_device = getDefaultOutputDevice()
self.stopAccessDevice()
speaker_device_name = default_device["device"]["name"] speaker_device_name = default_device["device"]["name"]
if speaker_device_name != config.CHOICE_SPEAKER_DEVICE: if speaker_device_name != config.CHOICE_SPEAKER_DEVICE:
fnc_speaker(speaker_device_name) fnc_speaker(speaker_device_name)
@@ -473,18 +452,14 @@ class Model:
def startMicTranscript(self, fnc): def startMicTranscript(self, fnc):
if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultInputDevice()
default_device = getDefaultInputDevice()
self.stopAccessDevice()
mic_host_name = default_device["host"]["name"] mic_host_name = default_device["host"]["name"]
mic_device_name = default_device["device"]["name"] mic_device_name = default_device["device"]["name"]
else: else:
mic_host_name = config.CHOICE_MIC_HOST mic_host_name = config.CHOICE_MIC_HOST
mic_device_name = config.CHOICE_MIC_DEVICE mic_device_name = config.CHOICE_MIC_DEVICE
self.startAccessDevice() mic_device_list = device_manager.getInputDevices().get(mic_host_name, [{"name": "NoDevice"}])
mic_device_list = getInputDevices().get(mic_host_name, [{"name": "NoDevice"}])
self.stopAccessDevice()
choice_mic_device = [device for device in mic_device_list if device["name"] == mic_device_name] choice_mic_device = [device for device in mic_device_list if device["name"] == mic_device_name]
if len(choice_mic_device) == 0: if len(choice_mic_device) == 0:
@@ -611,18 +586,14 @@ class Model:
self.check_mic_energy_fnc = fnc self.check_mic_energy_fnc = fnc
if config.ENABLE_MIC_AUTOMATIC_SELECTION is True: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultInputDevice()
default_device = getDefaultInputDevice()
self.stopAccessDevice()
mic_host_name = default_device["host"]["name"] mic_host_name = default_device["host"]["name"]
mic_device_name = default_device["device"]["name"] mic_device_name = default_device["device"]["name"]
else: else:
mic_host_name = config.CHOICE_MIC_HOST mic_host_name = config.CHOICE_MIC_HOST
mic_device_name = config.CHOICE_MIC_DEVICE mic_device_name = config.CHOICE_MIC_DEVICE
self.startAccessDevice() mic_device_list = device_manager.getInputDevices().get(mic_host_name, [{"name": "NoDevice"}])
mic_device_list = getInputDevices().get(mic_host_name, [{"name": "NoDevice"}])
self.stopAccessDevice()
choice_mic_device = [device for device in mic_device_list if device["name"] == mic_device_name] choice_mic_device = [device for device in mic_device_list if device["name"] == mic_device_name]
if len(choice_mic_device) == 0: if len(choice_mic_device) == 0:
@@ -657,16 +628,12 @@ class Model:
def startSpeakerTranscript(self, fnc): def startSpeakerTranscript(self, fnc):
if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultOutputDevice()
default_device = getDefaultOutputDevice()
self.stopAccessDevice()
speaker_device_name = default_device["device"]["name"] speaker_device_name = default_device["device"]["name"]
else: else:
speaker_device_name = config.CHOICE_SPEAKER_DEVICE speaker_device_name = config.CHOICE_SPEAKER_DEVICE
self.startAccessDevice() speaker_device_list = device_manager.getOutputDevices()
speaker_device_list = getOutputDevices()
self.stopAccessDevice()
choice_speaker_device = [device for device in speaker_device_list if device["name"] == speaker_device_name] choice_speaker_device = [device for device in speaker_device_list if device["name"] == speaker_device_name]
if len(choice_speaker_device) == 0: if len(choice_speaker_device) == 0:
@@ -753,16 +720,12 @@ class Model:
self.check_speaker_energy_fnc = fnc self.check_speaker_energy_fnc = fnc
if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
self.startAccessDevice() default_device = device_manager.getDefaultOutputDevice()
default_device = getDefaultOutputDevice()
self.stopAccessDevice()
speaker_device_name = default_device["device"]["name"] speaker_device_name = default_device["device"]["name"]
else: else:
speaker_device_name = config.CHOICE_SPEAKER_DEVICE speaker_device_name = config.CHOICE_SPEAKER_DEVICE
self.startAccessDevice() speaker_device_list = device_manager.getOutputDevices()
speaker_device_list = getOutputDevices()
self.stopAccessDevice()
choice_speaker_device = [device for device in speaker_device_list if device["name"] == speaker_device_name] choice_speaker_device = [device for device in speaker_device_list if device["name"] == speaker_device_name]
if len(choice_speaker_device) == 0: if len(choice_speaker_device) == 0:

View File

@@ -1,70 +1,127 @@
from time import sleep
from threading import Thread
from pyaudiowpatch import PyAudio, paWASAPI from pyaudiowpatch import PyAudio, paWASAPI
def getInputDevices(): class DeviceManager:
devices = {} _instance = None
with PyAudio() as p:
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']):
device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if device["maxInputChannels"] > 0 and device["isLoopbackDevice"] is False:
if host["name"] in devices.keys():
devices[host["name"]].append(device)
else:
devices[host["name"]] = [device]
if len(devices) == 0:
devices = {"NoHost": [{"name": "NoDevice"}]}
return devices
def getDefaultInputDevice(): def __new__(cls):
with PyAudio() as p: if cls._instance is None:
api_info = p.get_default_host_api_info() cls._instance = super(DeviceManager, cls).__new__(cls)
defaultInputDevice = api_info["defaultInputDevice"] cls._instance.init()
return cls._instance
for host_index in range(0, p.get_host_api_count()): def init(self):
host = p.get_host_api_info_by_index(host_index) self.input_devices = {"NoHost": [{"name": "NoDevice"}]}
for device_index in range(0, p.get_host_api_info_by_index(host_index)['deviceCount']): self.default_input_device = {"host": {"name": "NoHost"}, "device": {"name": "NoDevice"}}
device = p.get_device_info_by_host_api_device_index(host_index, device_index) self.output_devices = [{"name": "NoDevice"}]
if device["index"] == defaultInputDevice: self.default_output_device = {"device": {"name": "NoDevice"}}
return {"host": host, "device": device} self.update()
return {"host": {"name": "NoHost"}, "device": {"name": "NoDevice"}}
def getOutputDevices(): self.monitoring_flag = True
devices = [] self.th_monitoring = Thread(target=self.startMonitoring)
with PyAudio() as p: self.th_monitoring.daemon = True
wasapi_info = p.get_host_api_info_by_type(paWASAPI) self.th_monitoring.start()
for host_index in range(0, p.get_host_api_count()):
host = p.get_host_api_info_by_index(host_index) def update(self):
if host["name"] == wasapi_info["name"]: buffer_input_devices = {}
for device_index in range(0, p.get_host_api_info_by_index(host_index)['deviceCount']): buffer_default_input_device = {"host": {"name": "NoHost"}, "device": {"name": "NoDevice"}}
buffer_output_devices = []
buffer_default_output_device = {"device": {"name": "NoDevice"}}
with PyAudio() as p:
for host_index in range(p.get_host_api_count()):
host = p.get_host_api_info_by_index(host_index)
device_count = host.get('deviceCount', 0)
for device_index in range(device_count):
device = p.get_device_info_by_host_api_device_index(host_index, device_index) device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if not device["isLoopbackDevice"]: if device.get("maxInputChannels", 0) > 0 and not device.get("isLoopbackDevice", True):
for loopback in p.get_loopback_device_info_generator(): buffer_input_devices.setdefault(host["name"], []).append(device)
if device["name"] in loopback["name"]: if not buffer_input_devices:
devices.append(loopback) buffer_input_devices = {"NoHost": [{"name": "NoDevice"}]}
if len(devices) == 0: api_info = p.get_default_host_api_info()
devices = [{"name": "NoDevice"}] default_input_device = api_info["defaultInputDevice"]
else:
devices = [dict(t) for t in {tuple(d.items()) for d in devices}]
return devices
def getDefaultOutputDevice(): for host_index in range(p.get_host_api_count()):
with PyAudio() as p: host = p.get_host_api_info_by_index(host_index)
wasapi_info = p.get_host_api_info_by_type(paWASAPI) device_count = host.get('deviceCount', 0)
defaultOutputDevice = wasapi_info["defaultOutputDevice"] for device_index in range(device_count):
device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if device["index"] == default_input_device:
buffer_default_input_device = {"host": host, "device": device}
break
else:
continue
break
for host_index in range(0, p.get_host_api_count()): output_devices = []
for device_index in range(0, p. get_host_api_info_by_index(host_index)['deviceCount']): wasapi_info = p.get_host_api_info_by_type(paWASAPI)
device = p.get_device_info_by_host_api_device_index(host_index, device_index) wasapi_name = wasapi_info["name"]
if device["index"] == defaultOutputDevice: for host_index in range(p.get_host_api_count()):
default_speakers = device host = p.get_host_api_info_by_index(host_index)
if not default_speakers["isLoopbackDevice"]: if host["name"] == wasapi_name:
for loopback in p.get_loopback_device_info_generator(): device_count = host.get('deviceCount', 0)
if default_speakers["name"] in loopback["name"]: for device_index in range(device_count):
return {"device": loopback} device = p.get_device_info_by_host_api_device_index(host_index, device_index)
return {"device": {"name": "NoDevice"}} if not device.get("isLoopbackDevice", True):
for loopback in p.get_loopback_device_info_generator():
if device["name"] in loopback["name"]:
output_devices.append(loopback)
output_devices = [dict(t) for t in {tuple(d.items()) for d in output_devices}] or [{"name": "NoDevice"}]
buffer_output_devices = sorted(output_devices, key=lambda d: d['index'])
wasapi_info = p.get_host_api_info_by_type(paWASAPI)
default_output_device_index = wasapi_info["defaultOutputDevice"]
for host_index in range(p.get_host_api_count()):
host_info = p.get_host_api_info_by_index(host_index)
device_count = host_info.get('deviceCount', 0)
for device_index in range(0, device_count):
device = p.get_device_info_by_host_api_device_index(host_index, device_index)
if device["index"] == default_output_device_index:
default_speakers = device
if not default_speakers.get("isLoopbackDevice", True):
for loopback in p.get_loopback_device_info_generator():
if default_speakers["name"] in loopback["name"]:
buffer_default_output_device = {"device": loopback}
break
break
if buffer_default_output_device["device"]["name"] != "NoDevice":
break
self.input_devices = buffer_input_devices
self.default_input_device = buffer_default_input_device
self.output_devices = buffer_output_devices
self.default_output_device = buffer_default_output_device
def startMonitoring(self):
self.monitoring_flag = True
while self.monitoring_flag is True:
self.update()
sleep(1)
def stopMonitoring(self):
self.monitoring_flag = False
self.th_monitoring.join()
def getInputDevices(self):
return self.input_devices
def getDefaultInputDevice(self):
return self.default_input_device
def getOutputDevices(self):
return self.output_devices
def getDefaultOutputDevice(self):
return self.default_output_device
device_manager = DeviceManager()
if __name__ == "__main__": if __name__ == "__main__":
print("getOutputDevices()", getOutputDevices()) print("getInputDevices()", device_manager.getInputDevices())
print("getDefaultOutputDevice()", getDefaultOutputDevice()) print("getDefaultInputDevice()", device_manager.getDefaultInputDevice())
print("getOutputDevices()", device_manager.getOutputDevices())
print("getDefaultOutputDevice()", device_manager.getDefaultOutputDevice())