-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
83 lines (59 loc) · 1.98 KB
/
utils.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
import numpy as np
import librosa
from tslearn.neighbors import KNeighborsTimeSeries
model = KNeighborsTimeSeries.from_json("model/model.json")
def dtw(candidates, X, window_size, hop_length):
"""
Perform Dynamic Time Warping (DTW) on the candidates to find the closest match.
"""
min_cost = np.inf
result = None
for document in candidates:
vector = np.array(document["vector"])
cost = np.inf
for l in range(0, len(vector) - window_size, hop_length):
Y = vector[l: l + window_size]
Y = Y - np.mean(Y)
D = librosa.sequence.dtw(X, Y, subseq=True, global_constraints=True, band_rad=0.1, backtrack=False)
cost = min(D[-1, -1], cost)
if cost < min_cost:
min_cost = cost
result = document
return result
def hz_to_midi(frequencies):
"""
Convert frequencies to mean centered MIDI note numbers.
"""
note_nums = librosa.hz_to_midi(frequencies)
note_nums = note_nums - np.mean(note_nums)
return note_nums
def knn(collection, X):
"""
Select top candidates from the database.
"""
idx = set(model.kneighbors(X=[X.tolist()], n_neighbors=20, return_distance=False)[0])
candidates = []
for document in collection.find():
if not idx.isdisjoint(set(document["hashes"])):
candidates.append(document)
return candidates
def parse(request):
"""
Parses a request form.
"""
arr = np.fromstring(request.form['signal'], sep=',')
arr = arr / np.max(abs(arr))
fs = int(request.form['sample-rate'])
return arr, fs
def pyin(y, sr):
"""
F0 estimation using Probabilistic YIN (PYIN) algorithm
"""
f0, voiced_flag, voiced_probs = librosa.pyin(y, fmin=librosa.note_to_hz('E2'), fmax=librosa.note_to_hz('C5'), sr=sr)
y = np.isnan(f0)
for i in range(1, len(f0)):
if y[i]:
f0[i] = f0[i - 1]
f0 = f0[~np.isnan(f0)]
f0 = f0[0::4]
return f0