(y, hparams, center=False, complex=False)
| 43 | |
| 44 | |
| 45 | def mel_spectrogram(y, hparams, center=False, complex=False): |
| 46 | # hop_size: 512 # For 22050Hz, 275 ~= 12.5 ms (0.0125 * sample_rate) |
| 47 | # win_size: 2048 # For 22050Hz, 1100 ~= 50 ms (If None, win_size: fft_size) (0.05 * sample_rate) |
| 48 | # fmin: 55 # Set this to 55 if your speaker is male! if female, 95 should help taking off noise. (To test depending on dataset. Pitch info: male~[65, 260], female~[100, 525]) |
| 49 | # fmax: 10000 # To be increased/reduced depending on data. |
| 50 | # fft_size: 2048 # Extra window size is filled with 0 paddings to match this parameter |
| 51 | # n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, |
| 52 | n_fft = hparams['fft_size'] |
| 53 | num_mels = hparams['audio_num_mel_bins'] |
| 54 | sampling_rate = hparams['audio_sample_rate'] |
| 55 | hop_size = hparams['hop_size'] |
| 56 | win_size = hparams['win_size'] |
| 57 | fmin = hparams['fmin'] |
| 58 | fmax = hparams['fmax'] |
| 59 | y = y.clamp(min=-1., max=1.) |
| 60 | global mel_basis, hann_window |
| 61 | if fmax not in mel_basis: |
| 62 | mel = librosa_mel_fn(sampling_rate, n_fft, num_mels, fmin, fmax) |
| 63 | mel_basis[str(fmax) + '_' + str(y.device)] = torch.from_numpy(mel).float().to(y.device) |
| 64 | hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device) |
| 65 | |
| 66 | y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), |
| 67 | mode='reflect') |
| 68 | y = y.squeeze(1) |
| 69 | |
| 70 | spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)], |
| 71 | center=center, pad_mode='reflect', normalized=False, onesided=True) |
| 72 | |
| 73 | if not complex: |
| 74 | spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9)) |
| 75 | spec = torch.matmul(mel_basis[str(fmax) + '_' + str(y.device)], spec) |
| 76 | spec = spectral_normalize_torch(spec) |
| 77 | else: |
| 78 | B, C, T, _ = spec.shape |
| 79 | spec = spec.transpose(1, 2) # [B, T, n_fft, 2] |
| 80 | return spec |
nothing calls this directly
no test coverage detected