Geneku2
Geneku2

Reputation: 65

Isolating Input Device Audio and Output Device Audio with PyAudio

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:

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

Upvotes: 0

Views: 1376

Answers (1)

Geneku2
Geneku2

Reputation: 65

Windows actually offers a native way to record your systems audio without having to install to use other solutions. I primarily used this method because I had no idea how to install the PyAudio Fork mentioned here.

If you go to your system's sound settings, assuming its Windows, you will find a recording tab in the upper left hand corner. Upon clicking this, you'll also find a "Stereo Mix" icon which is normally disabled.Sound Settings with Stereo Mix shown2

If you enable this, this channel will capture all the devices output audio without needing to set up anything else. Therefore, you can set input_device_index with whatever the index of Stereo Mix on your computer is.

Upvotes: 0

Related Questions