python - 使用pyaudio在linux/ubuntu上运行Flask应用程序时出现ALSA错误

标签 python audio microphone alsa pyaudio

该应用程序可在Windows 10上按预期工作,但在Linux中崩溃。我正在尝试使用pyaudio(Python 3)在 flask 应用程序中通过麦克风录制音频。我正在Ubuntu 20.04中尝试它。
错误如下:

ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pulse.c:242:(pulse_connect) PulseAudio: Unable to connect: Connection refused

ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_dsnoop.c:641:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm_dmix.c:1089:(snd_pcm_dmix_open) unable to open slave

这是我用来录制音频的代码,逻辑没有错,所有变量都已正确分配,这只是代码的一部分,因此某些变量可能看起来很模糊。
p = pyaudio.PyAudio()
stream = p.open(format=sample_format,
                    channels=channels,
                    rate=fs,
                    frames_per_buffer=chunk,
                    input=True)
    frames = []  # Initialize array to store frames
    for i in range(0, int(fs / chunk * seconds)):
        if( fee =="T"):
            data = stream.read(chunk)
            frames.append(data)
        else:
            break
    # Stop and close the stream 
    stream.stop_stream()
    stream.close()
    # Terminate the PortAudio interface
    p.terminate()

    print('Finished recording')

    # Save the recorded data as a WAV file
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()

最佳答案

默认的麦克风设备可能不是您认为的那种。为了找到正确的麦克风设备(如果有),您可以使用以下方法:

def get_device_index(p):
    device_index = None            
    for i in range(p.get_device_count()):     
        devinfo = p.get_device_info_by_index(i)
        for keyword in ["mic","input"]:
            if keyword in devinfo["name"].lower():
                print( "Found an input: device %d - %s"%(i, devinfo["name"]) )
                device_index = i
                return device_index
    if device_index is None:
        print( "No preferred input found; using default input device." )

    return device_index

然后为了使用此设备索引:

device_index = get_device_index(p)
stream = p.open(format = sample_format,
                channels = channels,
                rate = fs,
                input = True,
                input_device_index = device_index,
                frames_per_buffer = chunk)

取自here

关于python - 使用pyaudio在linux/ubuntu上运行Flask应用程序时出现ALSA错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62040401/

相关文章:

python - 来自 GAE appcfg.py 的虚假 oauth2client.tools.run() 弃用警告(Linux,SDK 1.9.19)?

python - 需要一种使用 Python 将图像文件加载到 Mac 剪贴板的方法

python - PyPy:获取对字符串的原始访问

android - 如何在Android上模拟内置麦克风?

flash - 麦克风检测 Actionscript 3

python - 多项式特征变换

Python - 如何录制系统音频(扬声器的输出)?

ios - 使用 Monotouch 在 iPhone 上重用音频和录音

delphi - 音频文件标记(读/写)库?

swift - 在 OS X 中启用沙箱时 Audiokit 崩溃