Prevent ALSA underruns with PyAudio

孤街浪徒 提交于 2019-12-12 09:54:33

问题


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


回答1:


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.




回答2:


It's possible to prevent the underruns by filling silence in if needed. That looks like that:

#...
data = s.recv(CHUNK * WIDTH) # Receive data from peer
stream.write(data) # Play sound
free = stream.get_write_available() # How much space is left in the buffer?
if free > CHUNK # Is there a lot of space in the buffer?
    tofill = free - CHUNK
    stream.write(SILENCE * tofill) # Fill it with silence
#...


来源:https://stackoverflow.com/questions/19230983/prevent-alsa-underruns-with-pyaudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!