Skip to content

Script to check if audio is played #248

Open

Description

@nicolae-stroncea

I want to check is I am playing any audio on my PC(music, podcasts, from video, etc).

Here is the script for it:

import sounddevice as sd
import numpy as np

duration = 10
fs = 44100
myrecording = sd.rec(int(duration*fs), samplerate=fs, channels=2)
print(np.count_nonzero(myrecording > 0))

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?

Activity

  1. mgeier commented on Jun 17, 2020

    @mgeier
    Member

    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.

  2. tdbe commented on Apr 27, 2026

    @tdbe

    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.

  3. tdbe commented on Apr 28, 2026

    @tdbe

    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.

  4. mgeier commented on Apr 30, 2026

    @mgeier
    Member

    How does it detect the outputs?

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions