Julius8888/XJP_Voice
0
1import torch2import torch.utils.data3from librosa.filters import mel as librosa_mel_fn4import warnings5 6# warnings.simplefilter(action='ignore', category=FutureWarning)7warnings.filterwarnings(action="ignore")8MAX_WAV_VALUE = 32768.09 10 11def dynamic_range_compression_torch(x, C=1, clip_val=1e-5):12 """13 PARAMS14 ------15 C: compression factor16 """17 return torch.log(torch.clamp(x, min=clip_val) * C)18 19 20def dynamic_range_decompression_torch(x, C=1):21 """22 PARAMS23 ------24 C: compression factor used to compress25 """26 return torch.exp(x) / C27 28 29def spectral_normalize_torch(magnitudes):30 output = dynamic_range_compression_torch(magnitudes)31 return output32 33 34def spectral_de_normalize_torch(magnitudes):35 output = dynamic_range_decompression_torch(magnitudes)36 return output37 38 39mel_basis = {}40hann_window = {}41 42 43def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False):44 if torch.min(y) < -1.0:45 print("min value is ", torch.min(y))46 if torch.max(y) > 1.0:47 print("max value is ", torch.max(y))48 49 global hann_window50 dtype_device = str(y.dtype) + "_" + str(y.device)51 wnsize_dtype_device = str(win_size) + "_" + dtype_device52 if wnsize_dtype_device not in hann_window:53 hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(54 dtype=y.dtype, device=y.device55 )56 57 y = torch.nn.functional.pad(58 y.unsqueeze(1),59 (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),60 mode="reflect",61 )62 y = y.squeeze(1)63 64 spec = torch.stft(65 y,66 n_fft,67 hop_length=hop_size,68 win_length=win_size,69 window=hann_window[wnsize_dtype_device],70 center=center,71 pad_mode="reflect",72 normalized=False,73 onesided=True,74 return_complex=False,75 )76 77 spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)78 return spec79 80 81def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax):82 global mel_basis83 dtype_device = str(spec.dtype) + "_" + str(spec.device)84 fmax_dtype_device = str(fmax) + "_" + dtype_device85 if fmax_dtype_device not in mel_basis:86 mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)87 mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(88 dtype=spec.dtype, device=spec.device89 )90 spec = torch.matmul(mel_basis[fmax_dtype_device], spec)91 spec = spectral_normalize_torch(spec)92 return spec93 94 95def mel_spectrogram_torch(96 y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False97):98 if torch.min(y) < -1.0:99 print("min value is ", torch.min(y))100 if torch.max(y) > 1.0:101 print("max value is ", torch.max(y))102 103 global mel_basis, hann_window104 dtype_device = str(y.dtype) + "_" + str(y.device)105 fmax_dtype_device = str(fmax) + "_" + dtype_device106 wnsize_dtype_device = str(win_size) + "_" + dtype_device107 if fmax_dtype_device not in mel_basis:108 mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax)109 mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(110 dtype=y.dtype, device=y.device111 )112 if wnsize_dtype_device not in hann_window:113 hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(114 dtype=y.dtype, device=y.device115 )116 117 y = torch.nn.functional.pad(118 y.unsqueeze(1),119 (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)),120 mode="reflect",121 )122 y = y.squeeze(1)123 124 spec = torch.stft(125 y,126 n_fft,127 hop_length=hop_size,128 win_length=win_size,129 window=hann_window[wnsize_dtype_device],130 center=center,131 pad_mode="reflect",132 normalized=False,133 onesided=True,134 return_complex=False,135 )136 137 spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6)138 139 spec = torch.matmul(mel_basis[fmax_dtype_device], spec)140 spec = spectral_normalize_torch(spec)141 142 return spec143 