0

For a project i need to detect if system is playing some audio through its output speakers (internal or external speaker). Basically i need to get volume level of speaker at that moment so that i can proceed my script if volume level is below from a threshold.

I found some related links here. I tried to update the code but it says 0.00 volume level even when i am playing a youtube video at full sound level. Here is the code which is not working as expected:

If we can make it working. Any insights related to it will be helpful.

import sounddevice as sd
import numpy as np

def audio_callback(indata, frames, time, status):
    volume_norm = np.linalg.norm(indata) * 10
    print("Volume Level:", volume_norm)

def main():
    sd.default.channels = 2  # Set the number of channels (2 for stereo output)
    sd.default.samplerate = 44100  # Set the sample rate (adjust as needed)

    with sd.OutputStream(callback=audio_callback):
        sd.sleep(10000)  # Adjust the duration for measuring the volume

if __name__ == "__main__":
    main()

Pavan Yogi
  • 180
  • 2
  • 7
  • Maybe 0.00 is 0dBFS, maximum level possible in the digital domain – pierpy Jun 28 '23 at 16:29
  • Try to lower the main volume, you should see negative values – pierpy Jun 28 '23 at 16:29
  • I changed the volume level from 0 to maximum and in between as well but every time the code returns as Volume Level: 0.0 only. it looks like some fix is needed in the code. – Pavan Yogi Jun 29 '23 at 08:01

0 Answers0