voice-changer/client/python/audio_device_list.py

32 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-02-18 03:30:50 +03:00
import pyaudio
2023-02-18 14:52:39 +03:00
import json
2023-02-18 03:30:50 +03:00
if __name__ == '__main__':
audio = pyaudio.PyAudio()
audio_input_devices = []
audio_output_devices = []
audio_devices = {}
host_apis = []
for api_index in range(audio.get_host_api_count()):
host_apis.append(audio.get_host_api_info_by_index(api_index)['name'])
for x in range(0, audio.get_device_count()):
device = audio.get_device_info_by_index(x)
try:
deviceName = device['name'].encode('shift-jis').decode('utf-8')
except (UnicodeDecodeError, UnicodeEncodeError):
deviceName = device['name']
deviceIndex = device['index']
hostAPI = host_apis[device['hostApi']]
if device['maxInputChannels'] > 0:
audio_input_devices.append({"kind": "audioinput", "index": deviceIndex, "name": deviceName, "hostAPI": hostAPI})
if device['maxOutputChannels'] > 0:
audio_output_devices.append({"kind": "audiooutput", "index": deviceIndex, "name": deviceName, "hostAPI": hostAPI})
audio_devices["audio_input_devices"] = audio_input_devices
audio_devices["audio_output_devices"] = audio_output_devices
2023-02-18 14:52:39 +03:00
json_str = json.dumps(audio_devices, indent=2, ensure_ascii=True)
2023-02-18 03:30:50 +03:00
2023-02-18 14:52:39 +03:00
print(json_str)