code wiki / _hdl_build / nx_audio_dred_gate.nx

nx_audio_dred_gate.nx source

↩ module page · 55 lines · 3703 B

1// nx_audio_dred_gate.nx -- proves + MEASURES the deep-redundancy keystone (nx_audio_dred): a K-frame redundancy tail 2// recovers a BURST of lost frames with NO retransmit, where 1-frame FEC (our current voice-FEC) leaves a long gap. Also 3// shows the honest depth limit (a burst longer than K leaves B-K gaps -> deeper K needed) + the coarse-frame fidelity + 4// the overhead cost. Cited research: Opus 1.5 DRED recovers up to 1s of loss for 12-32 kb/s at ~1% CPU. 5import "nx_syscalls.nx" 6import "nx_gate_emit_lib.nx" 7import "nx_audio_dred.nx" 8 9func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 10// set a burst of B lost frames starting at L into the bitmap (rest = received) 11func mk_burst(lost: *i64, n: i64, L: i64, B: i64) -> i64 { 12 var i: i64 = 0; while i < n { lost[i] = 0; i = i + 1 } 13 i = L; while i < L + B { if i < n { lost[i] = 1 } i = i + 1 } 14 return 0 15} 16 17func main() -> i64 { 18 g_puts("nx_audio_dred gate (deep-redundancy burst recovery, no retransmit, MEASURED)\n" as *u8) 19 var pass: i64 = 0; var total: i64 = 0 20 let n: i64 = 50; let order: i64 = 10 21 let lost: *i64 = sys_mmap(n*8) as *i64 22 23 // a burst of B=8 consecutive frames lost (160ms @ 20ms frames) -- the classic bad-cellular dropout 24 let B: i64 = 8; let L: i64 = 10 25 mk_burst(lost, n, L, B) 26 27 let dred_gaps10: i64 = dred_gaps(lost, n, 10) // DRED depth K=10 (>= burst) 28 let dred_rec10: i64 = dred_recovered(lost, n, 10) 29 let fec_gaps1: i64 = dred_gaps(lost, n, 1) // baseline: 1-frame FEC (our current voice-FEC) 30 let fec_rec1: i64 = dred_recovered(lost, n, 1) 31 32 g_puts(" [measure] burst of " as *u8); g_pn(B); g_puts(" lost frames: DRED(K=10) recovered=" as *u8); g_pn(dred_rec10); g_puts(" gaps=" as *u8); g_pn(dred_gaps10); g_puts(" vs 1-frame-FEC recovered=" as *u8); g_pn(fec_rec1); g_puts(" gaps=" as *u8); g_pn(fec_gaps1); g_puts("\n" as *u8) 33 34 pass = pass + g_check("DRED recovers the WHOLE burst (0 gaps, no retransmit)" as *u8, dred_gaps10 == 0); total=total+1 35 pass = pass + g_check("DRED beats 1-frame FEC (FEC leaves B-1 gap frames = a freeze)" as *u8, fec_gaps1 == B - 1); total=total+1 36 37 // honest depth limit: a burst LONGER than K leaves B-K gaps -> deeper redundancy needed (K=50 ~= 1s) 38 mk_burst(lost, n, L, 15) 39 let deep_gaps: i64 = dred_gaps(lost, n, 10) 40 g_puts(" [measure] honest limit: burst=15 > K=10 -> gaps=" as *u8); g_pn(deep_gaps); g_puts(" (= B-K; recover ~1s by setting K~50)\n" as *u8) 41 pass = pass + g_check("burst > K leaves exactly B-K gaps (depth bounds the recoverable burst)" as *u8, deep_gaps == 15 - 10); total=total+1 42 43 // coarse-frame fidelity: a reflection coeff round-trips Q15->Q7->Q15 with bounded error (envelope preserved) 44 let k_q15: i64 = 22937 // ~0.70 in Q15 45 let err: i64 = g_abs(k_q15 - dred_coarse_dq(dred_coarse_q(k_q15))) 46 // overhead: coarse frame = order i8 + 1 energy byte; tail = K * that 47 let coarse_bytes: i64 = order + 1 48 let tail_bytes: i64 = 10 * coarse_bytes 49 g_puts(" [measure] coarse-coeff Q15->Q7 err=" as *u8); g_pn(err); g_puts(" (<256 = i8 step) tail=" as *u8); g_pn(tail_bytes); g_puts("B/packet (~" as *u8); g_pn(tail_bytes*50*8/1000); g_puts(" kb/s @50fps; neural codebook -> 12-32 kb/s is the moonshot)\n" as *u8) 50 pass = pass + g_check("coarse envelope preserved (Q7 reflection err < 256)" as *u8, err < 256); total=total+1 51 52 g_puts("---- audio dred gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 53 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 54 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 55}