misc/python/clap.py
2022-11-16 01:10:58 +01:00

29 lines
581 B
Python
Executable File

#!/usr/bin/env nix-script
#!>python
#! python3 | pyaudio
## Listen to claps from the microphone.
import pyaudio
import audioop
settings = {
"format": pyaudio.paInt16,
"channels": 2,
"rate": 44100,
"input": True,
"frames_per_buffer": 1024
}
audio = pyaudio.PyAudio()
stream = audio.open(**settings)
chunk = int(settings["rate"] * 0.025)
while True:
noisy = 0
for i in range(5):
block = stream.read(chunk, exception_on_overflow=False)
if audioop.rms(block, 2) > 100:
noisy += 1
if 3 <= noisy < 8:
print("clap")