aboutsummaryrefslogtreecommitdiff
path: root/tests/test_cli.py
blob: 858f43999e505ed64ad06f383ba69ff96921b556 (plain) (blame)
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
"""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()