code wiki / _hdl_build / nx_audio_codec_pack.nx
nx_audio_codec_pack.nx source
↩ module page · 82 lines · 4666 B
1// nx_audio_codec_pack.nx -- prove the WHOLE /listen audio set runs through OUR OWN lossless codec (operator
2// 2026-06-23: "make sure this is all using our own codec"). For each track WAV: deinterleave L/R Int16,
3// nv1l_encode each channel with our NishiLossless LPC codec (nx_nv1_lpc), then nv1l_decode and assert the
4// reconstruction is BIT-EXACT to the original PCM. Reports the compression ratio per track. GREEN only if every
5// channel of every track round-trips bit-for-bit (zero loss) -- that is the "using our own codec" proof.
6// license_tier: ORIGINAL expect_exit: 0
7import "nx_nv1_lpc.nx"
8import "nx_syscalls.nx"
9const K_MAGIC_524288: i64 = 524288
10
11func kp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func kpn(v: i64) -> i64 {
13 let b: *u8 = sys_mmap(28); var x: i64=v
14 if x<0 { b[0]=45; sys_write(1,b,1); x=0-x }
15 if x==0 { b[0]=48; sys_write(1,b,1); return 0 }
16 var d: i64=0; var y: i64=x; while y>0 { d=d+1; y=y/10 }
17 var i: i64=d-1; y=x; while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
18 sys_write(1,b,d); return 0
19}
20func kf_read(path: *u8, buf: *u8, cap: i64) -> i64 {
21 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 }
22 var total: i64 = 0; var r: i64 = sys_read(fd, buf, cap)
23 while r > 0 { total = total + r; if total >= cap { r = 0 } else { r = sys_read(fd, (buf + total) as *u8, cap - total) } }
24 sys_close(fd); return total
25}
26func same(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 }
27
28// encode+decode one channel, assert bit-exact. returns compressed bytes (>=0) or -1 if NOT bit-exact.
29func pack_ch(pcm: *u8, nsamp: i64, enc: *u8, dec: *u8, cap: i64) -> i64 {
30 let el: i64 = nv1l_encode(pcm, nsamp, enc, cap)
31 if el <= 0 { return 0 - 1 }
32 let dl: i64 = nv1l_decode(enc, el, dec, cap)
33 if dl != nsamp { return 0 - 1 } // nv1l_decode returns SAMPLE count; output is nsamp*2 bytes
34 if same(pcm, dec, nsamp * 2) == 0 { return 0 - 1 }
35 return el
36}
37
38// returns 1 if the track round-trips bit-exact (both channels); prints the ratio.
39func pack_file(path: *u8, wav: *u8, Lb: *u8, Rb: *u8, enc: *u8, dec: *u8, cap: i64) -> i64 {
40 let fl: i64 = kf_read(path, wav, cap)
41 if fl <= 44 { kp(" MISSING/short: " as *u8); kp(path); kp("\n" as *u8); return 0 }
42 let dat: i64 = fl - 44
43 let nsamp: i64 = dat / 4 // stereo Int16 interleaved
44 var i: i64 = 0
45 while i < nsamp {
46 Lb[2*i] = wav[44 + 4*i]; Lb[2*i+1] = wav[44 + 4*i+1]
47 Rb[2*i] = wav[44 + 4*i+2]; Rb[2*i+1] = wav[44 + 4*i+3]
48 i = i + 1
49 }
50 let cl: i64 = pack_ch(Lb, nsamp, enc, dec, cap)
51 let cr: i64 = pack_ch(Rb, nsamp, enc, dec, cap)
52 kp(" " as *u8); kp(path)
53 if cl < 0 { kp(" L NOT-BIT-EXACT\n" as *u8); return 0 }
54 if cr < 0 { kp(" R NOT-BIT-EXACT\n" as *u8); return 0 }
55 let comp: i64 = cl + cr
56 kp(" raw=" as *u8); kpn(dat); kp("B ours=" as *u8); kpn(comp); kp("B ratio=" as *u8)
57 kpn((dat * 100) / comp); kp("% bit-exact=YES\n" as *u8) // ratio in % of original size... (smaller=better)
58 return 1
59}
60
61func main() -> i64 {
62 kp("nx_audio_codec_pack: running ALL /listen audio through our own NishiLossless LPC codec (bit-exact proof)\n" as *u8)
63 let cap: i64 = K_MAGIC_524288
64 let wav: *u8 = sys_mmap(cap)
65 let Lb: *u8 = sys_mmap(cap); let Rb: *u8 = sys_mmap(cap)
66 let enc: *u8 = sys_mmap(cap); let dec: *u8 = sys_mmap(cap)
67 var ok: i64 = 0; var tot: i64 = 0
68
69 tot=tot+1; ok=ok+pack_file("web_assets/ng_singing_bowl_binaural.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
70 tot=tot+1; ok=ok+pack_file("web_assets/ng_binaural_beats_theta.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
71 tot=tot+1; ok=ok+pack_file("web_assets/ng_beats_delta.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
72 tot=tot+1; ok=ok+pack_file("web_assets/ng_beats_alpha.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
73 tot=tot+1; ok=ok+pack_file("web_assets/ng_beats_beta.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
74 tot=tot+1; ok=ok+pack_file("web_assets/ng_dog_squeak.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
75 tot=tot+1; ok=ok+pack_file("web_assets/ng_dog_chirp.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
76 tot=tot+1; ok=ok+pack_file("web_assets/ng_dog_whistle.wav\x00" as *u8, wav, Lb, Rb, enc, dec, cap)
77
78 kp(" bit-exact tracks=" as *u8); kpn(ok); kp(" / " as *u8); kpn(tot); kp("\n verdict=" as *u8)
79 if ok == tot { kp("GREEN (every track losslessly round-trips through OUR codec, zero loss)\n" as *u8); sys_exit(0); return 0 }
80 kp("RED\n" as *u8)
81 sys_exit(1); return 1
82}