0

The application works as intended on windows 10 but crashes in linux. I am trying to record audio through my microphone in a flask application using pyaudio(Python 3). I am trying it in Ubuntu 20.04. The error is as follows:

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

This is the code I am using to record audio, the logic is not wrong and all the variables are correctly assigned, this is just a snippet of the code so some variables might seem vague.

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()
Anol K
  • 11
  • 2

1 Answers1

1

The default microphone device is probably not the one you think it is. In order to find the correct microphone device (if there is one) you can use this:

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

Then in order to use this 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)

Taken from here.

geoph9
  • 357
  • 3
  • 18