-
Notifications
You must be signed in to change notification settings - Fork 2
/
pitch.py
261 lines (207 loc) · 7.54 KB
/
pitch.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
import abc
from typing import Optional
import librosa
import numpy as np
import parselmouth
import pyworld
import torch
class BasePE(abc.ABC):
def __init__(
self,
sample_rate=44100,
hop_length=512,
f0_min=0,
f0_max=22050,
keep_zeros=True,
) -> None:
super().__init__()
self.sample_rate = sample_rate
self.hop_length = hop_length
self.f0_min = f0_min
self.f0_max = f0_max
self.keep_zeros = keep_zeros
def __call__(self, x: torch.Tensor, pad_to=None):
f0 = self.process(x)
if isinstance(f0, np.ndarray):
f0 = torch.from_numpy(f0).float()
if pad_to is None:
pad_to = f0.shape[0]
else:
total_pad = pad_to - f0.shape[0]
if total_pad > 0:
f0 = np.pad(
f0, (total_pad // 2, total_pad - total_pad // 2), "constant"
)
f0 = torch.from_numpy(f0).float()
elif total_pad < 0:
f0 = f0[:total_pad]
vuv = torch.ones_like(f0)
vuv *= f0 > 0
if self.keep_zeros:
return f0, vuv, f0
org_f0 = torch.clone(f0)
# Remove zero frequencies and linearly interpolate
nzindex = torch.nonzero(f0).squeeze()
f0 = torch.index_select(f0, dim=0, index=nzindex)
time_org = self.hop_length / self.sample_rate * nzindex
time_frame = (
torch.arange(pad_to, device=x.device) * self.hop_length / self.sample_rate
)
if f0.shape[0] <= 0:
return torch.zeros(pad_to, dtype=torch.float, device=x.device), vuv, org_f0
if f0.shape[0] == 1:
return (
torch.ones(pad_to, dtype=torch.float, device=x.device) * f0[0],
vuv,
org_f0,
)
return (
self.interpolate(time_frame, time_org, f0, left=f0[0], right=f0[-1]),
vuv,
org_f0,
)
def interpolate(
self,
x: torch.Tensor,
xp: torch.Tensor,
fp: torch.Tensor,
left: Optional[torch.Tensor] = None,
right: Optional[torch.Tensor] = None,
):
"""Interpolate a 1-D function.
Args:
x (torch.Tensor): The x-coordinates at which to evaluate the interpolated values.
xp (torch.Tensor): A 1-D array of monotonically increasing real values.
fp (torch.Tensor): A 1-D array of real values, same length as xp.
left (torch.Tensor, optional): Value to return for x < xp[0], default is fp[0].
right (torch.Tensor, optional): Value to return for x > xp[-1], default is fp[-1].
Returns:
torch.Tensor: The interpolated values, same shape as x.
"""
# Ref: https://github.com/pytorch/pytorch/issues/1552#issuecomment-979998307
i = torch.clip(torch.searchsorted(xp, x, right=True), 1, len(xp) - 1)
interped = (fp[i - 1] * (xp[i] - x) + fp[i] * (x - xp[i - 1])) / (
xp[i] - xp[i - 1]
)
if left is None:
left = fp[0]
interped = torch.where(x < xp[0], left, interped)
if right is None:
right = fp[-1]
interped = torch.where(x > xp[-1], right, interped)
return interped
@abc.abstractclassmethod
def process(self, x: torch.Tensor):
raise NotImplementedError
class HarvestPE(BasePE):
def __init__(
self, sample_rate=44100, hop_length=512, f0_min=40, f0_max=1100, keep_zeros=True
) -> None:
super().__init__(sample_rate, hop_length, f0_min, f0_max, keep_zeros=keep_zeros)
def process(self, x: torch.Tensor):
x2 = x.cpu().numpy()[0].astype(np.float64)
_f0, t = pyworld.harvest(
x2,
self.sample_rate,
f0_floor=self.f0_min,
f0_ceil=self.f0_max,
frame_period=self.hop_length / self.sample_rate * 1000,
)
f0 = pyworld.stonemask(x2, _f0, t, self.sample_rate)
return f0
class DioPE(BasePE):
def __init__(
self, sample_rate=44100, hop_length=512, f0_min=40, f0_max=1100, keep_zeros=True
) -> None:
super().__init__(sample_rate, hop_length, f0_min, f0_max, keep_zeros=keep_zeros)
def process(self, x: torch.Tensor):
x2 = x.cpu().numpy()[0].astype(np.float64)
_f0, t = pyworld.dio(
x2,
self.sample_rate,
f0_floor=self.f0_min,
f0_ceil=self.f0_max,
frame_period=self.hop_length / self.sample_rate * 1000,
allowed_range=0.11,
)
f0 = pyworld.stonemask(x2, _f0, t, self.sample_rate)
return f0
class ParselmouthPE(BasePE):
def __init__(
self,
sample_rate=44100,
hop_length=512,
f0_min=40,
f0_max=1100,
keep_zeros=True,
very_accurate=True,
) -> None:
super().__init__(sample_rate, hop_length, f0_min, f0_max, keep_zeros=keep_zeros)
self.very_accurate = very_accurate
def process(self, x: torch.Tensor):
x2 = x.cpu().numpy()[0].astype(np.float64)
if self.very_accurate:
pad = 3.0
else:
pad = 1.5
l_pad = int(np.ceil(pad / self.f0_min * self.sample_rate))
r_pad = (
self.hop_length * ((len(x2) - 1) // self.hop_length + 1)
- len(x2)
+ l_pad
+ 1
)
x2 = np.pad(x2, (l_pad, r_pad))
s = parselmouth.Sound(x2, sampling_frequency=self.sample_rate).to_pitch_ac(
time_step=self.hop_length / self.sample_rate,
voicing_threshold=0.45,
pitch_floor=self.f0_min,
pitch_ceiling=self.f0_max,
very_accurate=self.very_accurate,
voiced_unvoiced_cost=0.1554,
)
assert np.abs(s.t1 - pad / self.f0_min) < 0.001
f0 = s.selected_array["frequency"].astype(np.float32)
return f0
class PyinPE(BasePE):
def __init__(
self, sample_rate=44100, hop_length=512, f0_min=40, f0_max=1100, keep_zeros=True
) -> None:
super().__init__(sample_rate, hop_length, f0_min, f0_max, keep_zeros=keep_zeros)
def process(self, x: torch.Tensor):
x2 = x.cpu().numpy()[0].astype(np.float64)
f0, voiced_flag, voiced_prob = librosa.pyin(
x2,
fmin=self.f0_min,
fmax=self.f0_max,
sr=self.sample_rate,
frame_length=self.hop_length * 4,
fill_na=0.0,
center=True,
pad_mode="reflect",
)
return f0
class MixedPE(BasePE):
def __init__(
self, sample_rate=44100, hop_length=512, f0_min=40, f0_max=1100, keep_zeros=True
) -> None:
super().__init__(sample_rate, hop_length, f0_min, f0_max, keep_zeros)
self.harvest = HarvestPE(sample_rate, hop_length, f0_min, f0_max, keep_zeros)
self.parsel = ParselmouthPE(sample_rate, hop_length, f0_min, f0_max, keep_zeros)
self.dio = DioPE(sample_rate, hop_length, f0_min, f0_max, keep_zeros)
def process(self, x: torch.Tensor):
f0p, _, _ = self.parsel(x)
f0h, _, _ = self.harvest(x, pad_to=f0p.shape[-1])
f0y, _, _ = self.dio(x, pad_to=f0p.shape[-1])
f0 = []
for f in zip(f0h, f0p, f0y):
f = sorted(f)
d1 = f[1] - f[0]
d2 = f[2] - f[1]
if d1 < d2:
f0.append((f[0] + f[1]) / 2)
elif d2 < d1:
f0.append((f[1] + f[2]) / 2)
else:
f0.append(f[1])
return np.array(f0)