Pyaudio:将输入连接到输出时出错

问题描述 投票:0回答:3

我正在 Intel Edison 主板上尝试 pyaudio,但内置测试失败。单独录音和播放在我的设置下工作得很好,但如果我尝试将输入连接到输出,则会出现错误。

文件“wire_full.py”,第 33 行,位于 data = stream.read(CHUNK) 文件“/usr/lib/python2.7/site-packages/pyaudio.py”,第 605 行,读取 return 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()
python portaudio pyaudio intel-edison
3个回答
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 提供线索! :)


1
投票

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

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()

0
投票

需要在防病毒软件中更改麦克风权限。这是最终的解决方案。然后防病毒软件会阻止 pyaudio

© www.soinside.com 2019 - 2024. All rights reserved.