How do I get a list of my device's audio sample rates using PyAudio or PortAudio?

跟風遠走 提交于 2019-11-29 10:49:48

In the pyaudio distribution, test/system_info.py shows how to determine supported sample rates for devices. See the section that starts at line 49.

In short, you use the PyAudio.is_format_supported method, e.g.


devinfo = p.get_device_info_by_index(1)  # Or whatever device you care about.
if p.is_format_supported(44100.0,  # Sample rate
                         input_device=devinfo['index'],
                         input_channels=devinfo['maxInputChannels'],
                         input_format=pyaudio.paInt16):
  print 'Yay!'

With the sounddevice module, you can do it like that:

import sounddevice as sd

samplerates = 32000, 44100, 48000, 96000, 128000
device = 0

supported_samplerates = []
for fs in samplerates:
    try:
        sd.check_output_settings(device=device, samplerate=fs)
    except Exception as e:
        print(fs, e)
    else:
        supported_samplerates.append(fs)
print(supported_samplerates)

When I tried this, I got:

32000 Invalid sample rate
128000 Invalid sample rate
[44100, 48000, 96000]

You can also check if a certain number of channels or a certain data type is supported. For more details, check the documentation: check_output_settings(). You can of course also check if a device is a supported input device with check_input_settings().

If you don't know the device ID, have a look at query_devices().

I don't think that's still relevant, but this also works with Python 2.6, you just have to remove the parentheses from the print statements and replace except Exception as e: with except Exception, e:.

Directly using Portaudio you can run the command below:

for (int i = 0, end = Pa_GetDeviceCount(); i != end; ++i) {
    PaDeviceInfo const* info = Pa_GetDeviceInfo(i);
    if (!info) continue;
    printf("%d: %s\n", i, info->name);
}

Thanks to another thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!