2011-07-03 63 views
2

我遇到了導致內存錯誤的代碼問題。我相信這是由這個功能引起的(見下文)。PyAudio內存錯誤

 
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!") 

該功能所做的功能是從麥克風中讀取,然後通過網絡發送該數據。但是,由於任何時候按鍵被按下並且有數據發送它(這可能是白噪聲等)。有沒有辦法我可以讓它不那麼糟糕我不知道這是使用按鍵我的意思是這種情況的正確方法。

Thnak您的回覆我的錯誤是

 
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 

回答

2

什麼是你得到的例外呢?如果它是來自PortAudio的輸入溢出,則可以嘗試增加塊大小。

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 
+0

喜TY爲我的錯誤被張貼在我的編輯 – Andrew

+0

@Andrew回覆:另外,在緩衝區上的白噪聲四溢,可以通過捕捉異常並返回一個空流處理時不當然,如果你已經嘗試過這種方法,但是可能會減少'stream.read'調用中的塊大小。 – ars