pythonpython-3.xaudioaudio-recordingpython-sounddevice

How to write speaker output to a file sounddevice


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.


Solution

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