PyAudio 工作正常,但每次都会吐出错误消息 [英] PyAudio working, but spits out error messages each time

查看:31
本文介绍了PyAudio 工作正常,但每次都会吐出错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 PyAudio 来记录来自麦克风的输入.

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

推荐答案

你可以尝试清理一下你的ALSA配置,例如

You can try to clean up your ALSA configuration, for example,

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

/usr/share/alsa/alsa.conf引起:

pcm.rear cards.pcm.rear
pcm.center_lfe cards.pcm.center_lfe
pcm.side cards.pcm.side

一旦你注释掉这些行,那些错误信息就会消失.您可能还想检查 ~/.asoundrc/etc/asound.conf.

Once you comment out these lines, those error message will be gone. You may also want to check ~/.asoundrc and /etc/asound.conf.

也就是说,其中一些消息表明您的配置有问题,尽管它们不会导致任何实际问题.不建议你清理alsa.conf,因为它原本是来自ALSA,更新alsa-lib时可能会被覆盖.

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.

Python 中有一种方法可以抑制消息,这里是一个示例代码:

There is a way to suppress the message in Python, here is a sample code:

#!/usr/bin/env python
from ctypes import *
import pyaudio

# From alsa-lib Git 3fd4ab9be0db7c7430ebd258f2717a976381715d
# $ grep -rn snd_lib_error_handler_t
# include/error.h:59:typedef void (*snd_lib_error_handler_t)(const char *file, int line, const char *function, int err, const char *fmt, ...) /* __attribute__ ((format (printf, 5, 6))) */;
# Define our error handler type
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
def py_error_handler(filename, line, function, err, fmt):
  print 'messages are yummy'
c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)

asound = cdll.LoadLibrary('libasound.so')
# Set error handler
asound.snd_lib_error_set_handler(c_error_handler)
# Initialize PyAudio
p = pyaudio.PyAudio()
p.terminate()

print '-'*40
# Reset to default error handler
asound.snd_lib_error_set_handler(None)
# Re-initialize
p = pyaudio.PyAudio()
p.terminate()

我电脑的输出:

messages are yummy
messages are yummy
messages are yummy
messages are yummy
messages are yummy
messages are yummy
----------------------------------------
ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
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

这些消息是由 alsa-lib 打印的,而不是 PyAudio 或 PortAudio.代码直接使用alsa-lib的snd_lib_error_set_handler函数来设置一个错误处理程序py_error_handler,你可以用它来丢弃任何消息.

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 handler py_error_handler, which you can use it to drop any message.

我检查了其他 Python ALSA 绑定,pyalsa 和 PyAlsaAudio,它们不支持设置错误处理程序.但是,PortAudio 上存在问题,之前似乎所有 ALSA 错误消息都被抑制了.

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.

这篇关于PyAudio 工作正常,但每次都会吐出错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆