PyAudio Memory Error

亡梦爱人 提交于 2020-01-01 16:05:53

问题


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

回答1:


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



回答2:


Try to increasing the chunk size. For the overflow issue, the only think you need to do is replace the code from

data = stream.read(chunk)  

to

data = stream.read(chunk, exception_on_overflow = False)  


来源:https://stackoverflow.com/questions/6560680/pyaudio-memory-error

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