ALSA 在linuxubuntu上使用pyaudio运行flask应用程序时出现错误。

问题描述 投票:0回答:1

这个应用程序在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()
python audio microphone alsa pyaudio
1个回答
0
投票

默认的麦克风设备可能不是你想的那个。为了找到正确的麦克风设备(如果有的话),你可以使用这个。

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)

取自 此处.

© www.soinside.com 2019 - 2024. All rights reserved.