gpt4 book ai didi

python - PyAudio 内存错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:26:10 25 4
gpt4 key购买 nike

我的代码出现问题,导致内存错误。我相信这是由这个函数引起的(见下文)。

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

该函数的作用是从麦克风读取数据,然后通过网络发送该数据。但是由于任何时候按下键并且它发送任何数据(这可能是白噪声等)。有没有一种方法可以减少故障我不确定这是使用按键来解决这种情况的正确方法我的意思是。

谢谢你的回复,我得到的错误是

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 的输入溢出,您可以尝试增加 block 大小。此外,当缓冲区因白噪声而溢出时,可以通过捕获异常并返回空白流来处理:

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

关于python - PyAudio 内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6560680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com