Change audio output device with python

Viewed 3151

I want to capture computer output, run it through a neural network, then play back the audio in real time. So far I have found BlackHole:

import sounddevice as sd
sd.default.device = 'BlackHole 2ch'

This works great for capturing output. The problem is I want to capture the audio with BlackHole, then feed audio back to the speaker output in real time. Setting sd.default.device = "MacBook Pro Speakers does not change the system audio output (it is still going to BlackHole). How can I programmatically change the device audio output?

1 Answers

You can do print(sd.query_devices()) to print out all devices that you may use, for example

>  1 Mikrofon (3 — Urzadzenie zgodne, MME (2 in, 0 out)
   2 Mikrofon (3 — Urzadzenie zgodne, MME (2 in, 0 out)
   3 Mapowanie dzwieku Microsoft - Output, MME (0 in, 2 out)
<  4 Sluchawki (3 — Urzadzenie zgodn, MME (0 in, 2 out)
   5 Glosniki (3 — Urzadzenie zgodne, MME (0 in, 2 out)

Right now you can see, that there is an input device with index 1 and an output device with index 4.

You can now set your pair of in/out devices by typing

sd.default.device = [index_of_input_device,index_of_output_device]

Related