0

I'm using the sounddevice library for Python. So far I was able to do some basic stuff with audio files through this library, like playing back a WAV format file using this code from one of the tutorials:

#!/usr/bin/env python3
import sounddevice as sd
import soundfile as sf

try:
    data, fs = sf.read('[path to audio file]', dtype='float32')
    sd.play(data, fs, device=None)
    status = sd.wait()
except KeyboardInterrupt:
    print("Keyboard interrupt")
except Exception as e:
    print("Internal exception")

However, I need to redirect that audio output to the system's microphone input instead of the speakers, and I'm not sure how to do that with this library since there isn't a lot of clear documentation on this. Is there a way to do this with sounddevice? I would like to avoid directly interacting with the OS sound drivers, since ideally this should be cross-platform.

Thanks!

Edit: It seems like it's not possible to make this fully cross-platform, so ideally I would like to be able to use this with Windows and the WSAPI.

javathunderman
  • 1,074
  • 2
  • 13
  • 31
  • There is no cross-platform way to do this. – Matthias Jan 27 '21 at 18:08
  • @Matthias OK, I understand. Is there a way to do this in Windows specifically then? – javathunderman Jan 27 '21 at 23:20
  • If there's no existing code for this online, you probably would have to do it all from scratch. I don't think that's in the scope of a StackOverflow question. – Random Davis Jan 27 '21 at 23:43
  • 1
    @javathunderman I don't know about Windows. But you can search for "virtual audio cable", "loopback device" or something like that. It's really independent from whatever Python library you use, this happens at a lower level. Once you have your virtual loopback cable, it will appear in the device list of the `sounddevice` module. – Matthias Jan 28 '21 at 19:47
  • 1
    You could see this WSAPI document: [Loopback Recording](https://learn.microsoft.com/en-us/windows/win32/coreaudio/loopback-recording) – Drake Wu Feb 01 '21 at 02:20
  • @Matthias @ Drake Wu - MSFT OK, thanks for your help. I'll look into these options. – javathunderman Feb 04 '21 at 16:24

1 Answers1

0

My original reason for asking this question was to create a cross-platform soundboard, and I've since discovered that Soundux works well for this use case.

javathunderman
  • 1,074
  • 2
  • 13
  • 31