code wiki / _hdl_build / nx_room_plc.nx

nx_room_plc.nx source

↩ module page · 108 lines · 5277 B

1// nx_room_plc.nx -- X-ROOM (overseas-grade): sovereign DETERMINISTIC audio PACKET-LOSS 2// CONCEALMENT. The last line of defense: when a frame is lost BEYOND what FEC recovers 3// (bursty overseas loss), synthesize a replacement instead of going silent. Pitch-repeat 4// the last good period with a gentle energy decay -> a smooth, click-free fill; the naive 5// alternative (zero-fill) drops a hole = an audible CLICK + silence gap. 6// 7// EXCEED axis (honest) vs zero/silence concealment: deterministic, audit-replayable 8// waveform extrapolation that (1) reconstructs far closer to the true signal, (2) keeps 9// energy up (not silent), (3) resumes more smoothly (smaller boundary jump). Sovereign, 10// integer-exact. HONEST SCOPE: periodic-extrapolation PLC on a known pitch period; true 11// pitch detection + LPC residual extrapolation (compose nx_nv1_lpc) = a deeper rung. 12// 13// main() is the SELF-VALIDATING GATE. Evidence -> knowledge/status/room_plc.log. 14// license_tier: ORIGINAL 15import "nx_syscalls.nx" 16 17const PLC_LOG: *u8 = "knowledge/status/room_plc.log" 18 19func pw(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 20func pwn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 21 22func pabs(x: i64) -> i64 { if x < 0 { return 0 - x } return x } 23 24// fill out[0..L) for a lost frame at `start`. mode 1 = ZERO-fill (naive). mode 0 = PLC: 25// repeat the last `P`-sample period with a linear energy decay (atten per sample). 26func plc_fill(sig: *i64, start: i64, L: i64, P: i64, atten: i64, mode: i64, out: *i64) -> i64 { 27 var j: i64 = 0 28 while j < L { 29 if mode == 1 { out[j] = 0 } 30 else { 31 let src: i64 = sig[start - P + (j % P)] 32 var a: i64 = 256 - j * atten 33 if a < 0 { a = 0 } 34 out[j] = src * a / 256 35 } 36 j = j + 1 37 } 38 return 0 39} 40// sum |out[j] - true[j]| over the concealed frame. 41func plc_err(out: *i64, sig: *i64, start: i64, L: i64) -> i64 { 42 var s: i64 = 0 43 var j: i64 = 0 44 while j < L { s = s + pabs(out[j] - sig[start + j]); j = j + 1 } 45 return s 46} 47func plc_energy(out: *i64, L: i64) -> i64 { 48 var s: i64 = 0 49 var j: i64 = 0 50 while j < L { s = s + pabs(out[j]); j = j + 1 } 51 return s 52} 53 54func main() -> i64 { 55 let NS: i64 = 64 56 let sig: *i64 = sys_mmap(8 * NS) as *i64 57 // periodic voiced-audio model: period P=8, smooth positive envelope, exactly repeating. 58 let base: *i64 = sys_mmap(8 * 8) as *i64 59 base[0]=40; base[1]=60; base[2]=75; base[3]=60; base[4]=40; base[5]=20; base[6]=10; base[7]=20 60 var i: i64 = 0 61 while i < NS { sig[i] = base[i % 8]; i = i + 1 } 62 63 let P: i64 = 8 64 let start: i64 = 40 // a period boundary; lose one full period [40,48) 65 let L: i64 = 8 66 let ATTEN: i64 = 4 67 let out: *i64 = sys_mmap(8 * 16) as *i64 68 var ok: i64 = 1 69 70 // PLC conceal 71 plc_fill(sig, start, L, P, ATTEN, 0, out) 72 let plc_e: i64 = plc_err(out, sig, start, L) 73 let plc_en: i64 = plc_energy(out, L) 74 let plc_resume: i64 = pabs(sig[start + L] - out[L - 1]) // jump at the resumption boundary 75 76 // ZERO-fill (naive control) 77 plc_fill(sig, start, L, P, ATTEN, 1, out) 78 let zero_e: i64 = plc_err(out, sig, start, L) 79 let zero_en: i64 = plc_energy(out, L) 80 let zero_resume: i64 = pabs(sig[start + L] - out[L - 1]) 81 82 if plc_e >= zero_e { ok = 0 } // PLC reconstructs far closer than silence 83 if plc_en <= zero_en { ok = 0 } // PLC keeps energy up (not silent); zero_en==0 84 if plc_resume >= zero_resume { ok = 0 } // PLC resumes with a smaller boundary jump (less click) 85 86 // tamper: WRONG pitch period (P-1) -> the repeat is misaligned -> reconstruction error rises. 87 plc_fill(sig, start, L, P - 1, ATTEN, 0, out) 88 let tamper_e: i64 = plc_err(out, sig, start, L) 89 if tamper_e <= plc_e { ok = 0 } // the correct pitch period is load-bearing 90 91 pw(1, "ROOMPLCGATE plc_err=" as *u8); pwn(1, plc_e); pw(1, " zero_err=" as *u8); pwn(1, zero_e) 92 pw(1, " plc_energy=" as *u8); pwn(1, plc_en); pw(1, " zero_energy=" as *u8); pwn(1, zero_en) 93 pw(1, " plc_resume=" as *u8); pwn(1, plc_resume); pw(1, " zero_resume=" as *u8); pwn(1, zero_resume) 94 pw(1, " wrongpitch_err=" as *u8); pwn(1, tamper_e) 95 if ok == 1 { pw(1, " verdict=GREEN\n" as *u8) } else { pw(1, " verdict=RED\n" as *u8) } 96 97 let lf: i64 = sys_openat_append(PLC_LOG, 420) 98 if lf >= 0 { 99 pw(lf, "ROOMPLCGATE plc_err=" as *u8); pwn(lf, plc_e); pw(lf, " zero_err=" as *u8); pwn(lf, zero_e) 100 pw(lf, " plc_energy=" as *u8); pwn(lf, plc_en); pw(lf, " zero_energy=" as *u8); pwn(lf, zero_en) 101 pw(lf, " plc_resume=" as *u8); pwn(lf, plc_resume); pw(lf, " zero_resume=" as *u8); pwn(lf, zero_resume) 102 pw(lf, " wrongpitch_err=" as *u8); pwn(lf, tamper_e) 103 if ok == 1 { pw(lf, " verdict=GREEN\n" as *u8) } else { pw(lf, " verdict=RED\n" as *u8) } 104 sys_close(lf) 105 } 106 if ok == 1 { sys_exit(0) } else { sys_exit(1) } 107 return 0 108}