-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoodlive.py
More file actions
169 lines (144 loc) · 3.49 KB
/
goodlive.py
File metadata and controls
169 lines (144 loc) · 3.49 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
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
'''
Code for live music playing
Authors: Tusic
'''
import queue
import pyaudio
import wave
import threading
import sys, termios, tty, os, time
import struct
import math
from serial import Serial, SerialException
cxn = Serial('/dev/tty.usbmodem1421', baudrate=9600)
def read_from_file():
'''
reads from the file with the calibration data
'''
with open("list.txt", "r") as ins:
array = []
for line in ins:
array.append(int(line.strip()))
return(array)
arr = list(read_from_file())
def record(queue, amp_threshold=0.4):
'''
Records 60 seconds of music and detects when a beat is played
Adapted and inspired from https://github.com/shunfu/python-beat-detector
'''
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
RECORD_SECONDS = 60
WAVE_OUTPUT_FILENAME = "output.wav"
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
output = True,
frames_per_buffer = chunk)
print("* recording")
maxNormal=1
prevVals=[0,255]
prev=0
all_1 = []
prev_value = 0
for i in range(0, int(round(RATE / chunk * RECORD_SECONDS))):
try:
data = stream.read(chunk)
except:
continue
stream.write(data, chunk)
all_1.append(data)
if len(all_1)>1:
#data = ''.join(all_1)
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
w = wave.open(WAVE_OUTPUT_FILENAME, 'rb')
summ = 0
value = 1
delta = 1
amps = [ ]
for j in range(0, w.getnframes()):
data = struct.unpack('<h', w.readframes(1))
summ += (data[0]*data[0]) / 2
if (j != 0 and (j % 1023) == 0):
value = int(math.sqrt(summ / 1023.0) / 10)
amps.append(value - delta)
summ = 0
tarW=str(amps[0]*1.0/delta/100)
#ser.write(tarW)
if abs(float(tarW)) > amp_threshold and prev_value < amp_threshold:
print('tarW')
print(tarW)
beat_array_tracker(queue)
prev_value = abs(float(tarW))
delta = value
all_1=[]
print("this should never print")
stream.close()
p.terminate()
serr.close()
def beat_array_tracker(queue):
for i in range(queue.qsize()):
# checks if the current beat number is equal to
# the one in the file
if queue.queue[i][0] == 'nbr_beats':
beat_nbr = queue.queue[i][1:][0]
if arr:
# if so turn the page and remove element from
# calibration list
if beat_nbr >= arr[0]:
turn_page()
beat_nbr = 0
arr.pop(0)
# if not, increment by one
else:
beat_nbr += 1
else:
beat_nbr += 1
queue.queue.clear()
queue.put(['nbr_beats', beat_nbr])
break
def turn_page():
'''
sends signal to Arduino to turn page
'''
cxn.write([1])
def arduino_start(queue):
'''
Gets input from the Arduino via serial connection
'''
running = True
while running==True:
result = str(cxn.readline())
print(result[2:])
# checks to see that player wants to start playing
if result[2:7] == 'start':
print('start')
queue.put(['record'])
running = False
def start_live_run():
'''
starts process for live playing
'''
queuel = queue.Queue()
queuel.put(['nbr_beats',0])
print(queuel.qsize())
pool = ThreadPool(processes=2)
b = threading.Thread(target=arduino_start, args=(queuel,))
b.start()
waiting = True
while waiting == True:
for i in range(queuel.qsize()):
if queuel.queue[i][0] == 'record':
record(queuel)
waiting = False
if __name__ == '__main__':
start_live_run()