File size: 1,583 Bytes
bc50ea0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""CPU regressions for exact H3 PCM boundaries and time-conformance."""

import importlib.util
import pathlib

import torch


ROOT = pathlib.Path(__file__).resolve().parents[1]
SPEC = importlib.util.spec_from_file_location(
    "h3_av_timing_unit", ROOT / "av_timing.py")
timing = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(timing)


def main():
    short = torch.ones((1, 2, 346_400), dtype=torch.float32)
    conformed = timing.conform_waveform_length(
        short, 346_667, "260-frame test")
    assert tuple(conformed.shape) == (1, 2, 346_667)
    assert torch.all(conformed > 0.98)

    long = torch.linspace(-1.0, 1.0, 165_600).reshape(1, 1, -1)
    contracted = timing.conform_waveform_length(
        long, 165_333, "124-frame test")
    assert tuple(contracted.shape) == (1, 1, 165_333)
    assert torch.isfinite(contracted).all()

    try:
        timing.conform_waveform_length(
            torch.ones((1, 1, 100)), 200, "bad wiring")
    except ValueError as exc:
        assert "too large" in str(exc)
    else:
        raise AssertionError("large audio mismatch was silently conformed")

    start = 0.3 / 32_000.0
    first = timing.sample_boundary_from_seconds(start, 32_000)
    last = timing.sample_boundary_from_seconds(start + 124 / 24.0, 32_000)
    assert first == 0
    assert last - first == 165_334
    assert timing.sample_boundary_from_frames(124, 32_000) == 165_333

    print("H3 AV timing: small grid drift is conformed without silence; absolute boundaries are exact")


if __name__ == "__main__":
    main()