#!/usr/bin/env python3
"""Generate the public benchmark library: synthetic audio, made-up artists and albums, generated cover art.

    make_library.py OUT_DIR [--seed N] [--albums N] [--jobs N] [--manifest FILE]

Everything comes from a fixed seed and ffmpeg (sine tones plus seeded pink noise; gradient cover images), so
there is nothing copyrighted in it and anyone can rebuild it. The default produces about 3,100 tracks in
374 albums, about 30 GB: roughly 83% MP3, 17% FLAC, a few M4A, tracks of 3 to 6 minutes, tags (artist,
album artist, album, title, track, disc, genre, year), multi-artist albums, "feat." artists and an embedded
cover in every file, plus one 63-minute FLAC (album "Long Form Drone") for the seek test.

The plan (names, tags, lengths, formats) depends only on --seed and the script, and is the same everywhere.
The audio bytes also depend on the ffmpeg build; checksums.sha256 records the bytes produced with
the ffmpeg named in plan.json. Check a rebuild with `cd OUT_DIR && sha256sum -c checksums.sha256`;
if your ffmpeg differs the sizes and tags still match but some checksums may not.
Only the standard library and ffmpeg are used.
"""
import argparse, concurrent.futures as cf, hashlib, json, os, random, subprocess, sys, time

ADJ = ["Velvet", "Silent", "Golden", "Hollow", "Neon", "Paper", "Crimson", "Quiet", "Electric", "Distant", "Glass",
       "Midnight", "Amber", "Blue", "Black", "Dark", "Bright", "Lost", "Wild", "Slow", "Northern", "Cold", "Burning",
       "Pale", "Open", "Broken", "Late", "Young", "Sunken", "Faded"]
NOUN = ["Harbor", "Engine", "Garden", "Signal", "Orchard", "Comet", "River", "Lantern", "Static", "Meadow", "Fire",
        "Star", "Sun", "Night", "Dream", "Love", "Echo", "Tide", "Atlas", "Mirror", "Canyon", "Station", "Parade",
        "Weather", "Anthem", "Circuit", "Pattern", "Season", "Ghost", "Island"]
GIVEN = ["Ada", "Marek", "Ines", "Oskar", "Lena", "Tomas", "Noor", "Elio", "Mara", "Jonas", "Yuki", "Sana", "Ravi",
         "Clara", "Teo", "Mina", "Hugo", "Isla", "Anton", "Zoe", "Rafi", "Greta", "Nico", "Lior", "Petra"]
SURN = ["Lindqvist", "Okafor", "Marlow", "Vance", "Haddad", "Brandt", "Castell", "Duarte", "Eklund", "Fontaine",
        "Grau", "Holloway", "Ito", "Jovanovic", "Keane", "Lund", "Moreau", "Novak", "Ortega", "Petrov"]
ACCENT = ["Björn Åkesson", "Zoë Müller", "Renée Dubois", "Søren Vik", "Łukasz Nowak", "Çağla Demir", "夜行 ネオン", "星屑 ラジオ"]
WORDS = ["love", "night", "the", "black", "dark", "sun", "fire", "blue", "star", "dream", "light", "road", "rain",
         "home", "city", "ocean", "moon", "wind", "gold", "heart", "summer", "winter", "again", "forever", "silver",
         "morning", "shadow", "radio", "paper", "glass", "river", "stone", "ghost", "garden", "machine", "letters"]
GENRES = ["Rock", "Pop", "Electronic", "Ambient", "Hip-Hop", "Jazz", "Folk", "Metal", "Classical", "Soul", "Punk", "Techno"]
TRACK_MP3_KBPS = [(128, 30), (192, 40), (256, 20), (320, 10)]


def rng_for(*parts):
    h = hashlib.sha256("|".join(str(p) for p in parts).encode()).digest()
    return random.Random(int.from_bytes(h[:8], "big"))


def title(r, n=None):
    n = n or r.choice([1, 2, 2, 3])
    ws = [r.choice(WORDS) for _ in range(n)]
    return " ".join(w.capitalize() if i == 0 or w not in ("the", "again") else w for i, w in enumerate(ws))


def artist_name(r):
    k = r.random()
    if k < 0.06:
        return r.choice(ACCENT)
    if k < 0.45:
        return f"{r.choice(GIVEN)} {r.choice(SURN)}"
    return f"{r.choice(ADJ)} {r.choice(NOUN)}"


def safe(s):
    return "".join(c for c in s if c not in '/\\:*?"<>|').strip()


def plan(seed, n_albums):
    r = random.Random(seed)
    n_art = max(4, n_albums // 3)
    artists, seen = [], set()
    while len(artists) < n_art:
        a = artist_name(r)
        if a not in seen:
            seen.add(a)
            artists.append(a)
    n_flac = round(n_albums * 0.167)
    n_m4a = 2
    kinds = ["flac"] * n_flac + ["m4a"] * n_m4a + ["mp3"] * (n_albums - n_flac - n_m4a - 1)
    r.shuffle(kinds)
    albums = []
    for i, kind in enumerate(kinds):
        ar = rng_for(seed, "album", i)
        comp = ar.random() < 0.12
        main = "Various Artists" if comp else ar.choice(artists)
        year = ar.randint(1985, 2025)
        name = title(ar, ar.choice([1, 2, 2, 3]))
        ntr = 10 if kind == "m4a" else ar.choice([5, 6, 7, 7, 8, 8, 9, 9, 10, 11, 12])
        tracks = []
        for t in range(ntr):
            tr = rng_for(seed, "track", i, t)
            a = ar.choice(artists) if comp else main
            tt = title(tr)
            if tr.random() < 0.15:
                tt += f" (feat. {tr.choice(artists)})"
            if not comp and tr.random() < 0.05:
                a = f"{main} & {tr.choice(artists)}"
            tracks.append({"n": t + 1, "title": tt, "artist": a, "seconds": tr.randint(180, 360),
                           "f1": tr.choice([196, 220, 247, 262, 294, 330, 392, 440, 494, 523]),
                           "f2": tr.choice([147, 165, 175, 196, 220, 247]), "noise": tr.randint(1, 10 ** 6)})
        kbps = None
        if kind == "mp3":
            kbps = ar.choices([k for k, _ in TRACK_MP3_KBPS], [w for _, w in TRACK_MP3_KBPS])[0]
        albums.append({"id": i, "kind": kind, "album": name, "album_artist": main, "year": year,
                       "genre": ar.choice(GENRES), "kbps": kbps, "compilation": comp, "tracks": tracks})
    # one album holding the 63-minute FLAC
    albums.append({"id": n_albums, "kind": "flac", "album": "Long Form Drone", "album_artist": "Test Tone Collective",
                   "year": 2003, "genre": "Ambient", "kbps": None, "compilation": False,
                   "tracks": [{"n": 1, "title": "Long Form Drone", "artist": "Test Tone Collective", "seconds": 3780,
                               "f1": 110, "f2": 165, "noise": 4242}]})
    # make album directories unique
    used = {}
    for a in albums:
        key = (safe(a["album_artist"]), a["year"], safe(a["album"]))
        used[key] = used.get(key, 0) + 1
        a["dir"] = f"{safe(a['album_artist'])}/[{a['year']}] {safe(a['album'])}" + (f" ({used[key]})" if used[key] > 1 else "")
    return albums


def ffmpeg(*args):
    r = subprocess.run(["nice", "-n", "10", "ffmpeg", "-v", "error", "-y", "-bitexact", "-fflags", "+bitexact", *args],
                       capture_output=True, text=True)
    if r.returncode:
        raise RuntimeError(r.stderr[:400])


def make_cover(path, seed):
    r = rng_for(seed, "cover")
    cols = ["0x%06x" % r.randrange(0x1000000) for _ in range(3)]
    ffmpeg("-f", "lavfi", "-i", f"gradients=s=500x500:c0={cols[0]}:c1={cols[1]}:c2={cols[2]}:n=3:seed={seed % 100000}:speed=0:d=1",
           "-frames:v", "1", "-q:v", "4", path)


def make_track(out_root, cover, album, t, total, seed):
    ext = {"flac": "flac", "mp3": "mp3", "m4a": "m4a"}[album["kind"]]
    path = os.path.join(out_root, album["dir"], f"{t['n']:02d} - {safe(t['title'])}.{ext}")
    os.makedirs(os.path.dirname(path), exist_ok=True)
    d = t["seconds"]
    src = ["-f", "lavfi", "-i", f"sine=f={t['f1']}:r=44100:d={d}",
           "-f", "lavfi", "-i", f"sine=f={t['f2']}:r=44100:d={d}",
           "-f", "lavfi", "-i", f"anoisesrc=color=pink:amplitude=0.015:seed={t['noise']}:r=44100:d={d}",
           "-i", cover]
    fc = "[0][1][2]amix=inputs=3:normalize=0,aformat=channel_layouts=stereo"
    if d > 3000:
        # the long file swells and fades (78 s period) so the seek check can tell offsets apart by loudness
        fc += ",volume='0.15+0.85*abs(sin(PI*t/78))':eval=frame"
    fc += "[a]"
    meta = ["-metadata", f"title={t['title']}", "-metadata", f"artist={t['artist']}", "-metadata", f"album={album['album']}",
            "-metadata", f"album_artist={album['album_artist']}", "-metadata", f"track={t['n']}/{total}",
            "-metadata", "disc=1/1", "-metadata", f"genre={album['genre']}", "-metadata", f"date={album['year']}"]
    if album["compilation"]:
        meta += ["-metadata", "compilation=1"]
    mapping = ["-filter_complex", fc, "-map", "[a]", "-map", "3:v"]
    if album["kind"] == "mp3":
        codec = ["-c:a", "libmp3lame", "-b:a", f"{album['kbps']}k", "-c:v", "copy", "-id3v2_version", "3",
                 "-metadata:s:v", "title=Cover", "-metadata:s:v", "comment=Cover (front)"]
    elif album["kind"] == "flac":
        codec = ["-c:a", "flac", "-compression_level", "5", "-c:v", "copy", "-disposition:v", "attached_pic"]
    else:
        codec = ["-c:a", "aac", "-b:a", "192k", "-c:v", "copy", "-disposition:v", "attached_pic"]
    ffmpeg(*src, *mapping, *meta, *codec, "-bitexact", "-fflags", "+bitexact", path)
    return path


def sha256(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for b in iter(lambda: f.read(1 << 20), b""):
            h.update(b)
    return h.hexdigest()


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("out")
    ap.add_argument("--seed", type=int, default=20261113)
    ap.add_argument("--albums", type=int, default=374, help="number of albums, including the one holding the long FLAC")
    ap.add_argument("--jobs", type=int, default=4)
    ap.add_argument("--manifest", default=None, help="checksum file (default OUT/checksums.sha256)")
    a = ap.parse_args()
    out = os.path.abspath(a.out)
    os.makedirs(out, exist_ok=True)
    albums = plan(a.seed, a.albums)
    ntr = sum(len(x["tracks"]) for x in albums)
    print(f"{len(albums)} albums, {ntr} tracks -> {out}", flush=True)
    cover_dir = os.path.join(out, ".covers")
    os.makedirs(cover_dir, exist_ok=True)
    jobs, t0 = [], time.time()
    with cf.ThreadPoolExecutor(a.jobs) as ex:
        covers = {al["id"]: ex.submit(make_cover, os.path.join(cover_dir, f"{al['id']}.jpg"), a.seed * 1000 + al["id"]) for al in albums}
        for f in covers.values():
            f.result()
        for al in albums:
            for t in al["tracks"]:
                jobs.append(ex.submit(make_track, out, os.path.join(cover_dir, f"{al['id']}.jpg"), al, t, len(al["tracks"]), a.seed))
        for i, j in enumerate(jobs):
            j.result()
            if i % 200 == 0:
                print(f"  {i}/{len(jobs)} tracks, {time.time() - t0:.0f}s", flush=True)
    for f in os.listdir(cover_dir):
        os.remove(os.path.join(cover_dir, f))
    os.rmdir(cover_dir)
    files = sorted(os.path.relpath(os.path.join(d, f), out) for d, _, fs in os.walk(out) for f in fs
                   if f not in ("checksums.sha256", "plan.json"))
    with cf.ThreadPoolExecutor(a.jobs) as ex:
        sums = list(ex.map(lambda p: sha256(os.path.join(out, p)), files))
    man = a.manifest or os.path.join(out, "checksums.sha256")
    with open(man, "w") as f:
        for h, p in zip(sums, files):
            f.write(f"{h}  {p}\n")
    sizes = [os.path.getsize(os.path.join(out, p)) for p in files]
    by = {}
    for p, s in zip(files, sizes):
        e = p.rsplit(".", 1)[-1]
        c, b = by.get(e, (0, 0))
        by[e] = (c + 1, b + s)
    ff = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True).stdout.splitlines()[0]
    summary = {"seed": a.seed, "albums": len(albums), "tracks": ntr, "files": len(files), "total_bytes": sum(sizes),
               "by_format": {k: {"files": c, "bytes": b} for k, (c, b) in sorted(by.items())}, "ffmpeg": ff,
               "long_flac": "Long Form Drone", "seconds": round(time.time() - t0)}
    json.dump(summary, open(os.path.join(os.path.dirname(man) if a.manifest else out, "plan.json"), "w"), indent=1)
    print(json.dumps(summary, indent=1))


if __name__ == "__main__":
    sys.exit(main())
