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

mtrw picture mtrw · Jan 7, 2011 · Viewed 12.7k times · Source

I'd like to query my audio device and get all its available sample rates. I'm using PyAudio 0.2, which runs on top of PortAudio v19, on an Ubuntu machine with Python 2.6.

Answer

John Wiseman picture John Wiseman · Aug 7, 2012

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!'