-
Notifications
You must be signed in to change notification settings - Fork 0
/
pitchtools.py
186 lines (174 loc) · 7.64 KB
/
pitchtools.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
import math
import numpy as np
from scipy import signal
def pitch_shift_linear(sig, ratio):
"""Shifts pitch using linear interpolator"""
n_res = round(len(sig) / ratio)
res = np.zeros(n_res, np.float32)
for ires in range(len(res)):
isig = int(ires * ratio)
x = ires * ratio - isig
if isig + 1 < len(sig):
# Linear interpolation
res[ires] = sig[isig] + x * (sig[isig + 1] - sig[isig])
elif isig < len(sig):
# Fallback to nearest value if we are at the boundary
res[ires] = sig[isig]
else:
res[ires] = 0.0
return res.astype(np.float32)
def pitch_shift_ovs2_linear(sig, ratio, upsample=True):
"""Shifts pitch using linear interpolator and 2x oversampling."""
if upsample:
sig = signal.resample_poly(sig, 2, 1)
n_res = round(len(sig) / ratio)
res = np.zeros(n_res, np.float32)
for ires in range(len(res)):
isig = int(ires * ratio)
x = ires * ratio - isig
if isig + 1 < len(sig):
res[ires] = sig[isig] + x * (sig[isig + 1] - sig[isig])
elif isig < len(sig):
# Fallback to nearest value if we are at the boundary
res[ires] = sig[isig]
else:
res[ires] = 0.0
return signal.resample_poly(res, 1, 2).astype(np.float32)
def pitch_shift_ovs2_poly_6p5o_orig(sig, ratio, upsample=True):
"""Shifts pitch using 6-point 5-order polynomial interpolator and 2x
oversampling.
This is the original version downsampling resulting signal with
signal.resample_poly().
"""
# The polynomial is taken from the publication "Polynomial Interpolators
# for High-Quality Resampling of Oversampled Audio" by Olli Niemitalo, 2001
if upsample:
sig = signal.resample_poly(sig, 2, 1)
n_res = round(len(sig) / ratio)
res = np.zeros(n_res, np.float32)
for ires in range(len(res)):
isig = int(ires * ratio)
x = ires * ratio - isig
if 2 <= isig < len(sig) - 3:
# Polynomial interpolation
z = x - 1/2.0
even1 = sig[isig + 1] + sig[isig]
odd1 = sig[isig + 1] - sig[isig]
even2 = sig[isig + 2] + sig[isig - 1]
odd2 = sig[isig + 2] - sig[isig - 1]
even3 = sig[isig + 3] + sig[isig - 2]
odd3 = sig[isig + 3] - sig[isig - 2]
c0 = even1*0.40513396007145713 + even2 * \
0.09251794438424393 + even3*0.00234806603570670
c1 = odd1*0.28342806338906690 + odd2 * \
0.21703277024054901 + odd3*0.01309294748731515
c2 = even1*-0.191337682540351941 + even2 * \
0.16187844487943592 + even3*0.02946017143111912
c3 = odd1*-0.16471626190554542 + odd2*- \
0.00154547203542499 + odd3*0.03399271444851909
c4 = even1*0.03845798729588149 + even2*- \
0.05712936104242644 + even3*0.01866750929921070
c5 = odd1*0.04317950185225609 + odd2*- \
0.01802814255926417 + odd3*0.00152170021558204
res[ires] = ((((c5*z+c4)*z+c3)*z+c2)*z+c1)*z+c0
if isig + 1 < len(sig):
# Fallback to linear if we are too close to boundary
res[ires] = sig[isig] + x * (sig[isig + 1] - sig[isig])
elif isig < len(sig):
# Fallback to nearest value if we are at the boundary
res[ires] = sig[isig]
else:
res[ires] = 0.0
return signal.resample_poly(res, 1, 2).astype(np.float32)
def pitch_shift_ovs2_poly_6p5o_v2(sig, ratio, upsample=True):
"""Shifts pitch using 6-point 5-order polynomial interpolator and 2x
oversampling.
"""
# The polynomial is taken from the publication "Polynomial Interpolators
# for High-Quality Resampling of Oversampled Audio" by Olli Niemitalo, 2001
if upsample:
sig = signal.resample_poly(sig, 2, 1)
n_res = round(len(sig) / ratio / 2)
res = np.zeros(n_res, np.float32)
for ires in range(len(res)):
pos = ires * ratio * 2
isig = int(pos)
x = pos - isig
if 2 <= isig < len(sig) - 3:
# Polynomial interpolation
z = x - 1/2.0
even1 = sig[isig + 1] + sig[isig]
odd1 = sig[isig + 1] - sig[isig]
even2 = sig[isig + 2] + sig[isig - 1]
odd2 = sig[isig + 2] - sig[isig - 1]
even3 = sig[isig + 3] + sig[isig - 2]
odd3 = sig[isig + 3] - sig[isig - 2]
c0 = even1*0.40513396007145713 + even2 * \
0.09251794438424393 + even3*0.00234806603570670
c1 = odd1*0.28342806338906690 + odd2 * \
0.21703277024054901 + odd3*0.01309294748731515
c2 = even1*-0.191337682540351941 + even2 * \
0.16187844487943592 + even3*0.02946017143111912
c3 = odd1*-0.16471626190554542 + odd2*- \
0.00154547203542499 + odd3*0.03399271444851909
c4 = even1*0.03845798729588149 + even2*- \
0.05712936104242644 + even3*0.01866750929921070
c5 = odd1*0.04317950185225609 + odd2*- \
0.01802814255926417 + odd3*0.00152170021558204
res[ires] = ((((c5*z+c4)*z+c3)*z+c2)*z+c1)*z+c0
if isig + 1 < len(sig):
# Fallback to linear if we are too close to boundary
res[ires] = sig[isig] + x * (sig[isig + 1] - sig[isig])
elif isig < len(sig):
# Fallback to nearest value if we are at the boundary
res[ires] = sig[isig]
else:
res[ires] = 0.0
return res.astype(np.float32)
def pitch_shift_poly(sig, pitch_buf, upsample=True):
"""Shifts pitch using 6-point 5-order polynomial interpolator and 2x
oversampling with pitch buffer.
"""
# The polynomial is taken from the publication "Polynomial Interpolators
# for High-Quality Resampling of Oversampled Audio" by Olli Niemitalo, 2001
if upsample:
sig = signal.resample_poly(sig, 2, 1)
n_res = len(pitch_buf)
res = np.zeros(n_res, np.float32)
pos = 0
for ires in range(len(res)):
#pos = ires * pitch_buf[ires] * 2
isig = int(pos)
x = pos - isig
pos += pitch_buf[ires] * 2
if 2 <= isig < len(sig) - 3:
# Polynomial interpolation
z = x - 1/2.0
even1 = sig[isig + 1] + sig[isig]
odd1 = sig[isig + 1] - sig[isig]
even2 = sig[isig + 2] + sig[isig - 1]
odd2 = sig[isig + 2] - sig[isig - 1]
even3 = sig[isig + 3] + sig[isig - 2]
odd3 = sig[isig + 3] - sig[isig - 2]
c0 = even1*0.40513396007145713 + even2 * \
0.09251794438424393 + even3*0.00234806603570670
c1 = odd1*0.28342806338906690 + odd2 * \
0.21703277024054901 + odd3*0.01309294748731515
c2 = even1*-0.191337682540351941 + even2 * \
0.16187844487943592 + even3*0.02946017143111912
c3 = odd1*-0.16471626190554542 + odd2*- \
0.00154547203542499 + odd3*0.03399271444851909
c4 = even1*0.03845798729588149 + even2*- \
0.05712936104242644 + even3*0.01866750929921070
c5 = odd1*0.04317950185225609 + odd2*- \
0.01802814255926417 + odd3*0.00152170021558204
res[ires] = ((((c5*z+c4)*z+c3)*z+c2)*z+c1)*z+c0
if isig + 1 < len(sig):
# Fallback to linear if we are too close to boundary
res[ires] = sig[isig] + x * (sig[isig + 1] - sig[isig])
elif isig < len(sig):
# Fallback to nearest value if we are at the boundary
res[ires] = sig[isig]
else:
res[ires] = 0.0
return res.astype(np.float32)