如何使用PyAudio或PortAudio获取设备的音频采样率列表?

时间:2011-01-07 07:55:22

标签: audio portaudio pyaudio

我想查询我的音频设备并获取所有可用的采样率。我在使用Python 2.6的Ubuntu机器上使用PyAudio 0.2,它运行在PortAudio v19之上。

3 个答案:

答案 0 :(得分:5)

在pyaudio发行版中,test/system_info.py显示了如何确定设备支持的采样率。请参阅section that starts at line 49

简而言之,您使用PyAudio.is_format_supported方法,例如


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

答案 1 :(得分:3)

使用sounddevice模块,您可以这样做:

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)

当我尝试这个时,我得到了:

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

您还可以检查是否支持一定数量的频道或某种数据类型。 有关更多详细信息,请查看文档:{​​{3}}。 您当然也可以使用check_output_settings()检查设备是否是受支持的输入设备

如果您不知道设备ID,请查看check_input_settings()

我不认为这仍然相关,但这也适用于Python 2.6,您只需从print语句中删除括号,并将except Exception as e:替换为{ {1}}。

答案 2 :(得分:1)

直接使用Portaudio,您可以运行以下命令:

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);
}

感谢另一个帖子

相关问题