-
Notifications
You must be signed in to change notification settings - Fork 0
/
combined.py
340 lines (305 loc) · 11.4 KB
/
combined.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import pyaudio
import wave
import struct
from time import sleep
import threading
import requests
def toggleLights():
#print('toggle lights')
try:
requests.get('https://elvigo.com/vigor/servers/lightscontrol/togglelights.php')
except Exception as e:
print("Internet connection not established")
print(e)
#print('\n\n\n')
smallChunk = 6
bigChunk = 20
startTrigger = 0.2
middleTrigger = 0.4
middleJump = 100
middleJumpTime = 0.003
endTrigger = 0.4 # percentage
endJump = 2300
endJumpTime = 0.085
checkLength = middleJump+endJump+bigChunk
#pa = pyaudio.PyAudio()
#for x in range(0,pa.get_device_count()):
# #print(pa.get_device_info_by_index(x))
form_1 = pyaudio.paInt16 # 16-bit resolution
chans = 1 # 1 channel
samp_rate = 48000 # 44.1kHz sampling rate
chunk = 8192 # 2^12 samples for buffer is default, now set to 2^13 as the sampling rate is higher than normal
record_secs = 1 # seconds to record
dev_index = 2 # device index found by p.get_device_info_by_index(ii)
# wav_output_filename = 'test1.wav' # name of .wav file
audio = pyaudio.PyAudio() # create pyaudio instantiation
print('####################################################\n\n\n\n')
for x in range(0,audio.get_device_count()):
print(audio.get_device_info_by_index(x))
print('\n\n\n\n####################################################')
# create pyaudio stream
stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
input_device_index = dev_index,input = True, \
frames_per_buffer=chunk)
stream.stop_stream()
stream.close()
# stream10 = audio.open(format = form_1,rate = samp_rate,channels = chans, \
# input_device_index = dev_index,input = True, \
# frames_per_buffer=10)
streambigchunk = audio.open(format = form_1,rate = samp_rate,channels = chans, \
input_device_index = dev_index,input = True, \
frames_per_buffer=bigChunk)
print("recording")
# frames = []
# numFrames = 0
# limit = Clap().checkLength*4
#
# loop through stream and append audio chunks to frame array
# for ii in range(0,int((samp_rate/chunk)*record_secs)):
# data = stream.read(chunk)
# frames.append(data)
# numFrames += len(data)
#
##print("finished recording")
def uptime():
with open('/proc/uptime', 'r') as f:
uptime_seconds = float(f.readline().split()[0])
return uptime_seconds
def parseToFloat(numFrames, channels, framesIn):
frames = b''.join(framesIn)
a = struct.unpack("%ih" % (numFrames* channels), frames)
a = [float(val) / pow(2, 15) for val in a]
return a
def checkClap(frames):
##print(frames)
average = 0
for f in frames:
average += abs(f)
#print(average/len(frames))
if average/len(frames) > startTrigger:
#print('\n\nstart: ' + str(average/len(frames)))
sleep(middleJumpTime)
frames = []
data = streambigchunk.read(bigChunk, exception_on_overflow = False)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
average = 0
for f in frames:
average += abs(f)
#print(average/len(frames))
if average/len(frames) > middleTrigger:
#print('peak: ' + str(average/len(frames)))
averagePeak = average
sleep(endJumpTime)
frames = []
data = streambigchunk.read(bigChunk, exception_on_overflow = False)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
average = 0
for f in frames:
average += abs(f)
#print(average/len(frames))
if average < averagePeak*endTrigger:
#print("""it's a clap""")
#print('end: ' + str(average/len(frames)))
return True
return False
isDoubleClap = False
def checkClapEntireArrLambda(startIdx, steps, waveArr):
global isDoubleClap
global smallChunk
global bigChunk
global startTrigger
global middleTrigger
global middleJump
global middleJumpTime
global endTrigger
global endJump
global endJumpTime
global checkLength
clap = False
for idx in range(startIdx, len(waveArr), steps):
##print(idx)
if idx+middleJump+endJump+bigChunk > len(waveArr) or isDoubleClap is True:
break
f = abs(waveArr[idx])
average = 0.0
for i in range(smallChunk):
average += abs(waveArr[idx+i])
average /= smallChunk
if average > startTrigger:
#print('start ' + str(average))
average = 0.0
for i in range(idx+middleJump, idx+middleJump+bigChunk):
average += abs(waveArr[i])
average /= bigChunk
if average > middleTrigger:
#print('middle ' + str(average))
averagePeak = average
average = 0.0
for i in range(idx+middleJump+endJump, idx+middleJump+endJump+bigChunk):
average += abs(waveArr[i])
average /= bigChunk
if average < averagePeak*endTrigger:
#print('end ' + str(average))
#print("""it's a clap""")
clap = True
break
isDoubleClap = True
def checkClapEntireArr(numFrames, channels, frames):
global isDoubleClap
waveArr = parseToFloat(numFrames, channels, frames)
global smallChunk
global bigChunk
global startTrigger
global middleTrigger
global middleJump
global middleJumpTime
global endTrigger
global endJump
global endJumpTime
global checkLength
clap = False
threading.Thread(target=(lambda: checkClapEntireArrLambda(1, 4, waveArr))).start()
threading.Thread(target=(lambda: checkClapEntireArrLambda(2, 4, waveArr))).start()
threading.Thread(target=(lambda: checkClapEntireArrLambda(3, 4, waveArr))).start()
for idx in range(0, len(waveArr), 4):
##print(idx)
if idx+middleJump+endJump+bigChunk > len(waveArr) or isDoubleClap is True:
break
f = abs(waveArr[idx])
average = 0.0
for i in range(smallChunk):
average += abs(waveArr[idx+i])
average /= smallChunk
if average > startTrigger:
#print('start ' + str(average))
average = 0.0
for i in range(idx+middleJump, idx+middleJump+bigChunk):
average += abs(waveArr[i])
average /= bigChunk
if average > middleTrigger:
#print('middle ' + str(average))
averagePeak = average
average = 0.0
for i in range(idx+middleJump+endJump, idx+middleJump+endJump+bigChunk):
average += abs(waveArr[i])
average /= bigChunk
if average < averagePeak*endTrigger:
#print('end ' + str(average))
print("""it's a clap""")
clap = True
return True
break
if isDoubleClap is True:
return True
isDoubleClap = False
if clap is False:
#print("""it's not a clap""")
return False
def checkDoubleClap(frames):
global streambigchunk
##print(frames)
average = 0
for f in frames:
average += abs(f)
##print(average/len(frames))
if average/len(frames) > startTrigger:
#print('\n\nstart: ' + str(average/len(frames)))
sleep(middleJumpTime)
frames = []
data = streambigchunk.read(bigChunk, exception_on_overflow = False)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
average = 0
for f in frames:
average += abs(f)
#print(average/len(frames))
if average/len(frames) > middleTrigger:
#print('peak: ' + str(average/len(frames)))
averagePeak = average
sleep(endJumpTime)
frames = []
data = streambigchunk.read(bigChunk, exception_on_overflow = False)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
average = 0
for f in frames:
average += abs(f)
#print(average/len(frames))
if average < averagePeak*endTrigger:
print("""it's a clap""")
#print('end: ' + str(average/len(frames)))
streambigchunk.stop_stream()
streambigchunk.close()
# stream = audio.open(format = form_1,rate = samp_rate,channels = chans, \
# input_device_index = dev_index,input = True, \
# frames_per_buffer=chunk)
#
# frames = []
# numFrames = 0
#
# #loop through stream and append audio chunks to frame array
print("searching for next clap")
# for ii in range(0,int((samp_rate/chunk)*record_secs)):
# data = stream.read(chunk)
# frames.append(data)
# numFrames += len(data)
#
# stream.stop_stream()
# stream.close()
#
# numFrames /= 2
# return checkClapEntireArr(numFrames, chans, frames)
for ii in range(0, 50):
frames = []
streambigchunk.stop_stream()
streambigchunk.close()
streambigchunk = audio.open(format = form_1,rate = samp_rate,channels = chans, \
input_device_index = dev_index,input = True, \
frames_per_buffer=bigChunk)
data = streambigchunk.read(bigChunk, exception_on_overflow = True)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
# threading.Thread(target=(lambda: checkClap(frames))).start()
if checkClap(frames):
return True
break
print('end')
return False
# run forever
while True:
frames = []
streambigchunk.stop_stream()
streambigchunk.close()
streambigchunk = audio.open(format = form_1,rate = samp_rate,channels = chans, \
input_device_index = dev_index,input = True, \
frames_per_buffer=bigChunk)
data = streambigchunk.read(bigChunk, exception_on_overflow = True)
frames.append(data)
frames = parseToFloat(bigChunk, chans, frames)
# threading.Thread(target=(lambda: checkClap(frames))).start()
if checkDoubleClap(frames):
toggleLights()
# stop the stream, close it, and terminate the pyaudio instantiation
streambigchunk.stop_stream()
streambigchunk.close()
audio.terminate()
#print(str(frames[0]))
#framesArr = str(frames[0]).split("""\\x""")
#outStr = ''
#for h in framesArr:
# outStr += str(int(h, 16))
#print(outStr)
# save the audio frames as .wav file
#wavefile = wave.open(wav_output_filename,'wb')
#wavefile.setnchannels(chans)
#wavefile.setsampwidth(audio.get_sample_size(form_1))
#wavefile.setframerate(samp_rate)
#wavefile.writeframes(b''.join(frames))
#wavefile.close()
#
# if GetClap().checkClap(numFrames, chans, frames):
# #print('\n\n\n\n This is a clap')
# else:
# #print('\n\n\n\n This is not a clap')