👍️[Update] Model: Default Audio Deviceの変更/Deviceの追加/削除された場合のみDevice情報を更新するように変更
This commit is contained in:
@@ -14,6 +14,7 @@ openvr==1.26.701
|
|||||||
pydub==0.25.1
|
pydub==0.25.1
|
||||||
psutil==5.9.8
|
psutil==5.9.8
|
||||||
pykakasi==2.3.0
|
pykakasi==2.3.0
|
||||||
|
pycaw==20240210
|
||||||
translators @ git+https://github.com/misyaguziya/translators@5.9.2.1
|
translators @ git+https://github.com/misyaguziya/translators@5.9.2.1
|
||||||
SpeechRecognition @ git+https://github.com/misyaguziya/custom_speech_recognition@3.10.4
|
SpeechRecognition @ git+https://github.com/misyaguziya/custom_speech_recognition@3.10.4
|
||||||
tinyoscquery @ git+https://github.com/cyberkitsune/tinyoscquery@0.1.2
|
tinyoscquery @ git+https://github.com/cyberkitsune/tinyoscquery@0.1.2
|
||||||
@@ -1,6 +1,23 @@
|
|||||||
from time import sleep
|
from time import sleep
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
import comtypes
|
||||||
from pyaudiowpatch import PyAudio, paWASAPI
|
from pyaudiowpatch import PyAudio, paWASAPI
|
||||||
|
from pycaw.callbacks import MMNotificationClient
|
||||||
|
from pycaw.utils import AudioUtilities
|
||||||
|
|
||||||
|
class Client(MMNotificationClient):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.loop = True
|
||||||
|
|
||||||
|
def on_default_device_changed(self, flow, flow_id, role, role_id, default_device_id):
|
||||||
|
self.loop = False
|
||||||
|
|
||||||
|
def on_device_added(self, added_device_id):
|
||||||
|
self.loop = False
|
||||||
|
|
||||||
|
def on_device_removed(self, removed_device_id):
|
||||||
|
self.loop = False
|
||||||
|
|
||||||
class DeviceManager:
|
class DeviceManager:
|
||||||
_instance = None
|
_instance = None
|
||||||
@@ -18,10 +35,8 @@ class DeviceManager:
|
|||||||
self.default_output_device = {"device": {"name": "NoDevice"}}
|
self.default_output_device = {"device": {"name": "NoDevice"}}
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
self.monitoring_flag = True
|
self.monitoring_flag = False
|
||||||
self.th_monitoring = Thread(target=self.startMonitoring)
|
self.startMonitoring()
|
||||||
self.th_monitoring.daemon = True
|
|
||||||
self.th_monitoring.start()
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
buffer_input_devices = {}
|
buffer_input_devices = {}
|
||||||
@@ -96,11 +111,32 @@ class DeviceManager:
|
|||||||
self.output_devices = buffer_output_devices
|
self.output_devices = buffer_output_devices
|
||||||
self.default_output_device = buffer_default_output_device
|
self.default_output_device = buffer_default_output_device
|
||||||
|
|
||||||
|
print("default_input_device", self.default_input_device)
|
||||||
|
|
||||||
|
def monitoring(self):
|
||||||
|
comtypes.CoInitialize()
|
||||||
|
cb = Client()
|
||||||
|
enumerator = AudioUtilities.GetDeviceEnumerator()
|
||||||
|
enumerator.RegisterEndpointNotificationCallback(cb)
|
||||||
|
try:
|
||||||
|
while self.monitoring_flag is True:
|
||||||
|
while cb.loop is True:
|
||||||
|
sleep(1)
|
||||||
|
enumerator.UnregisterEndpointNotificationCallback(cb)
|
||||||
|
self.update()
|
||||||
|
|
||||||
|
cb = Client()
|
||||||
|
enumerator = AudioUtilities.GetDeviceEnumerator()
|
||||||
|
enumerator.RegisterEndpointNotificationCallback(cb)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
comtypes.CoUninitialize()
|
||||||
|
|
||||||
def startMonitoring(self):
|
def startMonitoring(self):
|
||||||
self.monitoring_flag = True
|
self.monitoring_flag = True
|
||||||
while self.monitoring_flag is True:
|
self.th_monitoring = Thread(target=self.monitoring)
|
||||||
self.update()
|
self.th_monitoring.daemon = True
|
||||||
sleep(1)
|
self.th_monitoring.start()
|
||||||
|
|
||||||
def stopMonitoring(self):
|
def stopMonitoring(self):
|
||||||
self.monitoring_flag = False
|
self.monitoring_flag = False
|
||||||
@@ -121,7 +157,10 @@ class DeviceManager:
|
|||||||
device_manager = DeviceManager()
|
device_manager = DeviceManager()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("getInputDevices()", device_manager.getInputDevices())
|
# print("getInputDevices()", device_manager.getInputDevices())
|
||||||
print("getDefaultInputDevice()", device_manager.getDefaultInputDevice())
|
# print("getDefaultInputDevice()", device_manager.getDefaultInputDevice())
|
||||||
print("getOutputDevices()", device_manager.getOutputDevices())
|
# print("getOutputDevices()", device_manager.getOutputDevices())
|
||||||
print("getDefaultOutputDevice()", device_manager.getDefaultOutputDevice())
|
# print("getDefaultOutputDevice()", device_manager.getDefaultOutputDevice())
|
||||||
|
|
||||||
|
while True:
|
||||||
|
sleep(1)
|
||||||
Reference in New Issue
Block a user