PyAudio 工作正常,但每次都会发出错误消息

发布于 2024-11-29 17:17:54 字数 1064 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

来日方长 2024-12-06 17:17:54

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

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

也就是说,其中一些消息表明您的配置存在问题,尽管它们不会造成任何真正的问题。我不建议您清理alsa.conf,因为它最初来自ALSA,当您更新alsa-lib时它可能会被覆盖。

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

#!/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,您可以使用它来删除任何消息。

我检查了其他 Python ALSA 绑定、pyalsa 和 PyAlsaAudio,它们不支持设置错误处理程序。然而,PortAudio 上有一个问题,之前所有 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

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.

路还长,别太狂 2024-12-06 17:17:54

以上都是正确的,也是一个很好的解决方案。我来这里只是为了建议一种更好的重用错误处理程序代码的方法:

from contextlib import contextmanager
from ctypes import CFUNCTYPE, c_char_p, c_int, cdll
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)

执行此操作后,您可以通过使用 noalsaerr 上下文来重用错误处理程序:

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

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 contextlib import contextmanager
from ctypes import CFUNCTYPE, c_char_p, c_int, cdll
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)
...

我面临着同样的问题。

只需在命令末尾添加 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

尝蛊 2024-12-06 17:17:54

这些看起来像正常的调试消息,因为它确定如何在您的系统上运行。我不认为你有任何理由不应该压制他们。

您可能可以以某种方式关闭对插孔服务器、蓝牙设备、环绕声等的检测,但这不是必需的,而且您可能会把事情搞砸。不要搞乱正在运行的东西!

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!

给我一枪 2024-12-06 17:17:54

这将在创建 pyaudio 对象时抑制错误消息

它来自 Matthais 的精彩帖子

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()

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()
断爱 2024-12-06 17:17:54

如果您的默认音频子系统是 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.

旧人九事 2024-12-06 17:17:54

小附加点:

  1. 确保将 alsa.conf 复制到另一个备份位置。
  2. 确保在编辑 alsa.conf 时使用 Sudo 编辑器(例如 sudo vi alsa.conf),这样就不需要更改 alsa.conf 的文件权限。

在我的情况下,这仍然会抛出以下 ALSA 错误:

  • ALSA lib pcm_route。 c:867:(find_matching_chmap) 找不到匹配的通道映射
  • ALSA lib pcm_route.c:867:(find_matching_chmap) 找不到匹配的通道映射
  • ALSA lib pcm_route.c:867:(find_matching_chmap) 未找到匹配的通道映射
  • ALSA lib pcm_route.c:867:(find_matching_chmap) 未找到匹配的通道映射

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
[浮城] 2024-12-06 17:17:54

同时删除来自 JACK 的消息
(基于@livibetter 对 libasound.so 的建议,但此处使用 libjack.so.0)

# 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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文