jonathanjordan21 commited on
Commit
5b0829a
1 Parent(s): cc90e1e

Create synthesis.py

Browse files
Files changed (1) hide show
  1. synthesis.py +66 -0
synthesis.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from tqdm import tqdm
3
+ import librosa
4
+ from hparams import hparams
5
+ from wavenet_vocoder import builder
6
+
7
+ torch.set_num_threads(4)
8
+ use_cuda = torch.cuda.is_available()
9
+ device = torch.device("cuda" if use_cuda else "cpu")
10
+
11
+
12
+ def build_model():
13
+
14
+ model = getattr(builder, hparams.builder)(
15
+ out_channels=hparams.out_channels,
16
+ layers=hparams.layers,
17
+ stacks=hparams.stacks,
18
+ residual_channels=hparams.residual_channels,
19
+ gate_channels=hparams.gate_channels,
20
+ skip_out_channels=hparams.skip_out_channels,
21
+ cin_channels=hparams.cin_channels,
22
+ gin_channels=hparams.gin_channels,
23
+ weight_normalization=hparams.weight_normalization,
24
+ n_speakers=hparams.n_speakers,
25
+ dropout=hparams.dropout,
26
+ kernel_size=hparams.kernel_size,
27
+ upsample_conditional_features=hparams.upsample_conditional_features,
28
+ upsample_scales=hparams.upsample_scales,
29
+ freq_axis_kernel_size=hparams.freq_axis_kernel_size,
30
+ scalar_input=True,
31
+ legacy=hparams.legacy,
32
+ )
33
+ return model
34
+
35
+
36
+
37
+ def wavegen(model, c=None, tqdm=tqdm):
38
+ """Generate waveform samples by WaveNet.
39
+
40
+ """
41
+
42
+ model.eval()
43
+ model.make_generation_fast_()
44
+
45
+ Tc = c.shape[0]
46
+ upsample_factor = hparams.hop_size
47
+ # Overwrite length according to feature size
48
+ length = Tc * upsample_factor
49
+
50
+ # B x C x T
51
+ c = torch.FloatTensor(c.T).unsqueeze(0)
52
+
53
+ initial_input = torch.zeros(1, 1, 1).fill_(0.0)
54
+
55
+ # Transform data to GPU
56
+ initial_input = initial_input.to(device)
57
+ c = None if c is None else c.to(device)
58
+
59
+ with torch.no_grad():
60
+ y_hat = model.incremental_forward(
61
+ initial_input, c=c, g=None, T=length, tqdm=tqdm, softmax=True, quantize=True,
62
+ log_scale_min=hparams.log_scale_min)
63
+
64
+ y_hat = y_hat.view(-1).cpu().data.numpy()
65
+
66
+ return y_hat