PyAudio working, but spits out error messages each time PyAudio working, but spits out error messages each time python python

PyAudio working, but spits out error messages each time


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

ALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rearALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfeALSA lib pcm.c:2212:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side

are caused by /usr/share/alsa/alsa.conf:

pcm.rear cards.pcm.rearpcm.center_lfe cards.pcm.center_lfepcm.side cards.pcm.side

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:

#!/usr/bin/env pythonfrom 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 typeERROR_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 handlerasound.snd_lib_error_set_handler(c_error_handler)# Initialize PyAudiop = pyaudio.PyAudio()p.terminate()print '-'*40# Reset to default error handlerasound.snd_lib_error_set_handler(None)# Re-initializep = pyaudio.PyAudio()p.terminate()

An output from my computer:

messages are yummymessages are yummymessages are yummymessages are yummymessages are yummymessages are yummy----------------------------------------ALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slaveALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rearALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfeALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.sideALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback streamALSA lib pcm_dmix.c:1018:(snd_pcm_dmix_open) unable to open slave

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.

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.


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:

from ctypes import *from contextlib import contextmanagerimport pyaudioERROR_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):    passc_error_handler = ERROR_HANDLER_FUNC(py_error_handler)@contextmanagerdef noalsaerr():    asound = cdll.LoadLibrary('libasound.so')    asound.snd_lib_error_set_handler(c_error_handler)    yield    asound.snd_lib_error_set_handler(None)

After doing this you can re-use the error handler by using the noalsaerr context:

with noalsaerr():    p = pyaudio.PyAudio()stream = p.open(format=pyaudio.paFloat32, channels=1, rate=44100, output=1)...


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!