使用PyAudio防止ALSA欠载 [英] Prevent ALSA underruns with PyAudio

查看:255
本文介绍了使用PyAudio防止ALSA欠载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小程序,该程序可以记录麦克风中的语音,并通过网络发送并在那里播放。我正在使用PyAudio执行此任务。它几乎可以正常工作,但是在两台计算机上,我都从ALSA收到发生欠载的错误。我在谷歌上搜索了很多,现在我什至知道欠载了。但我仍然不知道如何解决该问题。大多数时候,声音都很好。但是,如果发生欠载,这听起来有些奇怪。我的代码中有什么需要注意的吗?

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.

我的系统:python:python3.3,操作系统:Linux Mint Debian Edition UP7,PyAudio v0。 2.7

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

推荐答案

您是否考虑过同步声音?
您没有提供代码,所以我想您需要在单独的线程中有一个计时器,该计时器将执行每个CHUNK_SIZE / RATE毫秒代码,如下所示:

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.

这篇关于使用PyAudio防止ALSA欠载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆