Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyAudio cannot use microphone on Ubuntu 14.04 with 'unable to open slave'

I have tried several days to use microphone on my ubuntu 14.04 with PyAudio. Actually I want to use 'Speech Recognition' package in the github.

I find it uses pyaudio internal, and It is nightmare on ubuntu 14.04. It shows the following error message and cannot recognize my voice from microphone:

ALSA lib pcm_dsnoop.c:618:(snd_pcm_dsnoop_open) unable to open slave
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2239:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_dmix.c:1022:(snd_pcm_dmix_open) unable to open slave

I find several similar posts on the websites, and there is no solution about this. I cannot record my voice through microphone, it just stucks there. Anyone run it successfully on ubuntu 14.04 ? (PyAudio to record the voice or Speech Recognition)

like image 846
James Fu Avatar asked Nov 17 '15 08:11

James Fu


1 Answers

I don't know if this also solves your problem, but in my case the HDMI-sound-devices and pulse came in the way (also Ubuntu 14.04). Since the indices of the devices seem to change, I came up with this little script that enumerates all available devices and gives me the index of the pulse-device (you may need another device, e.g. if you have a usb-mic or s.th.):

import pyaudio
pa = pyaudio.PyAudio()
chosen_device_index = -1
for x in xrange(0,pa.get_device_count()):
    info = pa.get_device_info_by_index(x)
    print pa.get_device_info_by_index(x)
    if info["name"] == "pulse":
        chosen_device_index = info["index"]
        print "Chosen index: ", chosen_device_index

After that I am able to open the stream:

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input_device_index=chosen_device_index, input=True, output=False)
stream.start_stream()
like image 137
TobiasWeis Avatar answered Sep 30 '22 18:09

TobiasWeis