File size: 8,381 Bytes
22257c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os
import numpy as np
import librosa
import pysptk
from scipy import signal
import pyworld as pw
import copy


def _get_padding_conv2d(input_size, output_size, kernel_size, stride, dilation=[1,1]):
    Pr = (output_size[0]-1)*stride[0]+(kernel_size[0]-1)*dilation[0]+1-input_size[0]
    Pc = (output_size[1]-1)*stride[1]+(kernel_size[1]-1)*dilation[1]+1-input_size[1]
    padding_h = (Pr/2, Pr-Pr/2)
    padding_w = (Pc/2, Pc-Pc/2)
    print(padding_h, padding_w)


def _get_padding_deconv2d(input_size, output_size, kernel_size, stride):
    padding_h = (input_size[0]-1)*stride[0]+kernel_size[0]-output_size[0]
    padding_w = (input_size[1]-1)*stride[1]+kernel_size[1]-output_size[1]
    print(padding_h/2, padding_w/2)


def _conv2d_simulator(input_dim, kernel_size, stride, padding, dilation=[1,1]):
    h_out = (input_dim[0]+2*padding[0]-dilation[0]*(kernel_size[0]-1)-1)/stride[0] + 1
    w_out = (input_dim[1]+2*padding[1]-dilation[1]*(kernel_size[1]-1)-1)/stride[1] + 1
    print('Floor of:', h_out, w_out)
    
    
def _deconv2d_simulator(input_dim, kernel_size, stride, padding, dilation=[1,1]):
    h_out = (input_dim[0]-1)*stride[0]-2*padding[0]+kernel_size[0]
    w_out = (input_dim[1]-1)*stride[1]-2*padding[1]+kernel_size[1]
    print(h_out, w_out)


def sptk_left_signal_padding(x, count):
    x = np.pad(x, (count,0), 'constant', constant_values=(0, 0))
    return x

def sptk_frame_zero_padding(x, winsz):
    x = np.pad(x, ((0,0),(winsz//2,winsz//2)), 'constant', constant_values=(0, 0))
    return x

def sptk_signal_padding(x, count):
    x = np.pad(x, (count,count), 'constant', constant_values=(0, 0))
    return x

def sptk_window(x, framesz, hopsz, winsz=None, windowing=None, normalize=False):
    x = librosa.util.frame(sptk_signal_padding(x, framesz//2), frame_length=framesz, hop_length=hopsz)
    if windowing is not None:
        win = pysptk.blackman(framesz)
        x = x.T * win
    else:
        x = x.T
    if winsz is not None and winsz != framesz:
        x = sptk_frame_zero_padding(x, winsz-framesz)
    if normalize:
        x = x / np.sqrt(np.expand_dims(sum(x**2, 1), 1) + 1e-16)
    return x

def hz2alpha(hz):
    alpha = 0.313 * np.log10(hz) + (-0.903)
    alpha = np.round(alpha*100) / 100.0
    return alpha

def sptk_mcep(x, order, winsz, hopsz, fftsz, fs, window_norm=False, noise_floor=1e-8):
    alpha = hz2alpha(fs)
    windowed = sptk_window(x, winsz, hopsz, fftsz, windowing='blackman', normalize=window_norm)
    cep = pysptk.mcep(windowed, order=order, alpha=alpha, miniter=2, maxiter=30,
                      threshold=0.001, etype=1, eps=noise_floor, min_det=1.0e-6, itype=0)
    return cep, alpha    



def my_world(x, fs, fft_size=1024, hopsz=256, lo=50, hi=550):
    frame_period = hopsz / float(fs) * 1000
    _f0, t = pw.harvest(x, fs, frame_period=frame_period, f0_floor=lo, f0_ceil=hi)
    f0 = pw.stonemask(x, _f0, t, fs)
    sp = pw.cheaptrick(x, f0, t, fs, fft_size=fft_size, f0_floor=lo)
    ap = pw.d4c(x, f0, t, fs, fft_size=fft_size)
    assert x.shape[0] >= (sp.shape[0]-1) * hopsz
    sig = x[:(sp.shape[0]-1) * hopsz]
    assert sig.shape[0] % hopsz == 0
    return f0[:-1], sp[:-1,:], ap[:-1,:], sig



def global_normalization(x, lo, hi):
    # normalize logf0 to [0,1]
    x = x.astype(float).copy()
    uv = x==0
    x[~uv] = (x[~uv] - np.log(lo)) / (np.log(hi)-np.log(lo))
    x = np.clip(x, 0, 1)
    return x


def speaker_normalization(f0, index_nonzero, mean_f0, std_f0):
    # f0 is logf0
    f0 = f0.astype(float).copy()
    #index_nonzero = f0 != 0
    f0[index_nonzero] = (f0[index_nonzero] - mean_f0) / std_f0 / 4.0
    f0[index_nonzero] = np.clip(f0[index_nonzero], -1, 1)
    f0[index_nonzero] = (f0[index_nonzero] + 1) / 2.0
    return f0


def speaker_normalization_tweak(f0, mean_f0, std_f0, mean_f0_trg, std_f0_trg):
    # f0 is logf0
    f0 = f0.astype(float).copy()
    index_nonzero = f0 != 0
    delta = (mean_f0_trg - mean_f0) * 0.1
    f0[index_nonzero] = (f0[index_nonzero] - mean_f0 + delta) / std_f0 / 4.0 
    f0 = np.clip(f0, -1, 1)
    f0[index_nonzero] = (f0[index_nonzero] + 1) / 2.0
    return f0


def quantize_f0(x, num_bins=256):
    # x is logf0
    assert x.ndim==1
    x = x.astype(float).copy()
    assert (x >= 0).all() and (x <= 1).all()
    uv = x==0
    x = np.round(x * (num_bins-1))
    x = x + 1
    x[uv] = 0
    enc = np.zeros((len(x), num_bins+1), dtype=np.float32)
    enc[np.arange(len(x)), x.astype(np.int32)] = 1.0
    return enc 


def quantize_f0_interp(x, num_bins=256):
    # x is logf0
    assert x.ndim==1
    x = x.astype(float).copy()
    uv = (x<0)
    x[uv] = 0.0
    assert (x >= 0).all() and (x <= 1).all()
    x = np.round(x * (num_bins-1))
    x = x + 1
    x[uv] = 0.0
    enc = np.zeros((len(x), num_bins+1), dtype=np.float32)
    enc[np.arange(len(x)), x.astype(np.int32)] = 1.0
    return enc 


def quantize_chroma(x, lo=50, hi=400, num_bins=120):
    # x is f0 in Hz
    assert x.ndim==1
    x = x.astype(float).copy()
    uv = x==0
    x[~uv] = np.clip(x[~uv], lo/2, hi*2) 
    # convert to chroma f0
    x[~uv] = (np.log2(x[~uv] / 440) * 12 + 57) % 12
    # xs ~ [0,12)
    x = np.floor(x / 12 * num_bins)
    x = x + 1
    x[uv] = 0 
    enc = np.zeros((len(x), num_bins+1), dtype=np.float32)
    enc[np.arange(len(x)), x.astype(np.int32)] += 1.0
    
    return enc    



def quantize_f0s(xs, lo=50, hi=400, num_bins=256):
    # xs is logf0
    xs = copy.copy(xs)
    uv = xs==0
    xs[~uv] = (xs[~uv] - np.log(lo)) / (np.log(hi)-np.log(lo))
    xs = np.clip(xs, 0, 1)
    # xs ~ [0,1]
    xs = np.round(xs * (num_bins-1))
    xs = xs + 1
    xs[uv] = 0
    enc = np.zeros((xs.shape[1], num_bins+1), dtype=np.float32)
    for i in range(xs.shape[0]):
        enc[np.arange(xs.shape[1]), xs[i].astype(np.int32)] += 1.0
    enc /= enc.sum(axis=1, keepdims=True)    
    return enc  




def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

def write_metadata(metadata, out_dir, sr=16000):
    with open(os.path.join(out_dir, 'train.txt'), 'w', encoding='utf-8') as f:
        for m in metadata:
            f.write('|'.join([str(x) for x in m]) + '\n')
    frames = sum([m[2] for m in metadata])
    hours = frames / sr / 3600
    print('Wrote %d utterances, %d time steps (%.2f hours)' % (len(metadata), frames, hours))
    
    
def world_dio(x, fs, fft_size=1024, hopsz=256, lo=50, hi=550, thr=0.1):
    frame_period = hopsz / float(fs) * 1000
    _f0, t = pw.dio(x, fs, frame_period=frame_period, f0_floor=lo, f0_ceil=hi, allowed_range=thr)
    f0 = pw.stonemask(x, _f0, t, fs)
    f0[f0!=0] = np.log(f0[f0!=0])
    return f0


def world_harvest(x, fs, fft_size=1024, hopsz=256, lo=50, hi=550):
    frame_period = hopsz / float(fs) * 1000
    _f0, t = pw.harvest(x, fs, frame_period=frame_period, f0_floor=lo, f0_ceil=hi)
    f0 = pw.stonemask(x, _f0, t, fs)
    f0[f0!=0] = np.log(f0[f0!=0])
    return f0

import torch
def get_mask_from_lengths(lengths, max_len):
    ids = torch.arange(0, max_len, device=lengths.device)
    mask = (ids >= lengths.unsqueeze(1)).byte()
    return mask
    
    
def pad_sequence_cnn(sequences, padding_value=0):

    # assuming trailing dimensions and type of all the Tensors
    # in sequences are same and fetching those from sequences[0]
    max_size = sequences[0].size()
    channel_dim = max_size[0]
    max_len = max([s.size(-1) for s in sequences])
    
    out_dims = (len(sequences), channel_dim, max_len)
    
    out_tensor = sequences[0].data.new(*out_dims).fill_(padding_value)
    for i, tensor in enumerate(sequences):
        length = tensor.size(-1)
        # use index notation to prevent duplicate references to the tensor
        out_tensor[i, :, :length] = tensor
    
    return out_tensor    



def interp_vector(vec, t_new):
    t = np.arange(vec.shape[0])
    out = np.zeros_like(vec)
    for j in range(vec.shape[1]):
        out[:,j] = np.interp(t_new, t, vec[:,j], left=np.nan, right=np.nan)
    assert not np.isnan(out).any()    
    return out



from scipy.interpolate import interp1d

def interp_vector_scipy(vec, t_new):
    t = np.arange(vec.shape[0])
    f_interp = interp1d(t, vec, axis=0, bounds_error=True, assume_sorted=True)
    out = f_interp(t_new)
    return out.astype(np.float32)