Pyaudio: Error when wiring input to output

谁说我不能喝 提交于 2019-12-24 03:53:16

问题


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

回答1:


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




回答2:


This code records audio and plays it back immediately:

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


来源:https://stackoverflow.com/questions/29235666/pyaudio-error-when-wiring-input-to-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!