forked from andabi/deep-voice-conversion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_load.py
176 lines (124 loc) · 5.15 KB
/
data_load.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
# -*- coding: utf-8 -*-
# /usr/bin/python2
import glob
import random
import librosa
import numpy as np
from tensorpack.dataflow.base import RNGDataFlow
from tensorpack.dataflow.common import BatchData
from tensorpack.dataflow import PrefetchData
from audio import read_wav, preemphasis, amp2db
from hparam import hparam as hp
from utils import normalize_0_1
class DataFlow(RNGDataFlow):
def __init__(self, data_path, batch_size):
self.batch_size = batch_size
self.wav_files = glob.glob(data_path)
def __call__(self, n_prefetch=1000, n_thread=1):
df = self
df = BatchData(df, self.batch_size)
df = PrefetchData(df, n_prefetch, n_thread)
return df
class Net1DataFlow(DataFlow):
def get_data(self):
while True:
wav_file = random.choice(self.wav_files)
yield get_mfccs_and_phones(wav_file=wav_file)
class Net2DataFlow(DataFlow):
def get_data(self):
while True:
wav_file = random.choice(self.wav_files)
yield get_mfccs_and_spectrogram(wav_file)
def load_data(mode):
wav_files = glob.glob(getattr(hp, mode).data_path)
return wav_files
def wav_random_crop(wav, sr, duration):
assert (wav.ndim <= 2)
target_len = sr * duration
wav_len = wav.shape[-1]
start = np.random.choice(range(np.maximum(1, wav_len - target_len)), 1)[0]
end = start + target_len
if wav.ndim == 1:
wav = wav[start:end]
else:
wav = wav[:, start:end]
return wav
def get_mfccs_and_phones(wav_file, trim=False, random_crop=True):
'''This is applied in `train1` or `test1` phase.
'''
# Load
wav = read_wav(wav_file, sr=hp.default.sr)
mfccs, _, _ = _get_mfcc_and_spec(wav, hp.default.preemphasis, hp.default.n_fft,
hp.default.win_length,
hp.default.hop_length)
# timesteps
num_timesteps = mfccs.shape[0]
# phones (targets)
phn_file = wav_file.replace("WAV.wav", "PHN").replace("wav", "PHN")
phn2idx, idx2phn = load_vocab()
phns = np.zeros(shape=(num_timesteps,))
bnd_list = []
for line in open(phn_file, 'r').read().splitlines():
start_point, _, phn = line.split()
bnd = int(start_point) // hp.default.hop_length
phns[bnd:] = phn2idx[phn]
bnd_list.append(bnd)
# Trim
if trim:
start, end = bnd_list[1], bnd_list[-1]
mfccs = mfccs[start:end]
phns = phns[start:end]
assert (len(mfccs) == len(phns))
# Random crop
n_timesteps = (hp.default.duration * hp.default.sr) // hp.default.hop_length + 1
if random_crop:
start = np.random.choice(range(np.maximum(1, len(mfccs) - n_timesteps)), 1)[0]
end = start + n_timesteps
mfccs = mfccs[start:end]
phns = phns[start:end]
assert (len(mfccs) == len(phns))
# Padding or crop
mfccs = librosa.util.fix_length(mfccs, n_timesteps, axis=0)
phns = librosa.util.fix_length(phns, n_timesteps, axis=0)
return mfccs, phns
def get_mfccs_and_spectrogram(wav_file, trim=True, random_crop=False):
'''This is applied in `train2`, `test2` or `convert` phase.
'''
# Load
wav, _ = librosa.load(wav_file, sr=hp.default.sr)
# Trim
if trim:
wav, _ = librosa.effects.trim(wav, frame_length=hp.default.win_length, hop_length=hp.default.hop_length)
if random_crop:
wav = wav_random_crop(wav, hp.default.sr, hp.default.duration)
# Padding or crop
length = hp.default.sr * hp.default.duration
wav = librosa.util.fix_length(wav, length)
return _get_mfcc_and_spec(wav, hp.default.preemphasis, hp.default.n_fft, hp.default.win_length, hp.default.hop_length)
# TODO refactoring
def _get_mfcc_and_spec(wav, preemphasis_coeff, n_fft, win_length, hop_length):
# Pre-emphasis
y_preem = preemphasis(wav, coeff=preemphasis_coeff)
# Get spectrogram
D = librosa.stft(y=y_preem, n_fft=n_fft, hop_length=hop_length, win_length=win_length)
mag = np.abs(D)
# Get mel-spectrogram
mel_basis = librosa.filters.mel(hp.default.sr, hp.default.n_fft, hp.default.n_mels) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, mag) # (n_mels, t) # mel spectrogram
# Get mfccs, amp to db
mag_db = amp2db(mag)
mel_db = amp2db(mel)
mfccs = np.dot(librosa.filters.dct(hp.default.n_mfcc, mel_db.shape[0]), mel_db)
# Normalization (0 ~ 1)
mag_db = normalize_0_1(mag_db, hp.default.max_db, hp.default.min_db)
mel_db = normalize_0_1(mel_db, hp.default.max_db, hp.default.min_db)
return mfccs.T, mag_db.T, mel_db.T # (t, n_mfccs), (t, 1+n_fft/2), (t, n_mels)
phns = ['h#', 'aa', 'ae', 'ah', 'ao', 'aw', 'ax', 'ax-h', 'axr', 'ay', 'b', 'bcl',
'ch', 'd', 'dcl', 'dh', 'dx', 'eh', 'el', 'em', 'en', 'eng', 'epi',
'er', 'ey', 'f', 'g', 'gcl', 'hh', 'hv', 'ih', 'ix', 'iy', 'jh',
'k', 'kcl', 'l', 'm', 'n', 'ng', 'nx', 'ow', 'oy', 'p', 'pau', 'pcl',
'q', 'r', 's', 'sh', 't', 'tcl', 'th', 'uh', 'uw', 'ux', 'v', 'w', 'y', 'z', 'zh']
def load_vocab():
phn2idx = {phn: idx for idx, phn in enumerate(phns)}
idx2phn = {idx: phn for idx, phn in enumerate(phns)}
return phn2idx, idx2phn