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

发布于 2024-10-10 18:54:31 字数 97 浏览 3 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮生未歇 2024-10-17 18:54:31

在 pyaudio 发行版中,test/system_info.py 展示了如何确定设备支持的采样率。请参阅 从第 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!'

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

橘味果▽酱 2024-10-17 18:54:31

使用 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]

你可以还要检查是否支持一定数量的通道或某种数据类型。
有关更多详细信息,请查看文档:check_output_settings()
当然,您也可以使用 输入设备 “noreferrer”>check_input_settings()

如果您不知道设备 ID,请查看 query_devices()< /a>.

我认为这仍然不相关,但这也适用于 Python 2.6,您只需从 print 语句中删除括号并将 except Exception 替换为 e: 即可异常除外,e:

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:.

自由如风 2024-10-17 18:54:31

直接使用 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);
}

感谢另一个线程

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文