forked from Illinois-IoT/main-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicrophone.py
40 lines (35 loc) · 836 Bytes
/
microphone.py
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
34
35
36
37
38
39
40
import RPi.GPIO as GPIO
import pyaudio
import audioop
LED = 27
CHUNK = 1024
FORMAT = pyaudio.paInt16
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 10
VOLUME_THRESHOLD = 2000
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
try:
# for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
while True:
data = stream.read(CHUNK)
rms = audioop.rms(data, WIDTH)
if rms > VOLUME_THRESHOLD:
print( "Sound Detected!")
GPIO.output(LED, GPIO.HIGH)
else:
GPIO.output(LED, GPIO.LOW)
except:
stream.stop_stream()
stream.close()
p.terminate()
finally:
GPIO.cleanup()