MDBDrumsPlusPlus / generate_metadata.py
xavriley's picture
Add files using upload-large-folder tool
4fdb5c5 verified
#!/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))