Is there a way that I can use the python library sounddevice to write the output through my speakers to a file? For example if I were to play any sounds through my computer they would be written to a mp4/wav file.
Is there a way that I can use the python library sounddevice to write the output through my speakers to a file? For example if I were to play any sounds through my computer they would be written to a mp4/wav file.
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)
This is a solution: (See comments)
import sounddevice as sd
from scipy.io.wavfile import write
fs = 44100 # Sample rate
seconds = 3 # Duration of recording
sd.default.device = 'digital output' # Speakers full name here
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
sd.wait() # Wait until recording is finished
write('output.wav', fs, myrecording) # Save as WAV file
The above code was from: https://realpython.com/playing-and-recording-sound-python/#python-sounddevice_1 which is a full tutorial in sound in python and how to record and play sound.