Pyaudio支持所有格式,尽管声卡不支持

1 投票
1 回答
934 浏览
提问于 2025-04-18 05:55

我正在尝试确定音频卡是否支持某种格式(比如采样率/位深)。我发现无论我指定什么采样率和位深,pyaudio总是返回True。那么我该如何只显示音频卡本身支持的模式呢?我在Windows、Mac、Ubuntu和Fedora上都复现了这个问题。我附上了一段可以工作的代码片段,希望能帮助解决这个问题。

import pyaudio

pa = pyaudio.PyAudio()

try:
    default_device_id = pa.get_default_output_device_info()['index']
except IOError:
    print ("There is no default device")
    exit(1)
try:
    result = pa.is_format_supported(rate=48000, output_device=default_device_id, output_channels=2, output_format = pyaudio.paFloat32)
    print("Unexpected, device does not really support this result was: %s" % result)
except ValueError:
    print("Expected Unsupported Device")

1 个回答

0

这段话讲的是PortAudio这个库背后用的API。简单来说,如果你在Windows上使用默认设备,很可能是在用DirectSound。DirectSound会告诉PortAudio,它请求的任何音频格式都是支持的,然后再把这些格式转换成硬件实际支持的格式,PortAudio对此并不知情。如果想要绕过这个问题,你需要让PortAudio使用更底层的主机API,比如在Windows上使用ASIO或者独占模式的WASAPI(在其他操作系统上可能是ALSA,但我只是猜的)。

撰写回答