Skip to content

Commit f4644ec

Browse files
committed
1 parent 4b68fb0 commit f4644ec

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

rvc/layers/generators.py

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ def __init__(
156156
self.sampling_rate = samp_rate
157157
self.voiced_threshold = voiced_threshold
158158

159+
def _f02sine(self, f0: torch.Tensor, upp: int):
160+
"""
161+
f0: (batchsize, length, dim)
162+
163+
where dim indicates fundamental tone and overtones
164+
"""
165+
a = torch.arange(1, upp + 1, dtype=f0.dtype, device=f0.device)
166+
rad = f0 / self.sampling_rate * a
167+
rad2 = torch.fmod(rad[:, :-1, -1:].float() + 0.5, 1.0) - 0.5
168+
rad_acc = rad2.cumsum(dim=1).fmod(1.0).to(f0)
169+
rad += F.pad(rad_acc, (0, 0, 1, 0), mode='constant')
170+
rad = rad.reshape(f0.shape[0], -1, 1)
171+
b = torch.arange(1, self.dim + 1, dtype=f0.dtype, device=f0.device).reshape(1, 1, -1)
172+
rad *= b
173+
rand_ini = torch.rand(1, 1, self.dim, device=f0.device)
174+
rand_ini[..., 0] = 0
175+
rad += rand_ini
176+
sines = torch.sin(2 * torch.pi * rad)
177+
return sines
178+
159179
def __call__(
160180
self, f0: torch.Tensor, upp: int
161181
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
@@ -171,45 +191,8 @@ def forward(
171191
output uv: tensor(batchsize=1, length, 1)
172192
"""
173193
with torch.no_grad():
174-
f0 = f0[:, None].transpose(1, 2)
175-
f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device)
176-
# fundamental component
177-
f0_buf[:, :, 0] = f0[:, :, 0]
178-
for idx in range(self.harmonic_num):
179-
f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (
180-
idx + 2
181-
) # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
182-
rad_values = (
183-
f0_buf / self.sampling_rate
184-
) % 1 ###%1意味着n_har的乘积无法后处理优化
185-
rand_ini = torch.rand(
186-
f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
187-
)
188-
rand_ini[:, 0] = 0
189-
rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
190-
tmp_over_one = torch.cumsum(
191-
rad_values, 1
192-
) # % 1 #####%1意味着后面的cumsum无法再优化
193-
tmp_over_one *= upp
194-
tmp_over_one: torch.Tensor = F.interpolate(
195-
tmp_over_one.transpose(2, 1),
196-
scale_factor=float(upp),
197-
mode="linear",
198-
align_corners=True,
199-
).transpose(2, 1)
200-
rad_values: torch.Tensor = F.interpolate(
201-
rad_values.transpose(2, 1), scale_factor=float(upp), mode="nearest"
202-
).transpose(
203-
2, 1
204-
) #######
205-
tmp_over_one %= 1
206-
tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
207-
cumsum_shift = torch.zeros_like(rad_values)
208-
cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
209-
sine_waves = torch.sin(
210-
torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * torch.pi
211-
)
212-
sine_waves = sine_waves * self.sine_amp
194+
f0 = f0.unsqueeze(-1)
195+
sine_waves = self._f02sine(f0, upp) * self.sine_amp
213196
uv = self._f02uv(f0)
214197
uv: torch.Tensor = F.interpolate(
215198
uv.transpose(2, 1), scale_factor=float(upp), mode="nearest"

0 commit comments

Comments
 (0)