-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalyzer.py
362 lines (315 loc) · 11.5 KB
/
analyzer.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import os
from os.path import join
import librosa
import numpy as np
import pyworld as pw
import tensorflow as tf
from tensorflow.python import debug as tf_debug
import pdb
import pickle
args = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('dir_to_wav', './dataset/vcc2018/wav', 'Dir to *.wav')
tf.app.flags.DEFINE_string('dir_to_bin', './dataset/vcc2018/bin', 'Dir to output *.bin')
tf.app.flags.DEFINE_integer('fs', 16000, 'Global sampling frequency')
tf.app.flags.DEFINE_float('f0_ceil', 500, 'Global f0 ceiling')
EPSILON = 1e-10
SETS = ['Evaluation Set']
SPEAKERS = [s.strip() for s in tf.gfile.GFile('./etc/speakers.tsv', 'r').readlines()]
FFT_SIZE = 1024
SP_DIM = FFT_SIZE // 2 + 1
FEAT_DIM = SP_DIM + SP_DIM + 1 + 1 + 1 # [sp, ap, f0, en, s]
RECORD_BYTES = FEAT_DIM * 4 # all features saved in `float32`
EMB_DIM = 300
EMB_BYTES = EMB_DIM * 4
IVEC_DIM = 100
IVEC_BYTES = IVEC_DIM * 4
def wav2pw(x, fs=16000, fft_size=FFT_SIZE):
""" Extract WORLD feature from waveform """
_f0, t = pw.dio(x, fs, f0_ceil=args.f0_ceil) # raw pitch extractor
f0 = pw.stonemask(x, _f0, t, fs) # pitch refinement
sp = pw.cheaptrick(x, f0, t, fs, fft_size=fft_size)
ap = pw.d4c(x, f0, t, fs, fft_size=fft_size) # extract aperiodicity
return {
'f0': f0,
'sp': sp,
'ap': ap,
}
def extract(filename, fft_size=FFT_SIZE, dtype=np.float32):
""" Basic (WORLD) feature extraction """
x, _ = librosa.load(filename, sr=args.fs, mono=True, dtype=np.float64)
features = wav2pw(x, args.fs, fft_size=fft_size)
ap = features['ap']
f0 = features['f0'].reshape([-1, 1])
sp = features['sp']
en = np.sum(sp + EPSILON, axis=1, keepdims=True)
sp = np.log10(sp / en)
return np.concatenate([sp, ap, f0, en], axis=1).astype(dtype)
def extract_and_save_bin_to(dir_to_bin, dir_to_source, sent_vec_dict, i_vec_dict):
sets = [s for s in os.listdir(dir_to_source) if s in SETS]
# print(sets)
# pdb.set_trace()
# sets is just training set
for d in sets:
path = join(dir_to_source, d)
speakers = [s for s in os.listdir(path) if s in SPEAKERS]
for s in speakers:
path = join(dir_to_source, d, s)
output_dir = join(dir_to_bin, d, s)
if not tf.gfile.Exists(output_dir):
tf.gfile.MakeDirs(output_dir)
for f in os.listdir(path):
filename = join(path, f)
print(filename)
if not os.path.isdir(filename):
features = extract(filename)
labels = SPEAKERS.index(s) * np.ones(
[features.shape[0], 1],
np.float32,
)
b = os.path.splitext(f)[0]
# text_emb = sent_vec_dict[f]
# pdb.set_trace()
i_vec = i_vec_dict[s]
# pdb.set_trace()
features = np.concatenate([features, labels], 1)
with open(join(output_dir, '{}.bin'.format(b)), 'wb') as fp:
fp.write(features.tostring())
# with open(join(output_dir, 't_{}.bin'.format(b)), 'wb') as tp:
# tp.write(text_emb.tostring())
with open(join(output_dir, 'i_{}.bin'.format(b)), 'wb') as ip:
ip.write(i_vec.tostring())
class Tanhize(object):
""" Normalizing `x` to [-1, 1] """
def __init__(self, xmin, xmax):
self.xmin = xmin
self.xmax = xmax
self.xscale = xmax - xmin
def forward_process(self, x):
x = (x - self.xmin) / self.xscale
return tf.clip_by_value(x, 0., 1.) * 2. - 1.
def backward_process(self, x):
return (x * .5 + .5) * self.xscale + self.xmin
def read(
file_pattern,
batch_size,
record_bytes=RECORD_BYTES,
capacity=256,
min_after_dequeue=128,
num_threads=8,
format='NCHW',
normalizer=None,
):
"""
Read only `sp` and `speaker`
Read only `sp` and `speaker`
Return:
`feature`: [b, c]
`speaker`: [b,]
"""
with tf.name_scope('InputSpectralFrame'):
files = tf.gfile.Glob(file_pattern)
filename_queue = tf.train.string_input_producer(files)
reader = tf.FixedLengthRecordReader(record_bytes)
_, value = reader.read(filename_queue)
value = tf.decode_raw(value, tf.float32)
value = tf.reshape(value, [FEAT_DIM, ])
feature = value[:SP_DIM] # NCHW format
if normalizer is not None:
feature = normalizer.forward_process(feature)
if format == 'NCHW':
feature = tf.reshape(feature, [1, SP_DIM, 1])
elif format == 'NHWC':
feature = tf.reshape(feature, [SP_DIM, 1, 1])
else:
pass
speaker = tf.cast(value[-1], tf.int64)
return tf.train.shuffle_batch(
[feature, speaker],
batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue,
num_threads=num_threads,
# enqueue_many=True,
)
def read_all(
file_pattern,
file_pattern2,
batch_size,
record_bytes=RECORD_BYTES,
capacity=256,
min_after_dequeue=128,
num_threads=8,
format='NCHW',
normalizer=None,
):
'''
Read only `sp` and `speaker`
Return:
`feature`: [b, c]
`speaker`: [b,]
'''
with tf.name_scope('InputSpectralFrame'):
files = tf.gfile.Glob(file_pattern)
filename_queue = tf.train.string_input_producer(files)
reader = tf.FixedLengthRecordReader(record_bytes)
_, value = reader.read(filename_queue)
value = tf.decode_raw(value, tf.float32)
value = tf.reshape(value, [FEAT_DIM, ])
feature = value[:SP_DIM] # NCHW format
files2 = tf.gfile.Glob(file_pattern2)
filename_queue2 = tf.train.string_input_producer(files2)
reader2 = tf.FixedLengthRecordReader(EMB_BYTES)
_, value2 = reader2.read(filename_queue2)
value2 = tf.decode_raw(value2, tf.float32)
if normalizer is not None:
feature = normalizer.forward_process(feature)
if format == 'NCHW':
feature = tf.reshape(feature, [1, SP_DIM, 1])
# text_emb = tf.reshape(value2, [1, 300, 1])
elif format == 'NHWC':
feature = tf.reshape(feature, [SP_DIM, 1, 1])
# text_emb = tf.reshape(value2, [300, 1, 1])
else:
pass
speaker = tf.cast(value[-1], tf.int64)
# print(value2.shape)
# tf_debug.
text_emb = tf.reshape(value2, [EMB_DIM, ])
# changed_file_pattern = file_pattern.split('/')
# text_emb = tf.random_uniform(shape=(300,))
# print(value)
# pdb.set_trace()
# pdb.set_trace()
# tf_debug.LocalCLIDebugWrapperSession(sess)
return tf.train.shuffle_batch(
[feature, speaker, text_emb],
batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue,
num_threads=num_threads,
# enqueue_many=True,
)
def read_i_all(
file_pattern,
file_pattern2,
batch_size,
record_bytes=RECORD_BYTES,
capacity=256,
min_after_dequeue=128,
num_threads=8,
format='NCHW',
normalizer=None,
):
'''
Read only `sp` and `speaker`
Return:
`feature`: [b, c]
`speaker`: [b,]
'''
with tf.name_scope('InputSpectralFrame'):
files = tf.gfile.Glob(file_pattern)
filename_queue = tf.train.string_input_producer(files)
reader = tf.FixedLengthRecordReader(record_bytes)
_, value = reader.read(filename_queue)
value = tf.decode_raw(value, tf.float32)
value = tf.reshape(value, [FEAT_DIM, ])
feature = value[:SP_DIM] # NCHW format
files2 = tf.gfile.Glob(file_pattern2)
filename_queue2 = tf.train.string_input_producer(files2)
reader2 = tf.FixedLengthRecordReader(IVEC_BYTES)
_, value2 = reader2.read(filename_queue2)
value2 = tf.decode_raw(value2, tf.float32)
if normalizer is not None:
feature = normalizer.forward_process(feature)
if format == 'NCHW':
feature = tf.reshape(feature, [1, SP_DIM, 1])
# text_emb = tf.reshape(value2, [1, 300, 1])
elif format == 'NHWC':
feature = tf.reshape(feature, [SP_DIM, 1, 1])
# text_emb = tf.reshape(value2, [300, 1, 1])
else:
pass
speaker = tf.cast(value[-1], tf.int64)
# print(value2.shape)
# tf_debug.
i_vec = tf.reshape(value2, [IVEC_DIM, ])
# changed_file_pattern = file_pattern.split('/')
# text_emb = tf.random_uniform(shape=(300,))
# print(value)
# pdb.set_trace()
# pdb.set_trace()
# tf_debug.LocalCLIDebugWrapperSession(sess)
return tf.train.shuffle_batch(
[feature, speaker, i_vec],
batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue,
num_threads=num_threads,
# enqueue_many=True,
)
def read_whole_features(file_pattern, num_epochs=1):
"""
Return
`feature`: `dict` whose keys are `sp`, `ap`, `f0`, `en`, `speaker`
"""
files = tf.gfile.Glob(file_pattern)
print('{} files found'.format(len(files)))
filename_queue = tf.train.string_input_producer(files, num_epochs=num_epochs)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
print("Processing {}".format(key), flush=True)
value = tf.decode_raw(value, tf.float32)
value = tf.reshape(value, [-1, FEAT_DIM])
return {
'sp': value[:, :SP_DIM],
'ap': value[:, SP_DIM: 2 * SP_DIM],
'f0': value[:, SP_DIM * 2],
'en': value[:, SP_DIM * 2 + 1],
'speaker': tf.cast(value[:, SP_DIM * 2 + 2], tf.int64),
'filename': key,
}
def read_i_vec(file_pattern, num_epochs=1):
files = tf.gfile.Glob(file_pattern)
filename_queue = tf.train.string_input_producer(files, num_epochs=num_epochs)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
value = tf.decode_raw(value, tf.float32)
value = tf.reshape(value, [-1, IVEC_DIM])
return value
def pw2wav(features, feat_dim=513, fs=16000):
''' NOTE: Use `order='C'` to ensure Cython compatibility '''
en = np.reshape(features['en'], [-1, 1])
sp = np.power(10., features['sp'])
sp = en * sp
if isinstance(features, dict):
return pw.synthesize(
features['f0'].astype(np.float64).copy(order='C'),
sp.astype(np.float64).copy(order='C'),
features['ap'].astype(np.float64).copy(order='C'),
fs,
)
features = features.astype(np.float64)
sp = features[:, :feat_dim]
ap = features[:, feat_dim:feat_dim * 2]
f0 = features[:, feat_dim * 2]
en = features[:, feat_dim * 2 + 1]
en = np.reshape(en, [-1, 1])
sp = np.power(10., sp)
sp = en * sp
return pw.synthesize(
f0.copy(order='C'),
sp.copy(order='C'),
ap.copy(order='C'),
fs
)
if __name__ == '__main__':
with open('./data/sent_emb.pkl', 'rb') as f:
sent_vec_dict = pickle.load(f)
with open('./data/i_vec_dict.pkl', 'rb') as g:
i_vec_dict = pickle.load(g)
extract_and_save_bin_to(
args.dir_to_bin,
args.dir_to_wav,
sent_vec_dict,
i_vec_dict,
)