(
y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False
)
| 102 | |
| 103 | |
| 104 | def mel_spectrogram_torch( |
| 105 | y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False |
| 106 | ): |
| 107 | if torch.min(y) < -1.0: |
| 108 | print("min value is ", torch.min(y)) |
| 109 | if torch.max(y) > 1.0: |
| 110 | print("max value is ", torch.max(y)) |
| 111 | |
| 112 | global mel_basis, hann_window |
| 113 | dtype_device = str(y.dtype) + "_" + str(y.device) |
| 114 | fmax_dtype_device = str(fmax) + "_" + dtype_device |
| 115 | wnsize_dtype_device = str(win_size) + "_" + dtype_device |
| 116 | if fmax_dtype_device not in mel_basis: |
| 117 | mel = librosa_mel_fn( |
| 118 | sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax |
| 119 | ) |
| 120 | mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to( |
| 121 | dtype=y.dtype, device=y.device |
| 122 | ) |
| 123 | if wnsize_dtype_device not in hann_window: |
| 124 | hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to( |
| 125 | dtype=y.dtype, device=y.device |
| 126 | ) |
| 127 | |
| 128 | y = torch.nn.functional.pad( |
| 129 | y.unsqueeze(1), |
| 130 | (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), |
| 131 | mode="reflect", |
| 132 | ) |
| 133 | y = y.squeeze(1) |
| 134 | |
| 135 | spec = torch.stft( |
| 136 | y, |
| 137 | n_fft, |
| 138 | hop_length=hop_size, |
| 139 | win_length=win_size, |
| 140 | window=hann_window[wnsize_dtype_device], |
| 141 | center=center, |
| 142 | pad_mode="reflect", |
| 143 | normalized=False, |
| 144 | onesided=True, |
| 145 | return_complex=False, |
| 146 | ) |
| 147 | |
| 148 | spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6) |
| 149 | |
| 150 | spec = torch.matmul(mel_basis[fmax_dtype_device], spec) |
| 151 | spec = spectral_normalize_torch(spec) |
| 152 | |
| 153 | return spec |
nothing calls this directly
no test coverage detected