PyAudio stream read is blocking (no data) on device with ALSA and pulseaudio

2k views Asked by At

I'm trying to get audio data in Python on a Linux device that runs PulseAudio.

The previous environment in which pyaudio worked was just ALSA. Now the environment runs PulseAudio. I've hooked ALSA into PulseAudio by setting the default ALSA device to be a pulse.

arecord -f dat test.wav and then aplay test.wav work fine for me right now.

However, it seems like pyaudio does not work under these settings.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

stream.read(CHUNK)

stream.read() hangs. strace shows it stuck in a loop of:

poll([{fd=3, events=POLLIN|POLLERR|POLLNVAL}], 1, 3) = 1 ([{fd=3, revents=POLLIN}])

I guess the meta question here is, does a stack of pyaudio -> port audio -> alsa -> PulseAudio work? If not, is there a canonical way of getting audio in Python from PulseAudio? A lot of the top of the python/PulseAudio libraries out there have not had any activity in the past couple years.

My asound.conf looks something like this

pcm.!default{
type plug
route_policy "average"
slave.pcm "asymed"
}

pcm.asymed{
type asym
playback.pcm "pulse_playback"
capture.pcm "pulse_capture"
}

pcm.pulse_playback {
type pulse
device "alsa_output.platform-sound_0.analog-stereo"
}

pcm.pulse_capture {
type pulse
device "alsa_input.platform-sound_1.analog-stereo"
}
0

There are 0 answers