PyAudio内存错误 [英] PyAudio Memory Error

查看:190
本文介绍了PyAudio内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码有问题,导致内存错误.我相信是由此功能引起的(请参见下文).

I am having an issue with my code which causes a memory error. I believe it is caused by this function (see below).


def sendAudio():
    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = CHANNELS,
                    rate = RATE,
                    input = True,
                    output = True,
                    frames_per_buffer = chunk)

    data = stream.read(chunk)
    client(chr(CMD_AUDIO), encrypt_my_audio_message(data))

def keypress(event):
    if event.keysym == 'Escape':
        root.destroy()
    if event.keysym == 'Control_L':
        #print("Sending Data...")
        sendAudio()
        #print("Data Sent!")

该功能的作用是从麦克风中读取,然后通过网络发送该数据.但是由于任何时候按下该键并且有任何数据都会发送(这可能是白噪声等).有什么办法可以减少毛刺吗?我不确定这是使用按键来解决这种情况的正确方法.

What the function does is read from the microphone then send that data over the network. But since any time the key is pressed and there is any data it sends it (this could be white noise etc). Is there a way I can just have it less glitchy I am not sure this is the right approach to this situation using a keypress I mean.

感谢您的答复,我得到的错误是

Thnak you for your reply the error I get is


Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 552, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 505, in run
    self.__target(*self.__args, **self.__kwargs)
  File "chat.py", line 62, in server
    frames_per_buffer = chunk)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 714, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\pyaudio.py", line 396, in __init__
    self._stream = pa.open(**arguments)
IOError: [Errno Insufficient memory] -9992

推荐答案

您遇到的异常是什么?如果是PortAudio的输入溢出,则可以尝试增加块大小.另外,当缓冲区因白噪声而溢出时,可以通过捕获异常并返回空白流来对其进行处理:

What's the exception you're getting? If it's an input overflow from PortAudio, you can try increasing the chunk size. Also, when the buffer is overflowing on white noise, it can be handled by catching the exception and returning a blank stream:

try:
    data = stream.read(chunk)
except IOError as ex:
    if ex[1] != pyaudio.paInputOverflowed:
        raise
    data = '\x00' * chunk  # or however you choose to handle it, e.g. return None

这篇关于PyAudio内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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