| 49 | |
| 50 | |
| 51 | def spectrogram_torch(y, n_fft, sampling_rate, hop_size, win_size, center=False): |
| 52 | if torch.min(y) < -1.0: |
| 53 | print("min value is ", torch.min(y)) |
| 54 | if torch.max(y) > 1.0: |
| 55 | print("max value is ", torch.max(y)) |
| 56 | |
| 57 | global hann_window |
| 58 | dtype_device = str(y.dtype) + "_" + str(y.device) |
| 59 | wnsize_dtype_device = str(win_size) + "_" + dtype_device |
| 60 | if wnsize_dtype_device not in hann_window: |
| 61 | hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to( |
| 62 | dtype=y.dtype, device=y.device |
| 63 | ) |
| 64 | |
| 65 | y = torch.nn.functional.pad( |
| 66 | y.unsqueeze(1), |
| 67 | (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), |
| 68 | mode="reflect", |
| 69 | ) |
| 70 | y = y.squeeze(1) |
| 71 | spec = torch.stft( |
| 72 | y, |
| 73 | n_fft, |
| 74 | hop_length=hop_size, |
| 75 | win_length=win_size, |
| 76 | window=hann_window[wnsize_dtype_device], |
| 77 | center=center, |
| 78 | pad_mode="reflect", |
| 79 | normalized=False, |
| 80 | onesided=True, |
| 81 | return_complex=False, |
| 82 | ) |
| 83 | |
| 84 | spec = torch.sqrt(spec.pow(2).sum(-1) + 1e-6) |
| 85 | return spec |
| 86 | |
| 87 | |
| 88 | def spec_to_mel_torch(spec, n_fft, num_mels, sampling_rate, fmin, fmax): |