覆盆子ALSA声音输出/输入从属

时间:2014-11-21 02:54:34

标签: python audio raspberry-pi alsa slave

我正在尝试设置一个设备进行播放,另一个设备用于捕获,我的nano /etc/asound.conf有这个:

pcm.!default {
        type asym
        playback.pcm "plughw:1,1"
        capture.pcm  "plughw:1,0"
}

ctl.!default {
        type hw
        card 0
}

我可以完美播放声音,但我无法录制声音,我的python代码会抛出这些错误:

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.c:2217:(snd_pcm_open_noupdate) 
Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline

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

这是我正在运行的代码的一部分:

import pyaudio
class Microphone(AudioSource):
    def __init__(self, device_index = None):
        self.device_index = device_index
        self.format = pyaudio.paInt16 # 16-bit int sampling
        self.SAMPLE_WIDTH = pyaudio.get_sample_size(self.format)
        self.RATE = 16000 # sampling rate in Hertz
        self.CHANNELS = 1 # mono audio
        self.CHUNK = 1024 # number of frames stored in each buffer

        self.audio = None
        self.stream = None

    def __enter__(self):
        self.audio = pyaudio.PyAudio()
        self.stream = self.audio.open(
            input_device_index = self.device_index,
            format = self.format, rate = self.RATE, channels = self.CHANNELS, frames_per_buffer = self.CHUNK,
            input = True, # stream is an input stream
        )
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.stream.stop_stream()
        self.stream.close()
        self.stream = None
        self.audio.terminate()

我该如何解决此错误?

提前谢谢。

2 个答案:

答案 0 :(得分:6)

pyaudio基于PortAudio;所有这些愚蠢的消息都是因为PortAudio试图枚举音频设备。

pyaudio设备索引不一定与ALSA卡索引相同。 使用get_device_count()get_device_info_by_index()查找要使用的输入设备。

答案 1 :(得分:2)

我唯一的答案是购买一台具有输入和输出功能的声卡,并将其作为默认卡片。"我想做同样的事情。我甚至尝试修改PyAudio的源代码,但没有成功。所以硬件路线目前是解决方案。