sounddevice did not work directly for me and I do not think it works without an intermedite virtual device. So the the only workaround I could record audio through Python was through Voicemeeter https://voicemeeter.com/ After instalation the output of print(sd.query_devices()) will include the Voicemeeter devices. Setup your machine sound Output to Voicemeeter Input (VB-Audio Voicemeeter VAIO) and in Voicemeeter Click Hardware out and select Speakers. Now in Python you select "Voicemeeter Out" device number (9 on my Windows Desktop) and run
data = sd.rec(int(sample_rate * duration), samplerate=sample_rate, channels=2, dtype='int16', device=input_device)
If this is close to what you are looking for let me know and I can provide more details