Pyaudio:将输入连接到输出时出错 [英] Pyaudio: Error when wiring input to output
问题描述
我正在Intel Edison板上尝试pyaudio,但由于内置测试失败.在我的设置下,单独录制和播放可以很好地工作,但是如果我尝试将输入连接到输出,则会出现错误.
文件"wire_full.py",第33行,在 数据= stream.read(CHUNK)文件"/usr/lib/python2.7/site-packages/pyaudio.py",第605行,处于读取状态 返回pa.read_stream(self._stream,num_frames)IOError:[Errno输入溢出] -9981
有人知道是什么问题吗?
下面是在pyaudio中连接输入到输出的示例代码:
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
This is the full duplex version.
"""
import pyaudio
import sys
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
if sys.platform == 'darwin':
CHANNELS = 1
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
print("* recording")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
stream.write(data, CHUNK)
print("* done")
stream.stop_stream()
stream.close()
p.terminate()
我通过将CHUNK大小更改为512来解决了这一问题.偶尔仍然会遇到相同的错误,但至少在大多数情况下都有效.>
如果有任何想法可以解决这个问题,或者我怎么可以完全消除该错误?
更新
好的,所以我用try ..包围了读/写代码.除了将CHUNK大小减小到512,现在它可以正常工作了,没有任何错误.如果我将1024用作CHUNK大小,将会丢失很多帧,并且音质也很差.
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
try:
data = stream.read(CHUNK)
stream.write(data, CHUNK)
except:
pass
感谢@Meta提供线索! :)
I'm trying out pyaudio on Intel Edison board, but it fails with the build-in tests. Recording and playing alone works fine with my setting, but if I'm trying to wire input to output, it gives an error.
File "wire_full.py", line 33, in data = stream.read(CHUNK) File "/usr/lib/python2.7/site-packages/pyaudio.py", line 605, in read return pa.read_stream(self._stream, num_frames) IOError: [Errno Input overflowed] -9981
Does anybody understand what's the problem?
Below is the example code for wiring input to output in pyaudio:
"""
PyAudio Example: Make a wire between input and output (i.e., record a
few samples and play them back immediately).
This is the full duplex version.
"""
import pyaudio
import sys
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
if sys.platform == 'darwin':
CHANNELS = 1
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
print("* recording")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
stream.write(data, CHUNK)
print("* done")
stream.stop_stream()
stream.close()
p.terminate()
I solved it by changing the CHUNK size to 512. Still having the same error once in a while, but at least it works most of the time.
If there's any idea why this works, or how I may remove the error totally?
Update
OK, so I surrounded the read/write code with try.. except, also lowering the CHUNK size to 512, now it's working without any errors. If I use 1024 as the CHUNK size, a lot frames will be lost and the sound quality is terrible.
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
try:
data = stream.read(CHUNK)
stream.write(data, CHUNK)
except:
pass
Thanks @Meta for the clue! :)
这篇关于Pyaudio:将输入连接到输出时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!