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 ..
# 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) stream.start_stream() while stream.is_active(): time.sleep(0.1) stream.stop_stream() stream.close() wf.close() p.terminate()