PyAudio 工作正常,但每次都会发出错误消息
我正在使用 PyAudio 记录麦克风的输入。
由于音频对我来说录制得很好,我是否应该尝试简单地抑制其错误消息?或者有没有办法解决它们?
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server socket
jack server is not running or cannot be started
I'm using PyAudio to record input from the microphone.
Since the audio is recording fine for me, should I try to simply suppress its error messages? Or would there be a way of resolving them?
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib audio/pcm_bluetooth.c:1613:(audioservice_expect) BT_GET_CAPABILITIES failed : Input/output error(5)
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave
Cannot connect to server socket err = No such file or directory
Cannot connect to server socket
jack server is not running or cannot be started
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
你可以尝试清理你的ALSA配置,例如,
由
/usr/share/alsa/alsa.conf
引起的:一旦你注释掉这些行,那些错误消息就会消失。您可能还想检查
~/.asoundrc
和/etc/asound.conf
。也就是说,其中一些消息表明您的配置存在问题,尽管它们不会造成任何真正的问题。我不建议您清理
alsa.conf
,因为它最初来自ALSA,当您更新alsa-lib时它可能会被覆盖。有一种方法可以在Python中抑制消息,这里是一个示例代码:
我的计算机的输出:
这些消息是由alsa-lib打印出来的,而不是PyAudio或PortAudio。代码直接使用 alsa-lib
snd_lib_error_set_handler
函数来设置错误处理程序py_error_handler
,您可以使用它来删除任何消息。我检查了其他 Python ALSA 绑定、pyalsa 和 PyAlsaAudio,它们不支持设置错误处理程序。然而,PortAudio 上有一个问题,之前所有 ALSA 错误消息似乎都被抑制了。
You can try to clean up your ALSA configuration, for example,
are caused by
/usr/share/alsa/alsa.conf
:Once you comment out these lines, those error message will be gone. You may also want to check
~/.asoundrc
and/etc/asound.conf
.That's said, some of those messages are telling something is wrong in your configuration, though they do not cause any real problem. I do not recommend you clean up the
alsa.conf
, because it's from ALSA originally, it may be overwritten when you update alsa-lib.There is a way to suppress the message in Python, here is a sample code:
An output from my computer:
Those messages are printed out by alsa-lib, not PyAudio or PortAudio. The code directly uses alsa-lib
snd_lib_error_set_handler
function to set an error handlerpy_error_handler
, which you can use it to drop any message.I have checked other Python ALSA bindings, pyalsa and PyAlsaAudio, they do not support setting error handler. However, there is an issue on PortAudio, all ALSA error messages seemed to be suppressed before.
以上都是正确的,也是一个很好的解决方案。我来这里只是为了建议一种更好的重用错误处理程序代码的方法:
执行此操作后,您可以通过使用
noalsaerr
上下文来重用错误处理程序:All of the above is true and a good solution. I just came here to suggest a nicer way of re-using the error handler code:
After doing this you can re-use the error handler by using the
noalsaerr
context:我面临着同样的问题。
只需在命令末尾添加
2>/dev/null
就解决了我的问题。我只是想抑制错误消息,它成功了。例如:
python marvin.py 2>/dev/null
I was facing the same issue.
Simply adding
2>/dev/null
at the end of the command solved the problem for me. I merely wanted to suppress error messages and it did the trick.eg:
python marvin.py 2>/dev/null
sounddevice 模块将抑制这些消息,请参阅 https://github.com/spatialaudio/python-sounddevice/issues/11。
The sounddevice module will suppress those messages, see https://github.com/spatialaudio/python-sounddevice/issues/11.
这些看起来像正常的调试消息,因为它确定如何在您的系统上运行。我不认为你有任何理由不应该压制他们。
您可能可以以某种方式关闭对插孔服务器、蓝牙设备、环绕声等的检测,但这不是必需的,而且您可能会把事情搞砸。不要搞乱正在运行的东西!
Those look like normal debug messages as it figures out how to run on your system. I don't see any reason you shouldn't suppress them.
You can probably turn off detection of
jack
servers, bluetooth devices, surround sound etc. somehow, but it's not necessary and you might screw things up. Don't mess with things that are working!这将在创建 pyaudio 对象时抑制错误消息
它来自 Matthais 的精彩帖子
this will suppress the errors msgs when creating a pyaudio object
it comes from Matthais's awesome post
如果您的默认音频子系统是 Pulseaudio(常见于 Fedora、Ubuntu、Debian),那么最好正确地重新编译 PyAudio 和底层 C 库 Portaudio,仅支持 Pulseaudio,而无需 Jack 和其他子系统。
If your default audio subsystem is Pulseaudio (common for Fedora, Ubuntu, Debian), it is better to properly recompile PyAudio and underlying C library Portaudio with Pulseaudio support only without Jack and other subsystems.
小附加点:
在我的情况下,这仍然会抛出以下 ALSA 错误:
Small additional points:
In my case this still threw out the following ALSA errors:
同时删除来自 JACK 的消息
(基于@livibetter 对 libasound.so 的建议,但此处使用 libjack.so.0)
To also get rid of the messages from JACK
(based on what @livibetter suggests for libasound.so, but using libjack.so.0 here)