-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_devices.py
More file actions
60 lines (50 loc) · 2.37 KB
/
Copy pathlist_devices.py
File metadata and controls
60 lines (50 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sounddevice as sd
def list_audio_devices():
"""
Prints a list of all available audio devices and their properties.
"""
print("Available audio devices:")
# query_devices() returns a list of dictionaries, one for each device
devices = sd.query_devices()
for i, device in enumerate(devices):
# Extract and print relevant information
name = device['name']
hostapi = device['hostapi']
max_in = device['max_input_channels']
max_out = device['max_output_channels']
print(f"* ID {i}: {name}, Host API: {hostapi} (Max Input: {max_in}, Max Output: {max_out})")
def get_default_device_name():
"""
Returns the name of the default input/output device.
"""
# query_devices() with no arguments returns a list of all devices.
# To get info on just the default device, you can use specific parameters.
# A simpler way to get the *current* default device name after it's set
# is to look at the default.device attribute
# First, print all devices to let the user see IDs
list_audio_devices()
try:
# Get the default device information
default_device_id = sd.default.device
# The default.device attribute can be an int or a pair of ints
if isinstance(default_device_id, int):
default_device = sd.query_devices(default_device_id)
print(f"\nDefault device ID is: {default_device_id}")
print(f"Default device name is: {default_device['name']}")
return default_device['name']
elif isinstance(default_device_id, tuple):
# Handle separate default input/output devices
in_id, out_id = default_device_id
in_device = sd.query_devices(in_id)
out_device = sd.query_devices(out_id)
print(f"\nDefault Input ID is: {in_id}, Name: {in_device['name']}")
print(f"Default Output ID is: {out_id}, Name: {out_device['name']}")
return in_device['name'], out_device['name']
except Exception as e:
print(f"\nCould not determine default device name: {e}")
return None
if __name__ == "__main__":
# Ensure the library is installed: pip install sounddevice
# You can also run `python -m sounddevice` in your terminal to see a list of devices.
list_audio_devices()
get_default_device_name()