How to write speaker output to a file sounddevice
Question:
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.
Answers:
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.
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)
I got this solution working, using device nr (6):
from scipy.io import wavfile
import sounddevice as REC
# to list devices:
# python -m sounddevice
# 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'
# REC.default.device = 'Speakers (Realtek High Definition Audio), Windows DirectSound'
REC.default.device = 6
REC.default.channels = 2,0
"""
0 Microsoft Sound Mapper - Input, MME (2 in, 0 out)
> 1 Desktop Microphone (Trust Webca, MME (2 in, 0 out)
2 Microphone (Realtek(R) Audio), MME (2 in, 0 out)
3 Microsoft Sound Mapper - Output, MME (0 in, 2 out)
< 4 Headphones (Realtek(R) Audio), MME (0 in, 2 out)
5 2 - C27F390 (AMD High Definitio, MME (0 in, 2 out)
6 Primary Sound Capture Driver, Windows DirectSound (2 in, 0 out)
7 Desktop Microphone (Trust Webcam), Windows DirectSound (2 in, 0 out)
8 Microphone (Realtek(R) Audio), Windows DirectSound (2 in, 0 out)
9 Primary Sound Driver, Windows DirectSound (0 in, 2 out)
10 Headphones (Realtek(R) Audio), Windows DirectSound (0 in, 2 out)
11 2 - C27F390 (AMD High Definition Audio Device), Windows DirectSound (0 in, 2 out)
12 Headphones (Realtek(R) Audio), Windows WASAPI (0 in, 2 out)
13 2 - C27F390 (AMD High Definition Audio Device), Windows WASAPI (0 in, 2 out)
14 Desktop Microphone (Trust Webcam), Windows WASAPI (1 in, 0 out)
15 Microphone (Realtek(R) Audio), Windows WASAPI (2 in, 0 out)
16 Headphones (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
17 Microphone (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
18 Microphone (Realtek HD Audio Front Mic input), Windows WDM-KS (2 in, 0 out)
19 Stereo Mix (Realtek HD Audio Stereo input), Windows WDM-KS (2 in, 0 out)
20 Desktop Microphone (Trust Webcam), Windows WDM-KS (1 in, 0 out)
21 Output (AMD HD Audio HDMI out #1), Windows WDM-KS (0 in, 2 out)
"""
print(f'Recording for {SECONDS} seconds')
# Starts recording
recording = REC.rec( int(SECONDS * SAMPLE_RATE), samplerate = SAMPLE_RATE, channels = 2)
REC.wait() # Waits for recording to finish
# Writes recorded data in to the wave file
# wavWrite('recording.wav', SAMPLE_RATE, recording)
wavfile.write('recording.wav', 44100, recording)
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.
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.
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)
I got this solution working, using device nr (6):
from scipy.io import wavfile
import sounddevice as REC
# to list devices:
# python -m sounddevice
# 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'
# REC.default.device = 'Speakers (Realtek High Definition Audio), Windows DirectSound'
REC.default.device = 6
REC.default.channels = 2,0
"""
0 Microsoft Sound Mapper - Input, MME (2 in, 0 out)
> 1 Desktop Microphone (Trust Webca, MME (2 in, 0 out)
2 Microphone (Realtek(R) Audio), MME (2 in, 0 out)
3 Microsoft Sound Mapper - Output, MME (0 in, 2 out)
< 4 Headphones (Realtek(R) Audio), MME (0 in, 2 out)
5 2 - C27F390 (AMD High Definitio, MME (0 in, 2 out)
6 Primary Sound Capture Driver, Windows DirectSound (2 in, 0 out)
7 Desktop Microphone (Trust Webcam), Windows DirectSound (2 in, 0 out)
8 Microphone (Realtek(R) Audio), Windows DirectSound (2 in, 0 out)
9 Primary Sound Driver, Windows DirectSound (0 in, 2 out)
10 Headphones (Realtek(R) Audio), Windows DirectSound (0 in, 2 out)
11 2 - C27F390 (AMD High Definition Audio Device), Windows DirectSound (0 in, 2 out)
12 Headphones (Realtek(R) Audio), Windows WASAPI (0 in, 2 out)
13 2 - C27F390 (AMD High Definition Audio Device), Windows WASAPI (0 in, 2 out)
14 Desktop Microphone (Trust Webcam), Windows WASAPI (1 in, 0 out)
15 Microphone (Realtek(R) Audio), Windows WASAPI (2 in, 0 out)
16 Headphones (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
17 Microphone (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
18 Microphone (Realtek HD Audio Front Mic input), Windows WDM-KS (2 in, 0 out)
19 Stereo Mix (Realtek HD Audio Stereo input), Windows WDM-KS (2 in, 0 out)
20 Desktop Microphone (Trust Webcam), Windows WDM-KS (1 in, 0 out)
21 Output (AMD HD Audio HDMI out #1), Windows WDM-KS (0 in, 2 out)
"""
print(f'Recording for {SECONDS} seconds')
# Starts recording
recording = REC.rec( int(SECONDS * SAMPLE_RATE), samplerate = SAMPLE_RATE, channels = 2)
REC.wait() # Waits for recording to finish
# Writes recorded data in to the wave file
# wavWrite('recording.wav', SAMPLE_RATE, recording)
wavfile.write('recording.wav', 44100, recording)