-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhit_counter.py
More file actions
65 lines (51 loc) · 1.45 KB
/
Copy pathhit_counter.py
File metadata and controls
65 lines (51 loc) · 1.45 KB
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
59
60
61
62
63
import pyaudio
import struct
import numpy as np
import matplotlib.pyplot as plt
import time
CHUNK = 1024 * 2
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK
)
# data = stream.read(CHUNK)
# data_int = struct.unpack(str(CHUNK) + 'h', data)
# fig, ax = plt.subplots()
# ax.plot(data_int, '-')
# plt.show()
fig, ax = plt.subplots()
x = np.arange(0, 2 * CHUNK, 2)
line, = ax.plot(x, np.random.rand(CHUNK))
ax.set_ylim(-4096, 4096)
ax.set_xlim(0, CHUNK)
# fig.show()
start = False
start_counter = 0
hit_counter = 0
while True:
data = stream.read(CHUNK)
data_int = struct.unpack(str(CHUNK) + 'h', data)
if start_counter > CHUNK:
start = True
for num in data_int: # look through ever value
if num > 500: # if its greater than 500, it could be a hit
start_counter = 0 # reset start_counter cause its not calm anymore
if start: #if there hasn't been a hit since its calmed down, its a hit
start = False
hit_counter += 1
print('HIT: ' + str(hit_counter))
elif -20 < num < 20: # if its calm, it gets closer to starting
start_counter += 1
# changes graph MAKE SURE TO UNCOMMENT SHOW()
# line.set_ydata(data_int)
# fig.canvas.draw()
# fig.canvas.flush_events()
# changes graph