Skip to content

Instantly share code, notes, and snippets.

@rolisz
Created February 2, 2023 15:41
Show Gist options
  • Save rolisz/df3aed4bda88e1a6c8ed33f8672b4b28 to your computer and use it in GitHub Desktop.
Save rolisz/df3aed4bda88e1a6c8ed33f8672b4b28 to your computer and use it in GitHub Desktop.
Script to alert on microphone loudness
import time
from collections import deque
import numpy as np
import sounddevice as sd
from beepy import beep
from infi.systray import SysTrayIcon
last_alert = time.time() - 10
q = deque(maxlen=200)
def print_sound(indata, frames, t, status):
global last_alert
volume_norm = np.linalg.norm(indata) * 10
q.append(volume_norm)
last_elements = [q[i] for i in range(-min(50, len(q)), 0)]
recent_avg_sound = sum(last_elements) / len(last_elements)
num_high_count = len([x for x in q if x > 20])
systray.update(hover_text=f"{recent_avg_sound} decibels")
if num_high_count > 30 or recent_avg_sound > 50:
if time.time() - last_alert > 10:
print(f"You are speaking at {volume_norm:.2f}. Think of Gloria!")
beep(sound="error")
last_alert = time.time()
def quit(systray):
global still_on
still_on = False
menu_options = ()
systray = SysTrayIcon("icon.ico", "Mic check tray icon", menu_options, on_quit=quit)
systray.start()
still_on = True
name = 'Microphone (Yeti Stereo Microph'
with sd.InputStream(device=name,callback=print_sound):
while still_on:
sd.sleep(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment