-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tap_engine.py
More file actions
65 lines (53 loc) · 2.36 KB
/
Copy pathtest_tap_engine.py
File metadata and controls
65 lines (53 loc) · 2.36 KB
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
import unittest
import numpy as np
from tap_engine import (
IGNORE_LABEL,
TapClassifier,
TapListener,
candidate_quality,
extract_features,
feature_size,
)
def synthetic_tap(channel: int = 0) -> np.ndarray:
rate = 44_100
audio = np.zeros((int(rate * 0.32), 4), dtype=np.float32)
start = 650
length = 1800
t = np.arange(length) / rate
ring = np.sin(2 * np.pi * (800 + channel * 550) * t) * np.exp(-t * 70)
for index in range(4):
delay = abs(index - channel) * 3
scale = 1.0 if index == channel else 0.35
audio[start + delay : start + delay + length, index] = ring * scale
return audio
class TapEngineTests(unittest.TestCase):
def test_multichannel_features_are_fixed_and_finite(self):
feature = extract_features(synthetic_tap())
self.assertEqual(feature.shape, (feature_size(),))
self.assertTrue(np.all(np.isfinite(feature)))
def test_array_features_change_with_tap_side(self):
left = extract_features(synthetic_tap(0))
right = extract_features(synthetic_tap(3))
self.assertGreater(float(np.linalg.norm(left - right)), 0.1)
def test_short_decaying_impact_has_quality(self):
quality, metrics = candidate_quality(synthetic_tap())
self.assertGreater(quality, 0.35)
self.assertLess(metrics["duration_ms"], 200)
def test_classifier_selects_zone_and_ignore_class(self):
rng = np.random.default_rng(7)
left = extract_features(synthetic_tap(0))
right = extract_features(synthetic_tap(3))
ignored = np.ones(feature_size(), dtype=np.float32) * 0.45
samples = {
"left": [(left + rng.normal(0, 0.002, feature_size())).tolist() for _ in range(8)],
"right": [(right + rng.normal(0, 0.002, feature_size())).tolist() for _ in range(8)],
IGNORE_LABEL: [(ignored + rng.normal(0, 0.002, feature_size())).tolist() for _ in range(8)],
}
model = TapClassifier(samples)
self.assertEqual(model.predict(right).zone_id, "right")
self.assertEqual(model.predict(ignored).zone_id, IGNORE_LABEL)
def test_listener_clamps_sensitivity(self):
self.assertEqual(TapListener(lambda *_: None, sensitivity=99).sensitivity, 10)
self.assertEqual(TapListener(lambda *_: None, sensitivity=-1).sensitivity, 1)
if __name__ == "__main__":
unittest.main()