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

查看:64
本文介绍了使用 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天全站免登陆