spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
1.02k stars 149 forks source link

How to monitor system sound on windows? #379

Closed realuser closed 2 years ago

realuser commented 2 years ago

I want sounddevice to monitor any sound coming from the system sound (in my particular case I need sounds coming from a web browser on Windows 10) and to detect if they are above a certain amplitude threshold. Is this possible with sounddevice?

Should I use sd.OutputStream for this?

Which device do I need?

C:\>python -m sounddevice
   0 Microsoft Sound Mapper - Output, MME (0 in, 2 out)
<  1 Speakers (High Definition Audio, MME (0 in, 2 out)
   2 Primary Sound Driver, Windows DirectSound (0 in, 2 out)
   3 Speakers (High Definition Audio Device), Windows DirectSound (0 in, 2 out)
   4 Speakers (High Definition Audio Device), Windows WASAPI (0 in, 2 out)
   5 SPDIF Out (HD Audio SPDIF out), Windows WDM-KS (0 in, 2 out)
   6 Speakers (HD Audio Speaker), Windows WDM-KS (0 in, 6 out)
   7 Output (NVIDIA High Definition Audio), Windows WDM-KS (0 in, 2 out)
   8 Output (NVIDIA High Definition Audio), Windows WDM-KS (0 in, 2 out)
   9 Output (), Windows WDM-KS (0 in, 2 out)
  10 Headphones (), Windows WDM-KS (0 in, 2 out)
  11 Headphones (), Windows WDM-KS (0 in, 2 out)

So far outdata is just a steam of zeroes with audio playing in the browser.

import sounddevice as sd

#sd.default.samplerate = 16000
#sd.default.device = 'microsoft sound mapper'
sd.default.device = 'Speakers (High Definition Audio, MME'

duration = 2.0  # seconds

def callback(outdata, frames, time, status):
        if status:
            text = ' ' + str(status) + ' '
            print(text)
        if outdata is not None:
            print(outdata[:, 0])
        else:
            print('no output')

def main():
    with sd.OutputStream(channels=1, callback=callback):
        sd.sleep(int(duration * 1000))

if __name__ == "__main__":
    main()
HaHeho commented 2 years ago

See https://github.com/spatialaudio/python-sounddevice/issues/287.