I'm trying to run the following code (an example from the PyAudio documentation) on my Mac (OS 10.7.2):
import pyaudio
import sys
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)
print "* recording"
for i in range(0, 44100 / chunk * RECORD_SECONDS):
data = stream.read(chunk)
stream.write(data, chunk)
print "* done"
stream.stop_stream()
stream.close()
p.terminate()
The error that I'm giving is:
Traceback (most recent call last):
File "PyAudioExample.py", line 24, in <module>
data = stream.read(chunk)
File "/Library/Python/2.7/site-packages/pyaudio.py", line 564, in read
return pa.read_stream(self._stream, num_frames)
IOError: [Errno Input overflowed] -9981
I did a search for this error on Google and found that either making the chunk bigger or smaller could help. I tried this and it made no difference. I also tried adding in the following code to catch an overload exception:
try:
data = stream.read(chunk)
except IOError as ex:
if ex[1] != pyaudio.paInputOverflowed:
raise
data = '\x00' * chunk
That avoided the error, but instead of outputting my input audio, I heard a bunch of loud clicking.
To troubleshoot, I commented out the output=True line and the program ran fine, but did not output anything. I commented out the input=True and read in a Wave file instead and the stream was able to output the audio. I tried creating 2 streams, one for input and one for output, and that didn't work either.
Is there something else that I can do to avoid this error?