pattidegner
pattidegner

Reputation: 97

Change audio output device with python

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?

Upvotes: 5

Views: 9470

Answers (2)

Nikolos
Nikolos

Reputation: 1

default_speaker = sc.default_speaker()
print("start - ", default_speaker)
print(sd.query_devices())
sd.default.device = [1, 5]
print(sd.query_devices())
default_speaker = sc.default_speaker()
print("fin - ", default_speaker)

In the terminal, we see that the sound output has switched, but in fact the sound continues to go through the Speakers (GS3), M (0 inputs, 2 outputs). Windows 11.

start - <Speaker Динамики (GS3) (2 channels)>

0 Переназначение звуковых устр. - Input, MME (2 in, 0 out) .> 1 Microphone (GS3), MME (2 in, 0 out) 2 Переназначение звуковых устр. - Output, MME (0 in, 2 out) < 3 Динамики (GS3), MME (0 in, 2 out) 4 Realtek Digital Output (Realtek, MME (0 in, 2 out) 5 LG HDR WFHD (NVIDIA High Defini, MME (0 in, 2 out)

0 Переназначение звуковых устр. - Input, MME (2 in, 0 out) .> 1 Microphone (GS3), MME (2 in, 0 out) 2 Переназначение звуковых устр. - Output, MME (0 in, 2 out) 3 Динамики (GS3), MME (0 in, 2 out) 4 Realtek Digital Output (Realtek, MME (0 in, 2 out) < 5 LG HDR WFHD (NVIDIA High Defini, MME (0 in, 2 out)

fin - <Speaker Динамики (GS3) (2 channels)>

Upvotes: 0

wikwoj
wikwoj

Reputation: 203

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]

Upvotes: 5

Related Questions