misc/python/clap.py

29 lines
581 B
Python
Raw Normal View History

2018-08-05 18:53:07 +02:00
#!/usr/bin/env nix-script
#!>python
2022-09-15 23:22:31 +02:00
#! python3 | pyaudio
2018-08-05 18:53:07 +02:00
## Listen to claps from the microphone.
2022-09-15 23:22:31 +02:00
import pyaudio
import audioop
2018-08-05 18:53:07 +02:00
settings = {
2022-09-15 23:22:31 +02:00
"format": pyaudio.paInt16,
"channels": 2,
"rate": 44100,
"input": True,
"frames_per_buffer": 1024
2018-08-05 18:53:07 +02:00
}
audio = pyaudio.PyAudio()
2022-09-15 23:22:31 +02:00
stream = audio.open(**settings)
chunk = int(settings["rate"] * 0.025)
2018-08-05 18:53:07 +02:00
while True:
2022-09-15 23:22:31 +02:00
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")