"""CLI vocabulary, caption/class output, and argument contracts.""" import csv import math from pathlib import Path import struct import subprocess import sys import tempfile import wave WEIGHTS = sys.argv[1] if len(sys.argv) > 1 else "models/framesed_weights.bin" def run(*args, code=0): result = subprocess.run( ["build/framesed", *map(str, args)], capture_output=True, text=True ) if result.returncode != code: raise AssertionError(f"{args}: exit {result.returncode}\n{result.stderr}") return result.stdout def main(): assert "--all-classes" in run("--help") with Path("framesed/classes.csv").open(newline="", encoding="utf-8") as source: rows = list(csv.DictReader(source)) expected = "".join( f"{row['index']}\t{row['mid']}\t{row['display_name']}\n" for row in rows ) assert len(rows) == 447 assert run("--list-classes") == expected assert run("--all-classes", code=2) == "" run("--threads", "0", code=2) run("--track-step", "nan", code=2) run("--unknown-option", code=2) with tempfile.TemporaryDirectory(prefix="framesed-cli-") as directory: path = Path(directory) / "music.wav" samples = [] for i in range(10 * 16000): t = i / 16000 value = sum(math.sin(2 * math.pi * f * t) for f in (261.6, 329.6, 392.0)) / 3 value *= 0.65 + 0.35 * math.sin(2 * math.pi * 4 * t) samples.append(int(value * 0.10 * 32767)) with wave.open(str(path), "wb") as output: output.setparams((1, 2, 16000, 0, "NONE", "not compressed")) output.writeframes(struct.pack(f"<{len(samples)}h", *samples)) caption = run("--weights", WEIGHTS, path) assert any(line.endswith(" 0 music") for line in caption.splitlines()) classes = run("--all-classes", "--track-step", "0.2", "--weights", WEIGHTS, path) events = 0 tracks = 0 music = False for line in classes.splitlines(): kind = line.split(" ", 1)[0] if kind == "event": _, start, end, score, index, label = line.split(" ", 5) assert 0 <= float(start) < float(end) <= 10.0 events += 1 music |= int(index) == 253 else: assert kind == "track" _, time, score, index, label = line.split(" ", 4) assert 0 <= float(time) < 10.0 tracks += 1 assert 0 <= float(score) <= 1 assert 0 <= int(index) < 447 assert label == rows[int(index)]["display_name"] assert events and tracks and music print("framesed CLI: 447 labels, caption/class detection, tracks, arguments passed") if __name__ == "__main__": main()