When using PyAudio with the RaspberryPi's built-in audio snd_bcm2835, we get this strange behaviour:

python test.py          
# ... after 10 or 15 seconds the sound is stuttering!

This problem doesn't exist when you use an external USB soundcard, a DAC, etc.

How to solve this stuttering problem?

It's very simple to reproduce the error. First you need PyAudio >= 0.2.8 (before, it doesn't support audio out via Callback function). Thus you can't apt-get install pyaudio (obsolete). Install PyAudio with:

sudo apt-get update ; sudo apt-get -y install python-dev portaudio19-dev
git clone http://people.csail.mit.edu/hubert/git/pyaudio.git ; cd pyaudio ; sudo python setup.py install ; cd ..

You need to download 20seconds_sine.wav. Then here is test.py (download link here):

# taken from PyAudio example of playing wav / callback version
# http://people.csail.mit.edu/hubert/pyaudio/docs/#id4

import pyaudio
import wave
import time

wf = wave.open('20seconds_sine.wav', 'rb')

p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    data = wf.readframes(frame_count)
    return (data, pyaudio.paContinue)

stream = p.open(format = pyaudio.paInt16, channels = 2, rate = 44100, frames_per_buffer = 2048, output = True, input = False, output_device_index = 0, stream_callback = callback)


while stream.is_active():




  • Does aplay work? – CL. Mar 8 '15 at 7:56
  • @CL. Yes aplay works. It is strange that this code (example from the PyAudio doc: people.csail.mit.edu/hubert/pyaudio/docs/…) doesn't work. If you have a few seconds, could you try it on your RPi too? – Basj Mar 8 '15 at 10:01
  • Try sudo nice --adjustment=-20 ./test.py – M Noit Jun 2 '15 at 12:07
  • I tried with nice --adjustment=-20 and with higher buffer (8192 instead of 2048), it improves a bit, but the problem always appears (after 30 sec or 40 sec) – Basj Jun 2 '15 at 19:40

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.