I am trying to record with the input sound and output sound with PyAudio. Looking through the PyAudio documentation (here) and I have determined that I will having to use the input, output, input_device_index
and output_device_index
variables to edit the result of my sound stream.
Given the documentation, I believe that I should have a way to turn input and output on/off and configure what device corresponds to what stream. My devices are:
#{'index': 0, 'name': 'Microsoft Sound Mapper - Input'}
#{'index': 1, 'name': 'Microphone Array (Intel® Smart '}
#{'index': 2, 'name': 'Microsoft Sound Mapper - Output'}
#{'index': 3, 'name': 'Speakers (Realtek(R) Audio)'}
So far, I have the input working as I am able to record any sound detected by the microphone. The problems I'm encountering are:
- Unable to turn input on
- Unable to turn output off
- Unable to get output audio only
- Unable to get both audio streams together
When I try doing input=False
I get the error raise IOError("Not input stream"
. My output stream seems to be not working at all.
My code thus far:
WAVE_OUTPUT_FILENAME = (r"C:\Users\USER\Downloads\output.wav")
p = pyaudio.PyAudio()
#{'index': 0, 'name': 'Microsoft Sound Mapper - Input'}
#{'index': 1, 'name': 'Microphone Array (Intel® Smart '}
#{'index': 2, 'name': 'Microsoft Sound Mapper - Output'}
#{'index': 3, 'name': 'Speakers (Realtek(R) Audio)'}
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
input_device_index=1,
output_device_index=2
)
print("* recording")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()