0

I'm writing some code that plays audio on a Raspberry Pi Zero while a button is pressed. When I test the code on my laptop, using mock pins to simulate button presses, the code below runs fine with no stutter. However, when I run this on a rPi zero, it stops and starts noticeably. It plays 'normal' chunks that are 200ms long most of the time, but about every third chunk it plays nothing...

I'm using an external sound card I bought off amazon, here, and forcing the pi to use it by blacklisting the built-in sound card. I did this by making this file:

pi@raspberry:/etc/modprobe.d $ cat alsa-blacklist.conf
blacklist snd_bcm2835

pi@raspberry:/etc/modprobe.d $ 

Here's a MWE of the code producing the error:

import wave
import pyaudio

fname = 'AUDIO_FILES/mortal_kombat.wav'

# Open audio stream for writing and file for reading
audio_stream = pyaudio.PyAudio()
audio_file = wave.open(fname, 'rb')

# Get the format of the audio file, number of channels (L,R speakers?), and framerate
width = audio_file.getsampwidth()
fmt = audio_stream.get_format_from_width(width)
n_channels = audio_file.getnchannels()
rate = audio_file.getframerate()
CHUNKSIZE = 1024

# Begin stream. This gets written to like a sdtout, only it comes
# out of your speakers not your terminal
stream = audio_stream.open(
    format=fmt,
    channels=n_channels,
    rate=rate,
    output=True,
)

data = audio_file.readframes(CHUNKSIZE)
while data:
    stream.write(data)
    data = audio_file.readframes(CHUNKSIZE)

# close stuff gracefully.
stream.stop_stream()
stream.close()
audio_stream.terminate()

If I run top while this is going, CPU usage is only down at ~15%. I'm honestly a bit surprised at how much trouble I'm having here. Timing how long reading data from the file takes shows it only using about 0.00043s, so I'm fairly sure the problem is during writing to the stream. Does anyone have any ideas?

EDIT:

I should probably include some timings. This is for a chunk size of 1024, and a rate of 44000Hz.

We expect writing to the stream to take 0.02322s
Took  0.00033s to write the chunk to stream,  0.00031s to read it from file
Took  0.00029s to write the chunk to stream,  0.00097s to read it from file
Took  0.02832s to write the chunk to stream,  0.00032s to read it from file
Took  0.04493s to write the chunk to stream,  0.00032s to read it from file
Took  0.00029s to write the chunk to stream,  0.00029s to read it from file
Took  0.06248s to write the chunk to stream,  0.00031s to read it from file
Took  0.00033s to write the chunk to stream,  0.00028s to read it from file
Took  0.02102s to write the chunk to stream,  0.00032s to read it from file
Took  0.04488s to write the chunk to stream,  0.00032s to read it from file
Took  0.00027s to write the chunk to stream,  0.00029s to read it from file
Took  0.05545s to write the chunk to stream,  0.00034s to read it from file
Took  0.00034s to write the chunk to stream,  0.00028s to read it from file
Took  0.02099s to write the chunk to stream,  0.00032s to read it from file
Took  0.04494s to write the chunk to stream,  0.00032s to read it from file
Took  0.00029s to write the chunk to stream,  0.00028s to read it from file
Took  0.07553s to write the chunk to stream,  0.00031s to read it from file
Took  0.00034s to write the chunk to stream,  0.00031s to read it from file
Took  0.02092s to write the chunk to stream,  0.00032s to read it from file
Took  0.04495s to write the chunk to stream,  0.00032s to read it from file
Took  0.00027s to write the chunk to stream,  0.00034s to read it from file
Took  0.05325s to write the chunk to stream,  0.00042s to read it from file
Took  0.00035s to write the chunk to stream,  0.00045s to read it from file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.