gpt4 book ai didi

python - PyAudio 输入溢出

转载 作者:IT老高 更新时间:2023-10-28 22:11:42 27 4
gpt4 key购买 nike

我正在尝试在 python 中制作实时绘图声音。我需要从我的麦克风中获取 block 。

使用PyAudio,尝试使用

import pyaudio
import wave
import sys

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk)
all.append(data)
print "* done recording"

stream.close()
p.terminate()

之后,我收到以下错误:

* recording
Traceback (most recent call last):
File "gg.py", line 23, in <module>
data = stream.read(chunk)
File "/usr/lib64/python2.7/site-packages/pyaudio.py", line 564, in read
return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981

我无法理解这个缓冲区。我想使用阻塞 IO 模式,所以如果 block 不可用,我想等待这些 block 。但是当我创建除段或 sleep (0.1)之外的尝试时,我听到咔嗒声,所以这不是我想要的。

请为我的问题提出最佳解决方案?

最佳答案

pyaudio.Stream.read() 有一个关键字参数exception_on_overflow,将此设置为False。

对于您的示例代码,如下所示:

import pyaudio
import wave
import sys

chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = chunk)

print "* recording"
all = []
for i in range(0, RATE / chunk * RECORD_SECONDS):
data = stream.read(chunk, exception_on_overflow = False)
all.append(data)
print "* done recording"

stream.close()
p.terminate()

PyAudio documentation更多细节。

关于python - PyAudio 输入溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733903/

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