-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
250 lines (191 loc) · 8.09 KB
/
utils.py
File metadata and controls
250 lines (191 loc) · 8.09 KB
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
import librosa
import os
from tqdm import tqdm
import torch
import numpy as np
from scipy.io.wavfile import write
import torchaudio
import torch
import numpy as np
import librosa.util as librosa_util
from scipy.signal import get_window
def peak_normalize(audio):
return (audio - np.min(audio, axis=-1, keepdims=True)) / (np.max(audio, axis=-1, keepdims=True) - np.min(audio, axis=-1, keepdims=True) + 1e-8)
def window_sumsquare(
window,
n_frames,
hop_length,
win_length,
n_fft,
dtype=np.float32,
norm=None,
):
"""
# from librosa 0.6
Compute the sum-square envelope of a window function at a given hop length.
This is used to estimate modulation effects induced by windowing
observations in short-time fourier transforms.
Parameters
----------
window : string, tuple, number, callable, or list-like
Window specification, as in `get_window`
n_frames : int > 0
The number of analysis frames
hop_length : int > 0
The number of samples to advance between frames
win_length : [optional]
The length of the window function. By default, this matches `n_fft`.
n_fft : int > 0
The length of each analysis frame.
dtype : np.dtype
The data type of the output
Returns
-------
wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))`
The sum-squared envelope of the window function
"""
if win_length is None:
win_length = n_fft
n = n_fft + hop_length * (n_frames - 1)
x = np.zeros(n, dtype=dtype)
# Compute the squared window at the desired length
win_sq = get_window(window, win_length, fftbins=True)
win_sq = librosa_util.normalize(win_sq, norm=norm) ** 2
win_sq = librosa_util.pad_center(win_sq, n_fft)
# Fill the envelope
for i in range(n_frames):
sample = i * hop_length
x[sample : min(n, sample + n_fft)] += win_sq[: max(0, min(n_fft, n - sample))]
return x
def griffin_lim(magnitudes, stft_fn, n_iters=30):
"""
PARAMS
------
magnitudes: spectrogram magnitudes
stft_fn: STFT class with transform (STFT) and inverse (ISTFT) methods
"""
angles = np.angle(np.exp(2j * np.pi * np.random.rand(*magnitudes.size())))
angles = angles.astype(np.float32)
angles = torch.autograd.Variable(torch.from_numpy(angles))
signal = stft_fn.inverse(magnitudes, angles).squeeze(1)
for i in range(n_iters):
_, angles = stft_fn.transform(signal)
signal = stft_fn.inverse(magnitudes, angles).squeeze(1)
return signal
def dynamic_range_compression(x, normalize_fun=torch.log, C=1, clip_val=1e-5):
"""
PARAMS
------
C: compression factor
"""
return normalize_fun(torch.clamp(x, min=clip_val) * C)
def dynamic_range_decompression(x, C=1):
"""
PARAMS
------
C: compression factor used to compress
"""
return torch.exp(x) / C
def pad_wav(waveform, segment_length):
waveform_length = waveform.shape[-1]
assert waveform_length > 100, "Waveform is too short, %s" % waveform_length
if segment_length is None or waveform_length == segment_length:
return waveform
elif waveform_length > segment_length:
return waveform[:segment_length]
elif waveform_length < segment_length:
temp_wav = np.zeros((1, segment_length))
temp_wav[:, :waveform_length] = waveform
return temp_wav
def normalize_wav(waveform):
waveform = waveform - np.mean(waveform)
waveform = waveform / (np.max(np.abs(waveform)) + 1e-8)
return waveform * 0.5
def read_wav_file(filename, segment_length):
# waveform, sr = librosa.load(filename, sr=None, mono=True) # 4 times slower
waveform, sr = torchaudio.load(filename) # Faster!!!
waveform = torchaudio.functional.resample(waveform, orig_freq=sr, new_freq=16000)
waveform = waveform.numpy()[0, ...]
waveform = normalize_wav(waveform)
waveform = waveform[None, ...]
waveform = pad_wav(waveform, segment_length)
waveform = waveform / np.max(np.abs(waveform))
waveform = 0.5 * waveform
return waveform
def get_mel_from_wav(audio, _stft):
audio = torch.clip(torch.FloatTensor(audio).unsqueeze(0), -1, 1)
audio = torch.autograd.Variable(audio, requires_grad=False)
melspec, magnitudes, phases, energy = _stft.mel_spectrogram(audio)
melspec = torch.squeeze(melspec, 0).numpy().astype(np.float32)
magnitudes = torch.squeeze(magnitudes, 0).numpy().astype(np.float32)
energy = torch.squeeze(energy, 0).numpy().astype(np.float32)
return melspec, magnitudes, energy
def inv_mel_spec(mel, out_filename, _stft, griffin_iters=60):
mel = torch.stack([mel])
mel_decompress = _stft.spectral_de_normalize(mel)
mel_decompress = mel_decompress.transpose(1, 2).data.cpu()
spec_from_mel_scaling = 1000
spec_from_mel = torch.mm(mel_decompress[0], _stft.mel_basis)
spec_from_mel = spec_from_mel.transpose(0, 1).unsqueeze(0)
spec_from_mel = spec_from_mel * spec_from_mel_scaling
audio = griffin_lim(
torch.autograd.Variable(spec_from_mel[:, :, :-1]), _stft._stft_fn, griffin_iters
)
audio = audio.squeeze()
audio = audio.cpu().numpy()
audio_path = out_filename
write(audio_path, _stft.sampling_rate, audio)
def _pad_spec(fbank, target_length=1024):
n_frames = fbank.shape[0]
p = target_length - n_frames
if p > 0:
m = torch.nn.ZeroPad2d((0, 0, 0, p))
fbank = m(fbank)
elif p < 0:
fbank = fbank[0:target_length, :]
if fbank.size(-1) % 2 != 0:
fbank = fbank[..., :-1]
return fbank
def wav_to_fbank(filename, target_length=1024, fn_STFT=None):
assert fn_STFT is not None
# mixup
waveform = read_wav_file(filename, target_length * 160) # hop size is 160
waveform = waveform[0, ...]
waveform = torch.FloatTensor(waveform)
fbank, log_magnitudes_stft, energy = get_mel_from_wav(waveform, fn_STFT)
fbank = torch.FloatTensor(fbank.T)
log_magnitudes_stft = torch.FloatTensor(log_magnitudes_stft.T)
fbank, log_magnitudes_stft = _pad_spec(fbank, target_length), _pad_spec(
log_magnitudes_stft, target_length
)
return fbank, log_magnitudes_stft, waveform
def preprocess(digital_path, record_low_path, segment_length=5, stride_length=0.5, target_sampling_rate=44100, total_files=60, mono=False, test_files=5):
train_val_files = total_files - test_files
train_end = int(0.8 * train_val_files)
val_end = train_end + int(0.2 * train_val_files)
train_digital_waveforms, val_digital_waveforms, test_digital_waveforms = [], [], []
train_record_low_waveforms, val_record_low_waveforms, test_record_low_waveforms = [], [], []
for i in tqdm(range(1, total_files + 1)):
if i <= test_files:
digital_waveforms = test_digital_waveforms
record_low_waveforms = test_record_low_waveforms
elif i <= train_end + test_files:
digital_waveforms = train_digital_waveforms
record_low_waveforms = train_record_low_waveforms
elif i <= val_end + test_files:
digital_waveforms = val_digital_waveforms
record_low_waveforms = val_record_low_waveforms
digital_file = os.path.join(digital_path, f"{i}.wav")
record_low_file = os.path.join(record_low_path, f"{i}.wav")
digital_data, _ = librosa.load(digital_file, sr=target_sampling_rate)
record_low_data, _ = librosa.load(record_low_file, sr=target_sampling_rate, mono=mono)
segment_samples = int(segment_length * target_sampling_rate)
stride_samples = int(stride_length * target_sampling_rate)
for start in range(0, len(digital_data) - segment_samples + 1, stride_samples):
digital_segment = digital_data[start:start + segment_samples]
record_low_segment = record_low_data[:, start:start + segment_samples]
digital_waveforms.append(digital_segment)
record_low_waveforms.append(record_low_segment)
return (train_digital_waveforms, train_record_low_waveforms), \
(val_digital_waveforms, val_record_low_waveforms), \
(test_digital_waveforms, test_record_low_waveforms)