Spaces:
Running
on
A10G
Running
on
A10G
File size: 8,416 Bytes
df2accb |
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 282 283 284 |
# Copyright (c) 2023 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import torch
from librosa.filters import mel as librosa_mel_fn
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):
return torch.log(torch.clamp(x, min=clip_val) * C)
def spectral_normalize_torch(magnitudes):
output = dynamic_range_compression_torch(magnitudes)
return output
def extract_linear_features(y, cfg, center=False):
if torch.min(y) < -1.0:
print("min value is ", torch.min(y))
if torch.max(y) > 1.0:
print("max value is ", torch.max(y))
global hann_window
hann_window[str(y.device)] = torch.hann_window(cfg.win_size).to(y.device)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(int((cfg.n_fft - cfg.hop_size) / 2), int((cfg.n_fft - cfg.hop_size) / 2)),
mode="reflect",
)
y = y.squeeze(1)
# complex tensor as default, then use view_as_real for future pytorch compatibility
spec = torch.stft(
y,
cfg.n_fft,
hop_length=cfg.hop_size,
win_length=cfg.win_size,
window=hann_window[str(y.device)],
center=center,
pad_mode="reflect",
normalized=False,
onesided=True,
return_complex=True,
)
spec = torch.view_as_real(spec)
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
spec = torch.squeeze(spec, 0)
return spec
def mel_spectrogram_torch(y, cfg, center=False):
if torch.min(y) < -1.0:
print("min value is ", torch.min(y))
if torch.max(y) > 1.0:
print("max value is ", torch.max(y))
global mel_basis, hann_window
if cfg.fmax not in mel_basis:
mel = librosa_mel_fn(
sr=cfg.sample_rate,
n_fft=cfg.n_fft,
n_mels=cfg.n_mel,
fmin=cfg.fmin,
fmax=cfg.fmax,
)
mel_basis[str(cfg.fmax) + "_" + str(y.device)] = (
torch.from_numpy(mel).float().to(y.device)
)
hann_window[str(y.device)] = torch.hann_window(cfg.win_size).to(y.device)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(int((cfg.n_fft - cfg.hop_size) / 2), int((cfg.n_fft - cfg.hop_size) / 2)),
mode="reflect",
)
y = y.squeeze(1)
spec = torch.stft(
y,
cfg.n_fft,
hop_length=cfg.hop_size,
win_length=cfg.win_size,
window=hann_window[str(y.device)],
center=center,
pad_mode="reflect",
normalized=False,
onesided=True,
return_complex=True,
)
spec = torch.view_as_real(spec)
spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)
spec = torch.matmul(mel_basis[str(cfg.fmax) + "_" + str(y.device)], spec)
spec = spectral_normalize_torch(spec)
return spec
mel_basis = {}
hann_window = {}
def extract_mel_features(
y,
cfg,
center=False
# n_fft, n_mel, sampling_rate, hop_size, win_size, fmin, fmax, center=False
):
"""Extract mel features
Args:
y (tensor): audio data in tensor
cfg (dict): configuration in cfg.preprocess
center (bool, optional): In STFT, whether t-th frame is centered at time t*hop_length. Defaults to False.
Returns:
tensor: a tensor containing the mel feature calculated based on STFT result
"""
if torch.min(y) < -1.0:
print("min value is ", torch.min(y))
if torch.max(y) > 1.0:
print("max value is ", torch.max(y))
global mel_basis, hann_window
if cfg.fmax not in mel_basis:
mel = librosa_mel_fn(
sr=cfg.sample_rate,
n_fft=cfg.n_fft,
n_mels=cfg.n_mel,
fmin=cfg.fmin,
fmax=cfg.fmax,
)
mel_basis[str(cfg.fmax) + "_" + str(y.device)] = (
torch.from_numpy(mel).float().to(y.device)
)
hann_window[str(y.device)] = torch.hann_window(cfg.win_size).to(y.device)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(int((cfg.n_fft - cfg.hop_size) / 2), int((cfg.n_fft - cfg.hop_size) / 2)),
mode="reflect",
)
y = y.squeeze(1)
# complex tensor as default, then use view_as_real for future pytorch compatibility
spec = torch.stft(
y,
cfg.n_fft,
hop_length=cfg.hop_size,
win_length=cfg.win_size,
window=hann_window[str(y.device)],
center=center,
pad_mode="reflect",
normalized=False,
onesided=True,
return_complex=True,
)
spec = torch.view_as_real(spec)
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
spec = torch.matmul(mel_basis[str(cfg.fmax) + "_" + str(y.device)], spec)
spec = spectral_normalize_torch(spec)
return spec.squeeze(0)
def extract_mel_features_tts(
y,
cfg,
center=False,
taco=False,
_stft=None,
):
"""Extract mel features
Args:
y (tensor): audio data in tensor
cfg (dict): configuration in cfg.preprocess
center (bool, optional): In STFT, whether t-th frame is centered at time t*hop_length. Defaults to False.
taco: use tacotron mel
Returns:
tensor: a tensor containing the mel feature calculated based on STFT result
"""
if not taco:
if torch.min(y) < -1.0:
print("min value is ", torch.min(y))
if torch.max(y) > 1.0:
print("max value is ", torch.max(y))
global mel_basis, hann_window
if cfg.fmax not in mel_basis:
mel = librosa_mel_fn(
sr=cfg.sample_rate,
n_fft=cfg.n_fft,
n_mels=cfg.n_mel,
fmin=cfg.fmin,
fmax=cfg.fmax,
)
mel_basis[str(cfg.fmax) + "_" + str(y.device)] = (
torch.from_numpy(mel).float().to(y.device)
)
hann_window[str(y.device)] = torch.hann_window(cfg.win_size).to(y.device)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(int((cfg.n_fft - cfg.hop_size) / 2), int((cfg.n_fft - cfg.hop_size) / 2)),
mode="reflect",
)
y = y.squeeze(1)
# complex tensor as default, then use view_as_real for future pytorch compatibility
spec = torch.stft(
y,
cfg.n_fft,
hop_length=cfg.hop_size,
win_length=cfg.win_size,
window=hann_window[str(y.device)],
center=center,
pad_mode="reflect",
normalized=False,
onesided=True,
return_complex=True,
)
spec = torch.view_as_real(spec)
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
spec = torch.matmul(mel_basis[str(cfg.fmax) + "_" + str(y.device)], spec)
spec = spectral_normalize_torch(spec)
spec = spec.squeeze(0)
else:
audio = torch.clip(y, -1, 1)
audio = torch.autograd.Variable(audio, requires_grad=False)
spec, energy = _stft.mel_spectrogram(audio)
spec = torch.squeeze(spec, 0)
spec = torch.matmul(mel_basis[str(cfg.fmax) + "_" + str(y.device)], spec)
spec = spectral_normalize_torch(spec)
return spec.squeeze(0)
def amplitude_phase_spectrum(y, cfg):
hann_window = torch.hann_window(cfg.win_size).to(y.device)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(int((cfg.n_fft - cfg.hop_size) / 2), int((cfg.n_fft - cfg.hop_size) / 2)),
mode="reflect",
)
y = y.squeeze(1)
stft_spec = torch.stft(
y,
cfg.n_fft,
hop_length=cfg.hop_size,
win_length=cfg.win_size,
window=hann_window,
center=False,
return_complex=True,
)
stft_spec = torch.view_as_real(stft_spec)
if stft_spec.size()[0] == 1:
stft_spec = stft_spec.squeeze(0)
if len(list(stft_spec.size())) == 4:
rea = stft_spec[:, :, :, 0] # [batch_size, n_fft//2+1, frames]
imag = stft_spec[:, :, :, 1] # [batch_size, n_fft//2+1, frames]
else:
rea = stft_spec[:, :, 0] # [n_fft//2+1, frames]
imag = stft_spec[:, :, 1] # [n_fft//2+1, frames]
log_amplitude = torch.log(
torch.abs(torch.sqrt(torch.pow(rea, 2) + torch.pow(imag, 2))) + 1e-5
) # [n_fft//2+1, frames]
phase = torch.atan2(imag, rea) # [n_fft//2+1, frames]
return log_amplitude, phase, rea, imag
|