3

我正在英特尔爱迪生板上尝试 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()
4

2 回答 2

2

我通过将 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 提供线索!:)

于 2015-03-26T17:00:07.080 回答
0

此代码记录音频并立即播放:

import pyaudio
import time
import pickle

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()

def callback(in_data, frame_count, time_info, status):
    return (in_data, pyaudio.paContinue)

stream = p.open(format=p.get_format_from_width(WIDTH),
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():    
    time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()
于 2016-03-29T12:00:37.640 回答