-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
207 lines (160 loc) · 4.9 KB
/
main.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
import sys
import numpy as np
import aubio
import matplotlib.pyplot as plt
def beat_detect(filename, win_s=512, hop_s=256):
samplerate = 0
s = aubio.source(filename, samplerate, hop_s)
samplerate = s.samplerate
o = aubio.tempo("default", win_s, hop_s, samplerate)
# tempo detection delay, in samples
# default to 4 blocks delay to catch up with
delay = 4. * hop_s
# list of beats, in samples
beats = []
# total number of frames read
total_frames = 0
while True:
samples, read = s()
is_beat = o(samples)
if is_beat:
this_beat = int(total_frames - delay + is_beat[0] * hop_s)
beats.append(this_beat)
total_frames += read
if read < hop_s: break
return beats
def onset_detect(filename, win_s=512, hop_s=256):
samplerate = 0
s = aubio.source(filename, samplerate, hop_s)
samplerate = s.samplerate
o = aubio.onset("default", win_s, hop_s, samplerate)
# list of onsets, in samples
onsets = []
# total number of frames read
total_frames = 0
while True:
samples, read = s()
if o(samples):
onsets.append(o.get_last())
total_frames += read
if read < hop_s: break
return onsets
def is_close(num1, num2, max_diff, percent=20):
diff = abs(num1 - num2)
return diff <= max_diff*percent/100
'''
upper = 1 + percent_range/100
lower = 1 - percent_range/100
if num1 >= (num2 * upper) or num1 <= (num2 * lower):
return False
return True
'''
def quantize_beats(beats):
gap = beats[1] - beats[0]
print ("GAP = {}".format(gap / 22050))
rhythm1 = [beats[0]]
p = beats[0]
for i in range(1, len(beats)-1):
next_beat = p + gap
j = i
while (j < len(beats)):
#print ("checking {0} vs {1}".format(next_beat/22050, beats[j]/22050))
if (is_close(next_beat, beats[j], gap) and beats[j] not in rhythm1):
#print ("{} IS GOOD.".format(beats[j] / 22050))
rhythm1.append(beats[j])
p = beats[j]
break
else:
p = rhythm1[-1]
if beats[j] > next_beat:
print ("lol")
break
j = j + 1
# OPTION 1
#p = next_beat
# OPTION 2
#p = beats[i]
#print ("p = {}".format(p/22050))
#print (next_beat / 22050)
print ("rhythm1 = {0}".format(np.array(rhythm1) / 22050))
'''
# plot rhythms
plt.figure(figsize=(18, 2))
plt.xlim(0, 20)
for xc in rhythm1:
plt.axvline(x=xc/22050, color='red')
plt.draw()
'''
rhythm2 = temp_q2(beats, rhythm1)
return (rhythm1, rhythm2)
def temp_q2(beats, rhythm1):
for b in beats:
if b not in rhythm1:
first = b
break
gap = first - beats[0]
print ("GAP = {}".format(gap / 22050))
rhythm2 = [beats[0]]
p = beats[0]
for i in range(1, len(beats)-1):
next_beat = p + gap
j = i
while (j < len(beats)):
#print ("checking {0} vs {1}".format(next_beat/22050, beats[j]/22050))
if (is_close(next_beat, beats[j], gap) and beats[j] not in rhythm2):
#print ("{} IS GOOD.".format(beats[j] / 22050))
rhythm2.append(beats[j])
p = beats[j]
break
else:
p = rhythm2[-1]
j = j+1
print ("rhythm2 = {0}".format(np.array(rhythm2) / 22050))
'''
# plot rhythms
plt.figure(figsize=(18, 2))
plt.xlim(0, 20)
for xc in rhythm2:
plt.axvline(x=xc/22050, color='green')
plt.draw()
'''
return rhythm2
def main():
print("Polyrhythmic Beat Detection")
filename = "input/120BPM34long.wav"
if(len(sys.argv) > 1):
filename = sys.argv[1]
print("Onsets:\n")
onsets = onset_detect(filename)
onsets_t = np.array(onsets) / 22050
# temp
#onsets = np.delete(onsets, 1)
#onsets_t = np.delete(onsets_t, 1)
print(np.array(onsets) / 22050)
'''
# plot onsets
plt.figure(figsize=(18, 2))
plt.xlim(0, 20)
for xc in onsets_t:
plt.axvline(x=xc)
plt.draw()
'''
#beats = beat_detect(filename)
#print("\nBeats:\n")
#print(np.array(beats) / 22050)
(rhythm1, rhythm2) = quantize_beats(onsets)
# Two subplots, the axes array is 1-d
f, (poly, r1, r2) = plt.subplots(3, sharex=True)
for xc in onsets_t:
poly.axvline(x=xc)
for xc in rhythm1:
r1.axvline(x=xc/22050, color='red')
for xc in rhythm2:
r2.axvline(x=xc/22050, color='green')
poly.set_title("Onsets for polyrhythmic 3:4 beat.")
poly.axes.get_yaxis().set_visible(False)
r1.axes.get_yaxis().set_visible(False)
r2.axes.get_yaxis().set_visible(False)
plt.show()
if __name__ == "__main__":
main()