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()