PyAudio working, but spits out error messages each time
Asked Answered
M

9

51

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
Mi answered 17/8, 2011 at 6:19 Comment(0)
G
49

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

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

pcm.rear cards.pcm.rear
pcm.center_lfe cards.pcm.center_lfe
pcm.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 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()

An output from my computer:

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

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.

Guerra answered 19/11, 2012 at 11:50 Comment(2)
This doesn't get rid of the messages from JACK, right?Liss
I wonder what those lines to comment out actually mean, in order to fathom whether their removal would have other effectsRayleigh
S
34

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 contextmanager
import pyaudio

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):
    pass

c_error_handler = ERROR_HANDLER_FUNC(py_error_handler)

@contextmanager
def 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)
...
Stasny answered 16/7, 2013 at 9:37 Comment(1)
That's definitely an elegant solution.Doc
B
6

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

Bethelbethena answered 12/12, 2021 at 17:32 Comment(1)
I was facing ALSA pcm.c under run error. This solution helps to eradicate this error.Flywheel
L
5

The sounddevice module will suppress those messages, see https://github.com/spatialaudio/python-sounddevice/issues/11.

Liss answered 20/3, 2016 at 16:30 Comment(2)
I've given a slightly more verbose answer there: stackoverflow.com/a/36966379Liss
This is the only solution that worked for mePeroneus
C
4

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!

Chickasaw answered 17/8, 2011 at 6:25 Comment(3)
Thanks for the advice! My problem is, the script displays the text needed to record. As soon as you hit record, that text basically gets bumped out of the way by these debug messages.Mi
If your question is how to suppress them, try redirecting the stderr to a file. stderr = open(logfile, 'wb') then sys.stderr = stderr. Let me know if it works.Chickasaw
That seems to make sense, but has no effect. Is it possible the messages are coming through another output stream, for example?Mi
H
4

this will suppress the errors msgs when creating a pyaudio object

it comes from Matthais's awesome post

import time, os, sys, contextlib

@contextlib.contextmanager
def ignoreStderr():
    devnull = os.open(os.devnull, os.O_WRONLY)
    old_stderr = os.dup(2)
    sys.stderr.flush()
    os.dup2(devnull, 2)
    os.close(devnull)
    try:
        yield
    finally:
        os.dup2(old_stderr, 2)
        os.close(old_stderr)

 ...
 ...
 with ignoreStderr():
     self._audio_interface = pyaudio.PyAudio()
Hecht answered 23/12, 2021 at 20:44 Comment(0)
G
1

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.

Gravure answered 27/12, 2016 at 14:33 Comment(0)
H
0

Small additional points:

  1. Make sure you copy alsa.conf to another backup location.
  2. Make sure you Sudo the editor while editing alsa.conf (e.g sudo vi alsa.conf) so that you do not need to change the file permissions of alsa.conf

In my case this still threw out the following ALSA errors:

  • ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
  • ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
  • ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
  • ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Hustings answered 17/5, 2019 at 12:45 Comment(0)
B
0

To also get rid of the messages from JACK (based on what @livibetter suggests for libasound.so, but using libjack.so.0 here)

# Define our error handler type
JACK_ERROR_HANDLER_FUNC = CFUNCTYPE(None)

def py_jack_error_handler():
    pass

jack_c_error_handler = JACK_ERROR_HANDLER_FUNC(py_jack_error_handler)
jack = cdll.LoadLibrary('libjack.so.0')
# Set error handler
jack.jack_set_error_function(jack_c_error_handler)
Bosco answered 4/4 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.