| | import torchaudio |
| | import torch |
| | import os |
| | from datasets import load_dataset |
| | import numpy as np |
| |
|
| | |
| | def convert_audio_sample_to_wav(audio_data, sample_rate, output_file): |
| | """ |
| | Converts a single audio sample (tensor) to a WAV file. |
| | |
| | Parameters: |
| | - audio_data (torch.Tensor): The audio data as a tensor. |
| | - sample_rate (int): The sample rate of the audio data. |
| | - output_file (str): The path to save the output WAV file. |
| | """ |
| | try: |
| | |
| | audio_tensor = audio_data / torch.max(torch.abs(audio_data)) |
| | audio_tensor = audio_tensor * 32767 |
| | audio_tensor = audio_tensor.short() |
| |
|
| | |
| | if audio_tensor.ndim == 1: |
| | audio_tensor = audio_tensor.unsqueeze(0) |
| |
|
| | |
| | torchaudio.save(output_file, audio_tensor, sample_rate) |
| |
|
| | print(f"Saved {output_file}") |
| |
|
| | except Exception as e: |
| | print(f"Error saving {output_file}: {e}") |
| |
|
| | |
| | if __name__ == "__main__": |
| | |
| | dataset_path = "/home/vikrant/Conversational-AI-Model/embedding_vocoder/English_Accent_DataSet_Local/westbrook___english_accent_data_set/default/0.0.0" |
| | split = "train" |
| |
|
| | |
| | try: |
| | dataset = load_dataset("arrow", data_dir=os.path.join(dataset_path, split), split=split) |
| | except Exception as e: |
| | print(f"Error loading dataset: {e}") |
| | exit() |
| |
|
| | |
| | output_wav_dir = "output_wavs" |
| |
|
| | |
| | if not os.path.exists(output_wav_dir): |
| | os.makedirs(output_wav_dir) |
| |
|
| | |
| | for index, example in enumerate(dataset): |
| | try: |
| | |
| | audio_id = example['audio_id'] |
| | audio_data = torch.tensor(example['audio']['array']).float() |
| | sample_rate = example['audio']['sampling_rate'] |
| |
|
| | |
| | output_file = os.path.join(output_wav_dir, f"{audio_id}.wav") |
| |
|
| | |
| | convert_audio_sample_to_wav(audio_data, sample_rate, output_file) |
| |
|
| | except Exception as e: |
| | print(f"Error processing example {index}: {e}") |
| |
|
| | print("Conversion complete.") |
| |
|