code wiki / _hdl_build / nx_perceptual.nx
nx_perceptual.nx
buildroot/runtime/_hdl_build/nx_perceptual.nx
about
nx_perceptual.nx -- R2 of the codec ladder: a PERCEPTUAL audio quantizer on the MDCT (the MP3/AAC/Opus idea).
Composes nx_mdct (R1) for the transform. Groups MDCT bins into widening (Bark-like) critical bands, derives a
per-band MASKING threshold (band energy + simple spreading + an absolute-hearing floor), then allocates the
quantizer step per band so the quantization NOISE sits AT the mask -- coarse where the signal hides it, fine
where it doesn't. GATEABLE WITHOUT EARS via the noise-to-mask ratio: noise[b] <= mask[b] in every band =
perceptually transparent. TEETH: uniform quantization that reaches the SAME transparency needs many more bits
-- so perceptual transparency is provably cheaper. No-float (integer sqrt). license_tier: ORIGINAL expect_exit: 0
dependencies 3 imports · 0 importers
imports: nx_mdct.nxnx_audio_osc.nxnx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 11 | const K_MAGIC_3840: i64 = 3840 |
| 12 | const K_MAGIC_9000: i64 = 9000 |
| 13 | const K_MAGIC_11520: i64 = 11520 |
| 14 | const K_MAGIC_2600: i64 = 2600 |
| 15 | const K_MAGIC_19200: i64 = 19200 |
| 17 | const N: i64 = 256 |
| 18 | const SMR: i64 = 16 // signal-to-mask ratio: a masker hides noise ~SMR x weaker (~12 dB). [config, not magic: psychoacoustics corpus] |
| 19 | const ATH: i64 = 250000 // absolute-threshold-of-hearing floor (flat approximation; real ATH is frequency-dependent) |
functions
| 21 | func pp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 22 | func ppn(v: i64) -> i64 |
| 30 | func isqrt(n: i64) -> i64 { if n < 2 { return n } var x: i64=n; var y: i64=(x+1)/2; while y < x { x=y; y=(x + n/x)/2 } return x } called by 1: main |
| 31 | func bitlen(v: i64) -> i64 { var a: i64=v; if a<0 {a=0-a} var b: i64=2; while a>0 { b=b+1; a=a>>1 } return b } // sign + magnitude called by 1: main |
| 33 | func quantize(X: i64, st: i64) -> i64 { if st < 1 { return X } if X>=0 { return (X + st/2)/st } return 0 - ((0-X + st/2)/st) } called by 1: main |
| 35 | func main() -> i64 |