nx_ts_marks_gate.nx source
↩ module page · 146 lines · 8827 B
1// nx_ts_marks_gate.nx -- unit gate for the dead-air (silence) clip-analysis logic. Tests the pure
2// ts_silence_marks + tm_median with SYNTHETIC audio-byte-per-bin sequences (proves the algorithm; the
3// real-.ts threshold calibration is a separate live step). bin_ms=500 -> minbins = 1500/500 = 3.
4import "nx_syscalls.nx"
5import "nx_ts_marks.nx"
6
7func gp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return sys_write(1, s, n) }
8func gn(v: i64) -> i64 {
9 let bb: *u8 = sys_mmap(28); var m: i64 = v
10 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
11 let t: *u8 = sys_mmap(28); var k: i64 = 0
12 if m == 0 { t[0] = 48 as u8; k = 1 }
13 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
14 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }
15 return sys_write(1, bb, k)
16}
17
18func main(argc: i64, argv: *i64) -> i64 {
19 var pass: i64 = 0
20 let bins: *i64 = sys_mmap(8 * 64) as *i64
21 let mt: *i64 = sys_mmap(8 * 64) as *i64
22 let ms: *i64 = sys_mmap(8 * 64) as *i64
23 let me: *i64 = sys_mmap(8 * 64) as *i64
24 var i: i64 = 0
25
26 // T1: loud x4, SILENT x4, loud x4 -> one dead-air marker [2000,4000], type=1
27 i = 0; while i < 12 { bins[i] = 100; i = i + 1 }
28 bins[4] = 5; bins[5] = 5; bins[6] = 5; bins[7] = 5
29 var n1: i64 = ts_silence_marks(bins, 12, 500, mt, ms, me, 64)
30 if n1 == 1 { if mt[0] == 1 { if ms[0] == 2000 { if me[0] == 4000 { pass = pass + 1; gp("T1 mid dead-air [2000,4000] OK\n" as *u8) } } } }
31 if n1 != 1 { gp("T1 FAIL nm=" as *u8); gn(n1); gp("\n" as *u8) }
32
33 // T2: loud x3, SILENT x2 (<3 bins = normal pause), loud x3 -> ZERO markers
34 i = 0; while i < 8 { bins[i] = 100; i = i + 1 }
35 bins[3] = 5; bins[4] = 5
36 var n2: i64 = ts_silence_marks(bins, 8, 500, mt, ms, me, 64)
37 if n2 == 0 { pass = pass + 1; gp("T2 short pause -> 0 markers OK\n" as *u8) } else { gp("T2 FAIL nm=" as *u8); gn(n2); gp("\n" as *u8) }
38
39 // T3: all loud -> ZERO markers
40 i = 0; while i < 6 { bins[i] = 100; i = i + 1 }
41 var n3: i64 = ts_silence_marks(bins, 6, 500, mt, ms, me, 64)
42 if n3 == 0 { pass = pass + 1; gp("T3 all-loud -> 0 markers OK\n" as *u8) } else { gp("T3 FAIL nm=" as *u8); gn(n3); gp("\n" as *u8) }
43
44 // T4: loud x5, SILENT x3 trailing (majority loud so median stays loud) -> marker [2500,4000], closed at EOF
45 i = 0; while i < 8 { bins[i] = 100; i = i + 1 }
46 bins[5] = 5; bins[6] = 5; bins[7] = 5
47 var n4: i64 = ts_silence_marks(bins, 8, 500, mt, ms, me, 64)
48 if n4 == 1 { if ms[0] == 2500 { if me[0] == 4000 { pass = pass + 1; gp("T4 trailing dead-air [2500,4000] OK\n" as *u8) } } }
49 if n4 != 1 { gp("T4 FAIL nm=" as *u8); gn(n4); gp("\n" as *u8) }
50
51 // T5: median is bitrate-relative -- same silence pattern at 10x bitrate still detected (self-calibration)
52 i = 0; while i < 12 { bins[i] = 1000; i = i + 1 }
53 bins[4] = 50; bins[5] = 50; bins[6] = 50; bins[7] = 50
54 var n5: i64 = ts_silence_marks(bins, 12, 500, mt, ms, me, 64)
55 if n5 == 1 { if ms[0] == 2000 { if me[0] == 4000 { pass = pass + 1; gp("T5 10x-bitrate self-calibrated OK\n" as *u8) } } }
56 if n5 != 1 { gp("T5 FAIL nm=" as *u8); gn(n5); gp("\n" as *u8) }
57
58 // T6: high-motion run (type 3) -- 3 windows above 1.6x median = sustained motion
59 i = 0; while i < 10 { bins[i] = 100; i = i + 1 }
60 bins[5] = 300; bins[6] = 300; bins[7] = 300
61 var n6: i64 = ts_motion_marks(bins, 10, 500, mt, ms, me, 64)
62 if n6 == 1 { if mt[0] == 3 { if ms[0] == 2500 { if me[0] == 4000 { pass = pass + 1; gp("T6 high-motion type3 [2500,4000] OK\n" as *u8) } } } }
63 if n6 != 1 { gp("T6 FAIL nm=" as *u8); gn(n6); gp("\n" as *u8) }
64
65 // T7: scene-cut (type 2) -- a window rising >2x the previous AND above median = a point cut
66 i = 0; while i < 6 { bins[i] = 100; i = i + 1 }
67 bins[3] = 400
68 var n7: i64 = ts_scene_marks(bins, 6, 500, mt, ms, me, 64)
69 if n7 == 1 { if mt[0] == 2 { if ms[0] == 1500 { pass = pass + 1; gp("T7 scene-cut type2 @1500ms OK\n" as *u8) } } }
70 if n7 != 1 { gp("T7 FAIL nm=" as *u8); gn(n7); gp("\n" as *u8) }
71
72 // T8: rhythmic/periodic motion (a period-4 repeated move over 16s) -> one rhythm marker (type 4)
73 i = 0; while i < 32 { if (i % 4) == 0 { bins[i] = 300 } else { bins[i] = 100 } i = i + 1 }
74 var n8: i64 = ts_rhythm_marks(bins, 32, 500, mt, ms, me, 64)
75 if n8 >= 1 { if mt[0] == 4 { pass = pass + 1; gp("T8 rhythm/cadence type4 OK\n" as *u8) } }
76 if n8 < 1 { gp("T8 FAIL nm=" as *u8); gn(n8); gp("\n" as *u8) }
77
78 // T9: flat (no motion variance) -> NO rhythm marker
79 i = 0; while i < 20 { bins[i] = 100; i = i + 1 }
80 var n9: i64 = ts_rhythm_marks(bins, 20, 500, mt, ms, me, 64)
81 if n9 == 0 { pass = pass + 1; gp("T9 flat -> 0 rhythm OK\n" as *u8) } else { gp("T9 FAIL nm=" as *u8); gn(n9); gp("\n" as *u8) }
82
83 // T10: a mostly-rhythmic clip -> content label 2 (rhythmic/dance-like)
84 let cl: *i64 = sys_mmap(8 * 8) as *i64
85 mt[0] = 4; ms[0] = 0; me[0] = 10000
86 var l10: i64 = ts_classify(mt, ms, me, 1, 20000, cl)
87 if l10 == 2 { pass = pass + 1; gp("T10 classify rhythmic -> label 2 OK\n" as *u8) } else { gp("T10 FAIL label=" as *u8); gn(l10); gp("\n" as *u8) }
88
89 // T11: a mostly-quiet clip -> content label 0 (quiet/static)
90 mt[0] = 1; ms[0] = 0; me[0] = 12000
91 var l11: i64 = ts_classify(mt, ms, me, 1, 20000, cl)
92 if l11 == 0 { pass = pass + 1; gp("T11 classify quiet -> label 0 OK\n" as *u8) } else { gp("T11 FAIL label=" as *u8); gn(l11); gp("\n" as *u8) }
93
94 // T12: tm_percentile ordering on a ramp
95 i = 0; while i < 60 { bins[i] = i * 10; i = i + 1 }
96 let q10: i64 = tm_percentile(bins, 60, 10)
97 let q25: i64 = tm_percentile(bins, 60, 25)
98 let q50: i64 = tm_percentile(bins, 60, 50)
99 let q90: i64 = tm_percentile(bins, 60, 90)
100 if q10 <= q25 { if q25 <= q50 { if q50 <= q90 { if q10 < q90 { pass = pass + 1; gp("T12 percentile ordering OK\n" as *u8) } } } }
101 if q10 >= q90 { gp("T12 FAIL p10=" as *u8); gn(q10); gp(" p90=" as *u8); gn(q90); gp("\n" as *u8) }
102
103 // T13: out-of-range percentile REFUSES rather than clamping
104 let qbad: i64 = tm_percentile(bins, 60, 101)
105 let qneg: i64 = tm_percentile(bins, 60, 0 - 1)
106 if qbad == 0 { if qneg == 0 { pass = pass + 1; gp("T13 percentile refuses out-of-range OK\n" as *u8) } }
107 if qbad != 0 { gp("T13 FAIL qbad=" as *u8); gn(qbad); gp("\n" as *u8) }
108
109 // T14: POSITIVE control -- a stream with real downward range fires static
110 i = 0; while i < 40 { bins[i] = 100; i = i + 1 }
111 i = 40; while i < 60 { bins[i] = 10; i = i + 1 }
112 let n14: i64 = ts_static_marks(bins, 60, 500, mt, ms, me, 64)
113 if n14 == 1 { if mt[0] == 5 { if ms[0] == 20000 { if me[0] == 30000 { pass = pass + 1; gp("T14 static fires on real downward range OK\n" as *u8) } } } }
114 if n14 != 1 { gp("T14 FAIL nm=" as *u8); gn(n14); gp("\n" as *u8) }
115
116 // T15: the MEASURED real case -- a rate-controlled stream. A run here WOULD
117 // qualify on length and threshold, and the guard must still refuse, because
118 // p25 sits at 79-94pct of median on every real file measured 2026-07-31.
119 i = 0; while i < 60 { bins[i] = 100; i = i + 1 }
120 i = 10; while i < 16 { bins[i] = 40; i = i + 1 }
121 let n15: i64 = ts_static_marks(bins, 60, 500, mt, ms, me, 64)
122 if n15 == 0 { pass = pass + 1; gp("T15 guard refuses flat stream despite a qualifying run OK\n" as *u8) } else { gp("T15 FAIL nm=" as *u8); gn(n15); gp("\n" as *u8) }
123
124 // T16: guard PASSES here, so a zero can only come from the run-length rule
125 i = 0; while i < 60 { bins[i] = 100; i = i + 1 }
126 i = 0; while i < 4 { bins[i] = 10; i = i + 1 }
127 i = 10; while i < 14 { bins[i] = 10; i = i + 1 }
128 i = 20; while i < 24 { bins[i] = 10; i = i + 1 }
129 i = 30; while i < 34 { bins[i] = 10; i = i + 1 }
130 i = 40; while i < 44 { bins[i] = 10; i = i + 1 }
131 let n16: i64 = ts_static_marks(bins, 60, 500, mt, ms, me, 64)
132 if n16 == 0 { pass = pass + 1; gp("T16 short static runs -> 0 markers OK\n" as *u8) } else { gp("T16 FAIL nm=" as *u8); gn(n16); gp("\n" as *u8) }
133
134 // T17: REGRESSION for the live defect. max=150 is BELOW 160pct of median=99,
135 // so the old fixed threshold could not fire. Percentile calibration must.
136 i = 0; while i < 50 { bins[i] = 100; i = i + 1 }
137 i = 20; while i < 25 { bins[i] = 150; i = i + 1 }
138 let n17: i64 = ts_motion_marks(bins, 50, 500, mt, ms, me, 64)
139 if n17 == 1 { if mt[0] == 3 { pass = pass + 1; gp("T17 motion fires when max < 160pct of median (regression) OK\n" as *u8) } }
140 if n17 != 1 { gp("T17 FAIL nm=" as *u8); gn(n17); gp("\n" as *u8) }
141
142 gp("TS-MARKS-GATE pass=" as *u8); gn(pass); gp("/17\n" as *u8)
143 if pass == 17 { gp("TS-MARKS-GATE GREEN 17/17 (dead-air + motion + scene + rhythm + content-classify)\n" as *u8); sys_exit(0) }
144 sys_exit(1)
145 return 0
146}