-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprepro.py
95 lines (71 loc) · 2.81 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
# -*- coding: utf-8 -*-
'''
By Yunchao He. [email protected]
'''
import numpy as np
import librosa
from hyperparams import Hyperparams as hp
import glob
import os
import tqdm
from multiprocessing.pool import Pool
def get_spectrograms(sound_file):
'''Returns normalized 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 (T, n_mels) <- Transposed
mag: A 2d array of shape (T, 1+n_fft/2) <- Transposed
'''
# 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)
def _process_wav(file):
fname = os.path.basename(file)
mel, dones, mag = get_spectrograms(file) # (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)
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)
pool = Pool()
files = glob.glob(os.path.join(wav_folder, "*"))
pool.map(_process_wav, files)