
精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
服务方向
联系方式
PyAudio, PortAudio, and ALSA messages PyAudio,PortAudio和ALSA消息
Monday, November 19, 2012 2012年11月19日,星期一
A StackOverflow question leads to an old question about error message when using PyAudio library, wrapper of PortAudio, when running 当使用PyAudio库(PortAudio的包装器)运行时,StackOverflow问题导致了关于错误消息的旧问题
import pyaudio 导入pyaudio
pyaudio.PyAudio() pyaudio.PyAudio()
, which results the similar messages as follows: ,结果类似的消息如下:
ALSA lib pcm_dsnoop.c:612:(snd_pcm_dsnoop_open) unable to open slave
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
Some developers who use PyAudio or PortAudio do not like these messages, even though their programs do work well regardless of these message. 一些使用PyAudio或PortAudio的开发人员不喜欢这些消息,即使他们的程序不管这些消息如何运行都很好。
Contents 内容
• 1 Who actually blah those? •1谁真正责备那些人?
• 2 Why the message? •2为什么显示消息?
• 3 Solution •3解决方案
• 4 Gotta get rid of them, seriously! •4要认真摆脱它们!
• 5 But dont, really! •5但请不要,真的!
1 Who actually blah those? 1谁惹的祸?
First, you need to know where is the source and the answer is alsa-lib. Line 74, from error.c, the default error handler: 首先,您需要知道来源在哪里,答案是alsa-lib。来自error.c的第74行,默认错误处理程序:
static void snd_lib_error_default(const char *file, int line, const char *function, int err, const char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
vfprintf(stderr, fmt, arg);
if (err)
fprintf(stderr, ": %s", snd_strerror(err));
putc('\n', stderr);
va_end(arg);
}
The message is printed out via the function by the code mentioned in the message. 消息通过消息中提到的代码通过功能打印出来。
2 Why the message? 2为什么显示消息?
Because there is something wrong with ALSA configuration, I am only talking about the message shows during ALSA initialization for your program, not what your code does wrong. 因为ALSA配置有问题,所以我只说的是在您的程序的ALSA初始化期间显示的消息,而不是您的代码做错了什么。
Every time, ALSA is used by a program, configuration will be parsed. There are two typical locations: 每次程序使用ALSA时,都会解析配置。有两个典型的位置:
~/.asoundrc
/etc/asound.conf
There are sourced from /usr/share/alsa/alsa.conf. You may need to test where the problem configuration locates. In my case, its actually in alsa.conf. If I remove: 它们来自/usr/share/alsa/alsa.conf。您可能需要测试问题配置的位置。就我而言,它实际上在alsa.conf中。如果我删除:
pcm.rear cards.pcm.rear
The following message is gone: 以下消息消失了:
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
3 Solution 3解决方案
Do nothing. 不做什么。
First of all, it doesnt affect anything, even the configuration isnt perfect. If you are not familiar with ALSA configuration, you may break something. Just let the message be there. 首先,它不会影响任何东西,即使配置也不完美。如果您不熟悉ALSA配置,则可能会发生问题。只是让消息在那里。
4 Gotta get rid of them, seriously! 4认真摆脱它们!
If you insist, there is a way. But we have to bypass PyAudio and PortAudio since they provide no ways to deal with errors. Actually, there seemed to be one in PortAudio, but the code was commented out. 如果您坚持,那是一种方法。但是我们必须绕过PyAudio和PortAudio,因为它们无法提供处理错误的方法。实际上,PortAudio中似乎只有一个,但是代码已被注释掉。
Using snd_lib_error_set_handler() of alsa-lib to set a new error handler instead of default one as shown above. Since its C, we need to use ctypes. I have look into pyalsa, official Python bindings, and PyAlsaAudio, both do not support setting error handler. I think its because of Variable Arguments. 使用alsa-lib的snd_lib_error_set_handler()设置新的错误处理程序,而不是如上所述的默认错误处理程序。从C开始,我们需要使用ctypes。我研究了pyalsa,官方Python绑定和PyAlsaAudio,它们都不支持设置错误处理程序。我认为这是因为可变参数。
Anyway, I have wrote a simple code to demonstrate: 无论如何,我编写了一个简单的代码来演示:
#!/usr/bin/env python
The code cant take variable arguments, they are just dropped. We are looking a way to silence, so those dont matter. 该代码不能使用变量参数,它们只是被删除。我们正在寻找一种沉默的方式,所以这些都没有关系。
5 But dont, really! 5但是不要,真的!
Dont do it, just let the messages be. If you worry about confusions in users, just do something like: 不要这样做,只是让消息成为可能。如果您担心用户混乱,请执行以下操作:
print "Initializing PyAudio..."
p = pyaudio.PyAudio()
print "(If you see messages above, it's not [Program name]'s fault.)"
print "End of PyAudio initialization."
There is a reason for those messages and there probably is no bugs in your code. 这些消息是有原因的,您的代码中可能没有错误。
If nothing is broken in your code, then fix nothing. 如果您的代码没有任何损坏,则不进行任何修复。