Search code examples
python-3.xalsapyaudio

Prevent ALSA underruns with PyAudio


I wrote a little program which records voice from the microphone and sends it over network and plays it there. I'm using PyAudio for this task. It works almost fine but on both computers i get errors from ALSA that an underrun occurred. I googled a lot about it and now I know what an underrun even is. But I still don't know how to fix the problem. Most of the time the sound is just fine. But it sounds a little bit strange if underruns occur. Is there anything I should take care of in my code? It feels like I'm doing an simple error and I miss it.

My system: python: python3.3, OS: Linux Mint Debian Edition UP7, PyAudio v0.2.7


Solution

  • Have you considered syncing sound? You didn't provide the code, so my guess is that you need to have a timer in separate thread, that will execute every CHUNK_SIZE/RATE milliseconds code that looks like this:

    silence = chr(0)*self.chunk*self.channels*2 
    out_stream = ... # is the output stream opened in pyaudio
    
    def play(data):
        # if data has not arrived, play the silence
        # yes, we will sacrifice a sound frame for output buffer consistency
        if data == '':
            data = silence
        out_stream.write(data) 
    

    Assuming this code will execute regularly, this way we will always supply some audio data to output audio stream.