code wiki / _hdl_build / nx_voice_codec_v2_gate.nx
nx_voice_codec_v2_gate.nx source
↩ module page · 65 lines · 3590 B
1// nx_voice_codec_v2_gate.nx -- proves the v2 voice codec (nx_voice_codec_v2) as a real encode/decode PAIR: PCM frame ->
2// v2_encode -> wire bytes -> v2_decode -> PCM, reconstructed faithfully and smaller than raw PCM. This is the v1
3// skeleton's "v2 path" realised as an actual codec.
4import "nx_syscalls_x86_64.nx"
5import "nx_voice_codec_v2.nx"
6import "nx_gate_verdict.nx"
7
8func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func g_pn(v: i64) -> i64 {
10 let b: *u8 = sys_mmap(28); var x: i64 = v
11 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x }
12 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 }
13 var d: i64=0; var y: i64=x
14 while y>0 { d=d+1; y=y/10 }
15 var i: i64=d-1; y=x
16 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
17 sys_write(1,b,d); return 0
18}
19func g_check(name: *u8, cond: i64) -> i64 {
20 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
21 g_puts(name); g_puts("\n" as *u8); return cond
22}
23func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
24func ld16(buf: *u8, idx: i64) -> i64 { var v: i64 = buf[idx*2] | (buf[idx*2+1] << 8); if v >= 32768 { v = v - 65536 } return v }
25func st16(buf: *u8, idx: i64, v: i64) -> i64 { var x: i64 = v; if x < 0 { x = x + 65536 } buf[idx*2] = x & 0xff; buf[idx*2+1] = (x >> 8) & 0xff; return 0 }
26
27func main() -> i64 {
28 g_puts("nx_voice_codec_v2 gate (real LPC-residual encode/decode pair, MEASURED)\n" as *u8)
29 var pass: i64 = 0; var total: i64 = 0
30 let n: i64 = 160; let order: i64 = 10 // 20ms @ 8kHz
31
32 let s_pcm: *u8 = sys_mmap(n*2)
33 let frame: *u8 = sys_mmap(4096)
34 let recon: *u8 = sys_mmap(n*2)
35
36 // voiced-ish periodic signal
37 var i: i64 = 0
38 while i < n { let p: i64 = i % 16; st16(s_pcm, i, 40 + p*(16-p)*2); i = i + 1 }
39 var sig_range: i64 = 0
40 i = 0; while i < n { let v: i64 = g_abs(ld16(s_pcm, i)); if v > sig_range { sig_range = v } i = i + 1 }
41
42 let enc_len: i64 = v2_encode(s_pcm, n, order, frame, 4096)
43 let dec_n: i64 = v2_decode(frame, enc_len, recon, n)
44
45 var maxerr: i64 = 0
46 i = 0; while i < n { let d: i64 = g_abs(ld16(recon, i) - ld16(s_pcm, i)); if d > maxerr { maxerr = d } i = i + 1 }
47 let raw: i64 = n * 2
48
49 g_puts(" [measure] v2 frame=" as *u8); g_pn(enc_len); g_puts(" B vs raw PCM=" as *u8); g_pn(raw); g_puts(" B (compression " as *u8); g_pn(raw*10/enc_len); g_puts("/10x) recon max-err=" as *u8); g_pn(maxerr); g_puts(" / signal " as *u8); g_pn(sig_range); g_puts("\n" as *u8)
50
51 pass = pass + g_check("decode returns the frame's sample count" as *u8, dec_n == n); total=total+1
52 pass = pass + g_check("v2 codec reconstructs faithfully (max-err <= 1/4 signal range)" as *u8, maxerr * 4 <= sig_range); total=total+1
53 pass = pass + g_check("v2 frame smaller than raw PCM (real compression)" as *u8, enc_len < raw); total=total+1
54
55 g_puts("---- voice codec v2 gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
56 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
57 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
58 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
59 let ctr__dry: *i64 = gv_ctr()
60 ctr__dry[0] = pass
61 ctr__dry[1] = total
62 let rc__dry: i64 = gv_verdict("VOICE-CODEC-V2-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
63 sys_exit(rc__dry)
64 return rc__dry
65}