File size: 3,791 Bytes
4fdb5c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python3
from __future__ import annotations

import csv
import sys
from pathlib import Path
import wave


def get_wav_info(wav_path: Path) -> tuple[int, int, float]:
    """Return (sample_rate_hz, num_channels, duration_sec) for a WAV file."""
    with wave.open(str(wav_path), "rb") as wf:
        sample_rate = wf.getframerate()
        num_channels = wf.getnchannels()
        num_frames = wf.getnframes()
        duration = float(num_frames) / float(sample_rate) if sample_rate else 0.0
    return sample_rate, num_channels, duration


def normalize_style(style_raw: str) -> str:
    """Normalize style names to canonical forms used by MIDI and Logic sessions."""
    overrides = {
        "BebopJazz": "Bebop",
        "Country1": "Country",
    }
    return overrides.get(style_raw, style_raw)


def main(repo_root: Path) -> int:
    audio_dir = repo_root / "audio"
    midi_dir = repo_root / "annotations" / "midi"
    logic_dir = repo_root / "logic_sessions"

    audio_files = sorted(audio_dir.glob("*.wav"))
    if not audio_files:
        print("No WAV files found in 'audio/'", file=sys.stderr)
        return 1

    csv_path = repo_root / "metadata.csv"
    fieldnames = [
        "track_id",
        "style",
        "split",
        "audio_path",
        "midi_path",
        "logic_project_path",
        "sample_rate_hz",
        "num_channels",
        "duration_sec",
        "license",
        "source",
        "notes",
    ]

    with csv_path.open("w", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()

        for wav_path in audio_files:
            name = wav_path.stem  # e.g., MusicDelta_BebopJazz_Drum
            base = name[:-5] if name.endswith("_Drum") else name
            if base.startswith("MusicDelta_"):
                style_raw = base[len("MusicDelta_") :]
            else:
                style_raw = base

            style = normalize_style(style_raw)
            track_id = f"MusicDelta_{style}"

            # Relative paths for CSV
            audio_rel = wav_path.relative_to(repo_root).as_posix()
            midi_rel = f"annotations/midi/{track_id}_Drum.mid"
            logic_rel = f"logic_sessions/{track_id}.logicx"

            notes: list[str] = []

            # Check MIDI and Logic existence
            midi_exists = (midi_dir / f"{track_id}_Drum.mid").exists()
            logic_exists = (logic_dir / f"{track_id}.logicx").exists()

            if not midi_exists:
                notes.append("Missing MIDI")
                midi_rel = ""
            if not logic_exists:
                notes.append("Missing Logic session")
                logic_rel = ""

            try:
                sample_rate, num_channels, duration = get_wav_info(wav_path)
            except Exception as exc:
                sample_rate, num_channels, duration = 0, 0, 0.0
                notes.append(f"Audio read error: {type(exc).__name__}")

            writer.writerow(
                {
                    "track_id": track_id,
                    "style": style,
                    "split": "full",
                    "audio_path": audio_rel,
                    "midi_path": midi_rel,
                    "logic_project_path": logic_rel,
                    "sample_rate_hz": sample_rate,
                    "num_channels": num_channels,
                    "duration_sec": round(duration, 6),
                    "license": "CC BY-NC-SA 4.0",
                    "source": "MedleyDB subset (MDB Drums++)",
                    "notes": "; ".join(notes),
                }
            )

    print(f"Wrote {csv_path.relative_to(repo_root)} with {len(audio_files)} rows.")
    return 0


if __name__ == "__main__":
    sys.exit(main(Path(__file__).resolve().parent))