-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_align.py
301 lines (271 loc) · 8.24 KB
/
run_align.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
#! /usr/bin/env python3
import argparse
import json
import numpy as np
"""A script to run an audalign alignment"""
parser = argparse.ArgumentParser(description="Niftier running.")
parser.add_argument(
"-f",
"--files",
type=str,
help="files to fingerprint",
required=True,
)
parser.add_argument(
"--fine-align", action="store_true", help="if present, runs a fine alignment"
)
parser.add_argument(
"-d",
"--destination",
type=str,
required=False,
default=None,
)
parser.add_argument("-w", "--write-extention", type=str, required=False, default=None)
parser.add_argument(
"--write-multi-channel",
help="If present, only writes a multi-channel file as output",
action="store_true",
)
parser.add_argument(
"--write-multi-channel-fine",
help="If present, only writes a multi-channel file as output for fine align",
action="store_true",
)
parser.add_argument(
"-t",
"--technique",
type=str,
help="alignment technique",
required=False,
default="fingerprints",
)
parser.add_argument(
"-l",
"--locality",
type=float,
help="locality",
required=False,
default=None,
)
parser.add_argument(
"-m",
"--sample-rate",
type=int,
help="sample rate to read the file in",
required=False,
default=44100,
)
parser.add_argument(
"-a",
"--accuracy",
type=int,
help="accuracy for fingerprints",
required=False,
default=2,
)
parser.add_argument(
"-s",
"--hash-style",
type=str,
help="hash style for fingerprints",
required=False,
default="panako_mod",
)
parser.add_argument(
"-r",
"--threshold",
type=int,
help="frequency threshold",
required=False,
default=100,
)
parser.add_argument(
"-n",
"--num-processors",
type=int,
help="number of processors to use",
required=False,
default=6,
)
parser.add_argument(
"-i",
"--img-width",
type=float,
help="image width for visual",
required=False,
default=0.5,
)
parser.add_argument(
"-v",
"--volume-threshold",
type=float,
help="volume threshold for visual",
required=False,
default=215,
)
parser.add_argument(
"--write_results",
action="store_true",
help='if present, writes results to "last_results.json"',
)
parser.add_argument(
"--fine-technique",
type=str,
help="fine alignment technique",
required=False,
default="correlation",
)
parser.add_argument(
"--fine-locality",
type=float,
help="fine alignment locality",
required=False,
default=None,
)
parser.add_argument(
"--fine-sample-rate",
type=int,
help="fine alignment sample rate to convert the files to",
required=False,
default=8000,
)
parser.add_argument(
"--fine-img-width",
type=float,
help="fine alingment image width for visual",
required=False,
default=0.5,
)
parser.add_argument(
"--fine-volume-threshold",
type=float,
help="fine alignment volume threshold for visual",
required=False,
default=215,
)
args = parser.parse_args()
# print(args.)
# print(args.sample_rate)
# print(args.files)
# print(args.threshold)
# print(args.locality)
# __name__ = "blah"
def main(args):
import os
import pickle
import pprint
import time
import audalign as ad
results = None
results_rank = None
multiprocessing = True
if args.num_processors == 1:
multiprocessing = False
elif args.num_processors == 0:
args.num_processors = None
if args.locality == 0:
args.locality = None
if args.technique == "fingerprints":
recognizer = ad.FingerprintRecognizer()
recognizer.config.set_accuracy(args.accuracy)
recognizer.config.set_hash_style(args.hash_style)
elif args.technique == "correlation":
recognizer = ad.CorrelationRecognizer()
elif args.technique == "correlation_spectrogram":
recognizer = ad.CorrelationSpectrogramRecognizer()
elif args.technique == "visual":
recognizer = ad.VisualRecognizer()
recognizer.config.volume_threshold = args.volume_threshold
recognizer.config.img_width = args.img_width
else:
raise ValueError(
f"technique '{args.technique}' must be 'fingerprints', 'correlation', 'correlation_spectrogram', or 'visual'"
)
recognizer.config.freq_threshold = args.threshold
recognizer.config.num_processors = args.num_processors
recognizer.config.multiprocessing = multiprocessing
recognizer.config.sample_rate = args.sample_rate
recognizer.config.locality = args.locality
t = time.time()
try:
print()
results = ad.align(
args.files,
destination_path=args.destination,
write_extension=args.write_extention,
write_multi_channel=args.write_multi_channel,
recognizer=recognizer,
)
if args.fine_align:
if args.fine_technique == "fingerprints":
recognizer = ad.FingerprintRecognizer()
recognizer.config.set_accuracy(args.accuracy)
recognizer.config.set_hash_style(args.hash_style)
elif args.fine_technique == "correlation":
recognizer = ad.CorrelationRecognizer()
elif args.fine_technique == "correlation_spectrogram":
recognizer = ad.CorrelationSpectrogramRecognizer()
elif args.fine_technique == "visual":
recognizer = ad.VisualRecognizer()
recognizer.config.volume_threshold = args.fine_volume_threshold
recognizer.config.img_width = args.fine_img_width
else:
raise ValueError(
f"fine_technique '{args.fine_technique}' must be 'fingerprints', 'correlation', 'correlation_spectrogram', or 'visual'"
)
recognizer.config.freq_threshold = args.threshold
recognizer.config.num_processors = args.num_processors
recognizer.config.multiprocessing = multiprocessing
recognizer.config.sample_rate = args.sample_rate
recognizer.config.locality = args.locality
results = ad.fine_align(
results,
write_extension=args.write_extention,
write_multi_channel=args.write_multi_channel_fine,
destination_path=args.destination,
)
print()
t = time.time() - t
except KeyboardInterrupt:
t = time.time() - t
print(f"\nRan for {ad.seconds_to_min_hrs(t)}.")
return
if results is not None and args.write_results:
with open("last_results.json", "w") as f:
json.dump(results, f, cls=NpEncoder)
# --------------------------------------------------------------------------
ad.pretty_print_results(results)
sum_, count, max_, min_ = 0, 0, 0, 9999999999
if results is not None:
for target in results["rankings"]["match_info"].keys():
temp_results = results["rankings"]["match_info"][target]
if temp_results == 0:
max_ = max(max_, 0)
min_ = min(min_, 0)
sum_ += 0
count += 1
continue
for rank in temp_results.values():
max_ = max(max_, rank)
min_ = min(min_, rank)
sum_ += rank
count += 1
print()
print(
f"Rankings -- Count: {count}, Sum: {sum_}, Min: {min_}, Average: {sum_ / count}, Max: {max_}"
)
print()
print(f"It took {ad.seconds_to_min_hrs(t)} seconds to complete.")
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
if __name__ == "__main__":
main(args=args)