Aitronssesin commited on
Commit
74d2eb9
1 Parent(s): 85d3b29

Upload 109 files

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. rvc/configs/__pycache__/config.cpython-39.pyc +0 -0
  2. rvc/infer/__pycache__/vc_infer_pipeline.cpython-39.pyc +0 -0
  3. rvc/lib/__pycache__/rmvpe.cpython-39.pyc +0 -0
  4. rvc/lib/__pycache__/utils.cpython-39.pyc +0 -0
  5. rvc/lib/infer_pack/__init__.py +0 -0
  6. rvc/lib/infer_pack/__pycache__/__init__.cpython-39.pyc +0 -0
  7. rvc/lib/infer_pack/__pycache__/attentions.cpython-39.pyc +0 -0
  8. rvc/lib/infer_pack/__pycache__/commons.cpython-39.pyc +0 -0
  9. rvc/lib/infer_pack/__pycache__/models.cpython-39.pyc +0 -0
  10. rvc/lib/infer_pack/__pycache__/modules.cpython-39.pyc +0 -0
  11. rvc/lib/infer_pack/__pycache__/transforms.cpython-39.pyc +0 -0
  12. rvc/lib/infer_pack/attentions.py +398 -0
  13. rvc/lib/infer_pack/commons.py +166 -0
  14. rvc/lib/infer_pack/models.py +1393 -0
  15. rvc/lib/infer_pack/modules.py +521 -0
  16. rvc/lib/infer_pack/modules/F0Predictor/DioF0Predictor.py +86 -0
  17. rvc/lib/infer_pack/modules/F0Predictor/F0Predictor.py +6 -0
  18. rvc/lib/infer_pack/modules/F0Predictor/HarvestF0Predictor.py +82 -0
  19. rvc/lib/infer_pack/modules/F0Predictor/PMF0Predictor.py +93 -0
  20. rvc/lib/infer_pack/modules/F0Predictor/__init__.py +0 -0
  21. rvc/lib/infer_pack/transforms.py +209 -0
  22. rvc/lib/process/__pycache__/model_fusion.cpython-39.pyc +0 -0
  23. rvc/lib/process/__pycache__/model_information.cpython-39.pyc +0 -0
  24. rvc/lib/process/model_fusion.py +33 -0
  25. rvc/lib/process/model_information.py +15 -0
  26. rvc/lib/rmvpe.py +388 -0
  27. rvc/lib/tools/__pycache__/pretrained_selector.cpython-39.pyc +0 -0
  28. rvc/lib/tools/__pycache__/split_audio.cpython-39.pyc +0 -0
  29. rvc/lib/tools/__pycache__/validators.cpython-39.pyc +0 -0
  30. rvc/lib/tools/gdown.py +402 -0
  31. rvc/lib/tools/launch_tensorboard.py +15 -0
  32. rvc/lib/tools/model_download.py +225 -0
  33. rvc/lib/tools/prerequisites_download.py +84 -0
  34. rvc/lib/tools/pretrained_selector.py +63 -0
  35. rvc/lib/tools/split_audio.py +105 -0
  36. rvc/lib/tools/tts.py +16 -0
  37. rvc/lib/tools/tts_voices.json +0 -0
  38. rvc/lib/tools/validators.py +67 -0
  39. rvc/lib/utils.py +26 -0
  40. rvc/pretraineds/pretrained/D32k.pth +3 -0
  41. rvc/pretraineds/pretrained/D40k.pth +3 -0
  42. rvc/pretraineds/pretrained/D48k.pth +3 -0
  43. rvc/pretraineds/pretrained/G32k.pth +3 -0
  44. rvc/pretraineds/pretrained/G40k.pth +3 -0
  45. rvc/pretraineds/pretrained/G48k.pth +3 -0
  46. rvc/pretraineds/pretrained/f0D32k.pth +3 -0
  47. rvc/pretraineds/pretrained/f0D40k.pth +3 -0
  48. rvc/pretraineds/pretrained/f0D48k.pth +3 -0
  49. rvc/pretraineds/pretrained/f0G32k.pth +3 -0
  50. rvc/pretraineds/pretrained/f0G40k.pth +3 -0
rvc/configs/__pycache__/config.cpython-39.pyc ADDED
Binary file (4.45 kB). View file
 
rvc/infer/__pycache__/vc_infer_pipeline.cpython-39.pyc ADDED
Binary file (10.4 kB). View file
 
rvc/lib/__pycache__/rmvpe.cpython-39.pyc ADDED
Binary file (11.1 kB). View file
 
rvc/lib/__pycache__/utils.cpython-39.pyc ADDED
Binary file (1.09 kB). View file
 
rvc/lib/infer_pack/__init__.py ADDED
File without changes
rvc/lib/infer_pack/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (163 Bytes). View file
 
rvc/lib/infer_pack/__pycache__/attentions.cpython-39.pyc ADDED
Binary file (9.6 kB). View file
 
rvc/lib/infer_pack/__pycache__/commons.cpython-39.pyc ADDED
Binary file (5.88 kB). View file
 
rvc/lib/infer_pack/__pycache__/models.cpython-39.pyc ADDED
Binary file (29.2 kB). View file
 
rvc/lib/infer_pack/__pycache__/modules.cpython-39.pyc ADDED
Binary file (11.7 kB). View file
 
rvc/lib/infer_pack/__pycache__/transforms.cpython-39.pyc ADDED
Binary file (3.93 kB). View file
 
rvc/lib/infer_pack/attentions.py ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ from torch import nn
4
+ from torch.nn import functional as F
5
+
6
+ from . import commons
7
+ from .modules import LayerNorm
8
+
9
+
10
+ class Encoder(nn.Module):
11
+ def __init__(
12
+ self,
13
+ hidden_channels,
14
+ filter_channels,
15
+ n_heads,
16
+ n_layers,
17
+ kernel_size=1,
18
+ p_dropout=0.0,
19
+ window_size=10,
20
+ **kwargs
21
+ ):
22
+ super().__init__()
23
+ self.hidden_channels = hidden_channels
24
+ self.filter_channels = filter_channels
25
+ self.n_heads = n_heads
26
+ self.n_layers = n_layers
27
+ self.kernel_size = kernel_size
28
+ self.p_dropout = p_dropout
29
+ self.window_size = window_size
30
+
31
+ self.drop = nn.Dropout(p_dropout)
32
+ self.attn_layers = nn.ModuleList()
33
+ self.norm_layers_1 = nn.ModuleList()
34
+ self.ffn_layers = nn.ModuleList()
35
+ self.norm_layers_2 = nn.ModuleList()
36
+ for i in range(self.n_layers):
37
+ self.attn_layers.append(
38
+ MultiHeadAttention(
39
+ hidden_channels,
40
+ hidden_channels,
41
+ n_heads,
42
+ p_dropout=p_dropout,
43
+ window_size=window_size,
44
+ )
45
+ )
46
+ self.norm_layers_1.append(LayerNorm(hidden_channels))
47
+ self.ffn_layers.append(
48
+ FFN(
49
+ hidden_channels,
50
+ hidden_channels,
51
+ filter_channels,
52
+ kernel_size,
53
+ p_dropout=p_dropout,
54
+ )
55
+ )
56
+ self.norm_layers_2.append(LayerNorm(hidden_channels))
57
+
58
+ def forward(self, x, x_mask):
59
+ attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
60
+ x = x * x_mask
61
+ for i in range(self.n_layers):
62
+ y = self.attn_layers[i](x, x, attn_mask)
63
+ y = self.drop(y)
64
+ x = self.norm_layers_1[i](x + y)
65
+
66
+ y = self.ffn_layers[i](x, x_mask)
67
+ y = self.drop(y)
68
+ x = self.norm_layers_2[i](x + y)
69
+ x = x * x_mask
70
+ return x
71
+
72
+
73
+ class Decoder(nn.Module):
74
+ def __init__(
75
+ self,
76
+ hidden_channels,
77
+ filter_channels,
78
+ n_heads,
79
+ n_layers,
80
+ kernel_size=1,
81
+ p_dropout=0.0,
82
+ proximal_bias=False,
83
+ proximal_init=True,
84
+ **kwargs
85
+ ):
86
+ super().__init__()
87
+ self.hidden_channels = hidden_channels
88
+ self.filter_channels = filter_channels
89
+ self.n_heads = n_heads
90
+ self.n_layers = n_layers
91
+ self.kernel_size = kernel_size
92
+ self.p_dropout = p_dropout
93
+ self.proximal_bias = proximal_bias
94
+ self.proximal_init = proximal_init
95
+
96
+ self.drop = nn.Dropout(p_dropout)
97
+ self.self_attn_layers = nn.ModuleList()
98
+ self.norm_layers_0 = nn.ModuleList()
99
+ self.encdec_attn_layers = nn.ModuleList()
100
+ self.norm_layers_1 = nn.ModuleList()
101
+ self.ffn_layers = nn.ModuleList()
102
+ self.norm_layers_2 = nn.ModuleList()
103
+ for i in range(self.n_layers):
104
+ self.self_attn_layers.append(
105
+ MultiHeadAttention(
106
+ hidden_channels,
107
+ hidden_channels,
108
+ n_heads,
109
+ p_dropout=p_dropout,
110
+ proximal_bias=proximal_bias,
111
+ proximal_init=proximal_init,
112
+ )
113
+ )
114
+ self.norm_layers_0.append(LayerNorm(hidden_channels))
115
+ self.encdec_attn_layers.append(
116
+ MultiHeadAttention(
117
+ hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout
118
+ )
119
+ )
120
+ self.norm_layers_1.append(LayerNorm(hidden_channels))
121
+ self.ffn_layers.append(
122
+ FFN(
123
+ hidden_channels,
124
+ hidden_channels,
125
+ filter_channels,
126
+ kernel_size,
127
+ p_dropout=p_dropout,
128
+ causal=True,
129
+ )
130
+ )
131
+ self.norm_layers_2.append(LayerNorm(hidden_channels))
132
+
133
+ def forward(self, x, x_mask, h, h_mask):
134
+ """
135
+ x: decoder input
136
+ h: encoder output
137
+ """
138
+ self_attn_mask = commons.subsequent_mask(x_mask.size(2)).to(
139
+ device=x.device, dtype=x.dtype
140
+ )
141
+ encdec_attn_mask = h_mask.unsqueeze(2) * x_mask.unsqueeze(-1)
142
+ x = x * x_mask
143
+ for i in range(self.n_layers):
144
+ y = self.self_attn_layers[i](x, x, self_attn_mask)
145
+ y = self.drop(y)
146
+ x = self.norm_layers_0[i](x + y)
147
+
148
+ y = self.encdec_attn_layers[i](x, h, encdec_attn_mask)
149
+ y = self.drop(y)
150
+ x = self.norm_layers_1[i](x + y)
151
+
152
+ y = self.ffn_layers[i](x, x_mask)
153
+ y = self.drop(y)
154
+ x = self.norm_layers_2[i](x + y)
155
+ x = x * x_mask
156
+ return x
157
+
158
+
159
+ class MultiHeadAttention(nn.Module):
160
+ def __init__(
161
+ self,
162
+ channels,
163
+ out_channels,
164
+ n_heads,
165
+ p_dropout=0.0,
166
+ window_size=None,
167
+ heads_share=True,
168
+ block_length=None,
169
+ proximal_bias=False,
170
+ proximal_init=False,
171
+ ):
172
+ super().__init__()
173
+ assert channels % n_heads == 0
174
+
175
+ self.channels = channels
176
+ self.out_channels = out_channels
177
+ self.n_heads = n_heads
178
+ self.p_dropout = p_dropout
179
+ self.window_size = window_size
180
+ self.heads_share = heads_share
181
+ self.block_length = block_length
182
+ self.proximal_bias = proximal_bias
183
+ self.proximal_init = proximal_init
184
+ self.attn = None
185
+
186
+ self.k_channels = channels // n_heads
187
+ self.conv_q = nn.Conv1d(channels, channels, 1)
188
+ self.conv_k = nn.Conv1d(channels, channels, 1)
189
+ self.conv_v = nn.Conv1d(channels, channels, 1)
190
+ self.conv_o = nn.Conv1d(channels, out_channels, 1)
191
+ self.drop = nn.Dropout(p_dropout)
192
+
193
+ if window_size is not None:
194
+ n_heads_rel = 1 if heads_share else n_heads
195
+ rel_stddev = self.k_channels**-0.5
196
+ self.emb_rel_k = nn.Parameter(
197
+ torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
198
+ * rel_stddev
199
+ )
200
+ self.emb_rel_v = nn.Parameter(
201
+ torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels)
202
+ * rel_stddev
203
+ )
204
+
205
+ nn.init.xavier_uniform_(self.conv_q.weight)
206
+ nn.init.xavier_uniform_(self.conv_k.weight)
207
+ nn.init.xavier_uniform_(self.conv_v.weight)
208
+ if proximal_init:
209
+ with torch.no_grad():
210
+ self.conv_k.weight.copy_(self.conv_q.weight)
211
+ self.conv_k.bias.copy_(self.conv_q.bias)
212
+
213
+ def forward(self, x, c, attn_mask=None):
214
+ q = self.conv_q(x)
215
+ k = self.conv_k(c)
216
+ v = self.conv_v(c)
217
+
218
+ x, self.attn = self.attention(q, k, v, mask=attn_mask)
219
+
220
+ x = self.conv_o(x)
221
+ return x
222
+
223
+ def attention(self, query, key, value, mask=None):
224
+ b, d, t_s, t_t = (*key.size(), query.size(2))
225
+ query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3)
226
+ key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
227
+ value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
228
+
229
+ scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1))
230
+ if self.window_size is not None:
231
+ assert (
232
+ t_s == t_t
233
+ ), "Relative attention is only available for self-attention."
234
+ key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s)
235
+ rel_logits = self._matmul_with_relative_keys(
236
+ query / math.sqrt(self.k_channels), key_relative_embeddings
237
+ )
238
+ scores_local = self._relative_position_to_absolute_position(rel_logits)
239
+ scores = scores + scores_local
240
+ if self.proximal_bias:
241
+ assert t_s == t_t, "Proximal bias is only available for self-attention."
242
+ scores = scores + self._attention_bias_proximal(t_s).to(
243
+ device=scores.device, dtype=scores.dtype
244
+ )
245
+ if mask is not None:
246
+ scores = scores.masked_fill(mask == 0, -1e4)
247
+ if self.block_length is not None:
248
+ assert (
249
+ t_s == t_t
250
+ ), "Local attention is only available for self-attention."
251
+ block_mask = (
252
+ torch.ones_like(scores)
253
+ .triu(-self.block_length)
254
+ .tril(self.block_length)
255
+ )
256
+ scores = scores.masked_fill(block_mask == 0, -1e4)
257
+ p_attn = F.softmax(scores, dim=-1)
258
+ p_attn = self.drop(p_attn)
259
+ output = torch.matmul(p_attn, value)
260
+ if self.window_size is not None:
261
+ relative_weights = self._absolute_position_to_relative_position(p_attn)
262
+ value_relative_embeddings = self._get_relative_embeddings(
263
+ self.emb_rel_v, t_s
264
+ )
265
+ output = output + self._matmul_with_relative_values(
266
+ relative_weights, value_relative_embeddings
267
+ )
268
+ output = output.transpose(2, 3).contiguous().view(b, d, t_t)
269
+ return output, p_attn
270
+
271
+ def _matmul_with_relative_values(self, x, y):
272
+ """
273
+ x: [b, h, l, m]
274
+ y: [h or 1, m, d]
275
+ ret: [b, h, l, d]
276
+ """
277
+ ret = torch.matmul(x, y.unsqueeze(0))
278
+ return ret
279
+
280
+ def _matmul_with_relative_keys(self, x, y):
281
+ """
282
+ x: [b, h, l, d]
283
+ y: [h or 1, m, d]
284
+ ret: [b, h, l, m]
285
+ """
286
+ ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1))
287
+ return ret
288
+
289
+ def _get_relative_embeddings(self, relative_embeddings, length):
290
+ pad_length = max(length - (self.window_size + 1), 0)
291
+ slice_start_position = max((self.window_size + 1) - length, 0)
292
+ slice_end_position = slice_start_position + 2 * length - 1
293
+ if pad_length > 0:
294
+ padded_relative_embeddings = F.pad(
295
+ relative_embeddings,
296
+ commons.convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]]),
297
+ )
298
+ else:
299
+ padded_relative_embeddings = relative_embeddings
300
+ used_relative_embeddings = padded_relative_embeddings[
301
+ :, slice_start_position:slice_end_position
302
+ ]
303
+ return used_relative_embeddings
304
+
305
+ def _relative_position_to_absolute_position(self, x):
306
+ """
307
+ x: [b, h, l, 2*l-1]
308
+ ret: [b, h, l, l]
309
+ """
310
+ batch, heads, length, _ = x.size()
311
+
312
+ x = F.pad(x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
313
+ x_flat = x.view([batch, heads, length * 2 * length])
314
+ x_flat = F.pad(
315
+ x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [0, length - 1]])
316
+ )
317
+
318
+ x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[
319
+ :, :, :length, length - 1 :
320
+ ]
321
+ return x_final
322
+
323
+ def _absolute_position_to_relative_position(self, x):
324
+ """
325
+ x: [b, h, l, l]
326
+ ret: [b, h, l, 2*l-1]
327
+ """
328
+ batch, heads, length, _ = x.size()
329
+ x = F.pad(
330
+ x, commons.convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])
331
+ )
332
+ x_flat = x.view([batch, heads, length**2 + length * (length - 1)])
333
+ x_flat = F.pad(x_flat, commons.convert_pad_shape([[0, 0], [0, 0], [length, 0]]))
334
+ x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:]
335
+ return x_final
336
+
337
+ def _attention_bias_proximal(self, length):
338
+ r = torch.arange(length, dtype=torch.float32)
339
+ diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1)
340
+ return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
341
+
342
+
343
+ class FFN(nn.Module):
344
+ def __init__(
345
+ self,
346
+ in_channels,
347
+ out_channels,
348
+ filter_channels,
349
+ kernel_size,
350
+ p_dropout=0.0,
351
+ activation=None,
352
+ causal=False,
353
+ ):
354
+ super().__init__()
355
+ self.in_channels = in_channels
356
+ self.out_channels = out_channels
357
+ self.filter_channels = filter_channels
358
+ self.kernel_size = kernel_size
359
+ self.p_dropout = p_dropout
360
+ self.activation = activation
361
+ self.causal = causal
362
+
363
+ if causal:
364
+ self.padding = self._causal_padding
365
+ else:
366
+ self.padding = self._same_padding
367
+
368
+ self.conv_1 = nn.Conv1d(in_channels, filter_channels, kernel_size)
369
+ self.conv_2 = nn.Conv1d(filter_channels, out_channels, kernel_size)
370
+ self.drop = nn.Dropout(p_dropout)
371
+
372
+ def forward(self, x, x_mask):
373
+ x = self.conv_1(self.padding(x * x_mask))
374
+ if self.activation == "gelu":
375
+ x = x * torch.sigmoid(1.702 * x)
376
+ else:
377
+ x = torch.relu(x)
378
+ x = self.drop(x)
379
+ x = self.conv_2(self.padding(x * x_mask))
380
+ return x * x_mask
381
+
382
+ def _causal_padding(self, x):
383
+ if self.kernel_size == 1:
384
+ return x
385
+ pad_l = self.kernel_size - 1
386
+ pad_r = 0
387
+ padding = [[0, 0], [0, 0], [pad_l, pad_r]]
388
+ x = F.pad(x, commons.convert_pad_shape(padding))
389
+ return x
390
+
391
+ def _same_padding(self, x):
392
+ if self.kernel_size == 1:
393
+ return x
394
+ pad_l = (self.kernel_size - 1) // 2
395
+ pad_r = self.kernel_size // 2
396
+ padding = [[0, 0], [0, 0], [pad_l, pad_r]]
397
+ x = F.pad(x, commons.convert_pad_shape(padding))
398
+ return x
rvc/lib/infer_pack/commons.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import numpy as np
3
+ import torch
4
+ from torch import nn
5
+ from torch.nn import functional as F
6
+
7
+
8
+ def init_weights(m, mean=0.0, std=0.01):
9
+ classname = m.__class__.__name__
10
+ if classname.find("Conv") != -1:
11
+ m.weight.data.normal_(mean, std)
12
+
13
+
14
+ def get_padding(kernel_size, dilation=1):
15
+ return int((kernel_size * dilation - dilation) / 2)
16
+
17
+
18
+ def convert_pad_shape(pad_shape):
19
+ l = pad_shape[::-1]
20
+ pad_shape = [item for sublist in l for item in sublist]
21
+ return pad_shape
22
+
23
+
24
+ def kl_divergence(m_p, logs_p, m_q, logs_q):
25
+ """KL(P||Q)"""
26
+ kl = (logs_q - logs_p) - 0.5
27
+ kl += (
28
+ 0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)
29
+ )
30
+ return kl
31
+
32
+
33
+ def rand_gumbel(shape):
34
+ """Sample from the Gumbel distribution, protect from overflows."""
35
+ uniform_samples = torch.rand(shape) * 0.99998 + 0.00001
36
+ return -torch.log(-torch.log(uniform_samples))
37
+
38
+
39
+ def rand_gumbel_like(x):
40
+ g = rand_gumbel(x.size()).to(dtype=x.dtype, device=x.device)
41
+ return g
42
+
43
+
44
+ def slice_segments(x, ids_str, segment_size=4):
45
+ ret = torch.zeros_like(x[:, :, :segment_size])
46
+ for i in range(x.size(0)):
47
+ idx_str = ids_str[i]
48
+ idx_end = idx_str + segment_size
49
+ ret[i] = x[i, :, idx_str:idx_end]
50
+ return ret
51
+
52
+
53
+ def slice_segments2(x, ids_str, segment_size=4):
54
+ ret = torch.zeros_like(x[:, :segment_size])
55
+ for i in range(x.size(0)):
56
+ idx_str = ids_str[i]
57
+ idx_end = idx_str + segment_size
58
+ ret[i] = x[i, idx_str:idx_end]
59
+ return ret
60
+
61
+
62
+ def rand_slice_segments(x, x_lengths=None, segment_size=4):
63
+ b, d, t = x.size()
64
+ if x_lengths is None:
65
+ x_lengths = t
66
+ ids_str_max = x_lengths - segment_size + 1
67
+ ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long)
68
+ ret = slice_segments(x, ids_str, segment_size)
69
+ return ret, ids_str
70
+
71
+
72
+ def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4):
73
+ position = torch.arange(length, dtype=torch.float)
74
+ num_timescales = channels // 2
75
+ log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (
76
+ num_timescales - 1
77
+ )
78
+ inv_timescales = min_timescale * torch.exp(
79
+ torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment
80
+ )
81
+ scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1)
82
+ signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0)
83
+ signal = F.pad(signal, [0, 0, 0, channels % 2])
84
+ signal = signal.view(1, channels, length)
85
+ return signal
86
+
87
+
88
+ def add_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4):
89
+ b, channels, length = x.size()
90
+ signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
91
+ return x + signal.to(dtype=x.dtype, device=x.device)
92
+
93
+
94
+ def cat_timing_signal_1d(x, min_timescale=1.0, max_timescale=1.0e4, axis=1):
95
+ b, channels, length = x.size()
96
+ signal = get_timing_signal_1d(length, channels, min_timescale, max_timescale)
97
+ return torch.cat([x, signal.to(dtype=x.dtype, device=x.device)], axis)
98
+
99
+
100
+ def subsequent_mask(length):
101
+ mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0)
102
+ return mask
103
+
104
+
105
+ @torch.jit.script
106
+ def fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels):
107
+ n_channels_int = n_channels[0]
108
+ in_act = input_a + input_b
109
+ t_act = torch.tanh(in_act[:, :n_channels_int, :])
110
+ s_act = torch.sigmoid(in_act[:, n_channels_int:, :])
111
+ acts = t_act * s_act
112
+ return acts
113
+
114
+
115
+ def convert_pad_shape(pad_shape):
116
+ l = pad_shape[::-1]
117
+ pad_shape = [item for sublist in l for item in sublist]
118
+ return pad_shape
119
+
120
+
121
+ def shift_1d(x):
122
+ x = F.pad(x, convert_pad_shape([[0, 0], [0, 0], [1, 0]]))[:, :, :-1]
123
+ return x
124
+
125
+
126
+ def sequence_mask(length, max_length=None):
127
+ if max_length is None:
128
+ max_length = length.max()
129
+ x = torch.arange(max_length, dtype=length.dtype, device=length.device)
130
+ return x.unsqueeze(0) < length.unsqueeze(1)
131
+
132
+
133
+ def generate_path(duration, mask):
134
+ """
135
+ duration: [b, 1, t_x]
136
+ mask: [b, 1, t_y, t_x]
137
+ """
138
+ device = duration.device
139
+
140
+ b, _, t_y, t_x = mask.shape
141
+ cum_duration = torch.cumsum(duration, -1)
142
+
143
+ cum_duration_flat = cum_duration.view(b * t_x)
144
+ path = sequence_mask(cum_duration_flat, t_y).to(mask.dtype)
145
+ path = path.view(b, t_x, t_y)
146
+ path = path - F.pad(path, convert_pad_shape([[0, 0], [1, 0], [0, 0]]))[:, :-1]
147
+ path = path.unsqueeze(1).transpose(2, 3) * mask
148
+ return path
149
+
150
+
151
+ def clip_grad_value_(parameters, clip_value, norm_type=2):
152
+ if isinstance(parameters, torch.Tensor):
153
+ parameters = [parameters]
154
+ parameters = list(filter(lambda p: p.grad is not None, parameters))
155
+ norm_type = float(norm_type)
156
+ if clip_value is not None:
157
+ clip_value = float(clip_value)
158
+
159
+ total_norm = 0
160
+ for p in parameters:
161
+ param_norm = p.grad.data.norm(norm_type)
162
+ total_norm += param_norm.item() ** norm_type
163
+ if clip_value is not None:
164
+ p.grad.data.clamp_(min=-clip_value, max=clip_value)
165
+ total_norm = total_norm ** (1.0 / norm_type)
166
+ return total_norm
rvc/lib/infer_pack/models.py ADDED
@@ -0,0 +1,1393 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ from torch import nn
4
+ from torch.nn import functional as F
5
+ from . import modules
6
+ from . import attentions
7
+ from . import commons
8
+ from .commons import init_weights, get_padding
9
+ from torch.nn import Conv1d, ConvTranspose1d, Conv2d
10
+ from torch.nn.utils import remove_weight_norm
11
+ from torch.nn.utils.parametrizations import spectral_norm, weight_norm
12
+ from typing import Optional
13
+
14
+ has_xpu = bool(hasattr(torch, "xpu") and torch.xpu.is_available())
15
+
16
+
17
+ class TextEncoder256(nn.Module):
18
+ def __init__(
19
+ self,
20
+ out_channels,
21
+ hidden_channels,
22
+ filter_channels,
23
+ n_heads,
24
+ n_layers,
25
+ kernel_size,
26
+ p_dropout,
27
+ f0=True,
28
+ ):
29
+ super(TextEncoder256, self).__init__()
30
+ self.out_channels = out_channels
31
+ self.hidden_channels = hidden_channels
32
+ self.filter_channels = filter_channels
33
+ self.n_heads = n_heads
34
+ self.n_layers = n_layers
35
+ self.kernel_size = kernel_size
36
+ self.p_dropout = float(p_dropout)
37
+ self.emb_phone = nn.Linear(256, hidden_channels)
38
+ self.lrelu = nn.LeakyReLU(0.1, inplace=True)
39
+ if f0 == True:
40
+ self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
41
+ self.encoder = attentions.Encoder(
42
+ hidden_channels,
43
+ filter_channels,
44
+ n_heads,
45
+ n_layers,
46
+ kernel_size,
47
+ float(p_dropout),
48
+ )
49
+ self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
50
+
51
+ def forward(
52
+ self, phone: torch.Tensor, pitch: Optional[torch.Tensor], lengths: torch.Tensor
53
+ ):
54
+ if pitch is None:
55
+ x = self.emb_phone(phone)
56
+ else:
57
+ x = self.emb_phone(phone) + self.emb_pitch(pitch)
58
+ x = x * math.sqrt(self.hidden_channels) # [b, t, h]
59
+ x = self.lrelu(x)
60
+ x = torch.transpose(x, 1, -1) # [b, h, t]
61
+ x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
62
+ x.dtype
63
+ )
64
+ x = self.encoder(x * x_mask, x_mask)
65
+ stats = self.proj(x) * x_mask
66
+
67
+ m, logs = torch.split(stats, self.out_channels, dim=1)
68
+ return m, logs, x_mask
69
+
70
+
71
+ class TextEncoder768(nn.Module):
72
+ def __init__(
73
+ self,
74
+ out_channels,
75
+ hidden_channels,
76
+ filter_channels,
77
+ n_heads,
78
+ n_layers,
79
+ kernel_size,
80
+ p_dropout,
81
+ f0=True,
82
+ ):
83
+ super(TextEncoder768, self).__init__()
84
+ self.out_channels = out_channels
85
+ self.hidden_channels = hidden_channels
86
+ self.filter_channels = filter_channels
87
+ self.n_heads = n_heads
88
+ self.n_layers = n_layers
89
+ self.kernel_size = kernel_size
90
+ self.p_dropout = float(p_dropout)
91
+ self.emb_phone = nn.Linear(768, hidden_channels)
92
+ self.lrelu = nn.LeakyReLU(0.1, inplace=True)
93
+ if f0 == True:
94
+ self.emb_pitch = nn.Embedding(256, hidden_channels) # pitch 256
95
+ self.encoder = attentions.Encoder(
96
+ hidden_channels,
97
+ filter_channels,
98
+ n_heads,
99
+ n_layers,
100
+ kernel_size,
101
+ float(p_dropout),
102
+ )
103
+ self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
104
+
105
+ def forward(self, phone: torch.Tensor, pitch: torch.Tensor, lengths: torch.Tensor):
106
+ if pitch is None:
107
+ x = self.emb_phone(phone)
108
+ else:
109
+ x = self.emb_phone(phone) + self.emb_pitch(pitch)
110
+ x = x * math.sqrt(self.hidden_channels) # [b, t, h]
111
+ x = self.lrelu(x)
112
+ x = torch.transpose(x, 1, -1) # [b, h, t]
113
+ x_mask = torch.unsqueeze(commons.sequence_mask(lengths, x.size(2)), 1).to(
114
+ x.dtype
115
+ )
116
+ x = self.encoder(x * x_mask, x_mask)
117
+ stats = self.proj(x) * x_mask
118
+
119
+ m, logs = torch.split(stats, self.out_channels, dim=1)
120
+ return m, logs, x_mask
121
+
122
+
123
+ class ResidualCouplingBlock(nn.Module):
124
+ def __init__(
125
+ self,
126
+ channels,
127
+ hidden_channels,
128
+ kernel_size,
129
+ dilation_rate,
130
+ n_layers,
131
+ n_flows=4,
132
+ gin_channels=0,
133
+ ):
134
+ super(ResidualCouplingBlock, self).__init__()
135
+ self.channels = channels
136
+ self.hidden_channels = hidden_channels
137
+ self.kernel_size = kernel_size
138
+ self.dilation_rate = dilation_rate
139
+ self.n_layers = n_layers
140
+ self.n_flows = n_flows
141
+ self.gin_channels = gin_channels
142
+
143
+ self.flows = nn.ModuleList()
144
+ for i in range(n_flows):
145
+ self.flows.append(
146
+ modules.ResidualCouplingLayer(
147
+ channels,
148
+ hidden_channels,
149
+ kernel_size,
150
+ dilation_rate,
151
+ n_layers,
152
+ gin_channels=gin_channels,
153
+ mean_only=True,
154
+ )
155
+ )
156
+ self.flows.append(modules.Flip())
157
+
158
+ def forward(
159
+ self,
160
+ x: torch.Tensor,
161
+ x_mask: torch.Tensor,
162
+ g: Optional[torch.Tensor] = None,
163
+ reverse: bool = False,
164
+ ):
165
+ if not reverse:
166
+ for flow in self.flows:
167
+ x, _ = flow(x, x_mask, g=g, reverse=reverse)
168
+ else:
169
+ for flow in self.flows[::-1]:
170
+ x = flow.forward(x, x_mask, g=g, reverse=reverse)
171
+ return x
172
+
173
+ def remove_weight_norm(self):
174
+ for i in range(self.n_flows):
175
+ self.flows[i * 2].remove_weight_norm()
176
+
177
+ def __prepare_scriptable__(self):
178
+ for i in range(self.n_flows):
179
+ for hook in self.flows[i * 2]._forward_pre_hooks.values():
180
+ if (
181
+ hook.__module__ == "torch.nn.utils.weight_norm"
182
+ and hook.__class__.__name__ == "WeightNorm"
183
+ ):
184
+ torch.nn.utils.remove_weight_norm(self.flows[i * 2])
185
+
186
+ return self
187
+
188
+
189
+ class PosteriorEncoder(nn.Module):
190
+ def __init__(
191
+ self,
192
+ in_channels,
193
+ out_channels,
194
+ hidden_channels,
195
+ kernel_size,
196
+ dilation_rate,
197
+ n_layers,
198
+ gin_channels=0,
199
+ ):
200
+ super(PosteriorEncoder, self).__init__()
201
+ self.in_channels = in_channels
202
+ self.out_channels = out_channels
203
+ self.hidden_channels = hidden_channels
204
+ self.kernel_size = kernel_size
205
+ self.dilation_rate = dilation_rate
206
+ self.n_layers = n_layers
207
+ self.gin_channels = gin_channels
208
+
209
+ self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
210
+ self.enc = modules.WN(
211
+ hidden_channels,
212
+ kernel_size,
213
+ dilation_rate,
214
+ n_layers,
215
+ gin_channels=gin_channels,
216
+ )
217
+ self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
218
+
219
+ def forward(
220
+ self, x: torch.Tensor, x_lengths: torch.Tensor, g: Optional[torch.Tensor] = None
221
+ ):
222
+ x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(
223
+ x.dtype
224
+ )
225
+ x = self.pre(x) * x_mask
226
+ x = self.enc(x, x_mask, g=g)
227
+ stats = self.proj(x) * x_mask
228
+ m, logs = torch.split(stats, self.out_channels, dim=1)
229
+ z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
230
+ return z, m, logs, x_mask
231
+
232
+ def remove_weight_norm(self):
233
+ self.enc.remove_weight_norm()
234
+
235
+ def __prepare_scriptable__(self):
236
+ for hook in self.enc._forward_pre_hooks.values():
237
+ if (
238
+ hook.__module__ == "torch.nn.utils.weight_norm"
239
+ and hook.__class__.__name__ == "WeightNorm"
240
+ ):
241
+ torch.nn.utils.remove_weight_norm(self.enc)
242
+ return self
243
+
244
+
245
+ class Generator(torch.nn.Module):
246
+ def __init__(
247
+ self,
248
+ initial_channel,
249
+ resblock,
250
+ resblock_kernel_sizes,
251
+ resblock_dilation_sizes,
252
+ upsample_rates,
253
+ upsample_initial_channel,
254
+ upsample_kernel_sizes,
255
+ gin_channels=0,
256
+ ):
257
+ super(Generator, self).__init__()
258
+ self.num_kernels = len(resblock_kernel_sizes)
259
+ self.num_upsamples = len(upsample_rates)
260
+ self.conv_pre = Conv1d(
261
+ initial_channel, upsample_initial_channel, 7, 1, padding=3
262
+ )
263
+ resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
264
+
265
+ self.ups = nn.ModuleList()
266
+ for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
267
+ self.ups.append(
268
+ weight_norm(
269
+ ConvTranspose1d(
270
+ upsample_initial_channel // (2**i),
271
+ upsample_initial_channel // (2 ** (i + 1)),
272
+ k,
273
+ u,
274
+ padding=(k - u) // 2,
275
+ )
276
+ )
277
+ )
278
+
279
+ self.resblocks = nn.ModuleList()
280
+ for i in range(len(self.ups)):
281
+ ch = upsample_initial_channel // (2 ** (i + 1))
282
+ for j, (k, d) in enumerate(
283
+ zip(resblock_kernel_sizes, resblock_dilation_sizes)
284
+ ):
285
+ self.resblocks.append(resblock(ch, k, d))
286
+
287
+ self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
288
+ self.ups.apply(init_weights)
289
+
290
+ if gin_channels != 0:
291
+ self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
292
+
293
+ def forward(self, x: torch.Tensor, g: Optional[torch.Tensor] = None):
294
+ x = self.conv_pre(x)
295
+ if g is not None:
296
+ x = x + self.cond(g)
297
+
298
+ for i in range(self.num_upsamples):
299
+ x = F.leaky_relu(x, modules.LRELU_SLOPE)
300
+ x = self.ups[i](x)
301
+ xs = None
302
+ for j in range(self.num_kernels):
303
+ if xs is None:
304
+ xs = self.resblocks[i * self.num_kernels + j](x)
305
+ else:
306
+ xs += self.resblocks[i * self.num_kernels + j](x)
307
+ x = xs / self.num_kernels
308
+ x = F.leaky_relu(x)
309
+ x = self.conv_post(x)
310
+ x = torch.tanh(x)
311
+
312
+ return x
313
+
314
+ def __prepare_scriptable__(self):
315
+ for l in self.ups:
316
+ for hook in l._forward_pre_hooks.values():
317
+ # The hook we want to remove is an instance of WeightNorm class, so
318
+ # normally we would do `if isinstance(...)` but this class is not accessible
319
+ # because of shadowing, so we check the module name directly.
320
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
321
+ if (
322
+ hook.__module__ == "torch.nn.utils.weight_norm"
323
+ and hook.__class__.__name__ == "WeightNorm"
324
+ ):
325
+ torch.nn.utils.remove_weight_norm(l)
326
+
327
+ for l in self.resblocks:
328
+ for hook in l._forward_pre_hooks.values():
329
+ if (
330
+ hook.__module__ == "torch.nn.utils.weight_norm"
331
+ and hook.__class__.__name__ == "WeightNorm"
332
+ ):
333
+ torch.nn.utils.remove_weight_norm(l)
334
+ return self
335
+
336
+ def remove_weight_norm(self):
337
+ for l in self.ups:
338
+ remove_weight_norm(l)
339
+ for l in self.resblocks:
340
+ l.remove_weight_norm()
341
+
342
+
343
+ class SineGen(torch.nn.Module):
344
+ """Definition of sine generator
345
+ SineGen(samp_rate, harmonic_num = 0,
346
+ sine_amp = 0.1, noise_std = 0.003,
347
+ voiced_threshold = 0,
348
+ flag_for_pulse=False)
349
+ samp_rate: sampling rate in Hz
350
+ harmonic_num: number of harmonic overtones (default 0)
351
+ sine_amp: amplitude of sine-wavefrom (default 0.1)
352
+ noise_std: std of Gaussian noise (default 0.003)
353
+ voiced_thoreshold: F0 threshold for U/V classification (default 0)
354
+ flag_for_pulse: this SinGen is used inside PulseGen (default False)
355
+ Note: when flag_for_pulse is True, the first time step of a voiced
356
+ segment is always sin(torch.pi) or cos(0)
357
+ """
358
+
359
+ def __init__(
360
+ self,
361
+ samp_rate,
362
+ harmonic_num=0,
363
+ sine_amp=0.1,
364
+ noise_std=0.003,
365
+ voiced_threshold=0,
366
+ flag_for_pulse=False,
367
+ ):
368
+ super(SineGen, self).__init__()
369
+ self.sine_amp = sine_amp
370
+ self.noise_std = noise_std
371
+ self.harmonic_num = harmonic_num
372
+ self.dim = self.harmonic_num + 1
373
+ self.sampling_rate = samp_rate
374
+ self.voiced_threshold = voiced_threshold
375
+
376
+ def _f02uv(self, f0):
377
+ # generate uv signal
378
+ uv = torch.ones_like(f0)
379
+ uv = uv * (f0 > self.voiced_threshold)
380
+ if uv.device.type == "privateuseone": # for DirectML
381
+ uv = uv.float()
382
+ return uv
383
+
384
+ def forward(self, f0: torch.Tensor, upp: int):
385
+ """sine_tensor, uv = forward(f0)
386
+ input F0: tensor(batchsize=1, length, dim=1)
387
+ f0 for unvoiced steps should be 0
388
+ output sine_tensor: tensor(batchsize=1, length, dim)
389
+ output uv: tensor(batchsize=1, length, 1)
390
+ """
391
+ with torch.no_grad():
392
+ f0 = f0[:, None].transpose(1, 2)
393
+ f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device)
394
+ # fundamental component
395
+ f0_buf[:, :, 0] = f0[:, :, 0]
396
+ for idx in range(self.harmonic_num):
397
+ f0_buf[:, :, idx + 1] = f0_buf[:, :, 0] * (
398
+ idx + 2
399
+ ) # idx + 2: the (idx+1)-th overtone, (idx+2)-th harmonic
400
+ rad_values = (f0_buf / float(self.sampling_rate)) % 1
401
+ rand_ini = torch.rand(
402
+ f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device
403
+ )
404
+ rand_ini[:, 0] = 0
405
+ rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini
406
+ tmp_over_one = torch.cumsum(rad_values, 1)
407
+ tmp_over_one *= upp
408
+ tmp_over_one = F.interpolate(
409
+ tmp_over_one.transpose(2, 1),
410
+ scale_factor=float(upp),
411
+ mode="linear",
412
+ align_corners=True,
413
+ ).transpose(2, 1)
414
+ rad_values = F.interpolate(
415
+ rad_values.transpose(2, 1), scale_factor=float(upp), mode="nearest"
416
+ ).transpose(
417
+ 2, 1
418
+ ) #######
419
+ tmp_over_one %= 1
420
+ tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0
421
+ cumsum_shift = torch.zeros_like(rad_values)
422
+ cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0
423
+ sine_waves = torch.sin(
424
+ torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * torch.pi
425
+ )
426
+ sine_waves = sine_waves * self.sine_amp
427
+ uv = self._f02uv(f0)
428
+ uv = F.interpolate(
429
+ uv.transpose(2, 1), scale_factor=float(upp), mode="nearest"
430
+ ).transpose(2, 1)
431
+ noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3
432
+ noise = noise_amp * torch.randn_like(sine_waves)
433
+ sine_waves = sine_waves * uv + noise
434
+ return sine_waves, uv, noise
435
+
436
+
437
+ class SourceModuleHnNSF(torch.nn.Module):
438
+ """SourceModule for hn-nsf
439
+ SourceModule(sampling_rate, harmonic_num=0, sine_amp=0.1,
440
+ add_noise_std=0.003, voiced_threshod=0)
441
+ sampling_rate: sampling_rate in Hz
442
+ harmonic_num: number of harmonic above F0 (default: 0)
443
+ sine_amp: amplitude of sine source signal (default: 0.1)
444
+ add_noise_std: std of additive Gaussian noise (default: 0.003)
445
+ note that amplitude of noise in unvoiced is decided
446
+ by sine_amp
447
+ voiced_threshold: threhold to set U/V given F0 (default: 0)
448
+ Sine_source, noise_source = SourceModuleHnNSF(F0_sampled)
449
+ F0_sampled (batchsize, length, 1)
450
+ Sine_source (batchsize, length, 1)
451
+ noise_source (batchsize, length 1)
452
+ uv (batchsize, length, 1)
453
+ """
454
+
455
+ def __init__(
456
+ self,
457
+ sampling_rate,
458
+ harmonic_num=0,
459
+ sine_amp=0.1,
460
+ add_noise_std=0.003,
461
+ voiced_threshod=0,
462
+ is_half=True,
463
+ ):
464
+ super(SourceModuleHnNSF, self).__init__()
465
+
466
+ self.sine_amp = sine_amp
467
+ self.noise_std = add_noise_std
468
+ self.is_half = is_half
469
+ # to produce sine waveforms
470
+ self.l_sin_gen = SineGen(
471
+ sampling_rate, harmonic_num, sine_amp, add_noise_std, voiced_threshod
472
+ )
473
+
474
+ # to merge source harmonics into a single excitation
475
+ self.l_linear = torch.nn.Linear(harmonic_num + 1, 1)
476
+ self.l_tanh = torch.nn.Tanh()
477
+ # self.ddtype:int = -1
478
+
479
+ def forward(self, x: torch.Tensor, upp: int = 1):
480
+ # if self.ddtype ==-1:
481
+ # self.ddtype = self.l_linear.weight.dtype
482
+ sine_wavs, uv, _ = self.l_sin_gen(x, upp)
483
+ # print(x.dtype,sine_wavs.dtype,self.l_linear.weight.dtype)
484
+ # if self.is_half:
485
+ # sine_wavs = sine_wavs.half()
486
+ # sine_merge = self.l_tanh(self.l_linear(sine_wavs.to(x)))
487
+ # print(sine_wavs.dtype,self.ddtype)
488
+ # if sine_wavs.dtype != self.l_linear.weight.dtype:
489
+ sine_wavs = sine_wavs.to(dtype=self.l_linear.weight.dtype)
490
+ sine_merge = self.l_tanh(self.l_linear(sine_wavs))
491
+ return sine_merge, None, None # noise, uv
492
+
493
+
494
+ class GeneratorNSF(torch.nn.Module):
495
+ def __init__(
496
+ self,
497
+ initial_channel,
498
+ resblock,
499
+ resblock_kernel_sizes,
500
+ resblock_dilation_sizes,
501
+ upsample_rates,
502
+ upsample_initial_channel,
503
+ upsample_kernel_sizes,
504
+ gin_channels,
505
+ sr,
506
+ is_half=False,
507
+ ):
508
+ super(GeneratorNSF, self).__init__()
509
+ self.num_kernels = len(resblock_kernel_sizes)
510
+ self.num_upsamples = len(upsample_rates)
511
+
512
+ self.f0_upsamp = torch.nn.Upsample(scale_factor=math.prod(upsample_rates))
513
+ self.m_source = SourceModuleHnNSF(
514
+ sampling_rate=sr, harmonic_num=0, is_half=is_half
515
+ )
516
+ self.noise_convs = nn.ModuleList()
517
+ self.conv_pre = Conv1d(
518
+ initial_channel, upsample_initial_channel, 7, 1, padding=3
519
+ )
520
+ resblock = modules.ResBlock1 if resblock == "1" else modules.ResBlock2
521
+
522
+ self.ups = nn.ModuleList()
523
+ for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)):
524
+ c_cur = upsample_initial_channel // (2 ** (i + 1))
525
+ self.ups.append(
526
+ weight_norm(
527
+ ConvTranspose1d(
528
+ upsample_initial_channel // (2**i),
529
+ upsample_initial_channel // (2 ** (i + 1)),
530
+ k,
531
+ u,
532
+ padding=(k - u) // 2,
533
+ )
534
+ )
535
+ )
536
+ if i + 1 < len(upsample_rates):
537
+ stride_f0 = math.prod(upsample_rates[i + 1 :])
538
+ self.noise_convs.append(
539
+ Conv1d(
540
+ 1,
541
+ c_cur,
542
+ kernel_size=stride_f0 * 2,
543
+ stride=stride_f0,
544
+ padding=stride_f0 // 2,
545
+ )
546
+ )
547
+ else:
548
+ self.noise_convs.append(Conv1d(1, c_cur, kernel_size=1))
549
+
550
+ self.resblocks = nn.ModuleList()
551
+ for i in range(len(self.ups)):
552
+ ch = upsample_initial_channel // (2 ** (i + 1))
553
+ for j, (k, d) in enumerate(
554
+ zip(resblock_kernel_sizes, resblock_dilation_sizes)
555
+ ):
556
+ self.resblocks.append(resblock(ch, k, d))
557
+
558
+ self.conv_post = Conv1d(ch, 1, 7, 1, padding=3, bias=False)
559
+ self.ups.apply(init_weights)
560
+
561
+ if gin_channels != 0:
562
+ self.cond = nn.Conv1d(gin_channels, upsample_initial_channel, 1)
563
+
564
+ self.upp = math.prod(upsample_rates)
565
+
566
+ self.lrelu_slope = modules.LRELU_SLOPE
567
+
568
+ def forward(self, x, f0, g: Optional[torch.Tensor] = None):
569
+ har_source, noi_source, uv = self.m_source(f0, self.upp)
570
+ har_source = har_source.transpose(1, 2)
571
+ x = self.conv_pre(x)
572
+ if g is not None:
573
+ x = x + self.cond(g)
574
+ # torch.jit.script() does not support direct indexing of torch modules
575
+ # That's why I wrote this
576
+ for i, (ups, noise_convs) in enumerate(zip(self.ups, self.noise_convs)):
577
+ if i < self.num_upsamples:
578
+ x = F.leaky_relu(x, self.lrelu_slope)
579
+ x = ups(x)
580
+ x_source = noise_convs(har_source)
581
+ x = x + x_source
582
+ xs: Optional[torch.Tensor] = None
583
+ l = [i * self.num_kernels + j for j in range(self.num_kernels)]
584
+ for j, resblock in enumerate(self.resblocks):
585
+ if j in l:
586
+ if xs is None:
587
+ xs = resblock(x)
588
+ else:
589
+ xs += resblock(x)
590
+ # This assertion cannot be ignored! \
591
+ # If ignored, it will cause torch.jit.script() compilation errors
592
+ assert isinstance(xs, torch.Tensor)
593
+ x = xs / self.num_kernels
594
+ x = F.leaky_relu(x)
595
+ x = self.conv_post(x)
596
+ x = torch.tanh(x)
597
+ return x
598
+
599
+ def remove_weight_norm(self):
600
+ for l in self.ups:
601
+ remove_weight_norm(l)
602
+ for l in self.resblocks:
603
+ l.remove_weight_norm()
604
+
605
+ def __prepare_scriptable__(self):
606
+ for l in self.ups:
607
+ for hook in l._forward_pre_hooks.values():
608
+ # The hook we want to remove is an instance of WeightNorm class, so
609
+ # normally we would do `if isinstance(...)` but this class is not accessible
610
+ # because of shadowing, so we check the module name directly.
611
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
612
+ if (
613
+ hook.__module__ == "torch.nn.utils.weight_norm"
614
+ and hook.__class__.__name__ == "WeightNorm"
615
+ ):
616
+ torch.nn.utils.remove_weight_norm(l)
617
+ for l in self.resblocks:
618
+ for hook in self.resblocks._forward_pre_hooks.values():
619
+ if (
620
+ hook.__module__ == "torch.nn.utils.weight_norm"
621
+ and hook.__class__.__name__ == "WeightNorm"
622
+ ):
623
+ torch.nn.utils.remove_weight_norm(l)
624
+ return self
625
+
626
+
627
+ sr2sr = {
628
+ "32k": 32000,
629
+ "40k": 40000,
630
+ "48k": 48000,
631
+ }
632
+
633
+
634
+ class SynthesizerTrnMs256NSFsid(nn.Module):
635
+ def __init__(
636
+ self,
637
+ spec_channels,
638
+ segment_size,
639
+ inter_channels,
640
+ hidden_channels,
641
+ filter_channels,
642
+ n_heads,
643
+ n_layers,
644
+ kernel_size,
645
+ p_dropout,
646
+ resblock,
647
+ resblock_kernel_sizes,
648
+ resblock_dilation_sizes,
649
+ upsample_rates,
650
+ upsample_initial_channel,
651
+ upsample_kernel_sizes,
652
+ spk_embed_dim,
653
+ gin_channels,
654
+ sr,
655
+ **kwargs
656
+ ):
657
+ super(SynthesizerTrnMs256NSFsid, self).__init__()
658
+ if isinstance(sr, str):
659
+ sr = sr2sr[sr]
660
+ self.spec_channels = spec_channels
661
+ self.inter_channels = inter_channels
662
+ self.hidden_channels = hidden_channels
663
+ self.filter_channels = filter_channels
664
+ self.n_heads = n_heads
665
+ self.n_layers = n_layers
666
+ self.kernel_size = kernel_size
667
+ self.p_dropout = float(p_dropout)
668
+ self.resblock = resblock
669
+ self.resblock_kernel_sizes = resblock_kernel_sizes
670
+ self.resblock_dilation_sizes = resblock_dilation_sizes
671
+ self.upsample_rates = upsample_rates
672
+ self.upsample_initial_channel = upsample_initial_channel
673
+ self.upsample_kernel_sizes = upsample_kernel_sizes
674
+ self.segment_size = segment_size
675
+ self.gin_channels = gin_channels
676
+ # self.hop_length = hop_length#
677
+ self.spk_embed_dim = spk_embed_dim
678
+ self.enc_p = TextEncoder256(
679
+ inter_channels,
680
+ hidden_channels,
681
+ filter_channels,
682
+ n_heads,
683
+ n_layers,
684
+ kernel_size,
685
+ float(p_dropout),
686
+ )
687
+ self.dec = GeneratorNSF(
688
+ inter_channels,
689
+ resblock,
690
+ resblock_kernel_sizes,
691
+ resblock_dilation_sizes,
692
+ upsample_rates,
693
+ upsample_initial_channel,
694
+ upsample_kernel_sizes,
695
+ gin_channels=gin_channels,
696
+ sr=sr,
697
+ is_half=kwargs["is_half"],
698
+ )
699
+ self.enc_q = PosteriorEncoder(
700
+ spec_channels,
701
+ inter_channels,
702
+ hidden_channels,
703
+ 5,
704
+ 1,
705
+ 16,
706
+ gin_channels=gin_channels,
707
+ )
708
+ self.flow = ResidualCouplingBlock(
709
+ inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
710
+ )
711
+ self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
712
+
713
+ def remove_weight_norm(self):
714
+ self.dec.remove_weight_norm()
715
+ self.flow.remove_weight_norm()
716
+ self.enc_q.remove_weight_norm()
717
+
718
+ def __prepare_scriptable__(self):
719
+ for hook in self.dec._forward_pre_hooks.values():
720
+ # The hook we want to remove is an instance of WeightNorm class, so
721
+ # normally we would do `if isinstance(...)` but this class is not accessible
722
+ # because of shadowing, so we check the module name directly.
723
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
724
+ if (
725
+ hook.__module__ == "torch.nn.utils.weight_norm"
726
+ and hook.__class__.__name__ == "WeightNorm"
727
+ ):
728
+ torch.nn.utils.remove_weight_norm(self.dec)
729
+ for hook in self.flow._forward_pre_hooks.values():
730
+ if (
731
+ hook.__module__ == "torch.nn.utils.weight_norm"
732
+ and hook.__class__.__name__ == "WeightNorm"
733
+ ):
734
+ torch.nn.utils.remove_weight_norm(self.flow)
735
+ if hasattr(self, "enc_q"):
736
+ for hook in self.enc_q._forward_pre_hooks.values():
737
+ if (
738
+ hook.__module__ == "torch.nn.utils.weight_norm"
739
+ and hook.__class__.__name__ == "WeightNorm"
740
+ ):
741
+ torch.nn.utils.remove_weight_norm(self.enc_q)
742
+ return self
743
+
744
+ @torch.jit.ignore
745
+ def forward(
746
+ self,
747
+ phone: torch.Tensor,
748
+ phone_lengths: torch.Tensor,
749
+ pitch: torch.Tensor,
750
+ pitchf: torch.Tensor,
751
+ y: torch.Tensor,
752
+ y_lengths: torch.Tensor,
753
+ ds: Optional[torch.Tensor] = None,
754
+ ): # 这里ds是id,[bs,1]
755
+ # print(1,pitch.shape)#[bs,t]
756
+ g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
757
+ m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
758
+ z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
759
+ z_p = self.flow(z, y_mask, g=g)
760
+ z_slice, ids_slice = commons.rand_slice_segments(
761
+ z, y_lengths, self.segment_size
762
+ )
763
+ # print(-1,pitchf.shape,ids_slice,self.segment_size,self.hop_length,self.segment_size//self.hop_length)
764
+ pitchf = commons.slice_segments2(pitchf, ids_slice, self.segment_size)
765
+ # print(-2,pitchf.shape,z_slice.shape)
766
+ o = self.dec(z_slice, pitchf, g=g)
767
+ return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
768
+
769
+ @torch.jit.export
770
+ def infer(
771
+ self,
772
+ phone: torch.Tensor,
773
+ phone_lengths: torch.Tensor,
774
+ pitch: torch.Tensor,
775
+ nsff0: torch.Tensor,
776
+ sid: torch.Tensor,
777
+ rate: Optional[torch.Tensor] = None,
778
+ ):
779
+ g = self.emb_g(sid).unsqueeze(-1)
780
+ m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
781
+ z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
782
+ if rate is not None:
783
+ assert isinstance(rate, torch.Tensor)
784
+ head = int(z_p.shape[2] * (1 - rate.item()))
785
+ z_p = z_p[:, :, head:]
786
+ x_mask = x_mask[:, :, head:]
787
+ nsff0 = nsff0[:, head:]
788
+ z = self.flow(z_p, x_mask, g=g, reverse=True)
789
+ o = self.dec(z * x_mask, nsff0, g=g)
790
+ return o, x_mask, (z, z_p, m_p, logs_p)
791
+
792
+
793
+ class SynthesizerTrnMs768NSFsid(nn.Module):
794
+ def __init__(
795
+ self,
796
+ spec_channels,
797
+ segment_size,
798
+ inter_channels,
799
+ hidden_channels,
800
+ filter_channels,
801
+ n_heads,
802
+ n_layers,
803
+ kernel_size,
804
+ p_dropout,
805
+ resblock,
806
+ resblock_kernel_sizes,
807
+ resblock_dilation_sizes,
808
+ upsample_rates,
809
+ upsample_initial_channel,
810
+ upsample_kernel_sizes,
811
+ spk_embed_dim,
812
+ gin_channels,
813
+ sr,
814
+ **kwargs
815
+ ):
816
+ super(SynthesizerTrnMs768NSFsid, self).__init__()
817
+ if isinstance(sr, str):
818
+ sr = sr
819
+ self.spec_channels = spec_channels
820
+ self.inter_channels = inter_channels
821
+ self.hidden_channels = hidden_channels
822
+ self.filter_channels = filter_channels
823
+ self.n_heads = n_heads
824
+ self.n_layers = n_layers
825
+ self.kernel_size = kernel_size
826
+ self.p_dropout = float(p_dropout)
827
+ self.resblock = resblock
828
+ self.resblock_kernel_sizes = resblock_kernel_sizes
829
+ self.resblock_dilation_sizes = resblock_dilation_sizes
830
+ self.upsample_rates = upsample_rates
831
+ self.upsample_initial_channel = upsample_initial_channel
832
+ self.upsample_kernel_sizes = upsample_kernel_sizes
833
+ self.segment_size = segment_size
834
+ self.gin_channels = gin_channels
835
+ # self.hop_length = hop_length#
836
+ self.spk_embed_dim = spk_embed_dim
837
+ self.enc_p = TextEncoder768(
838
+ inter_channels,
839
+ hidden_channels,
840
+ filter_channels,
841
+ n_heads,
842
+ n_layers,
843
+ kernel_size,
844
+ float(p_dropout),
845
+ )
846
+ self.dec = GeneratorNSF(
847
+ inter_channels,
848
+ resblock,
849
+ resblock_kernel_sizes,
850
+ resblock_dilation_sizes,
851
+ upsample_rates,
852
+ upsample_initial_channel,
853
+ upsample_kernel_sizes,
854
+ gin_channels=gin_channels,
855
+ sr=sr,
856
+ is_half=kwargs["is_half"],
857
+ )
858
+ self.enc_q = PosteriorEncoder(
859
+ spec_channels,
860
+ inter_channels,
861
+ hidden_channels,
862
+ 5,
863
+ 1,
864
+ 16,
865
+ gin_channels=gin_channels,
866
+ )
867
+ self.flow = ResidualCouplingBlock(
868
+ inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
869
+ )
870
+ self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
871
+
872
+ def remove_weight_norm(self):
873
+ self.dec.remove_weight_norm()
874
+ self.flow.remove_weight_norm()
875
+ self.enc_q.remove_weight_norm()
876
+
877
+ def __prepare_scriptable__(self):
878
+ for hook in self.dec._forward_pre_hooks.values():
879
+ # The hook we want to remove is an instance of WeightNorm class, so
880
+ # normally we would do `if isinstance(...)` but this class is not accessible
881
+ # because of shadowing, so we check the module name directly.
882
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
883
+ if (
884
+ hook.__module__ == "torch.nn.utils.weight_norm"
885
+ and hook.__class__.__name__ == "WeightNorm"
886
+ ):
887
+ torch.nn.utils.remove_weight_norm(self.dec)
888
+ for hook in self.flow._forward_pre_hooks.values():
889
+ if (
890
+ hook.__module__ == "torch.nn.utils.weight_norm"
891
+ and hook.__class__.__name__ == "WeightNorm"
892
+ ):
893
+ torch.nn.utils.remove_weight_norm(self.flow)
894
+ if hasattr(self, "enc_q"):
895
+ for hook in self.enc_q._forward_pre_hooks.values():
896
+ if (
897
+ hook.__module__ == "torch.nn.utils.weight_norm"
898
+ and hook.__class__.__name__ == "WeightNorm"
899
+ ):
900
+ torch.nn.utils.remove_weight_norm(self.enc_q)
901
+ return self
902
+
903
+ @torch.jit.ignore
904
+ def forward(
905
+ self, phone, phone_lengths, pitch, pitchf, y, y_lengths, ds
906
+ ): # 这里ds是id,[bs,1]
907
+ # print(1,pitch.shape)#[bs,t]
908
+ g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
909
+ m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
910
+ z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
911
+ z_p = self.flow(z, y_mask, g=g)
912
+ z_slice, ids_slice = commons.rand_slice_segments(
913
+ z, y_lengths, self.segment_size
914
+ )
915
+ # print(-1,pitchf.shape,ids_slice,self.segment_size,self.hop_length,self.segment_size//self.hop_length)
916
+ pitchf = commons.slice_segments2(pitchf, ids_slice, self.segment_size)
917
+ # print(-2,pitchf.shape,z_slice.shape)
918
+ o = self.dec(z_slice, pitchf, g=g)
919
+ return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
920
+
921
+ @torch.jit.export
922
+ def infer(
923
+ self,
924
+ phone: torch.Tensor,
925
+ phone_lengths: torch.Tensor,
926
+ pitch: torch.Tensor,
927
+ nsff0: torch.Tensor,
928
+ sid: torch.Tensor,
929
+ rate: Optional[torch.Tensor] = None,
930
+ ):
931
+ g = self.emb_g(sid).unsqueeze(-1)
932
+ m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
933
+ z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
934
+ if rate is not None:
935
+ head = int(z_p.shape[2] * (1.0 - rate.item()))
936
+ z_p = z_p[:, :, head:]
937
+ x_mask = x_mask[:, :, head:]
938
+ nsff0 = nsff0[:, head:]
939
+ z = self.flow(z_p, x_mask, g=g, reverse=True)
940
+ o = self.dec(z * x_mask, nsff0, g=g)
941
+ return o, x_mask, (z, z_p, m_p, logs_p)
942
+
943
+
944
+ class SynthesizerTrnMs256NSFsid_nono(nn.Module):
945
+ def __init__(
946
+ self,
947
+ spec_channels,
948
+ segment_size,
949
+ inter_channels,
950
+ hidden_channels,
951
+ filter_channels,
952
+ n_heads,
953
+ n_layers,
954
+ kernel_size,
955
+ p_dropout,
956
+ resblock,
957
+ resblock_kernel_sizes,
958
+ resblock_dilation_sizes,
959
+ upsample_rates,
960
+ upsample_initial_channel,
961
+ upsample_kernel_sizes,
962
+ spk_embed_dim,
963
+ gin_channels,
964
+ sr=None,
965
+ **kwargs
966
+ ):
967
+ super(SynthesizerTrnMs256NSFsid_nono, self).__init__()
968
+ self.spec_channels = spec_channels
969
+ self.inter_channels = inter_channels
970
+ self.hidden_channels = hidden_channels
971
+ self.filter_channels = filter_channels
972
+ self.n_heads = n_heads
973
+ self.n_layers = n_layers
974
+ self.kernel_size = kernel_size
975
+ self.p_dropout = float(p_dropout)
976
+ self.resblock = resblock
977
+ self.resblock_kernel_sizes = resblock_kernel_sizes
978
+ self.resblock_dilation_sizes = resblock_dilation_sizes
979
+ self.upsample_rates = upsample_rates
980
+ self.upsample_initial_channel = upsample_initial_channel
981
+ self.upsample_kernel_sizes = upsample_kernel_sizes
982
+ self.segment_size = segment_size
983
+ self.gin_channels = gin_channels
984
+ # self.hop_length = hop_length#
985
+ self.spk_embed_dim = spk_embed_dim
986
+ self.enc_p = TextEncoder256(
987
+ inter_channels,
988
+ hidden_channels,
989
+ filter_channels,
990
+ n_heads,
991
+ n_layers,
992
+ kernel_size,
993
+ float(p_dropout),
994
+ f0=False,
995
+ )
996
+ self.dec = Generator(
997
+ inter_channels,
998
+ resblock,
999
+ resblock_kernel_sizes,
1000
+ resblock_dilation_sizes,
1001
+ upsample_rates,
1002
+ upsample_initial_channel,
1003
+ upsample_kernel_sizes,
1004
+ gin_channels=gin_channels,
1005
+ )
1006
+ self.enc_q = PosteriorEncoder(
1007
+ spec_channels,
1008
+ inter_channels,
1009
+ hidden_channels,
1010
+ 5,
1011
+ 1,
1012
+ 16,
1013
+ gin_channels=gin_channels,
1014
+ )
1015
+ self.flow = ResidualCouplingBlock(
1016
+ inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
1017
+ )
1018
+ self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
1019
+
1020
+ def remove_weight_norm(self):
1021
+ self.dec.remove_weight_norm()
1022
+ self.flow.remove_weight_norm()
1023
+ self.enc_q.remove_weight_norm()
1024
+
1025
+ def __prepare_scriptable__(self):
1026
+ for hook in self.dec._forward_pre_hooks.values():
1027
+ # The hook we want to remove is an instance of WeightNorm class, so
1028
+ # normally we would do `if isinstance(...)` but this class is not accessible
1029
+ # because of shadowing, so we check the module name directly.
1030
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
1031
+ if (
1032
+ hook.__module__ == "torch.nn.utils.weight_norm"
1033
+ and hook.__class__.__name__ == "WeightNorm"
1034
+ ):
1035
+ torch.nn.utils.remove_weight_norm(self.dec)
1036
+ for hook in self.flow._forward_pre_hooks.values():
1037
+ if (
1038
+ hook.__module__ == "torch.nn.utils.weight_norm"
1039
+ and hook.__class__.__name__ == "WeightNorm"
1040
+ ):
1041
+ torch.nn.utils.remove_weight_norm(self.flow)
1042
+ if hasattr(self, "enc_q"):
1043
+ for hook in self.enc_q._forward_pre_hooks.values():
1044
+ if (
1045
+ hook.__module__ == "torch.nn.utils.weight_norm"
1046
+ and hook.__class__.__name__ == "WeightNorm"
1047
+ ):
1048
+ torch.nn.utils.remove_weight_norm(self.enc_q)
1049
+ return self
1050
+
1051
+ @torch.jit.ignore
1052
+ def forward(self, phone, phone_lengths, y, y_lengths, ds): # 这里ds是id,[bs,1]
1053
+ g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
1054
+ m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
1055
+ z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
1056
+ z_p = self.flow(z, y_mask, g=g)
1057
+ z_slice, ids_slice = commons.rand_slice_segments(
1058
+ z, y_lengths, self.segment_size
1059
+ )
1060
+ o = self.dec(z_slice, g=g)
1061
+ return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
1062
+
1063
+ @torch.jit.export
1064
+ def infer(
1065
+ self,
1066
+ phone: torch.Tensor,
1067
+ phone_lengths: torch.Tensor,
1068
+ sid: torch.Tensor,
1069
+ rate: Optional[torch.Tensor] = None,
1070
+ ):
1071
+ g = self.emb_g(sid).unsqueeze(-1)
1072
+ m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
1073
+ z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
1074
+ if rate is not None:
1075
+ head = int(z_p.shape[2] * (1.0 - rate.item()))
1076
+ z_p = z_p[:, :, head:]
1077
+ x_mask = x_mask[:, :, head:]
1078
+ z = self.flow(z_p, x_mask, g=g, reverse=True)
1079
+ o = self.dec(z * x_mask, g=g)
1080
+ return o, x_mask, (z, z_p, m_p, logs_p)
1081
+
1082
+
1083
+ class SynthesizerTrnMs768NSFsid_nono(nn.Module):
1084
+ def __init__(
1085
+ self,
1086
+ spec_channels,
1087
+ segment_size,
1088
+ inter_channels,
1089
+ hidden_channels,
1090
+ filter_channels,
1091
+ n_heads,
1092
+ n_layers,
1093
+ kernel_size,
1094
+ p_dropout,
1095
+ resblock,
1096
+ resblock_kernel_sizes,
1097
+ resblock_dilation_sizes,
1098
+ upsample_rates,
1099
+ upsample_initial_channel,
1100
+ upsample_kernel_sizes,
1101
+ spk_embed_dim,
1102
+ gin_channels,
1103
+ sr=None,
1104
+ **kwargs
1105
+ ):
1106
+ super(SynthesizerTrnMs768NSFsid_nono, self).__init__()
1107
+ self.spec_channels = spec_channels
1108
+ self.inter_channels = inter_channels
1109
+ self.hidden_channels = hidden_channels
1110
+ self.filter_channels = filter_channels
1111
+ self.n_heads = n_heads
1112
+ self.n_layers = n_layers
1113
+ self.kernel_size = kernel_size
1114
+ self.p_dropout = float(p_dropout)
1115
+ self.resblock = resblock
1116
+ self.resblock_kernel_sizes = resblock_kernel_sizes
1117
+ self.resblock_dilation_sizes = resblock_dilation_sizes
1118
+ self.upsample_rates = upsample_rates
1119
+ self.upsample_initial_channel = upsample_initial_channel
1120
+ self.upsample_kernel_sizes = upsample_kernel_sizes
1121
+ self.segment_size = segment_size
1122
+ self.gin_channels = gin_channels
1123
+ # self.hop_length = hop_length#
1124
+ self.spk_embed_dim = spk_embed_dim
1125
+ self.enc_p = TextEncoder768(
1126
+ inter_channels,
1127
+ hidden_channels,
1128
+ filter_channels,
1129
+ n_heads,
1130
+ n_layers,
1131
+ kernel_size,
1132
+ float(p_dropout),
1133
+ f0=False,
1134
+ )
1135
+ self.dec = Generator(
1136
+ inter_channels,
1137
+ resblock,
1138
+ resblock_kernel_sizes,
1139
+ resblock_dilation_sizes,
1140
+ upsample_rates,
1141
+ upsample_initial_channel,
1142
+ upsample_kernel_sizes,
1143
+ gin_channels=gin_channels,
1144
+ )
1145
+ self.enc_q = PosteriorEncoder(
1146
+ spec_channels,
1147
+ inter_channels,
1148
+ hidden_channels,
1149
+ 5,
1150
+ 1,
1151
+ 16,
1152
+ gin_channels=gin_channels,
1153
+ )
1154
+ self.flow = ResidualCouplingBlock(
1155
+ inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels
1156
+ )
1157
+ self.emb_g = nn.Embedding(self.spk_embed_dim, gin_channels)
1158
+
1159
+ def remove_weight_norm(self):
1160
+ self.dec.remove_weight_norm()
1161
+ self.flow.remove_weight_norm()
1162
+ self.enc_q.remove_weight_norm()
1163
+
1164
+ def __prepare_scriptable__(self):
1165
+ for hook in self.dec._forward_pre_hooks.values():
1166
+ # The hook we want to remove is an instance of WeightNorm class, so
1167
+ # normally we would do `if isinstance(...)` but this class is not accessible
1168
+ # because of shadowing, so we check the module name directly.
1169
+ # https://github.com/pytorch/pytorch/blob/be0ca00c5ce260eb5bcec3237357f7a30cc08983/torch/nn/utils/__init__.py#L3
1170
+ if (
1171
+ hook.__module__ == "torch.nn.utils.weight_norm"
1172
+ and hook.__class__.__name__ == "WeightNorm"
1173
+ ):
1174
+ torch.nn.utils.remove_weight_norm(self.dec)
1175
+ for hook in self.flow._forward_pre_hooks.values():
1176
+ if (
1177
+ hook.__module__ == "torch.nn.utils.weight_norm"
1178
+ and hook.__class__.__name__ == "WeightNorm"
1179
+ ):
1180
+ torch.nn.utils.remove_weight_norm(self.flow)
1181
+ if hasattr(self, "enc_q"):
1182
+ for hook in self.enc_q._forward_pre_hooks.values():
1183
+ if (
1184
+ hook.__module__ == "torch.nn.utils.weight_norm"
1185
+ and hook.__class__.__name__ == "WeightNorm"
1186
+ ):
1187
+ torch.nn.utils.remove_weight_norm(self.enc_q)
1188
+ return self
1189
+
1190
+ @torch.jit.ignore
1191
+ def forward(self, phone, phone_lengths, y, y_lengths, ds): # 这里ds是id,[bs,1]
1192
+ g = self.emb_g(ds).unsqueeze(-1) # [b, 256, 1]##1是t,广播的
1193
+ m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
1194
+ z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g)
1195
+ z_p = self.flow(z, y_mask, g=g)
1196
+ z_slice, ids_slice = commons.rand_slice_segments(
1197
+ z, y_lengths, self.segment_size
1198
+ )
1199
+ o = self.dec(z_slice, g=g)
1200
+ return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q)
1201
+
1202
+ @torch.jit.export
1203
+ def infer(
1204
+ self,
1205
+ phone: torch.Tensor,
1206
+ phone_lengths: torch.Tensor,
1207
+ sid: torch.Tensor,
1208
+ rate: Optional[torch.Tensor] = None,
1209
+ ):
1210
+ g = self.emb_g(sid).unsqueeze(-1)
1211
+ m_p, logs_p, x_mask = self.enc_p(phone, None, phone_lengths)
1212
+ z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
1213
+ if rate is not None:
1214
+ head = int(z_p.shape[2] * (1.0 - rate.item()))
1215
+ z_p = z_p[:, :, head:]
1216
+ x_mask = x_mask[:, :, head:]
1217
+ z = self.flow(z_p, x_mask, g=g, reverse=True)
1218
+ o = self.dec(z * x_mask, g=g)
1219
+ return o, x_mask, (z, z_p, m_p, logs_p)
1220
+
1221
+
1222
+ class MultiPeriodDiscriminator(torch.nn.Module):
1223
+ def __init__(self, use_spectral_norm=False):
1224
+ super(MultiPeriodDiscriminator, self).__init__()
1225
+ periods = [2, 3, 5, 7, 11, 17]
1226
+ # periods = [3, 5, 7, 11, 17, 23, 37]
1227
+
1228
+ discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
1229
+ discs = discs + [
1230
+ DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
1231
+ ]
1232
+ self.discriminators = nn.ModuleList(discs)
1233
+
1234
+ def forward(self, y, y_hat):
1235
+ y_d_rs = [] #
1236
+ y_d_gs = []
1237
+ fmap_rs = []
1238
+ fmap_gs = []
1239
+ for i, d in enumerate(self.discriminators):
1240
+ y_d_r, fmap_r = d(y)
1241
+ y_d_g, fmap_g = d(y_hat)
1242
+ # for j in range(len(fmap_r)):
1243
+ # print(i,j,y.shape,y_hat.shape,fmap_r[j].shape,fmap_g[j].shape)
1244
+ y_d_rs.append(y_d_r)
1245
+ y_d_gs.append(y_d_g)
1246
+ fmap_rs.append(fmap_r)
1247
+ fmap_gs.append(fmap_g)
1248
+
1249
+ return y_d_rs, y_d_gs, fmap_rs, fmap_gs
1250
+
1251
+
1252
+ class MultiPeriodDiscriminatorV2(torch.nn.Module):
1253
+ def __init__(self, use_spectral_norm=False):
1254
+ super(MultiPeriodDiscriminatorV2, self).__init__()
1255
+ # periods = [2, 3, 5, 7, 11, 17]
1256
+ periods = [2, 3, 5, 7, 11, 17, 23, 37]
1257
+
1258
+ discs = [DiscriminatorS(use_spectral_norm=use_spectral_norm)]
1259
+ discs = discs + [
1260
+ DiscriminatorP(i, use_spectral_norm=use_spectral_norm) for i in periods
1261
+ ]
1262
+ self.discriminators = nn.ModuleList(discs)
1263
+
1264
+ def forward(self, y, y_hat):
1265
+ y_d_rs = [] #
1266
+ y_d_gs = []
1267
+ fmap_rs = []
1268
+ fmap_gs = []
1269
+ for i, d in enumerate(self.discriminators):
1270
+ y_d_r, fmap_r = d(y)
1271
+ y_d_g, fmap_g = d(y_hat)
1272
+ # for j in range(len(fmap_r)):
1273
+ # print(i,j,y.shape,y_hat.shape,fmap_r[j].shape,fmap_g[j].shape)
1274
+ y_d_rs.append(y_d_r)
1275
+ y_d_gs.append(y_d_g)
1276
+ fmap_rs.append(fmap_r)
1277
+ fmap_gs.append(fmap_g)
1278
+
1279
+ return y_d_rs, y_d_gs, fmap_rs, fmap_gs
1280
+
1281
+
1282
+ class DiscriminatorS(torch.nn.Module):
1283
+ def __init__(self, use_spectral_norm=False):
1284
+ super(DiscriminatorS, self).__init__()
1285
+ norm_f = weight_norm if use_spectral_norm == False else spectral_norm
1286
+ self.convs = nn.ModuleList(
1287
+ [
1288
+ norm_f(Conv1d(1, 16, 15, 1, padding=7)),
1289
+ norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
1290
+ norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
1291
+ norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
1292
+ norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
1293
+ norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
1294
+ ]
1295
+ )
1296
+ self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
1297
+
1298
+ def forward(self, x):
1299
+ fmap = []
1300
+
1301
+ for l in self.convs:
1302
+ x = l(x)
1303
+ x = F.leaky_relu(x, modules.LRELU_SLOPE)
1304
+ fmap.append(x)
1305
+ x = self.conv_post(x)
1306
+ fmap.append(x)
1307
+ x = torch.flatten(x, 1, -1)
1308
+
1309
+ return x, fmap
1310
+
1311
+
1312
+ class DiscriminatorP(torch.nn.Module):
1313
+ def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
1314
+ super(DiscriminatorP, self).__init__()
1315
+ self.period = period
1316
+ self.use_spectral_norm = use_spectral_norm
1317
+ norm_f = weight_norm if use_spectral_norm == False else spectral_norm
1318
+ self.convs = nn.ModuleList(
1319
+ [
1320
+ norm_f(
1321
+ Conv2d(
1322
+ 1,
1323
+ 32,
1324
+ (kernel_size, 1),
1325
+ (stride, 1),
1326
+ padding=(get_padding(kernel_size, 1), 0),
1327
+ )
1328
+ ),
1329
+ norm_f(
1330
+ Conv2d(
1331
+ 32,
1332
+ 128,
1333
+ (kernel_size, 1),
1334
+ (stride, 1),
1335
+ padding=(get_padding(kernel_size, 1), 0),
1336
+ )
1337
+ ),
1338
+ norm_f(
1339
+ Conv2d(
1340
+ 128,
1341
+ 512,
1342
+ (kernel_size, 1),
1343
+ (stride, 1),
1344
+ padding=(get_padding(kernel_size, 1), 0),
1345
+ )
1346
+ ),
1347
+ norm_f(
1348
+ Conv2d(
1349
+ 512,
1350
+ 1024,
1351
+ (kernel_size, 1),
1352
+ (stride, 1),
1353
+ padding=(get_padding(kernel_size, 1), 0),
1354
+ )
1355
+ ),
1356
+ norm_f(
1357
+ Conv2d(
1358
+ 1024,
1359
+ 1024,
1360
+ (kernel_size, 1),
1361
+ 1,
1362
+ padding=(get_padding(kernel_size, 1), 0),
1363
+ )
1364
+ ),
1365
+ ]
1366
+ )
1367
+ self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
1368
+
1369
+ def forward(self, x):
1370
+ fmap = []
1371
+
1372
+ # 1d to 2d
1373
+ b, c, t = x.shape
1374
+ if t % self.period != 0: # pad first
1375
+ n_pad = self.period - (t % self.period)
1376
+ if has_xpu and x.dtype == torch.bfloat16:
1377
+ x = F.pad(x.to(dtype=torch.float16), (0, n_pad), "reflect").to(
1378
+ dtype=torch.bfloat16
1379
+ )
1380
+ else:
1381
+ x = F.pad(x, (0, n_pad), "reflect")
1382
+ t = t + n_pad
1383
+ x = x.view(b, c, t // self.period, self.period)
1384
+
1385
+ for l in self.convs:
1386
+ x = l(x)
1387
+ x = F.leaky_relu(x, modules.LRELU_SLOPE)
1388
+ fmap.append(x)
1389
+ x = self.conv_post(x)
1390
+ fmap.append(x)
1391
+ x = torch.flatten(x, 1, -1)
1392
+
1393
+ return x, fmap
rvc/lib/infer_pack/modules.py ADDED
@@ -0,0 +1,521 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import torch
3
+ from torch import nn
4
+ from torch.nn import functional as F
5
+
6
+ from torch.nn import Conv1d
7
+ from torch.nn.utils import remove_weight_norm
8
+ from torch.nn.utils.parametrizations import weight_norm
9
+
10
+
11
+ from . import commons
12
+ from .commons import init_weights, get_padding
13
+ from .transforms import piecewise_rational_quadratic_transform
14
+
15
+
16
+ LRELU_SLOPE = 0.1
17
+
18
+
19
+ class LayerNorm(nn.Module):
20
+ def __init__(self, channels, eps=1e-5):
21
+ super().__init__()
22
+ self.channels = channels
23
+ self.eps = eps
24
+
25
+ self.gamma = nn.Parameter(torch.ones(channels))
26
+ self.beta = nn.Parameter(torch.zeros(channels))
27
+
28
+ def forward(self, x):
29
+ x = x.transpose(1, -1)
30
+ x = F.layer_norm(x, (self.channels,), self.gamma, self.beta, self.eps)
31
+ return x.transpose(1, -1)
32
+
33
+
34
+ class ConvReluNorm(nn.Module):
35
+ def __init__(
36
+ self,
37
+ in_channels,
38
+ hidden_channels,
39
+ out_channels,
40
+ kernel_size,
41
+ n_layers,
42
+ p_dropout,
43
+ ):
44
+ super().__init__()
45
+ self.in_channels = in_channels
46
+ self.hidden_channels = hidden_channels
47
+ self.out_channels = out_channels
48
+ self.kernel_size = kernel_size
49
+ self.n_layers = n_layers
50
+ self.p_dropout = p_dropout
51
+ assert n_layers > 1, "Number of layers should be larger than 0."
52
+
53
+ self.conv_layers = nn.ModuleList()
54
+ self.norm_layers = nn.ModuleList()
55
+ self.conv_layers.append(
56
+ nn.Conv1d(
57
+ in_channels, hidden_channels, kernel_size, padding=kernel_size // 2
58
+ )
59
+ )
60
+ self.norm_layers.append(LayerNorm(hidden_channels))
61
+ self.relu_drop = nn.Sequential(nn.ReLU(), nn.Dropout(p_dropout))
62
+ for _ in range(n_layers - 1):
63
+ self.conv_layers.append(
64
+ nn.Conv1d(
65
+ hidden_channels,
66
+ hidden_channels,
67
+ kernel_size,
68
+ padding=kernel_size // 2,
69
+ )
70
+ )
71
+ self.norm_layers.append(LayerNorm(hidden_channels))
72
+ self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
73
+ self.proj.weight.data.zero_()
74
+ self.proj.bias.data.zero_()
75
+
76
+ def forward(self, x, x_mask):
77
+ x_org = x
78
+ for i in range(self.n_layers):
79
+ x = self.conv_layers[i](x * x_mask)
80
+ x = self.norm_layers[i](x)
81
+ x = self.relu_drop(x)
82
+ x = x_org + self.proj(x)
83
+ return x * x_mask
84
+
85
+
86
+ class DDSConv(nn.Module):
87
+ def __init__(self, channels, kernel_size, n_layers, p_dropout=0.0):
88
+ super().__init__()
89
+ self.channels = channels
90
+ self.kernel_size = kernel_size
91
+ self.n_layers = n_layers
92
+ self.p_dropout = p_dropout
93
+
94
+ self.drop = nn.Dropout(p_dropout)
95
+ self.convs_sep = nn.ModuleList()
96
+ self.convs_1x1 = nn.ModuleList()
97
+ self.norms_1 = nn.ModuleList()
98
+ self.norms_2 = nn.ModuleList()
99
+ for i in range(n_layers):
100
+ dilation = kernel_size**i
101
+ padding = (kernel_size * dilation - dilation) // 2
102
+ self.convs_sep.append(
103
+ nn.Conv1d(
104
+ channels,
105
+ channels,
106
+ kernel_size,
107
+ groups=channels,
108
+ dilation=dilation,
109
+ padding=padding,
110
+ )
111
+ )
112
+ self.convs_1x1.append(nn.Conv1d(channels, channels, 1))
113
+ self.norms_1.append(LayerNorm(channels))
114
+ self.norms_2.append(LayerNorm(channels))
115
+
116
+ def forward(self, x, x_mask, g=None):
117
+ if g is not None:
118
+ x = x + g
119
+ for i in range(self.n_layers):
120
+ y = self.convs_sep[i](x * x_mask)
121
+ y = self.norms_1[i](y)
122
+ y = F.gelu(y)
123
+ y = self.convs_1x1[i](y)
124
+ y = self.norms_2[i](y)
125
+ y = F.gelu(y)
126
+ y = self.drop(y)
127
+ x = x + y
128
+ return x * x_mask
129
+
130
+
131
+ class WN(torch.nn.Module):
132
+ def __init__(
133
+ self,
134
+ hidden_channels,
135
+ kernel_size,
136
+ dilation_rate,
137
+ n_layers,
138
+ gin_channels=0,
139
+ p_dropout=0,
140
+ ):
141
+ super(WN, self).__init__()
142
+ assert kernel_size % 2 == 1
143
+ self.hidden_channels = hidden_channels
144
+ self.kernel_size = (kernel_size,)
145
+ self.dilation_rate = dilation_rate
146
+ self.n_layers = n_layers
147
+ self.gin_channels = gin_channels
148
+ self.p_dropout = p_dropout
149
+
150
+ self.in_layers = torch.nn.ModuleList()
151
+ self.res_skip_layers = torch.nn.ModuleList()
152
+ self.drop = nn.Dropout(p_dropout)
153
+
154
+ if gin_channels != 0:
155
+ cond_layer = torch.nn.Conv1d(
156
+ gin_channels, 2 * hidden_channels * n_layers, 1
157
+ )
158
+ self.cond_layer = torch.nn.utils.parametrizations.weight_norm(
159
+ cond_layer, name="weight"
160
+ )
161
+
162
+ for i in range(n_layers):
163
+ dilation = dilation_rate**i
164
+ padding = int((kernel_size * dilation - dilation) / 2)
165
+ in_layer = torch.nn.Conv1d(
166
+ hidden_channels,
167
+ 2 * hidden_channels,
168
+ kernel_size,
169
+ dilation=dilation,
170
+ padding=padding,
171
+ )
172
+ in_layer = torch.nn.utils.parametrizations.weight_norm(
173
+ in_layer, name="weight"
174
+ )
175
+ self.in_layers.append(in_layer)
176
+ if i < n_layers - 1:
177
+ res_skip_channels = 2 * hidden_channels
178
+ else:
179
+ res_skip_channels = hidden_channels
180
+
181
+ res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1)
182
+ res_skip_layer = torch.nn.utils.parametrizations.weight_norm(
183
+ res_skip_layer, name="weight"
184
+ )
185
+ self.res_skip_layers.append(res_skip_layer)
186
+
187
+ def forward(self, x, x_mask, g=None, **kwargs):
188
+ output = torch.zeros_like(x)
189
+ n_channels_tensor = torch.IntTensor([self.hidden_channels])
190
+
191
+ if g is not None:
192
+ g = self.cond_layer(g)
193
+
194
+ for i in range(self.n_layers):
195
+ x_in = self.in_layers[i](x)
196
+ if g is not None:
197
+ cond_offset = i * 2 * self.hidden_channels
198
+ g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :]
199
+ else:
200
+ g_l = torch.zeros_like(x_in)
201
+
202
+ acts = commons.fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor)
203
+ acts = self.drop(acts)
204
+
205
+ res_skip_acts = self.res_skip_layers[i](acts)
206
+ if i < self.n_layers - 1:
207
+ res_acts = res_skip_acts[:, : self.hidden_channels, :]
208
+ x = (x + res_acts) * x_mask
209
+ output = output + res_skip_acts[:, self.hidden_channels :, :]
210
+ else:
211
+ output = output + res_skip_acts
212
+ return output * x_mask
213
+
214
+ def remove_weight_norm(self):
215
+ if self.gin_channels != 0:
216
+ torch.nn.utils.remove_weight_norm(self.cond_layer)
217
+ for l in self.in_layers:
218
+ torch.nn.utils.remove_weight_norm(l)
219
+ for l in self.res_skip_layers:
220
+ torch.nn.utils.remove_weight_norm(l)
221
+
222
+
223
+ class ResBlock1(torch.nn.Module):
224
+ def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)):
225
+ super(ResBlock1, self).__init__()
226
+ self.convs1 = nn.ModuleList(
227
+ [
228
+ weight_norm(
229
+ Conv1d(
230
+ channels,
231
+ channels,
232
+ kernel_size,
233
+ 1,
234
+ dilation=dilation[0],
235
+ padding=get_padding(kernel_size, dilation[0]),
236
+ )
237
+ ),
238
+ weight_norm(
239
+ Conv1d(
240
+ channels,
241
+ channels,
242
+ kernel_size,
243
+ 1,
244
+ dilation=dilation[1],
245
+ padding=get_padding(kernel_size, dilation[1]),
246
+ )
247
+ ),
248
+ weight_norm(
249
+ Conv1d(
250
+ channels,
251
+ channels,
252
+ kernel_size,
253
+ 1,
254
+ dilation=dilation[2],
255
+ padding=get_padding(kernel_size, dilation[2]),
256
+ )
257
+ ),
258
+ ]
259
+ )
260
+ self.convs1.apply(init_weights)
261
+
262
+ self.convs2 = nn.ModuleList(
263
+ [
264
+ weight_norm(
265
+ Conv1d(
266
+ channels,
267
+ channels,
268
+ kernel_size,
269
+ 1,
270
+ dilation=1,
271
+ padding=get_padding(kernel_size, 1),
272
+ )
273
+ ),
274
+ weight_norm(
275
+ Conv1d(
276
+ channels,
277
+ channels,
278
+ kernel_size,
279
+ 1,
280
+ dilation=1,
281
+ padding=get_padding(kernel_size, 1),
282
+ )
283
+ ),
284
+ weight_norm(
285
+ Conv1d(
286
+ channels,
287
+ channels,
288
+ kernel_size,
289
+ 1,
290
+ dilation=1,
291
+ padding=get_padding(kernel_size, 1),
292
+ )
293
+ ),
294
+ ]
295
+ )
296
+ self.convs2.apply(init_weights)
297
+
298
+ def forward(self, x, x_mask=None):
299
+ for c1, c2 in zip(self.convs1, self.convs2):
300
+ xt = F.leaky_relu(x, LRELU_SLOPE)
301
+ if x_mask is not None:
302
+ xt = xt * x_mask
303
+ xt = c1(xt)
304
+ xt = F.leaky_relu(xt, LRELU_SLOPE)
305
+ if x_mask is not None:
306
+ xt = xt * x_mask
307
+ xt = c2(xt)
308
+ x = xt + x
309
+ if x_mask is not None:
310
+ x = x * x_mask
311
+ return x
312
+
313
+ def remove_weight_norm(self):
314
+ for l in self.convs1:
315
+ remove_weight_norm(l)
316
+ for l in self.convs2:
317
+ remove_weight_norm(l)
318
+
319
+
320
+ class ResBlock2(torch.nn.Module):
321
+ def __init__(self, channels, kernel_size=3, dilation=(1, 3)):
322
+ super(ResBlock2, self).__init__()
323
+ self.convs = nn.ModuleList(
324
+ [
325
+ weight_norm(
326
+ Conv1d(
327
+ channels,
328
+ channels,
329
+ kernel_size,
330
+ 1,
331
+ dilation=dilation[0],
332
+ padding=get_padding(kernel_size, dilation[0]),
333
+ )
334
+ ),
335
+ weight_norm(
336
+ Conv1d(
337
+ channels,
338
+ channels,
339
+ kernel_size,
340
+ 1,
341
+ dilation=dilation[1],
342
+ padding=get_padding(kernel_size, dilation[1]),
343
+ )
344
+ ),
345
+ ]
346
+ )
347
+ self.convs.apply(init_weights)
348
+
349
+ def forward(self, x, x_mask=None):
350
+ for c in self.convs:
351
+ xt = F.leaky_relu(x, LRELU_SLOPE)
352
+ if x_mask is not None:
353
+ xt = xt * x_mask
354
+ xt = c(xt)
355
+ x = xt + x
356
+ if x_mask is not None:
357
+ x = x * x_mask
358
+ return x
359
+
360
+ def remove_weight_norm(self):
361
+ for l in self.convs:
362
+ remove_weight_norm(l)
363
+
364
+
365
+ class Log(nn.Module):
366
+ def forward(self, x, x_mask, reverse=False, **kwargs):
367
+ if not reverse:
368
+ y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask
369
+ logdet = torch.sum(-y, [1, 2])
370
+ return y, logdet
371
+ else:
372
+ x = torch.exp(x) * x_mask
373
+ return x
374
+
375
+
376
+ class Flip(nn.Module):
377
+ def forward(self, x, *args, reverse=False, **kwargs):
378
+ x = torch.flip(x, [1])
379
+ if not reverse:
380
+ logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device)
381
+ return x, logdet
382
+ else:
383
+ return x
384
+
385
+
386
+ class ElementwiseAffine(nn.Module):
387
+ def __init__(self, channels):
388
+ super().__init__()
389
+ self.channels = channels
390
+ self.m = nn.Parameter(torch.zeros(channels, 1))
391
+ self.logs = nn.Parameter(torch.zeros(channels, 1))
392
+
393
+ def forward(self, x, x_mask, reverse=False, **kwargs):
394
+ if not reverse:
395
+ y = self.m + torch.exp(self.logs) * x
396
+ y = y * x_mask
397
+ logdet = torch.sum(self.logs * x_mask, [1, 2])
398
+ return y, logdet
399
+ else:
400
+ x = (x - self.m) * torch.exp(-self.logs) * x_mask
401
+ return x
402
+
403
+
404
+ class ResidualCouplingLayer(nn.Module):
405
+ def __init__(
406
+ self,
407
+ channels,
408
+ hidden_channels,
409
+ kernel_size,
410
+ dilation_rate,
411
+ n_layers,
412
+ p_dropout=0,
413
+ gin_channels=0,
414
+ mean_only=False,
415
+ ):
416
+ assert channels % 2 == 0, "channels should be divisible by 2"
417
+ super().__init__()
418
+ self.channels = channels
419
+ self.hidden_channels = hidden_channels
420
+ self.kernel_size = kernel_size
421
+ self.dilation_rate = dilation_rate
422
+ self.n_layers = n_layers
423
+ self.half_channels = channels // 2
424
+ self.mean_only = mean_only
425
+
426
+ self.pre = nn.Conv1d(self.half_channels, hidden_channels, 1)
427
+ self.enc = WN(
428
+ hidden_channels,
429
+ kernel_size,
430
+ dilation_rate,
431
+ n_layers,
432
+ p_dropout=p_dropout,
433
+ gin_channels=gin_channels,
434
+ )
435
+ self.post = nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1)
436
+ self.post.weight.data.zero_()
437
+ self.post.bias.data.zero_()
438
+
439
+ def forward(self, x, x_mask, g=None, reverse=False):
440
+ x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
441
+ h = self.pre(x0) * x_mask
442
+ h = self.enc(h, x_mask, g=g)
443
+ stats = self.post(h) * x_mask
444
+ if not self.mean_only:
445
+ m, logs = torch.split(stats, [self.half_channels] * 2, 1)
446
+ else:
447
+ m = stats
448
+ logs = torch.zeros_like(m)
449
+
450
+ if not reverse:
451
+ x1 = m + x1 * torch.exp(logs) * x_mask
452
+ x = torch.cat([x0, x1], 1)
453
+ logdet = torch.sum(logs, [1, 2])
454
+ return x, logdet
455
+ else:
456
+ x1 = (x1 - m) * torch.exp(-logs) * x_mask
457
+ x = torch.cat([x0, x1], 1)
458
+ return x
459
+
460
+ def remove_weight_norm(self):
461
+ self.enc.remove_weight_norm()
462
+
463
+
464
+ class ConvFlow(nn.Module):
465
+ def __init__(
466
+ self,
467
+ in_channels,
468
+ filter_channels,
469
+ kernel_size,
470
+ n_layers,
471
+ num_bins=10,
472
+ tail_bound=5.0,
473
+ ):
474
+ super().__init__()
475
+ self.in_channels = in_channels
476
+ self.filter_channels = filter_channels
477
+ self.kernel_size = kernel_size
478
+ self.n_layers = n_layers
479
+ self.num_bins = num_bins
480
+ self.tail_bound = tail_bound
481
+ self.half_channels = in_channels // 2
482
+
483
+ self.pre = nn.Conv1d(self.half_channels, filter_channels, 1)
484
+ self.convs = DDSConv(filter_channels, kernel_size, n_layers, p_dropout=0.0)
485
+ self.proj = nn.Conv1d(
486
+ filter_channels, self.half_channels * (num_bins * 3 - 1), 1
487
+ )
488
+ self.proj.weight.data.zero_()
489
+ self.proj.bias.data.zero_()
490
+
491
+ def forward(self, x, x_mask, g=None, reverse=False):
492
+ x0, x1 = torch.split(x, [self.half_channels] * 2, 1)
493
+ h = self.pre(x0)
494
+ h = self.convs(h, x_mask, g=g)
495
+ h = self.proj(h) * x_mask
496
+
497
+ b, c, t = x0.shape
498
+ h = h.reshape(b, c, -1, t).permute(0, 1, 3, 2)
499
+
500
+ unnormalized_widths = h[..., : self.num_bins] / math.sqrt(self.filter_channels)
501
+ unnormalized_heights = h[..., self.num_bins : 2 * self.num_bins] / math.sqrt(
502
+ self.filter_channels
503
+ )
504
+ unnormalized_derivatives = h[..., 2 * self.num_bins :]
505
+
506
+ x1, logabsdet = piecewise_rational_quadratic_transform(
507
+ x1,
508
+ unnormalized_widths,
509
+ unnormalized_heights,
510
+ unnormalized_derivatives,
511
+ inverse=reverse,
512
+ tails="linear",
513
+ tail_bound=self.tail_bound,
514
+ )
515
+
516
+ x = torch.cat([x0, x1], 1) * x_mask
517
+ logdet = torch.sum(logabsdet * x_mask, [1, 2])
518
+ if not reverse:
519
+ return x, logdet
520
+ else:
521
+ return x
rvc/lib/infer_pack/modules/F0Predictor/DioF0Predictor.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from infer_pack.modules.F0Predictor.F0Predictor import F0Predictor
2
+ import pyworld
3
+ import numpy as np
4
+
5
+
6
+ class DioF0Predictor(F0Predictor):
7
+ def __init__(self, hop_length=512, f0_min=50, f0_max=1100, sampling_rate=44100):
8
+ self.hop_length = hop_length
9
+ self.f0_min = f0_min
10
+ self.f0_max = f0_max
11
+ self.sampling_rate = sampling_rate
12
+
13
+ def interpolate_f0(self, f0):
14
+ data = np.reshape(f0, (f0.size, 1))
15
+
16
+ vuv_vector = np.zeros((data.size, 1), dtype=np.float32)
17
+ vuv_vector[data > 0.0] = 1.0
18
+ vuv_vector[data <= 0.0] = 0.0
19
+
20
+ ip_data = data
21
+
22
+ frame_number = data.size
23
+ last_value = 0.0
24
+ for i in range(frame_number):
25
+ if data[i] <= 0.0:
26
+ j = i + 1
27
+ for j in range(i + 1, frame_number):
28
+ if data[j] > 0.0:
29
+ break
30
+ if j < frame_number - 1:
31
+ if last_value > 0.0:
32
+ step = (data[j] - data[i - 1]) / float(j - i)
33
+ for k in range(i, j):
34
+ ip_data[k] = data[i - 1] + step * (k - i + 1)
35
+ else:
36
+ for k in range(i, j):
37
+ ip_data[k] = data[j]
38
+ else:
39
+ for k in range(i, frame_number):
40
+ ip_data[k] = last_value
41
+ else:
42
+ ip_data[i] = data[i] # 这里可能存在一个没有必要的拷贝
43
+ last_value = data[i]
44
+
45
+ return ip_data[:, 0], vuv_vector[:, 0]
46
+
47
+ def resize_f0(self, x, target_len):
48
+ source = np.array(x)
49
+ source[source < 0.001] = np.nan
50
+ target = np.interp(
51
+ np.arange(0, len(source) * target_len, len(source)) / target_len,
52
+ np.arange(0, len(source)),
53
+ source,
54
+ )
55
+ res = np.nan_to_num(target)
56
+ return res
57
+
58
+ def compute_f0(self, wav, p_len=None):
59
+ if p_len is None:
60
+ p_len = wav.shape[0] // self.hop_length
61
+ f0, t = pyworld.dio(
62
+ wav.astype(np.double),
63
+ fs=self.sampling_rate,
64
+ f0_floor=self.f0_min,
65
+ f0_ceil=self.f0_max,
66
+ frame_period=1000 * self.hop_length / self.sampling_rate,
67
+ )
68
+ f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.sampling_rate)
69
+ for index, pitch in enumerate(f0):
70
+ f0[index] = round(pitch, 1)
71
+ return self.interpolate_f0(self.resize_f0(f0, p_len))[0]
72
+
73
+ def compute_f0_uv(self, wav, p_len=None):
74
+ if p_len is None:
75
+ p_len = wav.shape[0] // self.hop_length
76
+ f0, t = pyworld.dio(
77
+ wav.astype(np.double),
78
+ fs=self.sampling_rate,
79
+ f0_floor=self.f0_min,
80
+ f0_ceil=self.f0_max,
81
+ frame_period=1000 * self.hop_length / self.sampling_rate,
82
+ )
83
+ f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.sampling_rate)
84
+ for index, pitch in enumerate(f0):
85
+ f0[index] = round(pitch, 1)
86
+ return self.interpolate_f0(self.resize_f0(f0, p_len))
rvc/lib/infer_pack/modules/F0Predictor/F0Predictor.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ class F0Predictor(object):
2
+ def compute_f0(self, wav, p_len):
3
+ pass
4
+
5
+ def compute_f0_uv(self, wav, p_len):
6
+ pass
rvc/lib/infer_pack/modules/F0Predictor/HarvestF0Predictor.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from infer_pack.modules.F0Predictor.F0Predictor import F0Predictor
2
+ import pyworld
3
+ import numpy as np
4
+
5
+
6
+ class HarvestF0Predictor(F0Predictor):
7
+ def __init__(self, hop_length=512, f0_min=50, f0_max=1100, sampling_rate=44100):
8
+ self.hop_length = hop_length
9
+ self.f0_min = f0_min
10
+ self.f0_max = f0_max
11
+ self.sampling_rate = sampling_rate
12
+
13
+ def interpolate_f0(self, f0):
14
+ data = np.reshape(f0, (f0.size, 1))
15
+
16
+ vuv_vector = np.zeros((data.size, 1), dtype=np.float32)
17
+ vuv_vector[data > 0.0] = 1.0
18
+ vuv_vector[data <= 0.0] = 0.0
19
+
20
+ ip_data = data
21
+
22
+ frame_number = data.size
23
+ last_value = 0.0
24
+ for i in range(frame_number):
25
+ if data[i] <= 0.0:
26
+ j = i + 1
27
+ for j in range(i + 1, frame_number):
28
+ if data[j] > 0.0:
29
+ break
30
+ if j < frame_number - 1:
31
+ if last_value > 0.0:
32
+ step = (data[j] - data[i - 1]) / float(j - i)
33
+ for k in range(i, j):
34
+ ip_data[k] = data[i - 1] + step * (k - i + 1)
35
+ else:
36
+ for k in range(i, j):
37
+ ip_data[k] = data[j]
38
+ else:
39
+ for k in range(i, frame_number):
40
+ ip_data[k] = last_value
41
+ else:
42
+ ip_data[i] = data[i]
43
+ last_value = data[i]
44
+
45
+ return ip_data[:, 0], vuv_vector[:, 0]
46
+
47
+ def resize_f0(self, x, target_len):
48
+ source = np.array(x)
49
+ source[source < 0.001] = np.nan
50
+ target = np.interp(
51
+ np.arange(0, len(source) * target_len, len(source)) / target_len,
52
+ np.arange(0, len(source)),
53
+ source,
54
+ )
55
+ res = np.nan_to_num(target)
56
+ return res
57
+
58
+ def compute_f0(self, wav, p_len=None):
59
+ if p_len is None:
60
+ p_len = wav.shape[0] // self.hop_length
61
+ f0, t = pyworld.harvest(
62
+ wav.astype(np.double),
63
+ fs=self.sampling_rate,
64
+ f0_ceil=self.f0_max,
65
+ f0_floor=self.f0_min,
66
+ frame_period=1000 * self.hop_length / self.sampling_rate,
67
+ )
68
+ f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.fs)
69
+ return self.interpolate_f0(self.resize_f0(f0, p_len))[0]
70
+
71
+ def compute_f0_uv(self, wav, p_len=None):
72
+ if p_len is None:
73
+ p_len = wav.shape[0] // self.hop_length
74
+ f0, t = pyworld.harvest(
75
+ wav.astype(np.double),
76
+ fs=self.sampling_rate,
77
+ f0_floor=self.f0_min,
78
+ f0_ceil=self.f0_max,
79
+ frame_period=1000 * self.hop_length / self.sampling_rate,
80
+ )
81
+ f0 = pyworld.stonemask(wav.astype(np.double), f0, t, self.sampling_rate)
82
+ return self.interpolate_f0(self.resize_f0(f0, p_len))
rvc/lib/infer_pack/modules/F0Predictor/PMF0Predictor.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from infer_pack.modules.F0Predictor.F0Predictor import F0Predictor
2
+ import parselmouth
3
+ import numpy as np
4
+
5
+
6
+ class PMF0Predictor(F0Predictor):
7
+ def __init__(self, hop_length=512, f0_min=50, f0_max=1100, sampling_rate=44100):
8
+ self.hop_length = hop_length
9
+ self.f0_min = f0_min
10
+ self.f0_max = f0_max
11
+ self.sampling_rate = sampling_rate
12
+
13
+ def interpolate_f0(self, f0):
14
+ data = np.reshape(f0, (f0.size, 1))
15
+
16
+ vuv_vector = np.zeros((data.size, 1), dtype=np.float32)
17
+ vuv_vector[data > 0.0] = 1.0
18
+ vuv_vector[data <= 0.0] = 0.0
19
+
20
+ ip_data = data
21
+
22
+ frame_number = data.size
23
+ last_value = 0.0
24
+ for i in range(frame_number):
25
+ if data[i] <= 0.0:
26
+ j = i + 1
27
+ for j in range(i + 1, frame_number):
28
+ if data[j] > 0.0:
29
+ break
30
+ if j < frame_number - 1:
31
+ if last_value > 0.0:
32
+ step = (data[j] - data[i - 1]) / float(j - i)
33
+ for k in range(i, j):
34
+ ip_data[k] = data[i - 1] + step * (k - i + 1)
35
+ else:
36
+ for k in range(i, j):
37
+ ip_data[k] = data[j]
38
+ else:
39
+ for k in range(i, frame_number):
40
+ ip_data[k] = last_value
41
+ else:
42
+ ip_data[i] = data[i] # 这里可能存在一个没有必要的拷贝
43
+ last_value = data[i]
44
+
45
+ return ip_data[:, 0], vuv_vector[:, 0]
46
+
47
+ def compute_f0(self, wav, p_len=None):
48
+ x = wav
49
+ if p_len is None:
50
+ p_len = x.shape[0] // self.hop_length
51
+ else:
52
+ assert abs(p_len - x.shape[0] // self.hop_length) < 4, "pad length error"
53
+ time_step = self.hop_length / self.sampling_rate * 1000
54
+ f0 = (
55
+ parselmouth.Sound(x, self.sampling_rate)
56
+ .to_pitch_ac(
57
+ time_step=time_step / 1000,
58
+ voicing_threshold=0.6,
59
+ pitch_floor=self.f0_min,
60
+ pitch_ceiling=self.f0_max,
61
+ )
62
+ .selected_array["frequency"]
63
+ )
64
+
65
+ pad_size = (p_len - len(f0) + 1) // 2
66
+ if pad_size > 0 or p_len - len(f0) - pad_size > 0:
67
+ f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")
68
+ f0, uv = self.interpolate_f0(f0)
69
+ return f0
70
+
71
+ def compute_f0_uv(self, wav, p_len=None):
72
+ x = wav
73
+ if p_len is None:
74
+ p_len = x.shape[0] // self.hop_length
75
+ else:
76
+ assert abs(p_len - x.shape[0] // self.hop_length) < 4, "pad length error"
77
+ time_step = self.hop_length / self.sampling_rate * 1000
78
+ f0 = (
79
+ parselmouth.Sound(x, self.sampling_rate)
80
+ .to_pitch_ac(
81
+ time_step=time_step / 1000,
82
+ voicing_threshold=0.6,
83
+ pitch_floor=self.f0_min,
84
+ pitch_ceiling=self.f0_max,
85
+ )
86
+ .selected_array["frequency"]
87
+ )
88
+
89
+ pad_size = (p_len - len(f0) + 1) // 2
90
+ if pad_size > 0 or p_len - len(f0) - pad_size > 0:
91
+ f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")
92
+ f0, uv = self.interpolate_f0(f0)
93
+ return f0, uv
rvc/lib/infer_pack/modules/F0Predictor/__init__.py ADDED
File without changes
rvc/lib/infer_pack/transforms.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch.nn import functional as F
3
+
4
+ import numpy as np
5
+
6
+
7
+ DEFAULT_MIN_BIN_WIDTH = 1e-3
8
+ DEFAULT_MIN_BIN_HEIGHT = 1e-3
9
+ DEFAULT_MIN_DERIVATIVE = 1e-3
10
+
11
+
12
+ def piecewise_rational_quadratic_transform(
13
+ inputs,
14
+ unnormalized_widths,
15
+ unnormalized_heights,
16
+ unnormalized_derivatives,
17
+ inverse=False,
18
+ tails=None,
19
+ tail_bound=1.0,
20
+ min_bin_width=DEFAULT_MIN_BIN_WIDTH,
21
+ min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
22
+ min_derivative=DEFAULT_MIN_DERIVATIVE,
23
+ ):
24
+ if tails is None:
25
+ spline_fn = rational_quadratic_spline
26
+ spline_kwargs = {}
27
+ else:
28
+ spline_fn = unconstrained_rational_quadratic_spline
29
+ spline_kwargs = {"tails": tails, "tail_bound": tail_bound}
30
+
31
+ outputs, logabsdet = spline_fn(
32
+ inputs=inputs,
33
+ unnormalized_widths=unnormalized_widths,
34
+ unnormalized_heights=unnormalized_heights,
35
+ unnormalized_derivatives=unnormalized_derivatives,
36
+ inverse=inverse,
37
+ min_bin_width=min_bin_width,
38
+ min_bin_height=min_bin_height,
39
+ min_derivative=min_derivative,
40
+ **spline_kwargs
41
+ )
42
+ return outputs, logabsdet
43
+
44
+
45
+ def searchsorted(bin_locations, inputs, eps=1e-6):
46
+ bin_locations[..., -1] += eps
47
+ return torch.sum(inputs[..., None] >= bin_locations, dim=-1) - 1
48
+
49
+
50
+ def unconstrained_rational_quadratic_spline(
51
+ inputs,
52
+ unnormalized_widths,
53
+ unnormalized_heights,
54
+ unnormalized_derivatives,
55
+ inverse=False,
56
+ tails="linear",
57
+ tail_bound=1.0,
58
+ min_bin_width=DEFAULT_MIN_BIN_WIDTH,
59
+ min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
60
+ min_derivative=DEFAULT_MIN_DERIVATIVE,
61
+ ):
62
+ inside_interval_mask = (inputs >= -tail_bound) & (inputs <= tail_bound)
63
+ outside_interval_mask = ~inside_interval_mask
64
+
65
+ outputs = torch.zeros_like(inputs)
66
+ logabsdet = torch.zeros_like(inputs)
67
+
68
+ if tails == "linear":
69
+ unnormalized_derivatives = F.pad(unnormalized_derivatives, pad=(1, 1))
70
+ constant = np.log(np.exp(1 - min_derivative) - 1)
71
+ unnormalized_derivatives[..., 0] = constant
72
+ unnormalized_derivatives[..., -1] = constant
73
+
74
+ outputs[outside_interval_mask] = inputs[outside_interval_mask]
75
+ logabsdet[outside_interval_mask] = 0
76
+ else:
77
+ raise RuntimeError("{} tails are not implemented.".format(tails))
78
+
79
+ (
80
+ outputs[inside_interval_mask],
81
+ logabsdet[inside_interval_mask],
82
+ ) = rational_quadratic_spline(
83
+ inputs=inputs[inside_interval_mask],
84
+ unnormalized_widths=unnormalized_widths[inside_interval_mask, :],
85
+ unnormalized_heights=unnormalized_heights[inside_interval_mask, :],
86
+ unnormalized_derivatives=unnormalized_derivatives[inside_interval_mask, :],
87
+ inverse=inverse,
88
+ left=-tail_bound,
89
+ right=tail_bound,
90
+ bottom=-tail_bound,
91
+ top=tail_bound,
92
+ min_bin_width=min_bin_width,
93
+ min_bin_height=min_bin_height,
94
+ min_derivative=min_derivative,
95
+ )
96
+
97
+ return outputs, logabsdet
98
+
99
+
100
+ def rational_quadratic_spline(
101
+ inputs,
102
+ unnormalized_widths,
103
+ unnormalized_heights,
104
+ unnormalized_derivatives,
105
+ inverse=False,
106
+ left=0.0,
107
+ right=1.0,
108
+ bottom=0.0,
109
+ top=1.0,
110
+ min_bin_width=DEFAULT_MIN_BIN_WIDTH,
111
+ min_bin_height=DEFAULT_MIN_BIN_HEIGHT,
112
+ min_derivative=DEFAULT_MIN_DERIVATIVE,
113
+ ):
114
+ if torch.min(inputs) < left or torch.max(inputs) > right:
115
+ raise ValueError("Input to a transform is not within its domain")
116
+
117
+ num_bins = unnormalized_widths.shape[-1]
118
+
119
+ if min_bin_width * num_bins > 1.0:
120
+ raise ValueError("Minimal bin width too large for the number of bins")
121
+ if min_bin_height * num_bins > 1.0:
122
+ raise ValueError("Minimal bin height too large for the number of bins")
123
+
124
+ widths = F.softmax(unnormalized_widths, dim=-1)
125
+ widths = min_bin_width + (1 - min_bin_width * num_bins) * widths
126
+ cumwidths = torch.cumsum(widths, dim=-1)
127
+ cumwidths = F.pad(cumwidths, pad=(1, 0), mode="constant", value=0.0)
128
+ cumwidths = (right - left) * cumwidths + left
129
+ cumwidths[..., 0] = left
130
+ cumwidths[..., -1] = right
131
+ widths = cumwidths[..., 1:] - cumwidths[..., :-1]
132
+
133
+ derivatives = min_derivative + F.softplus(unnormalized_derivatives)
134
+
135
+ heights = F.softmax(unnormalized_heights, dim=-1)
136
+ heights = min_bin_height + (1 - min_bin_height * num_bins) * heights
137
+ cumheights = torch.cumsum(heights, dim=-1)
138
+ cumheights = F.pad(cumheights, pad=(1, 0), mode="constant", value=0.0)
139
+ cumheights = (top - bottom) * cumheights + bottom
140
+ cumheights[..., 0] = bottom
141
+ cumheights[..., -1] = top
142
+ heights = cumheights[..., 1:] - cumheights[..., :-1]
143
+
144
+ if inverse:
145
+ bin_idx = searchsorted(cumheights, inputs)[..., None]
146
+ else:
147
+ bin_idx = searchsorted(cumwidths, inputs)[..., None]
148
+
149
+ input_cumwidths = cumwidths.gather(-1, bin_idx)[..., 0]
150
+ input_bin_widths = widths.gather(-1, bin_idx)[..., 0]
151
+
152
+ input_cumheights = cumheights.gather(-1, bin_idx)[..., 0]
153
+ delta = heights / widths
154
+ input_delta = delta.gather(-1, bin_idx)[..., 0]
155
+
156
+ input_derivatives = derivatives.gather(-1, bin_idx)[..., 0]
157
+ input_derivatives_plus_one = derivatives[..., 1:].gather(-1, bin_idx)[..., 0]
158
+
159
+ input_heights = heights.gather(-1, bin_idx)[..., 0]
160
+
161
+ if inverse:
162
+ a = (inputs - input_cumheights) * (
163
+ input_derivatives + input_derivatives_plus_one - 2 * input_delta
164
+ ) + input_heights * (input_delta - input_derivatives)
165
+ b = input_heights * input_derivatives - (inputs - input_cumheights) * (
166
+ input_derivatives + input_derivatives_plus_one - 2 * input_delta
167
+ )
168
+ c = -input_delta * (inputs - input_cumheights)
169
+
170
+ discriminant = b.pow(2) - 4 * a * c
171
+ assert (discriminant >= 0).all()
172
+
173
+ root = (2 * c) / (-b - torch.sqrt(discriminant))
174
+ outputs = root * input_bin_widths + input_cumwidths
175
+
176
+ theta_one_minus_theta = root * (1 - root)
177
+ denominator = input_delta + (
178
+ (input_derivatives + input_derivatives_plus_one - 2 * input_delta)
179
+ * theta_one_minus_theta
180
+ )
181
+ derivative_numerator = input_delta.pow(2) * (
182
+ input_derivatives_plus_one * root.pow(2)
183
+ + 2 * input_delta * theta_one_minus_theta
184
+ + input_derivatives * (1 - root).pow(2)
185
+ )
186
+ logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
187
+
188
+ return outputs, -logabsdet
189
+ else:
190
+ theta = (inputs - input_cumwidths) / input_bin_widths
191
+ theta_one_minus_theta = theta * (1 - theta)
192
+
193
+ numerator = input_heights * (
194
+ input_delta * theta.pow(2) + input_derivatives * theta_one_minus_theta
195
+ )
196
+ denominator = input_delta + (
197
+ (input_derivatives + input_derivatives_plus_one - 2 * input_delta)
198
+ * theta_one_minus_theta
199
+ )
200
+ outputs = input_cumheights + numerator / denominator
201
+
202
+ derivative_numerator = input_delta.pow(2) * (
203
+ input_derivatives_plus_one * theta.pow(2)
204
+ + 2 * input_delta * theta_one_minus_theta
205
+ + input_derivatives * (1 - theta).pow(2)
206
+ )
207
+ logabsdet = torch.log(derivative_numerator) - 2 * torch.log(denominator)
208
+
209
+ return outputs, logabsdet
rvc/lib/process/__pycache__/model_fusion.cpython-39.pyc ADDED
Binary file (1.34 kB). View file
 
rvc/lib/process/__pycache__/model_information.cpython-39.pyc ADDED
Binary file (609 Bytes). View file
 
rvc/lib/process/model_fusion.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from collections import OrderedDict
3
+
4
+
5
+ def extract(ckpt):
6
+ model = ckpt["model"]
7
+ opt = OrderedDict()
8
+ opt["weight"] = {key: value for key, value in model.items() if "enc_q" not in key}
9
+ return opt
10
+
11
+
12
+ def model_fusion(model_name, pth_path_1, pth_path_2):
13
+ ckpt1 = torch.load(pth_path_1, map_location="cpu")
14
+ ckpt2 = torch.load(pth_path_2, map_location="cpu")
15
+ if "model" in ckpt1:
16
+ ckpt1 = extract(ckpt1)
17
+ else:
18
+ ckpt1 = ckpt1["weight"]
19
+ if "model" in ckpt2:
20
+ ckpt2 = extract(ckpt2)
21
+ else:
22
+ ckpt2 = ckpt2["weight"]
23
+ if sorted(ckpt1.keys()) != sorted(ckpt2.keys()):
24
+ return "Fail to merge the models. The model architectures are not the same."
25
+ opt = OrderedDict(
26
+ weight={
27
+ key: 1 * value.float() + (1 - 1) * ckpt2[key].float()
28
+ for key, value in ckpt1.items()
29
+ }
30
+ )
31
+ opt["info"] = f"Model fusion of {pth_path_1} and {pth_path_2}"
32
+ torch.save(opt, f"logs/{model_name}.pth")
33
+ print(f"Model fusion of {pth_path_1} and {pth_path_2} is done.")
rvc/lib/process/model_information.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ def model_information(path):
4
+ model_data = torch.load(path, map_location="cpu")
5
+
6
+ print(f"Loaded model from {path}")
7
+
8
+ data = model_data
9
+
10
+ epochs = data.get("info", "None")
11
+ sr = data.get("sr", "None")
12
+ f0 = data.get("f0", "None")
13
+ version = data.get("version", "None")
14
+
15
+ return(f"Epochs: {epochs}\nSampling rate: {sr}\nPitch guidance: {f0}\nVersion: {version}")
rvc/lib/rmvpe.py ADDED
@@ -0,0 +1,388 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+ import torch, numpy as np
3
+ import torch.nn.functional as F
4
+ from librosa.filters import mel
5
+
6
+
7
+ class BiGRU(nn.Module):
8
+ def __init__(self, input_features, hidden_features, num_layers):
9
+ super(BiGRU, self).__init__()
10
+ self.gru = nn.GRU(
11
+ input_features,
12
+ hidden_features,
13
+ num_layers=num_layers,
14
+ batch_first=True,
15
+ bidirectional=True,
16
+ )
17
+
18
+ def forward(self, x):
19
+ return self.gru(x)[0]
20
+
21
+
22
+ class ConvBlockRes(nn.Module):
23
+ def __init__(self, in_channels, out_channels, momentum=0.01):
24
+ super(ConvBlockRes, self).__init__()
25
+ self.conv = nn.Sequential(
26
+ nn.Conv2d(
27
+ in_channels=in_channels,
28
+ out_channels=out_channels,
29
+ kernel_size=(3, 3),
30
+ stride=(1, 1),
31
+ padding=(1, 1),
32
+ bias=False,
33
+ ),
34
+ nn.BatchNorm2d(out_channels, momentum=momentum),
35
+ nn.ReLU(),
36
+ nn.Conv2d(
37
+ in_channels=out_channels,
38
+ out_channels=out_channels,
39
+ kernel_size=(3, 3),
40
+ stride=(1, 1),
41
+ padding=(1, 1),
42
+ bias=False,
43
+ ),
44
+ nn.BatchNorm2d(out_channels, momentum=momentum),
45
+ nn.ReLU(),
46
+ )
47
+ if in_channels != out_channels:
48
+ self.shortcut = nn.Conv2d(in_channels, out_channels, (1, 1))
49
+ self.is_shortcut = True
50
+ else:
51
+ self.is_shortcut = False
52
+
53
+ def forward(self, x):
54
+ if self.is_shortcut:
55
+ return self.conv(x) + self.shortcut(x)
56
+ else:
57
+ return self.conv(x) + x
58
+
59
+
60
+ class Encoder(nn.Module):
61
+ def __init__(
62
+ self,
63
+ in_channels,
64
+ in_size,
65
+ n_encoders,
66
+ kernel_size,
67
+ n_blocks,
68
+ out_channels=16,
69
+ momentum=0.01,
70
+ ):
71
+ super(Encoder, self).__init__()
72
+ self.n_encoders = n_encoders
73
+ self.bn = nn.BatchNorm2d(in_channels, momentum=momentum)
74
+ self.layers = nn.ModuleList()
75
+ self.latent_channels = []
76
+ for i in range(self.n_encoders):
77
+ self.layers.append(
78
+ ResEncoderBlock(
79
+ in_channels, out_channels, kernel_size, n_blocks, momentum=momentum
80
+ )
81
+ )
82
+ self.latent_channels.append([out_channels, in_size])
83
+ in_channels = out_channels
84
+ out_channels *= 2
85
+ in_size //= 2
86
+ self.out_size = in_size
87
+ self.out_channel = out_channels
88
+
89
+ def forward(self, x):
90
+ concat_tensors = []
91
+ x = self.bn(x)
92
+ for i in range(self.n_encoders):
93
+ _, x = self.layers[i](x)
94
+ concat_tensors.append(_)
95
+ return x, concat_tensors
96
+
97
+
98
+ class ResEncoderBlock(nn.Module):
99
+ def __init__(
100
+ self, in_channels, out_channels, kernel_size, n_blocks=1, momentum=0.01
101
+ ):
102
+ super(ResEncoderBlock, self).__init__()
103
+ self.n_blocks = n_blocks
104
+ self.conv = nn.ModuleList()
105
+ self.conv.append(ConvBlockRes(in_channels, out_channels, momentum))
106
+ for i in range(n_blocks - 1):
107
+ self.conv.append(ConvBlockRes(out_channels, out_channels, momentum))
108
+ self.kernel_size = kernel_size
109
+ if self.kernel_size is not None:
110
+ self.pool = nn.AvgPool2d(kernel_size=kernel_size)
111
+
112
+ def forward(self, x):
113
+ for i in range(self.n_blocks):
114
+ x = self.conv[i](x)
115
+ if self.kernel_size is not None:
116
+ return x, self.pool(x)
117
+ else:
118
+ return x
119
+
120
+
121
+ class Intermediate(nn.Module): #
122
+ def __init__(self, in_channels, out_channels, n_inters, n_blocks, momentum=0.01):
123
+ super(Intermediate, self).__init__()
124
+ self.n_inters = n_inters
125
+ self.layers = nn.ModuleList()
126
+ self.layers.append(
127
+ ResEncoderBlock(in_channels, out_channels, None, n_blocks, momentum)
128
+ )
129
+ for i in range(self.n_inters - 1):
130
+ self.layers.append(
131
+ ResEncoderBlock(out_channels, out_channels, None, n_blocks, momentum)
132
+ )
133
+
134
+ def forward(self, x):
135
+ for i in range(self.n_inters):
136
+ x = self.layers[i](x)
137
+ return x
138
+
139
+
140
+ class ResDecoderBlock(nn.Module):
141
+ def __init__(self, in_channels, out_channels, stride, n_blocks=1, momentum=0.01):
142
+ super(ResDecoderBlock, self).__init__()
143
+ out_padding = (0, 1) if stride == (1, 2) else (1, 1)
144
+ self.n_blocks = n_blocks
145
+ self.conv1 = nn.Sequential(
146
+ nn.ConvTranspose2d(
147
+ in_channels=in_channels,
148
+ out_channels=out_channels,
149
+ kernel_size=(3, 3),
150
+ stride=stride,
151
+ padding=(1, 1),
152
+ output_padding=out_padding,
153
+ bias=False,
154
+ ),
155
+ nn.BatchNorm2d(out_channels, momentum=momentum),
156
+ nn.ReLU(),
157
+ )
158
+ self.conv2 = nn.ModuleList()
159
+ self.conv2.append(ConvBlockRes(out_channels * 2, out_channels, momentum))
160
+ for i in range(n_blocks - 1):
161
+ self.conv2.append(ConvBlockRes(out_channels, out_channels, momentum))
162
+
163
+ def forward(self, x, concat_tensor):
164
+ x = self.conv1(x)
165
+ x = torch.cat((x, concat_tensor), dim=1)
166
+ for i in range(self.n_blocks):
167
+ x = self.conv2[i](x)
168
+ return x
169
+
170
+
171
+ class Decoder(nn.Module):
172
+ def __init__(self, in_channels, n_decoders, stride, n_blocks, momentum=0.01):
173
+ super(Decoder, self).__init__()
174
+ self.layers = nn.ModuleList()
175
+ self.n_decoders = n_decoders
176
+ for i in range(self.n_decoders):
177
+ out_channels = in_channels // 2
178
+ self.layers.append(
179
+ ResDecoderBlock(in_channels, out_channels, stride, n_blocks, momentum)
180
+ )
181
+ in_channels = out_channels
182
+
183
+ def forward(self, x, concat_tensors):
184
+ for i in range(self.n_decoders):
185
+ x = self.layers[i](x, concat_tensors[-1 - i])
186
+ return x
187
+
188
+
189
+ class DeepUnet(nn.Module):
190
+ def __init__(
191
+ self,
192
+ kernel_size,
193
+ n_blocks,
194
+ en_de_layers=5,
195
+ inter_layers=4,
196
+ in_channels=1,
197
+ en_out_channels=16,
198
+ ):
199
+ super(DeepUnet, self).__init__()
200
+ self.encoder = Encoder(
201
+ in_channels, 128, en_de_layers, kernel_size, n_blocks, en_out_channels
202
+ )
203
+ self.intermediate = Intermediate(
204
+ self.encoder.out_channel // 2,
205
+ self.encoder.out_channel,
206
+ inter_layers,
207
+ n_blocks,
208
+ )
209
+ self.decoder = Decoder(
210
+ self.encoder.out_channel, en_de_layers, kernel_size, n_blocks
211
+ )
212
+
213
+ def forward(self, x):
214
+ x, concat_tensors = self.encoder(x)
215
+ x = self.intermediate(x)
216
+ x = self.decoder(x, concat_tensors)
217
+ return x
218
+
219
+
220
+ class E2E(nn.Module):
221
+ def __init__(
222
+ self,
223
+ n_blocks,
224
+ n_gru,
225
+ kernel_size,
226
+ en_de_layers=5,
227
+ inter_layers=4,
228
+ in_channels=1,
229
+ en_out_channels=16,
230
+ ):
231
+ super(E2E, self).__init__()
232
+ self.unet = DeepUnet(
233
+ kernel_size,
234
+ n_blocks,
235
+ en_de_layers,
236
+ inter_layers,
237
+ in_channels,
238
+ en_out_channels,
239
+ )
240
+ self.cnn = nn.Conv2d(en_out_channels, 3, (3, 3), padding=(1, 1))
241
+ if n_gru:
242
+ self.fc = nn.Sequential(
243
+ BiGRU(3 * 128, 256, n_gru),
244
+ nn.Linear(512, 360),
245
+ nn.Dropout(0.25),
246
+ nn.Sigmoid(),
247
+ )
248
+
249
+ def forward(self, mel):
250
+ mel = mel.transpose(-1, -2).unsqueeze(1)
251
+ x = self.cnn(self.unet(mel)).transpose(1, 2).flatten(-2)
252
+ x = self.fc(x)
253
+ return x
254
+
255
+
256
+ class MelSpectrogram(torch.nn.Module):
257
+ def __init__(
258
+ self,
259
+ is_half,
260
+ n_mel_channels,
261
+ sampling_rate,
262
+ win_length,
263
+ hop_length,
264
+ n_fft=None,
265
+ mel_fmin=0,
266
+ mel_fmax=None,
267
+ clamp=1e-5,
268
+ ):
269
+ super().__init__()
270
+ n_fft = win_length if n_fft is None else n_fft
271
+ self.hann_window = {}
272
+ mel_basis = mel(
273
+ sr=sampling_rate,
274
+ n_fft=n_fft,
275
+ n_mels=n_mel_channels,
276
+ fmin=mel_fmin,
277
+ fmax=mel_fmax,
278
+ htk=True,
279
+ )
280
+ mel_basis = torch.from_numpy(mel_basis).float()
281
+ self.register_buffer("mel_basis", mel_basis)
282
+ self.n_fft = win_length if n_fft is None else n_fft
283
+ self.hop_length = hop_length
284
+ self.win_length = win_length
285
+ self.sampling_rate = sampling_rate
286
+ self.n_mel_channels = n_mel_channels
287
+ self.clamp = clamp
288
+ self.is_half = is_half
289
+
290
+ def forward(self, audio, keyshift=0, speed=1, center=True):
291
+ factor = 2 ** (keyshift / 12)
292
+ n_fft_new = int(np.round(self.n_fft * factor))
293
+ win_length_new = int(np.round(self.win_length * factor))
294
+ hop_length_new = int(np.round(self.hop_length * speed))
295
+ keyshift_key = str(keyshift) + "_" + str(audio.device)
296
+ if keyshift_key not in self.hann_window:
297
+ self.hann_window[keyshift_key] = torch.hann_window(win_length_new).to(
298
+ audio.device
299
+ )
300
+ fft = torch.stft(
301
+ audio,
302
+ n_fft=n_fft_new,
303
+ hop_length=hop_length_new,
304
+ win_length=win_length_new,
305
+ window=self.hann_window[keyshift_key],
306
+ center=center,
307
+ return_complex=True,
308
+ )
309
+ magnitude = torch.sqrt(fft.real.pow(2) + fft.imag.pow(2))
310
+ if keyshift != 0:
311
+ size = self.n_fft // 2 + 1
312
+ resize = magnitude.size(1)
313
+ if resize < size:
314
+ magnitude = F.pad(magnitude, (0, 0, 0, size - resize))
315
+ magnitude = magnitude[:, :size, :] * self.win_length / win_length_new
316
+ mel_output = torch.matmul(self.mel_basis, magnitude)
317
+ if self.is_half == True:
318
+ mel_output = mel_output.half()
319
+ log_mel_spec = torch.log(torch.clamp(mel_output, min=self.clamp))
320
+ return log_mel_spec
321
+
322
+
323
+ class RMVPE:
324
+ def __init__(self, model_path, is_half, device=None):
325
+ self.resample_kernel = {}
326
+ model = E2E(4, 1, (2, 2))
327
+ ckpt = torch.load(model_path, map_location="cpu")
328
+ model.load_state_dict(ckpt)
329
+ model.eval()
330
+ if is_half == True:
331
+ model = model.half()
332
+ self.model = model
333
+ self.resample_kernel = {}
334
+ self.is_half = is_half
335
+ if device is None:
336
+ device = "cuda" if torch.cuda.is_available() else "cpu"
337
+ self.device = device
338
+ self.mel_extractor = MelSpectrogram(
339
+ is_half, 128, 16000, 1024, 160, None, 30, 8000
340
+ ).to(device)
341
+ self.model = self.model.to(device)
342
+ cents_mapping = 20 * np.arange(360) + 1997.3794084376191
343
+ self.cents_mapping = np.pad(cents_mapping, (4, 4)) # 368
344
+
345
+ def mel2hidden(self, mel):
346
+ with torch.no_grad():
347
+ n_frames = mel.shape[-1]
348
+ mel = F.pad(
349
+ mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect"
350
+ )
351
+ hidden = self.model(mel)
352
+ return hidden[:, :n_frames]
353
+
354
+ def decode(self, hidden, thred=0.03):
355
+ cents_pred = self.to_local_average_cents(hidden, thred=thred)
356
+ f0 = 10 * (2 ** (cents_pred / 1200))
357
+ f0[f0 == 10] = 0
358
+ return f0
359
+
360
+ def infer_from_audio(self, audio, thred=0.03):
361
+ audio = torch.from_numpy(audio).float().to(self.device).unsqueeze(0)
362
+ mel = self.mel_extractor(audio, center=True)
363
+ hidden = self.mel2hidden(mel)
364
+ hidden = hidden.squeeze(0).cpu().numpy()
365
+ if self.is_half == True:
366
+ hidden = hidden.astype("float32")
367
+ f0 = self.decode(hidden, thred=thred)
368
+ return f0
369
+
370
+ def to_local_average_cents(self, salience, thred=0.05):
371
+ center = np.argmax(salience, axis=1)
372
+ salience = np.pad(salience, ((0, 0), (4, 4)))
373
+ center += 4
374
+ todo_salience = []
375
+ todo_cents_mapping = []
376
+ starts = center - 4
377
+ ends = center + 5
378
+ for idx in range(salience.shape[0]):
379
+ todo_salience.append(salience[:, starts[idx] : ends[idx]][idx])
380
+ todo_cents_mapping.append(self.cents_mapping[starts[idx] : ends[idx]])
381
+ todo_salience = np.array(todo_salience)
382
+ todo_cents_mapping = np.array(todo_cents_mapping)
383
+ product_sum = np.sum(todo_salience * todo_cents_mapping, 1)
384
+ weight_sum = np.sum(todo_salience, 1)
385
+ devided = product_sum / weight_sum
386
+ maxx = np.max(salience, axis=1)
387
+ devided[maxx <= thred] = 0
388
+ return devided
rvc/lib/tools/__pycache__/pretrained_selector.cpython-39.pyc ADDED
Binary file (1.42 kB). View file
 
rvc/lib/tools/__pycache__/split_audio.cpython-39.pyc ADDED
Binary file (2.48 kB). View file
 
rvc/lib/tools/__pycache__/validators.cpython-39.pyc ADDED
Binary file (1.83 kB). View file
 
rvc/lib/tools/gdown.py ADDED
@@ -0,0 +1,402 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import print_function
2
+
3
+ import json
4
+ import os
5
+ import os.path as osp
6
+ import re
7
+ import warnings
8
+ from six.moves import urllib_parse
9
+ import shutil
10
+ import sys
11
+ import tempfile
12
+ import textwrap
13
+ import time
14
+
15
+ import requests
16
+ import six
17
+ import tqdm
18
+
19
+ def indent(text, prefix):
20
+ def prefixed_lines():
21
+ for line in text.splitlines(True):
22
+ yield (prefix + line if line.strip() else line)
23
+
24
+ return "".join(prefixed_lines())
25
+
26
+ class FileURLRetrievalError(Exception):
27
+ pass
28
+
29
+
30
+ class FolderContentsMaximumLimitError(Exception):
31
+ pass
32
+
33
+ def parse_url(url, warning=True):
34
+ """Parse URLs especially for Google Drive links.
35
+
36
+ file_id: ID of file on Google Drive.
37
+ is_download_link: Flag if it is download link of Google Drive.
38
+ """
39
+ parsed = urllib_parse.urlparse(url)
40
+ query = urllib_parse.parse_qs(parsed.query)
41
+ is_gdrive = parsed.hostname in ["drive.google.com", "docs.google.com"]
42
+ is_download_link = parsed.path.endswith("/uc")
43
+
44
+ if not is_gdrive:
45
+ return is_gdrive, is_download_link
46
+
47
+ file_id = None
48
+ if "id" in query:
49
+ file_ids = query["id"]
50
+ if len(file_ids) == 1:
51
+ file_id = file_ids[0]
52
+ else:
53
+ patterns = [
54
+ r"^/file/d/(.*?)/(edit|view)$",
55
+ r"^/file/u/[0-9]+/d/(.*?)/(edit|view)$",
56
+ r"^/document/d/(.*?)/(edit|htmlview|view)$",
57
+ r"^/document/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$",
58
+ r"^/presentation/d/(.*?)/(edit|htmlview|view)$",
59
+ r"^/presentation/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$",
60
+ r"^/spreadsheets/d/(.*?)/(edit|htmlview|view)$",
61
+ r"^/spreadsheets/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$",
62
+ ]
63
+ for pattern in patterns:
64
+ match = re.match(pattern, parsed.path)
65
+ if match:
66
+ file_id = match.groups()[0]
67
+ break
68
+
69
+ if warning and not is_download_link:
70
+ warnings.warn(
71
+ "You specified a Google Drive link that is not the correct link "
72
+ "to download a file. You might want to try `--fuzzy` option "
73
+ "or the following url: {url}".format(
74
+ url="https://drive.google.com/uc?id={}".format(file_id)
75
+ )
76
+ )
77
+
78
+ return file_id, is_download_link
79
+
80
+
81
+ CHUNK_SIZE = 512 * 1024 # 512KB
82
+ home = osp.expanduser("~")
83
+
84
+
85
+ def get_url_from_gdrive_confirmation(contents):
86
+ url = ""
87
+ m = re.search(r'href="(\/uc\?export=download[^"]+)', contents)
88
+ if m:
89
+ url = "https://docs.google.com" + m.groups()[0]
90
+ url = url.replace("&amp;", "&")
91
+ return url
92
+
93
+ m = re.search(r'href="/open\?id=([^"]+)"', contents)
94
+ if m:
95
+ url = m.groups()[0]
96
+ uuid = re.search(r'<input\s+type="hidden"\s+name="uuid"\s+value="([^"]+)"', contents)
97
+ uuid = uuid.groups()[0]
98
+ url = "https://drive.usercontent.google.com/download?id=" + url + "&confirm=t&uuid=" + uuid
99
+ return url
100
+
101
+
102
+ m = re.search(r'"downloadUrl":"([^"]+)', contents)
103
+ if m:
104
+ url = m.groups()[0]
105
+ url = url.replace("\\u003d", "=")
106
+ url = url.replace("\\u0026", "&")
107
+ return url
108
+
109
+ m = re.search(r'<p class="uc-error-subcaption">(.*)</p>', contents)
110
+ if m:
111
+ error = m.groups()[0]
112
+ raise FileURLRetrievalError(error)
113
+
114
+ raise FileURLRetrievalError(
115
+ "Cannot retrieve the public link of the file. "
116
+ "You may need to change the permission to "
117
+ "'Anyone with the link', or have had many accesses."
118
+ )
119
+ def _get_session(proxy, use_cookies, return_cookies_file=False):
120
+ sess = requests.session()
121
+
122
+ sess.headers.update(
123
+ {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"}
124
+ )
125
+
126
+ if proxy is not None:
127
+ sess.proxies = {"http": proxy, "https": proxy}
128
+ print("Using proxy:", proxy, file=sys.stderr)
129
+
130
+ # Load cookies if exists
131
+ cookies_file = osp.join(home, ".cache/gdown/cookies.json")
132
+ if osp.exists(cookies_file) and use_cookies:
133
+ with open(cookies_file) as f:
134
+ cookies = json.load(f)
135
+ for k, v in cookies:
136
+ sess.cookies[k] = v
137
+
138
+ if return_cookies_file:
139
+ return sess, cookies_file
140
+ else:
141
+ return sess
142
+
143
+
144
+ def download(
145
+ url=None,
146
+ output=None,
147
+ quiet=False,
148
+ proxy=None,
149
+ speed=None,
150
+ use_cookies=True,
151
+ verify=True,
152
+ id=None,
153
+ fuzzy=True,
154
+ resume=False,
155
+ format=None,
156
+ ):
157
+ """Download file from URL.
158
+
159
+ Parameters
160
+ ----------
161
+ url: str
162
+ URL. Google Drive URL is also supported.
163
+ output: str
164
+ Output filename. Default is basename of URL.
165
+ quiet: bool
166
+ Suppress terminal output. Default is False.
167
+ proxy: str
168
+ Proxy.
169
+ speed: float
170
+ Download byte size per second (e.g., 256KB/s = 256 * 1024).
171
+ use_cookies: bool
172
+ Flag to use cookies. Default is True.
173
+ verify: bool or string
174
+ Either a bool, in which case it controls whether the server's TLS
175
+ certificate is verified, or a string, in which case it must be a path
176
+ to a CA bundle to use. Default is True.
177
+ id: str
178
+ Google Drive's file ID.
179
+ fuzzy: bool
180
+ Fuzzy extraction of Google Drive's file Id. Default is False.
181
+ resume: bool
182
+ Resume the download from existing tmp file if possible.
183
+ Default is False.
184
+ format: str, optional
185
+ Format of Google Docs, Spreadsheets and Slides. Default is:
186
+ - Google Docs: 'docx'
187
+ - Google Spreadsheet: 'xlsx'
188
+ - Google Slides: 'pptx'
189
+
190
+ Returns
191
+ -------
192
+ output: str
193
+ Output filename.
194
+ """
195
+ if not (id is None) ^ (url is None):
196
+ raise ValueError("Either url or id has to be specified")
197
+ if id is not None:
198
+ url = "https://drive.google.com/uc?id={id}".format(id=id)
199
+
200
+ url_origin = url
201
+
202
+ sess, cookies_file = _get_session(
203
+ proxy=proxy, use_cookies=use_cookies, return_cookies_file=True
204
+ )
205
+
206
+ gdrive_file_id, is_gdrive_download_link = parse_url(url, warning=not fuzzy)
207
+
208
+ if fuzzy and gdrive_file_id:
209
+ # overwrite the url with fuzzy match of a file id
210
+ url = "https://drive.google.com/uc?id={id}".format(id=gdrive_file_id)
211
+ url_origin = url
212
+ is_gdrive_download_link = True
213
+
214
+
215
+
216
+ while True:
217
+ res = sess.get(url, stream=True, verify=verify)
218
+
219
+ if url == url_origin and res.status_code == 500:
220
+ # The file could be Google Docs or Spreadsheets.
221
+ url = "https://drive.google.com/open?id={id}".format(
222
+ id=gdrive_file_id
223
+ )
224
+ continue
225
+
226
+ if res.headers["Content-Type"].startswith("text/html"):
227
+ m = re.search("<title>(.+)</title>", res.text)
228
+ if m and m.groups()[0].endswith(" - Google Docs"):
229
+ url = (
230
+ "https://docs.google.com/document/d/{id}/export"
231
+ "?format={format}".format(
232
+ id=gdrive_file_id,
233
+ format="docx" if format is None else format,
234
+ )
235
+ )
236
+ continue
237
+ elif m and m.groups()[0].endswith(" - Google Sheets"):
238
+ url = (
239
+ "https://docs.google.com/spreadsheets/d/{id}/export"
240
+ "?format={format}".format(
241
+ id=gdrive_file_id,
242
+ format="xlsx" if format is None else format,
243
+ )
244
+ )
245
+ continue
246
+ elif m and m.groups()[0].endswith(" - Google Slides"):
247
+ url = (
248
+ "https://docs.google.com/presentation/d/{id}/export"
249
+ "?format={format}".format(
250
+ id=gdrive_file_id,
251
+ format="pptx" if format is None else format,
252
+ )
253
+ )
254
+ continue
255
+ elif (
256
+ "Content-Disposition" in res.headers
257
+ and res.headers["Content-Disposition"].endswith("pptx")
258
+ and format not in {None, "pptx"}
259
+ ):
260
+ url = (
261
+ "https://docs.google.com/presentation/d/{id}/export"
262
+ "?format={format}".format(
263
+ id=gdrive_file_id,
264
+ format="pptx" if format is None else format,
265
+ )
266
+ )
267
+ continue
268
+
269
+ if use_cookies:
270
+ if not osp.exists(osp.dirname(cookies_file)):
271
+ os.makedirs(osp.dirname(cookies_file))
272
+ # Save cookies
273
+ with open(cookies_file, "w") as f:
274
+ cookies = [
275
+ (k, v)
276
+ for k, v in sess.cookies.items()
277
+ if not k.startswith("download_warning_")
278
+ ]
279
+ json.dump(cookies, f, indent=2)
280
+
281
+ if "Content-Disposition" in res.headers:
282
+ # This is the file
283
+ break
284
+ if not (gdrive_file_id and is_gdrive_download_link):
285
+ break
286
+
287
+ # Need to redirect with confirmation
288
+ try:
289
+ url = get_url_from_gdrive_confirmation(res.text)
290
+ except FileURLRetrievalError as e:
291
+ message = (
292
+ "Failed to retrieve file url:\n\n{}\n\n"
293
+ "You may still be able to access the file from the browser:"
294
+ "\n\n\t{}\n\n"
295
+ "but Gdown can't. Please check connections and permissions."
296
+ ).format(
297
+ indent("\n".join(textwrap.wrap(str(e))), prefix="\t"),
298
+ url_origin,
299
+ )
300
+ raise FileURLRetrievalError(message)
301
+
302
+ if gdrive_file_id and is_gdrive_download_link:
303
+ content_disposition = six.moves.urllib_parse.unquote(
304
+ res.headers["Content-Disposition"]
305
+ )
306
+
307
+ m = re.search(r"filename\*=UTF-8''(.*)", content_disposition)
308
+ if not m:
309
+ m = re.search(r'filename=["\']?(.*?)["\']?$', content_disposition)
310
+ filename_from_url = m.groups()[0]
311
+ filename_from_url = filename_from_url.replace(osp.sep, "_")
312
+ else:
313
+ filename_from_url = osp.basename(url)
314
+
315
+ if output is None:
316
+ output = filename_from_url
317
+
318
+ output_is_path = isinstance(output, six.string_types)
319
+ if output_is_path and output.endswith(osp.sep):
320
+ if not osp.exists(output):
321
+ os.makedirs(output)
322
+ output = osp.join(output, filename_from_url)
323
+
324
+ if output_is_path:
325
+ existing_tmp_files = []
326
+ for file in os.listdir(osp.dirname(output) or "."):
327
+ if file.startswith(osp.basename(output)):
328
+ existing_tmp_files.append(osp.join(osp.dirname(output), file))
329
+ if resume and existing_tmp_files:
330
+ if len(existing_tmp_files) != 1:
331
+ print(
332
+ "There are multiple temporary files to resume:",
333
+ file=sys.stderr,
334
+ )
335
+ print("\n")
336
+ for file in existing_tmp_files:
337
+ print("\t", file, file=sys.stderr)
338
+ print("\n")
339
+ print(
340
+ "Please remove them except one to resume downloading.",
341
+ file=sys.stderr,
342
+ )
343
+ return
344
+ tmp_file = existing_tmp_files[0]
345
+ else:
346
+ resume = False
347
+ # mkstemp is preferred, but does not work on Windows
348
+ # https://github.com/wkentaro/gdown/issues/153
349
+ tmp_file = tempfile.mktemp(
350
+ suffix=tempfile.template,
351
+ prefix=osp.basename(output),
352
+ dir=osp.dirname(output),
353
+ )
354
+ f = open(tmp_file, "ab")
355
+ else:
356
+ tmp_file = None
357
+ f = output
358
+
359
+ if tmp_file is not None and f.tell() != 0:
360
+ headers = {"Range": "bytes={}-".format(f.tell())}
361
+ res = sess.get(url, headers=headers, stream=True, verify=verify)
362
+
363
+ if not quiet:
364
+ # print("Downloading...", file=sys.stderr)
365
+ if resume:
366
+ print("Resume:", tmp_file, file=sys.stderr)
367
+ # if url_origin != url:
368
+ # print("From (original):", url_origin, file=sys.stderr)
369
+ # print("From (redirected):", url, file=sys.stderr)
370
+ # else:
371
+ # print("From:", url, file=sys.stderr)
372
+ print(
373
+ "To:",
374
+ osp.abspath(output) if output_is_path else output,
375
+ file=sys.stderr,
376
+ )
377
+
378
+ try:
379
+ total = res.headers.get("Content-Length")
380
+ if total is not None:
381
+ total = int(total)
382
+ if not quiet:
383
+ pbar = tqdm.tqdm(total=total, unit="B", unit_scale=True)
384
+ t_start = time.time()
385
+ for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
386
+ f.write(chunk)
387
+ if not quiet:
388
+ pbar.update(len(chunk))
389
+ if speed is not None:
390
+ elapsed_time_expected = 1.0 * pbar.n / speed
391
+ elapsed_time = time.time() - t_start
392
+ if elapsed_time < elapsed_time_expected:
393
+ time.sleep(elapsed_time_expected - elapsed_time)
394
+ if not quiet:
395
+ pbar.close()
396
+ if tmp_file:
397
+ f.close()
398
+ shutil.move(tmp_file, output)
399
+ finally:
400
+ sess.close()
401
+
402
+ return output
rvc/lib/tools/launch_tensorboard.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from tensorboard import program
3
+
4
+ log_path = "logs"
5
+
6
+ if __name__ == "__main__":
7
+ tb = program.TensorBoard()
8
+ tb.configure(argv=[None, "--logdir", log_path])
9
+ url = tb.launch()
10
+ print(
11
+ f"Access the tensorboard using the following link:\n{url}?pinnedCards=%5B%7B%22plugin%22%3A%22scalars%22%2C%22tag%22%3A%22loss%2Fg%2Ftotal%22%7D%2C%7B%22plugin%22%3A%22scalars%22%2C%22tag%22%3A%22loss%2Fd%2Ftotal%22%7D%2C%7B%22plugin%22%3A%22scalars%22%2C%22tag%22%3A%22loss%2Fg%2Fkl%22%7D%2C%7B%22plugin%22%3A%22scalars%22%2C%22tag%22%3A%22loss%2Fg%2Fmel%22%7D%5D"
12
+ )
13
+
14
+ while True:
15
+ time.sleep(600)
rvc/lib/tools/model_download.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import wget
4
+ import zipfile
5
+ from bs4 import BeautifulSoup
6
+ import requests
7
+ from urllib.parse import unquote
8
+ import re
9
+
10
+ def find_folder_parent(search_dir, folder_name):
11
+ for dirpath, dirnames, _ in os.walk(search_dir):
12
+ if folder_name in dirnames:
13
+ return os.path.abspath(dirpath)
14
+ return None
15
+
16
+ now_dir = os.getcwd()
17
+ sys.path.append(now_dir)
18
+
19
+ import rvc.lib.tools.gdown as gdown
20
+
21
+ file_path = find_folder_parent(now_dir, "logs")
22
+
23
+ zips_path = os.getcwd() + "/logs/zips"
24
+
25
+
26
+ def search_pth_index(folder):
27
+ pth_paths = [
28
+ os.path.join(folder, file)
29
+ for file in os.listdir(folder)
30
+ if os.path.isfile(os.path.join(folder, file)) and file.endswith(".pth")
31
+ ]
32
+ index_paths = [
33
+ os.path.join(folder, file)
34
+ for file in os.listdir(folder)
35
+ if os.path.isfile(os.path.join(folder, file)) and file.endswith(".index")
36
+ ]
37
+
38
+ return pth_paths, index_paths
39
+
40
+
41
+ def get_mediafire_download_link(url):
42
+ response = requests.get(url)
43
+ response.raise_for_status()
44
+ soup = BeautifulSoup(response.text, "html.parser")
45
+ download_button = soup.find(
46
+ "a", {"class": "input popsok", "aria-label": "Download file"}
47
+ )
48
+ if download_button:
49
+ download_link = download_button.get("href")
50
+ return download_link
51
+ else:
52
+ return None
53
+
54
+
55
+ def download_from_url(url):
56
+ os.makedirs(zips_path, exist_ok=True)
57
+ if url != "":
58
+ if "drive.google.com" in url:
59
+ if "file/d/" in url:
60
+ file_id = url.split("file/d/")[1].split("/")[0]
61
+ elif "id=" in url:
62
+ file_id = url.split("id=")[1].split("&")[0]
63
+ else:
64
+ return None
65
+
66
+ if file_id:
67
+ os.chdir(zips_path)
68
+ try:
69
+ gdown.download(
70
+ f"https://drive.google.com/uc?id={file_id}",
71
+ quiet=False,
72
+ fuzzy=True,
73
+ )
74
+ except Exception as error:
75
+ error_message = str(error)
76
+ if (
77
+ "Too many users have viewed or downloaded this file recently"
78
+ in error_message
79
+ ):
80
+ os.chdir(now_dir)
81
+ return "too much use"
82
+ elif (
83
+ "Cannot retrieve the public link of the file." in error_message
84
+ ):
85
+ os.chdir(now_dir)
86
+ return "private link"
87
+ else:
88
+ print(error_message)
89
+ os.chdir(now_dir)
90
+ return None
91
+
92
+ elif "/blob/" in url or "/resolve/" in url:
93
+ os.chdir(zips_path)
94
+ if "/blob/" in url:
95
+ url = url.replace("/blob/", "/resolve/")
96
+
97
+ response = requests.get(url, stream=True)
98
+ if response.status_code == 200:
99
+ file_name = url.split("/")[-1]
100
+ file_name = unquote(file_name)
101
+
102
+ file_name = re.sub(r"[^a-zA-Z0-9_.-]", "_", file_name)
103
+
104
+ total_size_in_bytes = int(response.headers.get("content-length", 0))
105
+ block_size = 1024
106
+ progress_bar_length = 50
107
+ progress = 0
108
+
109
+ with open(os.path.join(zips_path, file_name), "wb") as file:
110
+ for data in response.iter_content(block_size):
111
+ file.write(data)
112
+ progress += len(data)
113
+ progress_percent = int((progress / total_size_in_bytes) * 100)
114
+ num_dots = int(
115
+ (progress / total_size_in_bytes) * progress_bar_length
116
+ )
117
+ progress_bar = (
118
+ "["
119
+ + "." * num_dots
120
+ + " " * (progress_bar_length - num_dots)
121
+ + "]"
122
+ )
123
+ print(
124
+ f"{progress_percent}% {progress_bar} {progress}/{total_size_in_bytes} ",
125
+ end="\r",
126
+ )
127
+ if progress_percent == 100:
128
+ print("\n")
129
+
130
+ else:
131
+ os.chdir(now_dir)
132
+ return None
133
+ elif "/tree/main" in url:
134
+ os.chdir(zips_path)
135
+ response = requests.get(url)
136
+ soup = BeautifulSoup(response.content, "html.parser")
137
+ temp_url = ""
138
+ for link in soup.find_all("a", href=True):
139
+ if link["href"].endswith(".zip"):
140
+ temp_url = link["href"]
141
+ break
142
+ if temp_url:
143
+ url = temp_url
144
+ url = url.replace("blob", "resolve")
145
+ if "huggingface.co" not in url:
146
+ url = "https://huggingface.co" + url
147
+
148
+ wget.download(url)
149
+ else:
150
+ os.chdir(now_dir)
151
+ return None
152
+ else:
153
+ try:
154
+ os.chdir(zips_path)
155
+ wget.download(url)
156
+ except Exception as error:
157
+ os.chdir(now_dir)
158
+ print(error)
159
+ return None
160
+
161
+ for currentPath, _, zipFiles in os.walk(zips_path):
162
+ for Files in zipFiles:
163
+ filePart = Files.split(".")
164
+ extensionFile = filePart[len(filePart) - 1]
165
+ filePart.pop()
166
+ nameFile = "_".join(filePart)
167
+ realPath = os.path.join(currentPath, Files)
168
+ os.rename(realPath, nameFile + "." + extensionFile)
169
+
170
+ os.chdir(now_dir)
171
+ return "downloaded"
172
+
173
+ os.chdir(now_dir)
174
+ return None
175
+
176
+
177
+ def extract_and_show_progress(zipfile_path, unzips_path):
178
+ try:
179
+ with zipfile.ZipFile(zipfile_path, "r") as zip_ref:
180
+ for file_info in zip_ref.infolist():
181
+ zip_ref.extract(file_info, unzips_path)
182
+ os.remove(zipfile_path)
183
+ return True
184
+ except Exception as error:
185
+ print(error)
186
+ return False
187
+
188
+
189
+ def unzip_file(zip_path, zip_file_name):
190
+ zip_file_path = os.path.join(zip_path, zip_file_name + ".zip")
191
+ extract_path = os.path.join(file_path, zip_file_name)
192
+ with zipfile.ZipFile(zip_file_path, "r") as zip_ref:
193
+ zip_ref.extractall(extract_path)
194
+ os.remove(zip_file_path)
195
+
196
+
197
+ url = sys.argv[1]
198
+ verify = download_from_url(url)
199
+
200
+ if verify == "downloaded":
201
+ extract_folder_path = ""
202
+ for filename in os.listdir(zips_path):
203
+ if filename.endswith(".zip"):
204
+ zipfile_path = os.path.join(zips_path, filename)
205
+ print("Proceeding with the extraction...")
206
+
207
+ model_name = os.path.basename(zipfile_path)
208
+ extract_folder_path = os.path.join(
209
+ "logs",
210
+ os.path.normpath(str(model_name).replace(".zip", "")),
211
+ )
212
+
213
+ success = extract_and_show_progress(zipfile_path, extract_folder_path)
214
+ if success:
215
+ print(f"Model {model_name} downloaded!")
216
+ else:
217
+ print(f"Error downloading {model_name}")
218
+ sys.exit()
219
+ if extract_folder_path == "":
220
+ print("No zip founded...")
221
+ sys.exit()
222
+ result = search_pth_index(extract_folder_path)
223
+ else:
224
+ message = "Error"
225
+ sys.exit()
rvc/lib/tools/prerequisites_download.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import wget
3
+ import sys
4
+
5
+ url_base = "https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main"
6
+ models_download = [
7
+ (
8
+ "pretrained/",
9
+ [
10
+ "D32k.pth",
11
+ "D40k.pth",
12
+ "D48k.pth",
13
+ "G32k.pth",
14
+ "G40k.pth",
15
+ "G48k.pth",
16
+ "f0D32k.pth",
17
+ "f0D40k.pth",
18
+ "f0D48k.pth",
19
+ "f0G32k.pth",
20
+ "f0G40k.pth",
21
+ "f0G48k.pth",
22
+ ],
23
+ ),
24
+ (
25
+ "pretrained_v2/",
26
+ [
27
+ "D32k.pth",
28
+ "D40k.pth",
29
+ "D48k.pth",
30
+ "G32k.pth",
31
+ "G40k.pth",
32
+ "G48k.pth",
33
+ "f0D32k.pth",
34
+ "f0D40k.pth",
35
+ "f0D48k.pth",
36
+ "f0G32k.pth",
37
+ "f0G40k.pth",
38
+ "f0G48k.pth",
39
+ ],
40
+ ),
41
+ ]
42
+
43
+ models_file = [
44
+ "hubert_base.pt",
45
+ "rmvpe.pt",
46
+ # "rmvpe.onnx",
47
+ ]
48
+
49
+ executables_file = [
50
+ "ffmpeg.exe",
51
+ "ffprobe.exe",
52
+ ]
53
+
54
+ folder_mapping = {
55
+ "pretrained/": "rvc/pretraineds/pretrained_v1/",
56
+ "pretrained_v2/": "rvc/pretraineds/pretrained_v2/",
57
+ }
58
+
59
+ for file_name in models_file:
60
+ destination_path = os.path.join(file_name)
61
+ url = f"{url_base}/{file_name}"
62
+ if not os.path.exists(destination_path):
63
+ os.makedirs(os.path.dirname(destination_path) or ".", exist_ok=True)
64
+ print(f"\nDownloading {url} to {destination_path}...")
65
+ wget.download(url, out=destination_path)
66
+
67
+ for file_name in executables_file:
68
+ if sys.platform == "win32":
69
+ destination_path = os.path.join(file_name)
70
+ url = f"{url_base}/{file_name}"
71
+ if not os.path.exists(destination_path):
72
+ os.makedirs(os.path.dirname(destination_path) or ".", exist_ok=True)
73
+ print(f"\nDownloading {url} to {destination_path}...")
74
+ wget.download(url, out=destination_path)
75
+
76
+ for remote_folder, file_list in models_download:
77
+ local_folder = folder_mapping.get(remote_folder, "")
78
+ for file in file_list:
79
+ destination_path = os.path.join(local_folder, file)
80
+ url = f"{url_base}/{remote_folder}{file}"
81
+ if not os.path.exists(destination_path):
82
+ os.makedirs(os.path.dirname(destination_path) or ".", exist_ok=True)
83
+ print(f"\nDownloading {url} to {destination_path}...")
84
+ wget.download(url, out=destination_path)
rvc/lib/tools/pretrained_selector.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def pretrained_selector(pitch_guidance):
2
+ if pitch_guidance:
3
+ return {
4
+ "v1": {
5
+ "32000": (
6
+ "rvc/pretraineds/pretrained_v1/f0G32k.pth",
7
+ "rvc/pretraineds/pretrained_v1/f0D32k.pth",
8
+ ),
9
+ "40000": (
10
+ "rvc/pretraineds/pretrained_v1/f0G40k.pth",
11
+ "rvc/pretraineds/pretrained_v1/f0D40k.pth",
12
+ ),
13
+ "48000": (
14
+ "rvc/pretraineds/pretrained_v1/f0G48k.pth",
15
+ "rvc/pretraineds/pretrained_v1/f0D48k.pth",
16
+ ),
17
+ },
18
+ "v2": {
19
+ "32000": (
20
+ "rvc/pretraineds/pretrained_v2/f0G32k.pth",
21
+ "rvc/pretraineds/pretrained_v2/f0D32k.pth",
22
+ ),
23
+ "40000": (
24
+ "rvc/pretraineds/pretrained_v2/f0G40k.pth",
25
+ "rvc/pretraineds/pretrained_v2/f0D40k.pth",
26
+ ),
27
+ "48000": (
28
+ "rvc/pretraineds/pretrained_v2/f0G48k.pth",
29
+ "rvc/pretraineds/pretrained_v2/f0D48k.pth",
30
+ ),
31
+ },
32
+ }
33
+ else:
34
+ return {
35
+ "v1": {
36
+ "32000": (
37
+ "rvc/pretraineds/pretrained_v1/G32k.pth",
38
+ "rvc/pretraineds/pretrained_v1/D32k.pth",
39
+ ),
40
+ "40000": (
41
+ "rvc/pretraineds/pretrained_v1/G40k.pth",
42
+ "rvc/pretraineds/pretrained_v1/D40k.pth",
43
+ ),
44
+ "48000": (
45
+ "rvc/pretraineds/pretrained_v1/G48k.pth",
46
+ "rvc/pretraineds/pretrained_v1/D48k.pth",
47
+ ),
48
+ },
49
+ "v2": {
50
+ "32000": (
51
+ "rvc/pretraineds/pretrained_v2/G32k.pth",
52
+ "rvc/pretraineds/pretrained_v2/D32k.pth",
53
+ ),
54
+ "40000": (
55
+ "rvc/pretraineds/pretrained_v2/G40k.pth",
56
+ "rvc/pretraineds/pretrained_v2/D40k.pth",
57
+ ),
58
+ "48000": (
59
+ "rvc/pretraineds/pretrained_v2/G48k.pth",
60
+ "rvc/pretraineds/pretrained_v2/D48k.pth",
61
+ ),
62
+ },
63
+ }
rvc/lib/tools/split_audio.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydub.silence import detect_nonsilent
2
+ from pydub import AudioSegment
3
+ import numpy as np
4
+ import re
5
+ import os
6
+
7
+ from rvc.lib.utils import format_title
8
+
9
+
10
+ def process_audio(file_path):
11
+ try:
12
+ # load audio file
13
+ song = AudioSegment.from_file(file_path)
14
+
15
+ # set silence threshold and duration
16
+ silence_thresh = -70 # dB
17
+ min_silence_len = 750 # ms, adjust as needed
18
+
19
+ # detect nonsilent parts
20
+ nonsilent_parts = detect_nonsilent(song, min_silence_len=min_silence_len, silence_thresh=silence_thresh)
21
+
22
+ # Create a new directory to store chunks
23
+ file_dir = os.path.dirname(file_path)
24
+ file_name = os.path.basename(file_path).split('.')[0]
25
+ file_name = format_title(file_name)
26
+ new_dir_path = os.path.join(file_dir, file_name)
27
+ os.makedirs(new_dir_path, exist_ok=True)
28
+
29
+ # Check if timestamps file exists, if so delete it
30
+ timestamps_file = os.path.join(file_dir, f"{file_name}_timestamps.txt")
31
+ if os.path.isfile(timestamps_file):
32
+ os.remove(timestamps_file)
33
+
34
+ # export chunks and save start times
35
+ segment_count = 0
36
+ for i, (start_i, end_i) in enumerate(nonsilent_parts):
37
+ chunk = song[start_i:end_i]
38
+ chunk_file_path = os.path.join(new_dir_path, f"chunk{i}.wav")
39
+ chunk.export(chunk_file_path, format="wav")
40
+
41
+ print(f"Segment {i} created!")
42
+ segment_count += 1
43
+
44
+ # write start times to file
45
+ with open(timestamps_file, "a", encoding="utf-8") as f:
46
+ f.write(f"{chunk_file_path} starts at {start_i} ms\n")
47
+
48
+ print(f"Total segments created: {segment_count}")
49
+ print(f"Split all chunks for {file_path} successfully!")
50
+
51
+ return "Finish", new_dir_path
52
+
53
+ except Exception as e:
54
+ print(f"An error occurred: {e}")
55
+ return "Error", None
56
+
57
+
58
+ def merge_audio(timestamps_file):
59
+ try:
60
+ # Extract prefix from the timestamps filename
61
+ prefix = os.path.basename(timestamps_file).replace('_timestamps.txt', '')
62
+ timestamps_dir = os.path.dirname(timestamps_file)
63
+
64
+ # Open the timestamps file
65
+ with open(timestamps_file, "r", encoding="utf-8") as f:
66
+ lines = f.readlines()
67
+
68
+ # Initialize empty list to hold audio segments
69
+ audio_segments = []
70
+ last_end_time = 0
71
+
72
+ print(f"Processing file: {timestamps_file}")
73
+
74
+ for line in lines:
75
+ # Extract filename and start time from line
76
+ match = re.search(r"(chunk\d+.wav) starts at (\d+) ms", line)
77
+ if match:
78
+ filename, start_time = match.groups()
79
+ start_time = int(start_time)
80
+
81
+ # Construct the complete path to the chunk file
82
+ chunk_file = os.path.join(timestamps_dir, prefix, filename)
83
+
84
+ # Add silence from last_end_time to start_time
85
+ silence_duration = max(start_time - last_end_time, 0)
86
+ silence = AudioSegment.silent(duration=silence_duration)
87
+ audio_segments.append(silence)
88
+
89
+ # Load audio file and append to list
90
+ audio = AudioSegment.from_wav(chunk_file)
91
+ audio_segments.append(audio)
92
+
93
+ # Update last_end_time
94
+ last_end_time = start_time + len(audio)
95
+
96
+ print(f"Processed chunk: {chunk_file}")
97
+
98
+ # Concatenate all audio_segments and export
99
+ merged_audio = sum(audio_segments)
100
+ merged_audio_np = np.array(merged_audio.get_array_of_samples())
101
+ #print(f"Exported merged file: {merged_filename}\n")
102
+ return merged_audio.frame_rate, merged_audio_np
103
+
104
+ except Exception as e:
105
+ print(f"An error occurred: {e}")
rvc/lib/tools/tts.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import asyncio
3
+ import edge_tts
4
+
5
+
6
+ async def main():
7
+ text = sys.argv[1]
8
+ voice = sys.argv[2]
9
+ output_file = sys.argv[3]
10
+
11
+ await edge_tts.Communicate(text, voice).save(output_file)
12
+ print(f"TTS with {voice} completed. Output TTS file: '{output_file}'")
13
+
14
+
15
+ if __name__ == "__main__":
16
+ asyncio.run(main())
rvc/lib/tools/tts_voices.json ADDED
The diff for this file is too large to render. See raw diff
 
rvc/lib/tools/validators.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+ import json
4
+
5
+
6
+ def validate_sampling_rate(value):
7
+ valid_sampling = [
8
+ "32000",
9
+ "40000",
10
+ "48000",
11
+ ]
12
+ if value in valid_sampling:
13
+ return value
14
+ else:
15
+ raise argparse.ArgumentTypeError(
16
+ f"Invalid sampling_rate. Please choose from {valid_sampling} not {value}"
17
+ )
18
+
19
+
20
+ def validate_f0up_key(value):
21
+ f0up_key = int(value)
22
+ if -24 <= f0up_key <= 24:
23
+ return f0up_key
24
+ else:
25
+ raise argparse.ArgumentTypeError(f"f0up_key must be in the range of -24 to +24")
26
+
27
+ def validate_true_false(value):
28
+ valid_tf = [
29
+ "True",
30
+ "False",
31
+ ]
32
+ if value in valid_tf:
33
+ return value
34
+ else:
35
+ raise argparse.ArgumentTypeError(
36
+ f"Invalid true_false. Please choose from {valid_tf} not {value}"
37
+ )
38
+
39
+ def validate_f0method(value):
40
+ valid_f0methods = [
41
+ "pm",
42
+ "dio",
43
+ "crepe",
44
+ "crepe-tiny",
45
+ "harvest",
46
+ "rmvpe",
47
+ ]
48
+ if value in valid_f0methods:
49
+ return value
50
+ else:
51
+ raise argparse.ArgumentTypeError(
52
+ f"Invalid f0method. Please choose from {valid_f0methods} not {value}"
53
+ )
54
+
55
+ def validate_tts_voices(value):
56
+ json_path = os.path.join("rvc", "lib", "tools", "tts_voices.json")
57
+ with open(json_path, 'r') as file:
58
+ tts_voices_data = json.load(file)
59
+
60
+ # Extrae los valores de "ShortName" del JSON
61
+ short_names = [voice.get("ShortName", "") for voice in tts_voices_data]
62
+ if value in short_names:
63
+ return value
64
+ else:
65
+ raise argparse.ArgumentTypeError(
66
+ f"Invalid voice. Please choose from {short_names} not {value}"
67
+ )
rvc/lib/utils.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import ffmpeg
2
+ import numpy as np
3
+ import re
4
+ import unicodedata
5
+
6
+
7
+ def load_audio(file, sampling_rate):
8
+ try:
9
+ file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
10
+ out, _ = (
11
+ ffmpeg.input(file, threads=0)
12
+ .output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sampling_rate)
13
+ .run(cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True)
14
+ )
15
+ except Exception as error:
16
+ raise RuntimeError(f"Failed to load audio: {error}")
17
+
18
+ return np.frombuffer(out, np.float32).flatten()
19
+
20
+
21
+ def format_title(title):
22
+ formatted_title = unicodedata.normalize('NFKD', title).encode('ascii', 'ignore').decode('utf-8')
23
+ formatted_title = re.sub(r'[\u2500-\u257F]+', '', formatted_title)
24
+ formatted_title = re.sub(r'[^\w\s.-]', '', formatted_title)
25
+ formatted_title = re.sub(r'\s+', '_', formatted_title)
26
+ return formatted_title
rvc/pretraineds/pretrained/D32k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ab20645829460fdad0d3c44254f1ab53c32cae50c22a66c926ae5aa30abda6f
3
+ size 109978943
rvc/pretraineds/pretrained/D40k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:547f66dbbcd9023b9051ed244d12ab043ba8a4e854b154cc28761ac7c002909b
3
+ size 109978943
rvc/pretraineds/pretrained/D48k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8cc013fa60ed9c3f902f5bd99f48c7e3b9352d763d4d3cd6bc241c37b0bfd9ad
3
+ size 109978943
rvc/pretraineds/pretrained/G32k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:81817645cde7ed2e2d83f23ef883f33dda564924b497e84d792743912eca4c23
3
+ size 72653893
rvc/pretraineds/pretrained/G40k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e428573bda1124b0ae0ae843fd8dcded6027d3993444790b3e9b0100938b2113
3
+ size 72763063
rvc/pretraineds/pretrained/G48k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3862a67ea6313e8ffefc05cee6bee656ef3e089442e9ecf4a6618d60721f3e95
3
+ size 72850501
rvc/pretraineds/pretrained/f0D32k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:294db3087236e2c75260d6179056791c9231245daf5d0485545d9e54c4057c77
3
+ size 109978943
rvc/pretraineds/pretrained/f0D40k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7d4f5a441594b470d67579958b2fd4c6b992852ded28ff9e72eda67abcebe423
3
+ size 109978943
rvc/pretraineds/pretrained/f0D48k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b84c8bf347ad1e539c842e8f2a4c36ecd9e7fb23c16041189e4877e9b07925c
3
+ size 109978943
rvc/pretraineds/pretrained/f0G32k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:285f524bf48bb692c76ad7bd0bc654c12bd9e5edeb784dddf7f61a789a608574
3
+ size 72795627
rvc/pretraineds/pretrained/f0G40k.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9115654aeef1995f7dd3c6fc4140bebbef0ca9760bed798105a2380a34299831
3
+ size 72909665