MCPcopy Create free account
hub / github.com/FastFlowLM/FastFlowLM / load_audio_torchcodec

Function load_audio_torchcodec

src/common/AutoModel/audio_utils.py:91–112  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

89
90
91def 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
115def load_audio_librosa(audio: str | np.ndarray, sampling_rate=16000, timeout=None) -> np.ndarray:

Callers 1

load_audioFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected