-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsys_sound_util.py
More file actions
39 lines (32 loc) · 1.36 KB
/
sys_sound_util.py
File metadata and controls
39 lines (32 loc) · 1.36 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
import sounddevice as sd
class SysSoundUtil:
@staticmethod
def get_supported_sample_rates_of_device(device_id=None, rates_to_test: list[int]=[]) -> list[int]:
"""
Probe which sample rates are supported by the output device.
"""
if not rates_to_test:
rates_to_test = [22050, 24000, 44100, 48000, 88200, 96000, 176400, 192000]
if device_id is None:
# Get default output device's id
device_info = sd.query_devices(kind='output')
device_id = device_info['index']
supported = []
for rate in rates_to_test:
try:
# Check if device supports this sample rate
sd.check_output_settings(device=device_id, samplerate=rate)
supported.append(rate)
except sd.PortAudioError as e:
# `rate` is not supported
pass
return supported
@staticmethod
def get_current_device_default_sample_rate() -> int:
"""Get the preferred/default sample rate for the output device."""
try:
device_info = sd.query_devices(kind='output')
return int(device_info['default_samplerate'])
except Exception as e:
print("Couldn't get device default samplerate, falling back to 44100:", e)
return 44100