From cf0e497e039938c7575f8a853ceeb1dec25318bd Mon Sep 17 00:00:00 2001 From: Soham Date: Sun, 1 Oct 2023 10:29:41 -0700 Subject: [PATCH] Resampler update --- src/CLAPWrapper.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CLAPWrapper.py b/src/CLAPWrapper.py index d20a5ed..df9c79f 100644 --- a/src/CLAPWrapper.py +++ b/src/CLAPWrapper.py @@ -202,21 +202,21 @@ class CLAPWrapper(): raise TypeError(self.default_collate_err_msg_format.format(elem_type)) - def read_audio(self, audio_path, resample=False): + def read_audio(self, audio_path, resample=True): r"""Loads audio file or array and returns a torch tensor""" # Randomly sample a segment of audio_duration from the clip or pad to match duration audio_time_series, sample_rate = torchaudio.load(audio_path) resample_rate = self.args.sampling_rate - if resample: + if resample and resample_rate != sample_rate: resampler = T.Resample(sample_rate, resample_rate) audio_time_series = resampler(audio_time_series) - return audio_time_series, sample_rate + return audio_time_series, resample_rate def load_audio_into_tensor(self, audio_path, audio_duration, resample=False): r"""Loads audio file and returns raw audio.""" # Randomly sample a segment of audio_duration from the clip or pad to match duration - audio_time_series, sample_rate = self.read_audio(audio_path, resample=False) + audio_time_series, sample_rate = self.read_audio(audio_path, resample=resample) audio_time_series = audio_time_series.reshape(-1) # audio_time_series is shorter than predefined audio duration, @@ -266,7 +266,7 @@ class CLAPWrapper(): preprocessed_text = self.preprocess_text(class_labels) return self._get_text_embeddings(preprocessed_text) - def get_audio_embeddings(self, audio_files, resample): + def get_audio_embeddings(self, audio_files, resample=True): r"""Load list of audio files and return a audio embeddings""" preprocessed_audio = self.preprocess_audio(audio_files, resample) return self._get_audio_embeddings(preprocessed_audio) @@ -405,4 +405,4 @@ class CLAPWrapper(): output_texts = [self.tokenizer.decode(output[:int(length)]) for output, length in zip(output_list, seq_lengths)] order = scores.argsort(descending=True) output_texts = [output_texts[i] for i in order] - return output_texts \ No newline at end of file + return output_texts