Datasets:

License:
File size: 3,509 Bytes
380b711
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
import json
from pydub import AudioSegment
from multiprocessing import Pool, cpu_count
from functools import partial
from tqdm import tqdm

def load_tsv(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
    lines = lines[1:]  # Skip header
    output = []
    for line in lines:
        splitted = line.strip().split('\t')
        output.append({
            "TRACK_ID": splitted[0],
            "ARTIST_ID": splitted[1],
            "ALBUM_ID": splitted[2],
            "PATH": splitted[3],
            "DURATION": float(splitted[4]),
            "TAGS": splitted[5:],
        })
    return output


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 an audio file.
    """
    audio = AudioSegment.from_file(audio_path)
    duration = audio.duration_seconds
    sample_rate = audio.frame_rate
    num_samples = int(audio.frame_count())
    sample_width = audio.sample_width
    bit_depth = sample_width * 8
    channels = audio.channels
    return duration, sample_rate, num_samples, bit_depth, channels


def process_item(tsv_dict, prefix):
    path = os.path.join(prefix, tsv_dict["PATH"]).replace(".mp3", ".low.flac")
    try:
        duration, sr, num_samples, bit_depth, channels = get_audio_info(path)
        return {
            "audio_path": path,
            "label": tsv_dict["TAGS"],
            "duration": duration,
            "sample_rate": sr,
            "num_samples": num_samples,
            "bit_depth": bit_depth,
            "channels": channels
        }
    except Exception as e:
        print(f"Error reading {path}: {e}")
        return None


def convert_mtg_to_jsonl(output_dir, task='MTGTop50'):
    task2tsv = {
        "MTGTop50": "top50tags",
        "MTGGenre": "genre",
        "MTGInstrument": "instrument",
        "MTGMood": "moodtheme",
    }
    tsv_name = task2tsv[task]
    # Paths to filtered file lists
    splits = {
        'train': f"mtg-jamendo-dataset/data/splits/split-0/autotagging_{tsv_name}-train.tsv",
        'val':   f"mtg-jamendo-dataset/data/splits/split-0/autotagging_{tsv_name}-validation.tsv",
        'test':  f"mtg-jamendo-dataset/data/splits/split-0/autotagging_{tsv_name}-test.tsv",
    }
    prefix = os.path.join(output_dir, "audio")

    os.makedirs(output_dir, exist_ok=True)

    for split, rel_path in splits.items():
        tsv_path = os.path.join(output_dir, rel_path)
        tsv_list = load_tsv(tsv_path)
        out_list = []

        # Prepare partial function for multiprocessing
        worker = partial(process_item, prefix=prefix)
        with Pool(processes=cpu_count()) as pool:
            for result in tqdm(pool.imap(worker, tsv_list), total=len(tsv_list), desc=f"Processing {task} {split}"):
                if result:
                    out_list.append(result)

        # Write output
        out_file = os.path.join(output_dir, f"{task}.{split}.jsonl")
        write_jsonl(out_list, out_file)
        print(f"{split} done: {len(out_list)} items written to {out_file}")


if __name__ == "__main__":
    output_base = "data/MTG"
    tasks = ['MTGTop50', 'MTGGenre', 'MTGInstrument', 'MTGMood']
    for task in tasks:
        convert_mtg_to_jsonl(output_base, task)
    print("All tasks completed.")