-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.py
63 lines (51 loc) · 2.1 KB
/
devices.py
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
61
62
63
import sounddevice as sd
import logging
def list_devices(device_type='input'):
"""
List audio devices based on the device type (input or output).
Parameters:
device_type (str): Type of device to list ('input' or 'output').
Returns:
set: A set of indices of available devices of the specified type.
"""
devices = sd.query_devices()
if len(devices) == 0:
raise ValueError("No audio devices found. Make sure you are running this script on a local machine.")
indices = set()
print(f"\n{device_type.capitalize()} Devices:")
for i, device in enumerate(devices):
if (device_type == 'input' and device['max_input_channels'] >= 1) or \
(device_type == 'output' and device['max_output_channels'] >= 1):
print(f"{i}: {device['name']}")
indices.add(i)
return indices
def choose_device(device_type='input'):
"""
Choose an audio device based on the device type (input or output).
Parameters:
device_type (str): Type of device to choose ('input' or 'output').
Returns:
tuple: A tuple containing the device index and device name.
"""
while True:
try:
indices = list_devices(device_type)
index = int(input(f"Enter the index of your {device_type} device: "))
if index in indices:
device_info = sd.query_devices(index)
return index, device_info['name']
else:
print(f"Selected index is not a {device_type} device. Please choose another device.")
except (ValueError, IndexError):
print("Invalid index. Please enter a valid device index from the list.")
def choose_devices():
"""
Choose both input and output devices.
Returns:
tuple: A tuple containing the indices of the chosen input and output devices.
"""
input_device = choose_device('input')
output_device = choose_device('output')
logging.info('[IN] Input device: %s', input_device[1])
logging.info('[OUT] Output device: %s', output_device[1])
return input_device[0], output_device[0]