-
Notifications
You must be signed in to change notification settings - Fork 1
/
sickBeetz.py
136 lines (114 loc) · 3.86 KB
/
sickBeetz.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
import numpy
import sys
import librosa
import segmentr
import klassifier
import reconstructor
import os
import timeit
#import sick_beetz_gui
def main(file_path, kit):
time, quantized, labels, inputLength = quantize_and_classify(file_path, klassifier.load_classifier(), False)
print build_output(time, quantized, labels, kit, file_path, inputLength, False)
def timeMain(filename, kit):
start_time = timeit.default_timer()
y,sr = librosa.load(filename, sr=None)
print("librosa.load: ")
print(timeit.default_timer() - start_time)
start_time = timeit.default_timer()
segments = segmentr.segment_audio(y, sr)
print("segmenter: ")
print(timeit.default_timer() - start_time)
start_time = timeit.default_timer()
model = klassifier.load_classifier()
print("Load Klassifier: ")
print(timeit.default_timer() - start_time)
samples = [s[0] for s in segments]
times = [s[1] for s in segments]
labels = []
i = 1
start_time = timeit.default_timer()
for seg in samples:
label = klassifier.use_classifier(model, seg)
labels.append(label)
i += 1
print("all classifications: ")
print(timeit.default_timer() - start_time)
start_time = timeit.default_timer()
quantized_times = quantize_times(y, sr, times)
print("Quantize_times: ")
print(timeit.default_timer() - start_time)
def quantize_times(y, sr, times):
print 'quantizing'
result = []
if not times:
return result
tempo = librosa.beat.beat_track(y, sr)[0]
while tempo > 220:
tempo /= 2
while tempo < 90:
tempo *= 2
beet = 16/tempo
first_time = times[0]
for time in times:
time = time - first_time
time = beet*round(float(time)/beet)
time = time + first_time
result.append(time)
return result
def quantize_and_classify(filename, model, quantized=False):
# load and segment audio signal
y, sr = librosa.load(filename, sr=None)
segments = segmentr.segment_audio(y, sr)
samples = [s[0] for s in segments]
times = [s[1] for s in segments]
labels = []
for seg in samples:
label = klassifier.use_classifier(model, seg)
labels.append(label)
# quantize onset times to estimated tempo
if quantized:
quantized_times = quantize_times(y, sr, times)
else:
quantized_times = times
return times, quantized_times, labels, len(y)
def build_output(times, quantized_times, labels, kit, file_path, inputLength, quantized=False):
# check for empty arrays
if not times or not labels:
return False
labels = [label[0] for label in labels]
# replace beatbox with drums
drums = []
label_to_kit = {}
for label in labels:
if label in label_to_kit:
drum = label_to_kit[label]
else:
drum, ssr = librosa.load('../kits/'+kit+'/'+label+'.wav', sr=None)
label_to_kit[label] = drum
drums.append(drum)
# reconstruct signal from replaced sounds
if quantized:
result = reconstructor.replace(quantized_times, drums, ssr, inputLength)
else:
result = reconstructor.replace(times, drums, ssr, inputLength)
# write output signal to .wav
librosa.output.write_wav(file_path[:-4]+'-out.wav', result, ssr)
return result, ssr
def relative_path(path):
"""
Get file path relative to calling script's directory
:param path: filename or file path
:return: full path name, relative to script location
"""
return os.path.join(os.path.join(os.getcwd(), os.path.dirname(__file__)), path)
if __name__ == "__main__":
# if len(sys.argv) == 1:
# #sick_beetz_gui.main()
# el
if len(sys.argv) == 3:
main(sys.argv[1], sys.argv[2])
elif len(sys.argv) == 4:
timeMain(sys.argv[1], sys.argv[2])
else:
print 'usage: python sickBeetz [path_to_wav] [kit]'