forked from Kyubyong/deepvoice3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepro.py
155 lines (132 loc) · 4.6 KB
/
prepro.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
#/usr/bin/python2
# -*- coding: utf-8 -*-
'''
By kyubyong park. [email protected].
https://www.github.com/kyubyong/deepvoice3
'''
import numpy as np
import librosa
from hyperparams import Hyperparams as hp
import glob
import os
import tqdm
def get_spectrograms(sound_file):
'''Extracts log(melspectrogram) and log(magnitude) from `sound_file`.
Args:
sound_file: A string. The full path of a sound file.
Returns:
mel: A 2d array of shape (n_mels, T)
mag: A 2d array of shape (1+n_fft/2, T)
'''
# Loading sound file
y, sr = librosa.load(sound_file, sr=hp.sr)
# Trimming
y, _ = librosa.effects.trim(y)
# Preemphasis
y = np.append(y[0], y[1:] - hp.preemphasis * y[:-1])
# stft
linear = librosa.stft(y=y,
n_fft=hp.n_fft,
hop_length=hp.hop_length,
win_length=hp.win_length)
# magnitude spectrogram
mag = np.abs(linear) # (1+n_fft//2, T)
# mel spectrogram
mel_basis = librosa.filters.mel(hp.sr, hp.n_fft, hp.n_mels) # (n_mels, 1+n_fft//2)
mel = np.dot(mel_basis, mag**2) # (n_mels, t)
# Transpose
mel = mel.T.astype(np.float32) # (T, n_mels)
mag = mag.T.astype(np.float32) # (T, 1+n_fft//2)
# Sequence length
dones = np.ones_like(mel[:, 0])
# Padding
mel = np.pad(mel, ((0, hp.T_y - len(mel)), (0, 0)), mode="constant")[:hp.T_y]
mag = np.pad(mag, ((0, hp.T_y - len(mag)), (0, 0)), mode="constant")[:hp.T_y]
dones = np.pad(dones, ((0, hp.T_y - len(dones))), mode="constant")[:hp.T_y]
# Log
mel = np.log10(mel + 1e-8)
mag = np.log10(mag + 1e-8)
# Normalize
mel = (mel - hp.mel_mean) / hp.mel_std
mag = (mag - hp.mag_mean) / hp.mag_std
# Reduce frames for net1
mel = np.reshape(mel, (-1, hp.n_mels*hp.r))
dones = np.reshape(dones, (-1, hp.r))
dones = np.equal(np.sum(dones, -1), 0).astype(np.int32) # 1 for done, 0 for undone.
return mel, dones, mag # (T/r, n_mels*r), (T/r,), (T, 1_n_fft/2)
if __name__ == "__main__":
wav_folder = os.path.join(hp.data, 'wavs')
mel_folder = os.path.join(hp.data, 'mels')
dones_folder = os.path.join(hp.data, 'dones')
mag_folder = os.path.join(hp.data, 'mags')
for folder in (mel_folder, dones_folder, mag_folder):
if not os.path.exists(folder): os.mkdir(folder)
files = glob.glob(os.path.join(wav_folder, "*"))
for f in tqdm.tqdm(files):
fname = os.path.basename(f)
mel, dones, mag = get_spectrograms(f) # (n_mels, T), (1+n_fft/2, T) float32
np.save(os.path.join(mel_folder, fname.replace(".wav", ".npy")), mel)
np.save(os.path.join(dones_folder, fname.replace(".wav", ".npy")), dones)
np.save(os.path.join(mag_folder, fname.replace(".wav", ".npy")), mag)
# def get_spectrograms(sound_file):
# '''Extracts melspectrogram and linear-scale spectrogram (=magnitude) from `sound_file`.
# Args:
# sound_file: A string. The full path of a sound file.
#
# Returns:
# mel: A 2d array of shape (n_mels, T)
# mag: A 2d array of shape (1+n_fft/2, T)
# '''
# # Loading sound file
# y, sr = librosa.load(sound_file, sr=hp.sr)
#
# # Trimming
# y, _ = librosa.effects.trim(y)
#
# # Preemphasis
# y = np.append(y[0], y[1:] - hp.preemphasis * y[:-1])
#
# # stft
# linear = librosa.stft(y=y,
# n_fft=hp.n_fft,
# hop_length=hp.hop_length,
# win_length=hp.win_length)
#
# # magnitude spectrogram
# mag = np.abs(linear) # (1+n_fft//2, T)
#
# # mel spectrogram
# mel_basis = librosa.filters.mel(hp.sr, hp.n_fft, hp.n_mels) # (n_mels, 1+n_fft//2)
# mel = np.dot(mel_basis, mag ** 2) # (n_mels, t)
#
# # Transpose -> Dtype conversion
# mel = mel.T.astype(np.float32) # (T, n_mels)
# mag = mag.T.astype(np.float32) # (T, 1+n_fft//2)
#
# # Padding
# mel = np.pad(mel, ((0, hp.T_y - len(mel)), (0, 0)), mode="constant")
# mag = np.pad(mag, ((0, hp.T_y - len(mag)), (0, 0)), mode="constant")
#
# # Log
# mel = np.log10(mel + 1e-8)
# mag = np.log10(mag + 1e-8)
#
# return mel, mag
#
# wav_folder = os.path.join(hp.data, 'wavs')
#
# files = glob.glob(os.path.join(wav_folder, "*"))
# mels, mags = [], []
# for f in files[:200]:
# mel, mag = get_spectrograms(f)
# mel = mel.flatten()
# mag = mag.flatten()
# mels.extend(list(mel))
# mags.extend(list(mag))
#
# mel_mean = np.mean(np.array(mels))
# mel_std = np.std(np.array(mels))
# mag_mean = np.mean(np.array(mags))
# mag_std = np.std(np.array(mags))
#
# print(mel_mean,mel_std, mag_mean,mag_std)