| | |
| | | """ |
| | | if torch.is_tensor(array): |
| | | if array.shape[axis] > length: |
| | | array = array.index_select( |
| | | dim=axis, index=torch.arange(length, device=array.device) |
| | | ) |
| | | array = array.index_select(dim=axis, index=torch.arange(length, device=array.device)) |
| | | |
| | | if array.shape[axis] < length: |
| | | pad_widths = [(0, 0)] * array.ndim |
| | |
| | | |
| | | |
| | | @lru_cache(maxsize=None) |
| | | def mel_filters(device, n_mels: int) -> torch.Tensor: |
| | | def mel_filters(device, n_mels: int, filters_path: str = None) -> torch.Tensor: |
| | | """ |
| | | load the mel filterbank matrix for projecting STFT into a Mel spectrogram. |
| | | Allows decoupling librosa dependency; saved using: |
| | |
| | | ) |
| | | """ |
| | | assert n_mels in {80, 128}, f"Unsupported n_mels: {n_mels}" |
| | | |
| | | filters_path = os.path.join(os.path.dirname(__file__), "assets", "mel_filters.npz") |
| | | if filters_path is None: |
| | | filters_path = os.path.join(os.path.dirname(__file__), "assets", "mel_filters.npz") |
| | | with np.load(filters_path, allow_pickle=False) as f: |
| | | return torch.from_numpy(f[f"mel_{n_mels}"]).to(device) |
| | | |