-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclapper.py
More file actions
33 lines (29 loc) · 790 Bytes
/
clapper.py
File metadata and controls
33 lines (29 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# Copyright (c) 2019 Jeppe Pihl
# All Rights Reserved
#
# Distributed under the "BSD License". See the accompanying LICENSE file.
import time
import array
import pyaudio
CHUNK_SIZE = 1024
MIN_VOLUME = 10000
def clapper(stopped, clap_callback):
stream = pyaudio.PyAudio().open(
format=pyaudio.paInt16,
channels=2,
rate=44100,
input=True,
frames_per_buffer=1024)
last = 0
while True:
if stopped.wait(timeout=0):
break
chunk = array.array('h', stream.read(CHUNK_SIZE))
volume = max(chunk)
if volume >= MIN_VOLUME:
now = time.time()
diff = now - last
if diff > 0.2 and diff < 1.0:
clap_callback()
last = now