Skip to content

Check it yourself

stepmaniay-verification Public

Tests anyone can run against StepManiaY's published builds and ITGmania 1.3.0, the release it grew from.

Clone

Clone with HTTPS

Plain git over HTTPS: no account, no app, nothing else to install.

Download the source tarball 3.0 MB

67 lines · 2.3 KB

Raw
"""The timing test's song: StepManiaY's own 653-note test chart (Timing Test.ssc: BPM changes,
four stops, a delay, 16th and 32nd jacks, 24th and 32nd streams, jumps, holds, rolls and mines)
with a click on every beat of its own timing as the music, made here, so the kit carries no one
else's audio.

    python3 make_song.py <Songs folder>

writes <Songs folder>/SMY Timing/Timing Test/.
"""

from __future__ import annotations

import math
import re
import struct
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
GROUP, FOLDER = "SMY Timing", "Timing Test"
RATE = 44100


def pairs(ssc: str, tag: str) -> list[tuple[float, float]]:
    m = re.search(rf"^#{tag}:([^;]*);", ssc, re.M)
    return [tuple(map(float, p.split("="))) for p in m.group(1).split(",") if "=" in p] if m else []


def seconds_at(beat: float, bpms, stops, delays) -> float:
    t = 0.0
    for i, (start, bpm) in enumerate(bpms):
        end = bpms[i + 1][0] if i + 1 < len(bpms) else math.inf
        if beat <= start:
            break
        t += (min(beat, end) - start) * 60.0 / bpm
    t += sum(s for b, s in stops if b < beat)
    t += sum(d for b, d in delays if b <= beat)
    return t


def click_track(ssc: str, beats: int) -> bytes:
    bpms, stops, delays = pairs(ssc, "BPMS"), pairs(ssc, "STOPS"), pairs(ssc, "DELAYS")
    clicks = [seconds_at(b, bpms, stops, delays) for b in range(beats + 1)]
    frames = int((clicks[-1] + 3.0) * RATE)
    pcm = bytearray(frames * 4)
    for at in clicks:
        start = int(at * RATE)
        for i in range(int(0.005 * RATE)):
            v = int(12000 * math.sin(2 * math.pi * 1000 * i / RATE))
            struct.pack_into("<hh", pcm, (start + i) * 4, v, v)
    fmt = struct.pack("<HHIIHH", 1, 2, RATE, RATE * 4, 4, 16)
    body = b"WAVE" + b"fmt " + struct.pack("<I", len(fmt)) + fmt + b"data" + struct.pack("<I", len(pcm)) + bytes(pcm)
    return b"RIFF" + struct.pack("<I", len(body)) + body


def write(songs: Path) -> Path:
    folder = songs / GROUP / FOLDER
    folder.mkdir(parents=True, exist_ok=True)
    ssc = (HERE / "Timing Test.ssc").read_text()
    measures = ssc.split("#NOTES:", 1)[1].split(";", 1)[0].count(",") + 1
    (folder / "Timing Test.ssc").write_text(ssc)
    (folder / "music.wav").write_bytes(click_track(ssc, measures * 4))
    return folder


if __name__ == "__main__":
    print(write(Path(sys.argv[1])))