code wiki / _hdl_build / nx_voice_codec_v2_gate.nx

nx_voice_codec_v2_gate.nx source

↩ module page · 57 lines · 3142 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" 6 7func 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 } 8func g_pn(v: i64) -> i64 { 9 let b: *u8 = sys_mmap(28); var x: i64 = v 10 if x < 0 { b[0]=45; sys_write(1,b,1); x = 0 - x } 11 if x == 0 { b[0]=48; sys_write(1,b,1); return 0 } 12 var d: i64=0; var y: i64=x 13 while y>0 { d=d+1; y=y/10 } 14 var i: i64=d-1; y=x 15 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 16 sys_write(1,b,d); return 0 17} 18func g_check(name: *u8, cond: i64) -> i64 { 19 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } 20 g_puts(name); g_puts("\n" as *u8); return cond 21} 22func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 23func 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 } 24func 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 } 25 26func main() -> i64 { 27 g_puts("nx_voice_codec_v2 gate (real LPC-residual encode/decode pair, MEASURED)\n" as *u8) 28 var pass: i64 = 0; var total: i64 = 0 29 let n: i64 = 160; let order: i64 = 10 // 20ms @ 8kHz 30 31 let s_pcm: *u8 = sys_mmap(n*2) 32 let frame: *u8 = sys_mmap(4096) 33 let recon: *u8 = sys_mmap(n*2) 34 35 // voiced-ish periodic signal 36 var i: i64 = 0 37 while i < n { let p: i64 = i % 16; st16(s_pcm, i, 40 + p*(16-p)*2); i = i + 1 } 38 var sig_range: i64 = 0 39 i = 0; while i < n { let v: i64 = g_abs(ld16(s_pcm, i)); if v > sig_range { sig_range = v } i = i + 1 } 40 41 let enc_len: i64 = v2_encode(s_pcm, n, order, frame, 4096) 42 let dec_n: i64 = v2_decode(frame, enc_len, recon, n) 43 44 var maxerr: i64 = 0 45 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 } 46 let raw: i64 = n * 2 47 48 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) 49 50 pass = pass + g_check("decode returns the frame's sample count" as *u8, dec_n == n); total=total+1 51 pass = pass + g_check("v2 codec reconstructs faithfully (max-err <= 1/4 signal range)" as *u8, maxerr * 4 <= sig_range); total=total+1 52 pass = pass + g_check("v2 frame smaller than raw PCM (real compression)" as *u8, enc_len < raw); total=total+1 53 54 g_puts("---- voice codec v2 gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 55 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 56 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 57}