Loads `audio` to an np.ndarray object. 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. It should be same
(audio: str | np.ndarray, sampling_rate=16000, timeout=None)
| 58 | |
| 59 | |
| 60 | def load_audio(audio: str | np.ndarray, sampling_rate=16000, timeout=None) -> np.ndarray: |
| 61 | """ |
| 62 | Loads `audio` to an np.ndarray object. |
| 63 | |
| 64 | Args: |
| 65 | audio (`str` or `np.ndarray`): |
| 66 | The audio to be loaded to the numpy array format. |
| 67 | sampling_rate (`int`, *optional*, defaults to 16000): |
| 68 | The sampling rate to be used when loading the audio. It should be same as the |
| 69 | sampling rate the model you will be using further was trained with. |
| 70 | timeout (`float`, *optional*): |
| 71 | The timeout value in seconds for the URL request. |
| 72 | |
| 73 | Returns: |
| 74 | `np.ndarray`: A numpy array representing the audio. |
| 75 | """ |
| 76 | if isinstance(audio, str): |
| 77 | # Try to load with `torchcodec` but do not enforce users to install it. If not found |
| 78 | # fallback to `librosa`. If using an audio-only model, most probably `torchcodec` won't be |
| 79 | # needed. Do not raise any errors if not installed or versions do not match |
| 80 | if is_torchcodec_available() and version.parse("0.3.0") <= TORCHCODEC_VERSION: |
| 81 | audio = load_audio_torchcodec(audio, sampling_rate=sampling_rate) |
| 82 | else: |
| 83 | audio = load_audio_librosa(audio, sampling_rate=sampling_rate, timeout=timeout) |
| 84 | elif not isinstance(audio, np.ndarray): |
| 85 | raise TypeError( |
| 86 | "Incorrect format used for `audio`. Should be an url linking to an audio, a local path, or numpy array." |
| 87 | ) |
| 88 | return audio |
| 89 | |
| 90 | |
| 91 | def load_audio_torchcodec(audio: str | np.ndarray, sampling_rate=16000) -> np.ndarray: |
nothing calls this directly
no test coverage detected