Created
October 14, 2020 10:29
-
-
Save MarcScott/2b5f5ef4d55346e6ff40a3aec0046e65 to your computer and use it in GitHub Desktop.
Detect claps via mic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyaudio | |
import numpy as np | |
from time import sleep | |
CHUNK = 2**11 | |
RATE = 44100 | |
audio=pyaudio.PyAudio() | |
stream=audio.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True, | |
frames_per_buffer=CHUNK) | |
while True: | |
data_stream = np.fromstring(stream.read(CHUNK),dtype=np.int16) | |
peak_vol=np.average(np.abs(data_stream))*2 | |
try: | |
if peak_vol > 15000: | |
print('clap') | |
sleep(2) | |
except KeyboardInterrupt: | |
print('exiting') | |
stream.stop_stream() | |
stream.close() | |
audio.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment