You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently my assumption is that if no audio is playing, then all elements of numpy array will be <=0. Is this correct?
This code is also listening in on the microphone, whilst I strictly want to record audio being played directly from the PC. Is it possible to only check for that?
Currently my assumption is that if no audio is playing, then all elements of numpy array will be <=0. Is this correct?
Not if you are using an analogue source (e.g. a microphone).
There will be some background noise causing small signal values around 0.
Also, there might be a "DC offset" in your signal.
Typically, one would choose some threshold value to detect "silence".
If you want to check signal values, you shouldn't forget negative values, so you should calculate the absolute signal value before comparing to the threshold.
This code is also listening in on the microphone, whilst I strictly want to record audio being played directly from the PC. Is it possible to only check for that?
It's not straightforward, but depending on your OS you might be able to route all your signals through a "virtual device" which you can then use as input device for the sounddevice module.
Is this still the only way to use sounddevice to check if the PC is currently playing sound? I tried the code in the OP and it always returns false while I'm listening to either builtin speakers or plugged in headphones. How does it detect the outputs?
Also it would be great if we could list all playback options and choose to blacklist some by e.g. name if it has "recording" or "loopback" or "virtual" or "mic" etc.
Note: for Windows, if you're not interested in recording sound, and you're only interested in checking whether audio is playing (and other controls), there's a dedicated python library to do this: pycaw.
The PortAudio library (which is used in the implementation of the sounddevice module) only knows about "input" devices (where it can read audio signals from) and "output" devices (where it can write audio signals to). It cannot "look" into an output device and see whether any other program writes audio to it. Sometimes, but this is highly platform-dependent, there are "loopback" devices which can be used as "output" by some other program and at the same time the signal is "looped back" and it can be used as "input" by PortAudio/sounddevice.
In general, PortAudio does not know whether a device is "loopback" or not, except if you use a pre-release version of PortAudio and you also use WASAPI, then you can use sd._lib.PaWasapi_IsLoopback(), see #392.
If the device name string happens to contain any helpful information, you can of course also check that, but this is not something that would work in the general case.
Activity
mgeier commentedon Jun 17, 2020
Not if you are using an analogue source (e.g. a microphone).
There will be some background noise causing small signal values around 0.
Also, there might be a "DC offset" in your signal.
Typically, one would choose some threshold value to detect "silence".
If you want to check signal values, you shouldn't forget negative values, so you should calculate the absolute signal value before comparing to the threshold.
It's not straightforward, but depending on your OS you might be able to route all your signals through a "virtual device" which you can then use as input device for the
sounddevicemodule.tdbe commentedon Apr 27, 2026
Is this still the only way to use sounddevice to check if the PC is currently playing sound? I tried the code in the OP and it always returns false while I'm listening to either builtin speakers or plugged in headphones. How does it detect the outputs?
Also it would be great if we could list all playback options and choose to blacklist some by e.g. name if it has "recording" or "loopback" or "virtual" or "mic" etc.
tdbe commentedon Apr 28, 2026
Note: for Windows, if you're not interested in recording sound, and you're only interested in checking whether audio is playing (and other controls), there's a dedicated python library to do this: pycaw.
mgeier commentedon Apr 30, 2026
The PortAudio library (which is used in the implementation of the
sounddevicemodule) only knows about "input" devices (where it can read audio signals from) and "output" devices (where it can write audio signals to). It cannot "look" into an output device and see whether any other program writes audio to it. Sometimes, but this is highly platform-dependent, there are "loopback" devices which can be used as "output" by some other program and at the same time the signal is "looped back" and it can be used as "input" by PortAudio/sounddevice.In general, PortAudio does not know whether a device is "loopback" or not, except if you use a pre-release version of PortAudio and you also use WASAPI, then you can use
sd._lib.PaWasapi_IsLoopback(), see #392.If the device name string happens to contain any helpful information, you can of course also check that, but this is not something that would work in the general case.