-
Notifications
You must be signed in to change notification settings - Fork 6
/
analyzer.py
199 lines (173 loc) · 6.46 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
import os
from os.path import join
import time
import librosa
import numpy as np
import pyworld as pw
import tensorflow as tf
args = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('dir_to_wav', './dataset/vcc2016/wav', 'Dir to *.wav')
tf.app.flags.DEFINE_string('dir_to_bin', './dataset/vcc2016/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 = ['Training Set', 'Testing Set'] # TODO: for VCC2016 only
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`
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):
sets = [s for s in os.listdir(dir_to_source) if s in SETS]
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]
features = np.concatenate([features, labels], 1)
#print "features shape:", features.shape
with open(join(output_dir, '{}.bin'.format(b)), 'wb') as fp:
fp.write(features.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='NHWC',
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)
print "value 1:", value.get_shape().as_list()
value = tf.decode_raw(value, tf.float32)
print "value 2:", value.get_shape().as_list()
value = tf.reshape(value, [FEAT_DIM,])
print "value 3:", value.get_shape().as_list()
feature = value[:SP_DIM] # NCHW format
print "feature shape:", feature.get_shape().as_list()
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)
print "shuffle batch"
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_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))
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 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(
np.array(features['f0']).astype(np.float64).copy(order='C'),
sp.astype(np.float64).copy(order='C'),
np.array(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__':
extract_and_save_bin_to(
args.dir_to_bin,
args.dir_to_wav,
)