4

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.

TheFluffDragon9
  • 514
  • 5
  • 11
  • 2
    It does not; In that, question, they are trying to write tones based on frequencies and amplitudes. In my question, I would like to record any sounds that my computer makes and write them to a file. Like if I were to play a song it would all be recorded to a file. I appreciate that I'm not the best at wording questions. Thanks for replying so fast though @EnoGerguri – TheFluffDragon9 Jun 26 '20 at 14:08
  • See https://stackoverflow.com/q/58780206/, https://github.com/spatialaudio/python-sounddevice/issues/129, https://github.com/spatialaudio/python-sounddevice/issues/244. – Matthias Jun 28 '20 at 08:34

3 Answers3

3

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)
GTDT
  • 54
  • 3
  • 2
    also you can get a list of all available devices with `python -m sounddevice` and the active ones will be marked with `<` and `>`. – jnnnnn Jun 09 '22 at 03:15
0

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

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.

Eno Gerguri
  • 639
  • 5
  • 22
  • 1
    This is helps with writing to the wav file but it records sound through my microphone instead of the sounds made by my computer like if i played a video on it; the sound that would come out of my speakers. – TheFluffDragon9 Jun 26 '20 at 14:33
  • @TheFluffDragon9 I edited my answer to change the input device to your speaker. If this does not work, I will delete my answer. – Eno Gerguri Jun 26 '20 at 14:40
  • How do I find my speaker's full name? Is there a way to print a dveice list or somethin? Thanks for all of your help! Is that full stop after `'digital output'` meant to be there btw? – TheFluffDragon9 Jun 26 '20 at 15:13
  • @TheFluffDragon9 if you are using windows you can go to your sound settings and see the name of the output device, that is the one you want to enter. – Eno Gerguri Jun 26 '20 at 15:15
  • Sorry to keep asking questions, but I just want to get this right. If [this](https://imgur.com/7WSbfHl) is what my sound settings say and I want the bottom speakers, what does my line of code look like? – TheFluffDragon9 Jun 26 '20 at 15:25
  • @TheFluffDragon9 `sd.default.device = 'Intel SST Audio Device (WDM)'` you should try the speakers also. – Eno Gerguri Jun 26 '20 at 15:28
  • When I use that line it says: `ValueError: Multiple input devices found for 'Intel SST Audio Device (WDM)': [6] Microphone (Intel SST Audio Device (WDM)), Windows DirectSound [12] Microphone (Intel SST Audio Device (WDM)), Windows WASAPI` – TheFluffDragon9 Jun 26 '20 at 15:32
  • These are both microphone devices; I'm beginning to think it's not possible to record from an output device – TheFluffDragon9 Jun 26 '20 at 15:32
  • Uninstall the one you don’t use, do not worry it will automatically reinstall when you plug it back in. (I think) – Eno Gerguri Jun 26 '20 at 15:33
  • It is talking about two microphones associated with the computer not the two speakers in the image though. I have unplugged the speakers and the error is still the same – TheFluffDragon9 Jun 26 '20 at 15:36
  • This is because both speakers in the image have the same name. – Eno Gerguri Jun 26 '20 at 15:37
  • I know but I have removed one of them and the error is the same because it is talking about Intel SST Audio Device (WDM) microphones not speakers – TheFluffDragon9 Jun 26 '20 at 15:39
  • On the image it has the title of `Speakers` – Eno Gerguri Jun 26 '20 at 15:40
  • Ye but I think it's talking about [this](https://imgur.com/T5EhG57) Since both of the devices say `Microphone` before them in the error – TheFluffDragon9 Jun 26 '20 at 15:43
  • Try: `2270W` as my last try to help you. – Eno Gerguri Jun 26 '20 at 15:50
  • Says it can't find any devices by that name. I appreciate your effort though – TheFluffDragon9 Jun 26 '20 at 15:51
  • @TheFluffDragon9 Sorry I could not help, but I shall leave you with this: https://stackoverflow.com/questions/26573556/record-speakers-output-with-pyaudio – Eno Gerguri Jun 26 '20 at 17:06
  • 1
    You can use `query_devices()` to get a list of devices – Eno Gerguri Jun 26 '20 at 20:22