Record Speakers Output with Pyaudio

Isolating Input Device Audio and Output Device Audio with PyAudio

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.

record output sound in python

You can install Soundflower, which allows you to create extra audio devices and route audio between them. This way you can define your system's output to the Soundflower device and read the audio from it using PyAudio.

You can also take a look at PyJack, an audio client for Jack.

Default speaker output for all OSes

After gaining more experience, I understood that asking PC to select default speaker on any OS is too much.
Better approach would be to let your user choose Microphone and Speakers by providing a list of available devices and let them check if it's working as expected:

  • first, let them check speakers by making a ping or any other sound from user selected possible speaker device
  • for Mic, let them record themselves and hear the output, if it's correct one

It's pretty easy to do and demo of it can be seen in any common video call apps, such as Zoom.

PyAudio -- How to capture microphone and system sounds in a single stream?

You can use 2 separate threads to record from 2 different devices ( providing separate device Index) into separate Wav files.

Then Mix these 2 files Using the pydub library

from pydub import AudioSegment

speakersound = AudioSegment.from_file("/path/speaker.wav")
micsound = AudioSegment.from_file("/path/mic.wav")

mixsound = speakersound.overlay(micsound)

mixsound.export("/path/mixsound.wav", format='wav')

How to write speaker output to a file sounddevice

You can just specify the output device - for example:

import sounddevice as REC
REC.default.device = 'Speakers (Realtek High Definition Audio), Windows DirectSound'

To get all the sound devices that sounddevice recognizes you can use this command in ur command line:

this:    py -m sounddevice
or this: python -m sounddevice
or this: python3 -m sounddevice

working code for me:

from scipy.io.wavfile import wavWrite
import sounddevice as REC

# Recording properties
SAMPLE_RATE = 44100
SECONDS = 10

# Channels
MONO = 1
STEREO = 2

# Command to get all devices listed: py -m sounddevice
# Device you want to record
REC.default.device = 'VoiceMeeter VAIO3 Output (VB-Audio VoiceMeeter VAIO3), Windows DirectSound'

print(f'Recording for {SECONDS} seconds')

# Starts recording
recording = REC.rec( int(SECONDS * SAMPLE_RATE), samplerate = SAMPLE_RATE, channels = MONO)
REC.wait() # Waits for recording to finish

# Writes recorded data in to the wave file
wavWrite('recording.wav', SAMPLE_RATE, recording)


Related Topics



Leave a reply



Submit