Loads `audio` to an np.ndarray object using `torchcodec`. Args: audio (`str` or `np.ndarray`): The audio to be loaded to the numpy array format. sampling_rate (`int`, *optional*, defaults to 16000): The sampling rate to be used when loading the audio
(audio: str | np.ndarray, sampling_rate=16000)
| 89 | |
| 90 | |
| 91 | def load_audio_torchcodec(audio: str | np.ndarray, sampling_rate=16000) -> np.ndarray: |
| 92 | """ |
| 93 | Loads `audio` to an np.ndarray object using `torchcodec`. |
| 94 | |
| 95 | Args: |
| 96 | audio (`str` or `np.ndarray`): |
| 97 | The audio to be loaded to the numpy array format. |
| 98 | sampling_rate (`int`, *optional*, defaults to 16000): |
| 99 | The sampling rate to be used when loading the audio. It should be same as the |
| 100 | sampling rate the model you will be using further was trained with. |
| 101 | |
| 102 | Returns: |
| 103 | `np.ndarray`: A numpy array representing the audio. |
| 104 | """ |
| 105 | # Lazy import so that issues in torchcodec compatibility don't crash the whole library |
| 106 | requires_backends(load_audio_torchcodec, ["torchcodec"]) |
| 107 | from torchcodec.decoders import AudioDecoder |
| 108 | |
| 109 | # Set `num_channels` to `1` which is what most models expects and the default in librosa |
| 110 | decoder = AudioDecoder(audio, sample_rate=sampling_rate, num_channels=1) |
| 111 | audio = decoder.get_all_samples().data[0].numpy() # NOTE: feature extractors don't accept torch tensors |
| 112 | return audio |
| 113 | |
| 114 | |
| 115 | def load_audio_librosa(audio: str | np.ndarray, sampling_rate=16000, timeout=None) -> np.ndarray: |