forked from onejiin/CycleGAN-VC2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_save.py
282 lines (227 loc) · 9.18 KB
/
data_save.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
# -*- coding: utf-8 -*-
import os
import numpy as np
import h5py
from preprocess import world_decompose, world_encode_spectral_envelop
import librosa
def world_encode_data_toSave_spec(num_mcep, hdf5_dir, wav_dir, sr, frame_period=5.0, coded_dim24=24, coded_dim36=36):
# 1. 32dim MCEP [32 x frame]
# 2. 512dim cheaptrick [512 x frame]
# 3. 512dim spectrogram [512 x frame]
f0s = list()
timeaxes = list()
sps = list()
aps = list()
coded_sps24 = list()
coded_sps36 = list()
coded_sps32 = list()
spectrograms = list()
def calc_spec_wav(wav, f0):
sr = 44000
duration = 0.005
hop_size = int(sr * duration) # 44000 * 0.005 = 80 sample
spectrograms = list()
for i in range(wav.shape[0] // (hop_size) + 1):
start = i * hop_size
if f0[i] == 0:
segment_wav = wav[start : start+1024]
else:
segment_wav = wav[start : start+int(3 * 1 / f0[i] * 44000)]
fft_size = 1024
# D = np.abs(librosa.stft(segment_wav, n_fft=fft_size, hop_length=segment_wav.shape[0]*2))
if segment_wav.shape[0] == 0:
D = np.zeros((513))
else:
D = np.abs(librosa.stft(segment_wav, n_fft=fft_size, hop_length=2048))[:, 0]
magnitude = D#[:-1]
spectrograms.append(magnitude)
return spectrograms
for file in os.listdir(wav_dir):
print("----")
print(file)
file_path = os.path.join(wav_dir, file)
wav, _ = librosa.load(file_path, sr=sr, mono=True)
f0, timeaxis, sp, ap = world_decompose(wav=wav, fs=sr, frame_period=frame_period)
spectrogram = calc_spec_wav(wav, f0)
coded_sp24 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=coded_dim24)
coded_sp36 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=coded_dim36)
coded_sp32 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=32)
spectrogram = np.array(spectrogram)
f0s.append(f0)
timeaxes.append(timeaxis)
sps.append(sp)
aps.append(ap)
coded_sps24.append(coded_sp24)
coded_sps36.append(coded_sp36)
coded_sps32.append(coded_sp32)
spectrograms.append(spectrogram)
# file write
item = {"f0": f0, "timeaxe": timeaxis, "ap": ap, "sp": sp, "coded24": coded_sp24, "coded36": coded_sp36,
"coded32": coded_sp32, "spectrogram": spectrogram}
hdf5_file_name = os.path.join(hdf5_dir, os.path.splitext(file)[0]+".h5")
hdf5_write(hdf5_file_name, item)
assert num_mcep == 24 or num_mcep == 36 or num_mcep == 32, "spectral envelop dimension misatch"
if num_mcep == 24:
coded_sps = coded_sps24
elif num_mcep == 36:
coded_sps = coded_sps36
elif num_mcep == 32:
coded_sps = coded_sps32
return f0s, timeaxes, sps, aps, coded_sps, spectrograms
def world_encode_data_toSave(num_mcep, hdf5_dir, wav_dir, sr, frame_period=5.0, coded_dim24=24, coded_dim36=36):
f0s = list()
timeaxes = list()
sps = list()
aps = list()
coded_sps24 = list()
coded_sps36 = list()
coded_sps32 = list()
for file in os.listdir(wav_dir):
file_path = os.path.join(wav_dir, file)
wav, _ = librosa.load(file_path, sr=sr, mono=True)
f0, timeaxis, sp, ap = world_decompose(wav=wav, fs=sr, frame_period=frame_period)
coded_sp24 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=coded_dim24)
coded_sp36 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=coded_dim36)
coded_sp32 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=32)
f0s.append(f0)
timeaxes.append(timeaxis)
sps.append(sp)
aps.append(ap)
coded_sps24.append(coded_sp24)
coded_sps36.append(coded_sp36)
coded_sps32.append(coded_sp32)
# file write
item = {"f0": f0, "timeaxe": timeaxis, "ap": ap, "sp": sp, "coded24": coded_sp24, "coded36": coded_sp36, "coded32": coded_sp32}
hdf5_file_name = os.path.join(hdf5_dir, os.path.splitext(file)[0]+".h5")
hdf5_write(hdf5_file_name, item)
assert num_mcep == 24 or num_mcep == 36 or num_mcep == 32, "spectral envelop dimension misatch"
if num_mcep == 24:
coded_sps = coded_sps24
elif num_mcep == 36:
coded_sps = coded_sps36
elif num_mcep == 32:
coded_sps = coded_sps32
return f0s, timeaxes, sps, aps, coded_sps
def world_encode_data_toLoad(num_mcep, hdf5_dir):
f0s = list()
timeaxes = list()
sps = list()
aps = list()
coded_sps24 = list()
coded_sps36 = list()
coded_sps32 = list()
for file in os.listdir(hdf5_dir):
file_path = os.path.join(hdf5_dir, file)
file = h5py.File(file_path, 'r')
f0 = file["f0"].value
timeaxe = file["timeaxe"].value
sp = file["ap"].value
ap = file["sp"].value
coded_sp24 = file["coded24"].value
coded_sp36 = file["coded36"].value
coded_sp32 = file["coded32"].value
f0s.append(f0)
timeaxes.append(timeaxe)
sps.append(sp)
aps.append(ap)
coded_sps24.append(coded_sp24)
coded_sps36.append(coded_sp36)
coded_sps32.append(coded_sp32)
assert num_mcep == 24 or num_mcep == 36 or num_mcep == 32, "spectral envelop dimension misatch"
if num_mcep == 24:
coded_sps = coded_sps24
elif num_mcep == 36:
coded_sps = coded_sps36
elif num_mcep == 32:
coded_sps = coded_sps32
return f0s, timeaxes, sps, aps, coded_sps
def world_encode_data_toLoad_spec(num_mcep, hdf5_dir):
f0s = list()
timeaxes = list()
sps = list()
aps = list()
coded_sps24 = list()
coded_sps36 = list()
coded_sps32 = list()
spectrograms = list()
for file in os.listdir(hdf5_dir):
file_path = os.path.join(hdf5_dir, file)
file = h5py.File(file_path, 'r')
f0 = file["f0"].value
timeaxe = file["timeaxe"].value
sp = file["ap"].value
ap = file["sp"].value
coded_sp24 = file["coded24"].value
coded_sp36 = file["coded36"].value
coded_sp32 = file["coded32"].value
spectrogram = file["spectrogram"].value
f0s.append(f0)
timeaxes.append(timeaxe)
sps.append(sp)
aps.append(ap)
coded_sps24.append(coded_sp24)
coded_sps36.append(coded_sp36)
coded_sps32.append(coded_sp32)
spectrograms.append(spectrogram)
assert num_mcep == 24 or num_mcep == 36 or num_mcep == 32, "spectral envelop dimension misatch"
if num_mcep == 24:
coded_sps = coded_sps24
elif num_mcep == 36:
coded_sps = coded_sps36
elif num_mcep == 32:
coded_sps = coded_sps32
return f0s, timeaxes, sps, aps, coded_sps, spectrograms
def hdf5_write(file_name, item):
dset = h5py.File(file_name, 'w')
dset['f0'] = item["f0"]
dset['timeaxe'] = item["timeaxe"]
dset['ap'] = item["ap"]
dset['sp'] = item["sp"]
dset['coded24'] = item["coded24"]
dset['coded36'] = item["coded36"]
dset['coded32'] = item["coded32"]
dset['spectrogram'] = item["spectrogram"]
dset.close()
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='hdf5 DB write')
wav_file_default = '/root/onejin/S2SCycleGAN/data/debug/ma/arctic_a0010.wav'
hdf5_file_default = '/root/onejin/S2SCycleGAN/data/debug_hdf5/ma/arctic_a0010.h5'
parser.add_argument('--wav_file', type=str, help='Directory for A.', default=wav_file_default)
parser.add_argument('--hdf_file', type=str, help='Directory for B.', default=hdf5_file_default)
argv = parser.parse_args()
wav_file = argv.wav_file
hdf_file = argv.hdf_file
sr = 44000
wav, _ = librosa.load(wav_file, sr=sr, mono=True)
f0, timeaxis, sp, ap = world_decompose(wav=wav, fs=sr, frame_period=5.0)
def calc_spec_wav(wav, f0):
sr = 44000
duration = 0.005
hop_size = int(sr * duration) # 44000 * 0.005 = 80 sample
spectrograms = list()
for i in range(wav.shape[0] // (hop_size) + 1):
start = i * hop_size
if f0[i] == 0:
segment_wav = wav[start : start+1024]
else:
segment_wav = wav[start : start+int(3 * 1 / f0[i] * 44000)]
fft_size = 1024
# D = np.abs(librosa.stft(segment_wav, n_fft=fft_size, hop_length=segment_wav.shape[0]*2))
if segment_wav.shape[0] == 0:
D = np.zeros((513))
else:
D = np.abs(librosa.stft(segment_wav, n_fft=fft_size, hop_length=2048))[:, 0]
magnitude = D#[:-1]
spectrograms.append(magnitude)
return spectrograms
spectrogram = calc_spec_wav(wav, f0)
coded_sp24 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=24)
coded_sp36 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=36)
coded_sp32 = world_encode_spectral_envelop(sp=sp, fs=sr, dim=32)
spectrogram = np.array(spectrogram)
# file write
item = {"f0": f0, "timeaxe": timeaxis, "ap": ap, "sp": sp, "coded24": coded_sp24, "coded36": coded_sp36,
"coded32": coded_sp32, "spectrogram": spectrogram}
hdf5_file_name = hdf_file
hdf5_write(hdf5_file_name, item)