forked from tanishaness/SPROCTOR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
58 lines (49 loc) · 1.67 KB
/
audio.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sounddevice as sd
import numpy as np
# place holders and global variables
SOUND_AMPLITUDE = 0
AUDIO_CHEAT = 0
# sound variables
# SUS means next sound packet is worth analyzing
CALLBACKS_PER_SECOND = 38 # callbacks per sec(system dependent)
SUS_FINDING_FREQUENCY = 2 # calculates SUS *n* times every sec
SOUND_AMPLITUDE_THRESHOLD = 20 # amplitude considered for SUS calc
# packing *n* frames to calculate SUS
FRAMES_COUNT = int(CALLBACKS_PER_SECOND/SUS_FINDING_FREQUENCY)
AMPLITUDE_LIST = list([0]*FRAMES_COUNT)
SUS_COUNT = 0
count = 0
def print_sound(indata, outdata, frames, time, status):
avg_amp = 0
global SOUND_AMPLITUDE, SUS_COUNT, count, SOUND_AMPLITUDE_THRESHOLD, AUDIO_CHEAT
vnorm = int(np.linalg.norm(indata)*10)
AMPLITUDE_LIST.append(vnorm)
count += 1
AMPLITUDE_LIST.pop(0)
if count == FRAMES_COUNT:
avg_amp = sum(AMPLITUDE_LIST)/FRAMES_COUNT
SOUND_AMPLITUDE = avg_amp
if SUS_COUNT >= 2:
#print("!!!!!!!!!!!! FBI OPEN UP !!!!!!!!!!!!")
AUDIO_CHEAT = 1
SUS_COUNT = 0
if avg_amp > SOUND_AMPLITUDE_THRESHOLD:
SUS_COUNT += 1
#print("Sus...", SUS_COUNT)
else:
SUS_COUNT = 0
AUDIO_CHEAT = 0
count = 0
def sound():
with sd.Stream(callback=print_sound):
sd.sleep(-1)
def sound_analysis():
global AMPLITUDE_LIST, FRAMES_COUNT, SOUND_AMPLITUDE
while True:
AMPLITUDE_LIST.append(SOUND_AMPLITUDE)
AMPLITUDE_LIST.pop(0)
avg_amp = sum(AMPLITUDE_LIST)/FRAMES_COUNT
if avg_amp > 10:
print("Sus...")
if __name__ == "__main__":
sound()