Reputation: 1473
I'm trying to write the most simple program to take two recordings and record two wave files. You can get the raw code: https://gist.github.com/579095ac89fa2fff58db
import pyaudio import wave CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100 p = pyaudio.PyAudio() def record(file_name): stream = p.open(format = FORMAT, channels = CHANNELS, rate = RATE, input = True, frames_per_buffer = CHUNK) frames = [] print "starting recording " + file_name + "." for i in range(0, int(RATE / CHUNK * 2)): data = stream.read(CHUNK) frames.append(data) print "finished recording" stream.stop_stream() stream.close() p.terminate() wf = wave.open(file_name, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() record('first.wav') # try again record('second.wav')
Everything works fine if I only call the record function once, but if I try to call it again, I get: IOError: [Errno -9996] Invalid input device (no default output device).
python record2x.py
starting recording first.wav.
finished recording
Traceback (most recent call last):
File "record2x.py", line 38, in <module>
record('second')
File "record2x.py", line 16, in record
frames_per_buffer = CHUNK)
File "/usr/local/lib/python2.7/site-packages/pyaudio.py", line 750, in open
stream = Stream(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pyaudio.py", line 441, in __init__
self._stream = pa.open(**arguments)
IOError: [Errno -9996] Invalid input device (no default output device)
Upvotes: 2
Views: 13130
Reputation: 4236
Nothing odd. You called p.terminate()
in your function meaning that you dismissed PyAudio completely. So It detached from audio cards and awaits new initialization.
p.terminate()
call only upon exiting of your program.
Also, here, in your function, stream.stop_stream()
is unnecessary as stream.close()
will do the trick. Unless you experience some clickings when starting some audio later, but this may only be when using output=True
, not input=True
.
Upvotes: 3
Reputation: 1473
I was terminating the stream prematurely. I just needed to remove the terminate call.
Upvotes: 1