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