Datasets:

Formats:
json
Size:
< 1K
Libraries:
Datasets
pandas
License:
File size: 2,409 Bytes
c650031
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, json, wave

def load_jsonl(file_path):
    with open(file_path, 'r') as file:
        d = json.load(file)
    return d

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_emo_to_jsonl(output_dir="data/EMO"):
    metadata = load_jsonl(os.path.join(output_dir, "emomusic/meta.json"))
    train, val, test = [], [], []
    for uid, info in metadata.items():
        audio_path = os.path.join(output_dir, "emomusic", "wav", f"{uid}.wav")
        split = info['split']
        label = info['y']
        try:
            duration, sr, num_samples, bit_depth, channels = get_audio_info(audio_path)
        except Exception as e:
            print(f"Error reading {audio_path}: {e}")
            continue
        o = {
            "audio_path": audio_path,
            "label": label,
            "duration": duration,
            "sample_rate": sr,
            "num_samples": num_samples,
            "bit_depth": bit_depth,
            "channels": channels
        }
        if split == "train":
            train.append(o)
        elif split == "valid":
            val.append(o)
        elif split == "test":
            test.append(o)
        else:
            print(f"Unknown split {split} for {uid}")
    os.makedirs(output_dir, exist_ok=True)
    write_jsonl(train, os.path.join(output_dir, "EMO.train.jsonl"))
    write_jsonl(val, os.path.join(output_dir, "EMO.val.jsonl"))
    write_jsonl(test, os.path.join(output_dir, "EMO.test.jsonl"))
    print(f"train: {len(train)}, val: {len(val)}, test: {len(test)}")
    print("Conversion completed.")

if __name__ == "__main__":
    convert_emo_to_jsonl("data/EMO")
    print("EMO dataset conversion to JSONL completed.")