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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
from torch.nn import functional as F
|
4 |
+
|
5 |
+
import modules.attentions as attentions
|
6 |
+
import modules.commons as commons
|
7 |
+
import modules.modules as modules
|
8 |
+
|
9 |
+
from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d
|
10 |
+
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm
|
11 |
+
|
12 |
+
import utils
|
13 |
+
from modules.commons import init_weights, get_padding
|
14 |
+
from vdecoder.hifigan.models import Generator
|
15 |
+
from utils import f0_to_coarse
|
16 |
+
|
17 |
+
|
18 |
+
class ResidualCouplingBlock(nn.Module):
|
19 |
+
def __init__(self,
|
20 |
+
channels,
|
21 |
+
hidden_channels,
|
22 |
+
kernel_size,
|
23 |
+
dilation_rate,
|
24 |
+
n_layers,
|
25 |
+
n_flows=4,
|
26 |
+
gin_channels=0):
|
27 |
+
super().__init__()
|
28 |
+
self.channels = channels
|
29 |
+
self.hidden_channels = hidden_channels
|
30 |
+
self.kernel_size = kernel_size
|
31 |
+
self.dilation_rate = dilation_rate
|
32 |
+
self.n_layers = n_layers
|
33 |
+
self.n_flows = n_flows
|
34 |
+
self.gin_channels = gin_channels
|
35 |
+
|
36 |
+
self.flows = nn.ModuleList()
|
37 |
+
for i in range(n_flows):
|
38 |
+
self.flows.append(
|
39 |
+
modules.ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers,
|
40 |
+
gin_channels=gin_channels, mean_only=True))
|
41 |
+
self.flows.append(modules.Flip())
|
42 |
+
|
43 |
+
def forward(self, x, x_mask, g=None, reverse=False):
|
44 |
+
if not reverse:
|
45 |
+
for flow in self.flows:
|
46 |
+
x, _ = flow(x, x_mask, g=g, reverse=reverse)
|
47 |
+
else:
|
48 |
+
for flow in reversed(self.flows):
|
49 |
+
x = flow(x, x_mask, g=g, reverse=reverse)
|
50 |
+
return x
|
51 |
+
|
52 |
+
|
53 |
+
class Encoder(nn.Module):
|
54 |
+
def __init__(self,
|
55 |
+
in_channels,
|
56 |
+
out_channels,
|
57 |
+
hidden_channels,
|
58 |
+
kernel_size,
|
59 |
+
dilation_rate,
|
60 |
+
n_layers,
|
61 |
+
gin_channels=0):
|
62 |
+
super().__init__()
|
63 |
+
self.in_channels = in_channels
|
64 |
+
self.out_channels = out_channels
|
65 |
+
self.hidden_channels = hidden_channels
|
66 |
+
self.kernel_size = kernel_size
|
67 |
+
self.dilation_rate = dilation_rate
|
68 |
+
self.n_layers = n_layers
|
69 |
+
self.gin_channels = gin_channels
|
70 |
+
|
71 |
+
self.pre = nn.Conv1d(in_channels, hidden_channels, 1)
|
72 |
+
self.enc = modules.WN(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels)
|
73 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
74 |
+
|
75 |
+
def forward(self, x, x_lengths, g=None):
|
76 |
+
# print(x.shape,x_lengths.shape)
|
77 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype)
|
78 |
+
x = self.pre(x) * x_mask
|
79 |
+
x = self.enc(x, x_mask, g=g)
|
80 |
+
stats = self.proj(x) * x_mask
|
81 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
82 |
+
z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask
|
83 |
+
return z, m, logs, x_mask
|
84 |
+
|
85 |
+
|
86 |
+
class TextEncoder(nn.Module):
|
87 |
+
def __init__(self,
|
88 |
+
out_channels,
|
89 |
+
hidden_channels,
|
90 |
+
kernel_size,
|
91 |
+
n_layers,
|
92 |
+
gin_channels=0,
|
93 |
+
filter_channels=None,
|
94 |
+
n_heads=None,
|
95 |
+
p_dropout=None):
|
96 |
+
super().__init__()
|
97 |
+
self.out_channels = out_channels
|
98 |
+
self.hidden_channels = hidden_channels
|
99 |
+
self.kernel_size = kernel_size
|
100 |
+
self.n_layers = n_layers
|
101 |
+
self.gin_channels = gin_channels
|
102 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels * 2, 1)
|
103 |
+
self.f0_emb = nn.Embedding(256, hidden_channels)
|
104 |
+
|
105 |
+
self.enc_ = attentions.Encoder(
|
106 |
+
hidden_channels,
|
107 |
+
filter_channels,
|
108 |
+
n_heads,
|
109 |
+
n_layers,
|
110 |
+
kernel_size,
|
111 |
+
p_dropout)
|
112 |
+
|
113 |
+
def forward(self, x, x_mask, f0=None, z=None):
|
114 |
+
x = x + self.f0_emb(f0).transpose(1, 2)
|
115 |
+
x = self.enc_(x * x_mask, x_mask)
|
116 |
+
stats = self.proj(x) * x_mask
|
117 |
+
m, logs = torch.split(stats, self.out_channels, dim=1)
|
118 |
+
z = (m + z * torch.exp(logs)) * x_mask
|
119 |
+
return z, m, logs, x_mask
|
120 |
+
|
121 |
+
|
122 |
+
class DiscriminatorP(torch.nn.Module):
|
123 |
+
def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False):
|
124 |
+
super(DiscriminatorP, self).__init__()
|
125 |
+
self.period = period
|
126 |
+
self.use_spectral_norm = use_spectral_norm
|
127 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
128 |
+
self.convs = nn.ModuleList([
|
129 |
+
norm_f(Conv2d(1, 32, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
130 |
+
norm_f(Conv2d(32, 128, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
131 |
+
norm_f(Conv2d(128, 512, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
132 |
+
norm_f(Conv2d(512, 1024, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0))),
|
133 |
+
norm_f(Conv2d(1024, 1024, (kernel_size, 1), 1, padding=(get_padding(kernel_size, 1), 0))),
|
134 |
+
])
|
135 |
+
self.conv_post = norm_f(Conv2d(1024, 1, (3, 1), 1, padding=(1, 0)))
|
136 |
+
|
137 |
+
def forward(self, x):
|
138 |
+
fmap = []
|
139 |
+
|
140 |
+
# 1d to 2d
|
141 |
+
b, c, t = x.shape
|
142 |
+
if t % self.period != 0: # pad first
|
143 |
+
n_pad = self.period - (t % self.period)
|
144 |
+
x = F.pad(x, (0, n_pad), "reflect")
|
145 |
+
t = t + n_pad
|
146 |
+
x = x.view(b, c, t // self.period, self.period)
|
147 |
+
|
148 |
+
for l in self.convs:
|
149 |
+
x = l(x)
|
150 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
151 |
+
fmap.append(x)
|
152 |
+
x = self.conv_post(x)
|
153 |
+
fmap.append(x)
|
154 |
+
x = torch.flatten(x, 1, -1)
|
155 |
+
|
156 |
+
return x, fmap
|
157 |
+
|
158 |
+
|
159 |
+
class DiscriminatorS(torch.nn.Module):
|
160 |
+
def __init__(self, use_spectral_norm=False):
|
161 |
+
super(DiscriminatorS, self).__init__()
|
162 |
+
norm_f = weight_norm if use_spectral_norm == False else spectral_norm
|
163 |
+
self.convs = nn.ModuleList([
|
164 |
+
norm_f(Conv1d(1, 16, 15, 1, padding=7)),
|
165 |
+
norm_f(Conv1d(16, 64, 41, 4, groups=4, padding=20)),
|
166 |
+
norm_f(Conv1d(64, 256, 41, 4, groups=16, padding=20)),
|
167 |
+
norm_f(Conv1d(256, 1024, 41, 4, groups=64, padding=20)),
|
168 |
+
norm_f(Conv1d(1024, 1024, 41, 4, groups=256, padding=20)),
|
169 |
+
norm_f(Conv1d(1024, 1024, 5, 1, padding=2)),
|
170 |
+
])
|
171 |
+
self.conv_post = norm_f(Conv1d(1024, 1, 3, 1, padding=1))
|
172 |
+
|
173 |
+
def forward(self, x):
|
174 |
+
fmap = []
|
175 |
+
|
176 |
+
for l in self.convs:
|
177 |
+
x = l(x)
|
178 |
+
x = F.leaky_relu(x, modules.LRELU_SLOPE)
|
179 |
+
fmap.append(x)
|
180 |
+
x = self.conv_post(x)
|
181 |
+
fmap.append(x)
|
182 |
+
x = torch.flatten(x, 1, -1)
|
183 |
+
|
184 |
+
return x, fmap
|
185 |
+
|
186 |
+
|
187 |
+
class F0Decoder(nn.Module):
|
188 |
+
def __init__(self,
|
189 |
+
out_channels,
|
190 |
+
hidden_channels,
|
191 |
+
filter_channels,
|
192 |
+
n_heads,
|
193 |
+
n_layers,
|
194 |
+
kernel_size,
|
195 |
+
p_dropout,
|
196 |
+
spk_channels=0):
|
197 |
+
super().__init__()
|
198 |
+
self.out_channels = out_channels
|
199 |
+
self.hidden_channels = hidden_channels
|
200 |
+
self.filter_channels = filter_channels
|
201 |
+
self.n_heads = n_heads
|
202 |
+
self.n_layers = n_layers
|
203 |
+
self.kernel_size = kernel_size
|
204 |
+
self.p_dropout = p_dropout
|
205 |
+
self.spk_channels = spk_channels
|
206 |
+
|
207 |
+
self.prenet = nn.Conv1d(hidden_channels, hidden_channels, 3, padding=1)
|
208 |
+
self.decoder = attentions.FFT(
|
209 |
+
hidden_channels,
|
210 |
+
filter_channels,
|
211 |
+
n_heads,
|
212 |
+
n_layers,
|
213 |
+
kernel_size,
|
214 |
+
p_dropout)
|
215 |
+
self.proj = nn.Conv1d(hidden_channels, out_channels, 1)
|
216 |
+
self.f0_prenet = nn.Conv1d(1, hidden_channels, 3, padding=1)
|
217 |
+
self.cond = nn.Conv1d(spk_channels, hidden_channels, 1)
|
218 |
+
|
219 |
+
def forward(self, x, norm_f0, x_mask, spk_emb=None):
|
220 |
+
x = torch.detach(x)
|
221 |
+
if spk_emb is not None:
|
222 |
+
x = x + self.cond(spk_emb)
|
223 |
+
x += self.f0_prenet(norm_f0)
|
224 |
+
x = self.prenet(x) * x_mask
|
225 |
+
x = self.decoder(x * x_mask, x_mask)
|
226 |
+
x = self.proj(x) * x_mask
|
227 |
+
return x
|
228 |
+
|
229 |
+
|
230 |
+
class SynthesizerTrn(nn.Module):
|
231 |
+
"""
|
232 |
+
Synthesizer for Training
|
233 |
+
"""
|
234 |
+
|
235 |
+
def __init__(self,
|
236 |
+
spec_channels,
|
237 |
+
segment_size,
|
238 |
+
inter_channels,
|
239 |
+
hidden_channels,
|
240 |
+
filter_channels,
|
241 |
+
n_heads,
|
242 |
+
n_layers,
|
243 |
+
kernel_size,
|
244 |
+
p_dropout,
|
245 |
+
resblock,
|
246 |
+
resblock_kernel_sizes,
|
247 |
+
resblock_dilation_sizes,
|
248 |
+
upsample_rates,
|
249 |
+
upsample_initial_channel,
|
250 |
+
upsample_kernel_sizes,
|
251 |
+
gin_channels,
|
252 |
+
ssl_dim,
|
253 |
+
n_speakers,
|
254 |
+
sampling_rate=44100,
|
255 |
+
**kwargs):
|
256 |
+
super().__init__()
|
257 |
+
self.spec_channels = spec_channels
|
258 |
+
self.inter_channels = inter_channels
|
259 |
+
self.hidden_channels = hidden_channels
|
260 |
+
self.filter_channels = filter_channels
|
261 |
+
self.n_heads = n_heads
|
262 |
+
self.n_layers = n_layers
|
263 |
+
self.kernel_size = kernel_size
|
264 |
+
self.p_dropout = p_dropout
|
265 |
+
self.resblock = resblock
|
266 |
+
self.resblock_kernel_sizes = resblock_kernel_sizes
|
267 |
+
self.resblock_dilation_sizes = resblock_dilation_sizes
|
268 |
+
self.upsample_rates = upsample_rates
|
269 |
+
self.upsample_initial_channel = upsample_initial_channel
|
270 |
+
self.upsample_kernel_sizes = upsample_kernel_sizes
|
271 |
+
self.segment_size = segment_size
|
272 |
+
self.gin_channels = gin_channels
|
273 |
+
self.ssl_dim = ssl_dim
|
274 |
+
self.emb_g = nn.Embedding(n_speakers, gin_channels)
|
275 |
+
|
276 |
+
self.pre = nn.Conv1d(ssl_dim, hidden_channels, kernel_size=5, padding=2)
|
277 |
+
|
278 |
+
self.enc_p = TextEncoder(
|
279 |
+
inter_channels,
|
280 |
+
hidden_channels,
|
281 |
+
filter_channels=filter_channels,
|
282 |
+
n_heads=n_heads,
|
283 |
+
n_layers=n_layers,
|
284 |
+
kernel_size=kernel_size,
|
285 |
+
p_dropout=p_dropout
|
286 |
+
)
|
287 |
+
hps = {
|
288 |
+
"sampling_rate": sampling_rate,
|
289 |
+
"inter_channels": inter_channels,
|
290 |
+
"resblock": resblock,
|
291 |
+
"resblock_kernel_sizes": resblock_kernel_sizes,
|
292 |
+
"resblock_dilation_sizes": resblock_dilation_sizes,
|
293 |
+
"upsample_rates": upsample_rates,
|
294 |
+
"upsample_initial_channel": upsample_initial_channel,
|
295 |
+
"upsample_kernel_sizes": upsample_kernel_sizes,
|
296 |
+
"gin_channels": gin_channels,
|
297 |
+
}
|
298 |
+
self.dec = Generator(h=hps)
|
299 |
+
self.enc_q = Encoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels)
|
300 |
+
self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 4, gin_channels=gin_channels)
|
301 |
+
self.f0_decoder = F0Decoder(
|
302 |
+
1,
|
303 |
+
hidden_channels,
|
304 |
+
filter_channels,
|
305 |
+
n_heads,
|
306 |
+
n_layers,
|
307 |
+
kernel_size,
|
308 |
+
p_dropout,
|
309 |
+
spk_channels=gin_channels
|
310 |
+
)
|
311 |
+
self.emb_uv = nn.Embedding(2, hidden_channels)
|
312 |
+
self.predict_f0 = False
|
313 |
+
|
314 |
+
def forward(self, c, f0, mel2ph, uv, noise=None, g=None):
|
315 |
+
|
316 |
+
decoder_inp = F.pad(c, [0, 0, 1, 0])
|
317 |
+
mel2ph_ = mel2ph.unsqueeze(2).repeat([1, 1, c.shape[-1]])
|
318 |
+
c = torch.gather(decoder_inp, 1, mel2ph_).transpose(1, 2) # [B, T, H]
|
319 |
+
|
320 |
+
c_lengths = (torch.ones(c.size(0)) * c.size(-1)).to(c.device)
|
321 |
+
g = g.unsqueeze(0)
|
322 |
+
g = self.emb_g(g).transpose(1, 2)
|
323 |
+
x_mask = torch.unsqueeze(commons.sequence_mask(c_lengths, c.size(2)), 1).to(c.dtype)
|
324 |
+
x = self.pre(c) * x_mask + self.emb_uv(uv.long()).transpose(1, 2)
|
325 |
+
|
326 |
+
if self.predict_f0:
|
327 |
+
lf0 = 2595. * torch.log10(1. + f0.unsqueeze(1) / 700.) / 500
|
328 |
+
norm_lf0 = utils.normalize_f0(lf0, x_mask, uv, random_scale=False)
|
329 |
+
pred_lf0 = self.f0_decoder(x, norm_lf0, x_mask, spk_emb=g)
|
330 |
+
f0 = (700 * (torch.pow(10, pred_lf0 * 500 / 2595) - 1)).squeeze(1)
|
331 |
+
|
332 |
+
z_p, m_p, logs_p, c_mask = self.enc_p(x, x_mask, f0=f0_to_coarse(f0), z=noise)
|
333 |
+
z = self.flow(z_p, c_mask, g=g, reverse=True)
|
334 |
+
o = self.dec(z * c_mask, g=g, f0=f0)
|
335 |
+
return o
|