Skip to content

Instantly share code, notes, and snippets.

@MarcScott
Created October 14, 2020 10:29
Show Gist options
  • Save MarcScott/2b5f5ef4d55346e6ff40a3aec0046e65 to your computer and use it in GitHub Desktop.
Save MarcScott/2b5f5ef4d55346e6ff40a3aec0046e65 to your computer and use it in GitHub Desktop.
Detect claps via mic
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