Datasets:

Modalities:
Audio
Image
License:
File size: 2,734 Bytes
67998a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import wave

def load_txt(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
    return [line.strip() for line in lines]

def write_jsonl(data, output_file):
    with open(output_file, 'w') as file:
        for item in data:
            json.dump(item, file)
            file.write('\n')

def get_audio_info(audio_path):
    """
    Extract duration (seconds), sample_rate (Hz), num_samples (int), bit_depth (bits), and channels (int) from a WAV file.
    """
    with wave.open(audio_path, 'rb') as wf:
        sample_rate = wf.getframerate()
        num_samples = wf.getnframes()
        duration = num_samples / float(sample_rate)
        sample_width = wf.getsampwidth()  # in bytes
        bit_depth = sample_width * 8      # convert to bits
        channels = wf.getnchannels()     # number of audio channels
    return duration, sample_rate, num_samples, bit_depth, channels

def convert_gtzan_to_jsonl(output_dir):
    # Paths to filtered file lists
    train_txt = os.path.join(output_dir, "train_filtered.txt")
    val_txt   = os.path.join(output_dir, "valid_filtered.txt")
    test_txt  = os.path.join(output_dir, "test_filtered.txt")

    train_list = load_txt(train_txt)
    val_list   = load_txt(val_txt)
    test_list  = load_txt(test_txt)

    train_jsonl, val_jsonl, test_jsonl = [], [], []
    prefix = os.path.join(output_dir, "genres")

    # Process each split and append metadata
    for split_list, out_list in [(train_list, train_jsonl), (val_list, val_jsonl), (test_list, test_jsonl)]:
        for rel_path in split_list:
            path = os.path.join(prefix, rel_path)
            label = rel_path.split(os.sep)[0]
            try:
                duration, sr, num_samples, bit_depth, channels = get_audio_info(path)
            except Exception as e:
                print(f"Error reading {path}: {e}")
                continue
            out_list.append({
                "audio_path": path,
                "label": label,
                "duration": duration,
                "sample_rate": sr,
                "num_samples": num_samples,
                "bit_depth": bit_depth,
                "channels": channels
            })

    # Ensure output directory exists
    os.makedirs(output_dir, exist_ok=True)
    write_jsonl(train_jsonl, os.path.join(output_dir, "GTZANGenre.train.jsonl"))
    write_jsonl(val_jsonl,   os.path.join(output_dir, "GTZANGenre.val.jsonl"))
    write_jsonl(test_jsonl,  os.path.join(output_dir, "GTZANGenre.test.jsonl"))

    print(f"train: {len(train_jsonl)}, val: {len(val_jsonl)}, test: {len(test_jsonl)}")
    print("Conversion completed.")

if __name__ == "__main__":
    convert_gtzan_to_jsonl("data/GTZAN")