File size: 3,627 Bytes
e9f552f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import soundfile as sf
from tqdm import tqdm

def create_hf_metafile(dataset_type, base_data_path, output_meta_path):
    subset_path = os.path.join(base_data_path, dataset_type)  # Path to the train or test directory
    prompts_file = os.path.join(subset_path, "prompts.txt")
    waves_base_dir = os.path.join(subset_path, "waves")

    if not os.path.exists(prompts_file):
        print(f"Error: Cannot find file {prompts_file}")
        return
    if not os.path.exists(waves_base_dir):
        print(f"Error: Cannot find directory {waves_base_dir}")
        return

    print(f"Processing {dataset_type}...")

    # Read prompts.txt
    prompt_data = {}
    with open(prompts_file, "r", encoding="utf-8") as pf:
        for line in pf:
            try:
                parts = line.strip().split(" ", 1)
                if len(parts) == 2:
                    file_id, transcription = parts
                    prompt_data[file_id] = transcription.upper()  # Convert to uppercase for consistency
            except ValueError:
                print(f"Ignoring line with incorrect format in {prompts_file}: {line.strip()}")
                continue

    with open(output_meta_path, "w", encoding="utf-8") as meta_f:
        # Iterate through speaker directories in waves_base_dir
        for speaker_dir in tqdm(os.listdir(waves_base_dir)):
            speaker_path = os.path.join(waves_base_dir, speaker_dir)
            if os.path.isdir(speaker_path):
                for wav_filename in os.listdir(speaker_path):
                    if wav_filename.endswith(".wav"):
                        file_id_without_ext = os.path.splitext(wav_filename)[0]

                        if file_id_without_ext in prompt_data:
                            transcription = prompt_data[file_id_without_ext]
                            full_wav_path = os.path.join(speaker_path, wav_filename)

                            try:
                                frames = sf.SoundFile(full_wav_path).frames
                                samplerate = sf.SoundFile(full_wav_path).samplerate
                                duration = frames / samplerate
                            except Exception as e:
                                print(f"Error reading file {full_wav_path}: {e}. Skipping.")
                                continue

                            # Create relative path for Hugging Face Hub
                            # Example: train/waves/SPEAKER01/SPEAKER01_001.wav
                            relative_path = os.path.join(dataset_type, "waves", speaker_dir, wav_filename).replace(os.sep, '/')

                            meta_f.write(f"{relative_path}|{transcription}|{duration:.4f}\n")
                        # else:
                            # print(f"No transcription found for {file_id_without_ext} in {dataset_type}")
    print(f"Meta file created: {output_meta_path}")

if __name__ == "__main__":
    current_script_dir = os.path.dirname(os.path.abspath(__file__))  # Directory containing this script

    # Generate meta file for training set
    create_hf_metafile(
        dataset_type="train",
        base_data_path=current_script_dir,  # Root directory of the dataset is the current directory
        output_meta_path=os.path.join(current_script_dir, "train_meta.txt")
    )

    # Generate meta file for test set
    create_hf_metafile(
        dataset_type="test",
        base_data_path=current_script_dir,  # Root directory of the dataset is the current directory
        output_meta_path=os.path.join(current_script_dir, "test_meta.txt")
    )
    print("Meta file creation completed.")