voice-changer/server/voice_changer/Local/AudioDeviceList.py

59 lines
2.1 KiB
Python
Raw Normal View History

2023-05-13 08:30:15 +03:00
import sounddevice as sd
2023-05-06 22:18:18 +03:00
from dataclasses import dataclass
from const import ServerAudioDeviceTypes
@dataclass
class ServerAudioDevice:
kind: ServerAudioDeviceTypes = ServerAudioDeviceTypes.audioinput
index: int = 0
name: str = ""
hostAPI: str = ""
2023-05-13 08:30:15 +03:00
maxInputChannels: int = 0
maxOutputChannels: int = 0
2023-05-26 10:26:17 +03:00
default_samplerate: int = 0
2023-05-06 22:18:18 +03:00
def list_audio_device():
2023-06-16 10:12:15 +03:00
try:
audioDeviceList = sd.query_devices()
except Exception as e:
print("[Voice Changer] ex:query_devices")
print(e)
return [], []
2023-05-13 08:30:15 +03:00
inputAudioDeviceList = [d for d in audioDeviceList if d["max_input_channels"] > 0]
2023-05-26 10:26:17 +03:00
outputAudioDeviceList = [d for d in audioDeviceList if d["max_output_channels"] > 0]
2023-05-13 08:30:15 +03:00
hostapis = sd.query_hostapis()
2023-05-16 04:38:23 +03:00
# print("input:", inputAudioDeviceList)
# print("output:", outputDeviceList)
# print("hostapis", hostapis)
2023-05-13 08:30:15 +03:00
2023-06-16 09:06:35 +03:00
serverAudioInputDevices: list[ServerAudioDevice] = []
serverAudioOutputDevices: list[ServerAudioDevice] = []
2023-05-13 08:30:15 +03:00
for d in inputAudioDeviceList:
serverInputAudioDevice: ServerAudioDevice = ServerAudioDevice(
kind=ServerAudioDeviceTypes.audioinput,
index=d["index"],
name=d["name"],
hostAPI=hostapis[d["hostapi"]]["name"],
maxInputChannels=d["max_input_channels"],
maxOutputChannels=d["max_output_channels"],
2023-05-26 10:26:17 +03:00
default_samplerate=d["default_samplerate"],
2023-05-13 08:30:15 +03:00
)
serverAudioInputDevices.append(serverInputAudioDevice)
2023-05-26 10:26:17 +03:00
for d in outputAudioDeviceList:
2023-05-13 08:30:15 +03:00
serverOutputAudioDevice: ServerAudioDevice = ServerAudioDevice(
kind=ServerAudioDeviceTypes.audiooutput,
index=d["index"],
name=d["name"],
hostAPI=hostapis[d["hostapi"]]["name"],
maxInputChannels=d["max_input_channels"],
maxOutputChannels=d["max_output_channels"],
2023-05-26 10:26:17 +03:00
default_samplerate=d["default_samplerate"],
2023-05-13 08:30:15 +03:00
)
serverAudioOutputDevices.append(serverOutputAudioDevice)
return serverAudioInputDevices, serverAudioOutputDevices