Load audio data and return processed audio tensor Args: prompt_audio: Can be in the following formats: - String: audio file path - Tuple: (wav, sr) result from torchaudio.load - Dict: {"speaker1": path_or_tuple, "speaker2": path_or_tuple}
(prompt_audio, target_sample_rate=16000)
| 176 | |
| 177 | |
| 178 | def load_audio_data(prompt_audio, target_sample_rate=16000): |
| 179 | """Load audio data and return processed audio tensor |
| 180 | |
| 181 | Args: |
| 182 | prompt_audio: Can be in the following formats: |
| 183 | - String: audio file path |
| 184 | - Tuple: (wav, sr) result from torchaudio.load |
| 185 | - Dict: {"speaker1": path_or_tuple, "speaker2": path_or_tuple} |
| 186 | """ |
| 187 | if prompt_audio is None: |
| 188 | return None |
| 189 | |
| 190 | try: |
| 191 | # Check if prompt_audio is a dictionary (containing speaker1 and speaker2) |
| 192 | if ( |
| 193 | isinstance(prompt_audio, dict) |
| 194 | and "speaker1" in prompt_audio |
| 195 | and "speaker2" in prompt_audio |
| 196 | ): |
| 197 | # Process audio from both speakers separately |
| 198 | wav1, sr1 = _load_single_audio(prompt_audio["speaker1"]) |
| 199 | wav2, sr2 = _load_single_audio(prompt_audio["speaker2"]) |
| 200 | # Merge audio from both speakers |
| 201 | wav = merge_speaker_audios(wav1, sr1, wav2, sr2, target_sample_rate) |
| 202 | if wav is None: |
| 203 | return None |
| 204 | else: |
| 205 | # Single audio |
| 206 | wav, sr = _load_single_audio(prompt_audio) |
| 207 | # Resample to 16k |
| 208 | if sr != target_sample_rate: |
| 209 | wav = torchaudio.functional.resample(wav, sr, target_sample_rate) |
| 210 | # Ensure mono channel |
| 211 | if wav.shape[0] > 1: |
| 212 | wav = wav.mean(dim=0, keepdim=True) # Convert multi-channel to mono |
| 213 | if len(wav.shape) == 1: |
| 214 | wav = wav.unsqueeze(0) |
| 215 | |
| 216 | return wav |
| 217 | except Exception as e: |
| 218 | print(f"Error loading audio data: {e}") |
| 219 | raise |
| 220 | |
| 221 | |
| 222 | def _load_single_audio(audio_input): |
no test coverage detected