code wiki / (root) / nx_audio_resample_test.nx

nx_audio_resample_test.nx source

↩ module page · 64 lines · 2753 B

1// nx_audio_resample_test.nx -- KAT gate for the sovereign resampler (audio R1). 2// Upsample 2x (linear interp), downsample 2x, and identity. expect_exit: 0. 3 4import "nx_syscalls.nx" 5import "nx_audio_mix.nx" 6import "nx_audio_resample.nx" 7 8func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func wn(v: i64) -> i64 { 10 let b: *u8 = sys_mmap(28); var m: i64 = v 11 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } 12 let t: *u8 = sys_mmap(28); var k: i64 = 0 13 if m == 0 { t[0] = 48 as u8; k = 1 } 14 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 15 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 16 sys_write(1, b, k); return 0 17} 18func chk(cond: i64, pass: *i64, fail: *i64, label: *u8) -> i64 { 19 if cond == 1 { pass[0] = pass[0] + 1; w(" ok " as *u8); w(label); w("\n" as *u8) } 20 else { fail[0] = fail[0] + 1; w(" XX " as *u8); w(label); w("\n" as *u8) } 21 return 0 22} 23 24func main() -> i64 { 25 let pass: *i64 = (sys_mmap(8)) as *i64 26 let fail: *i64 = (sys_mmap(8)) as *i64 27 pass[0] = 0 28 fail[0] = 0 29 w("=== nx_audio_resample KAT (sovereign linear resampler, R1) ===\n" as *u8) 30 31 let inb: *i64 = (sys_mmap(64)) as *i64 32 let out: *i64 = (sys_mmap(64)) as *i64 33 34 // ---- upsample 2x: [0,100] @1 -> @2 = [0,50,100,100] ---- 35 inb[0] = 0; inb[1] = 100 36 let nu: i64 = ar_resample_linear(inb, 2, 1, 2, out, 64) 37 chk(nu == 4, pass, fail, "upsample n_out=4" as *u8) 38 chk(out[0] == 0, pass, fail, "up[0]=0" as *u8) 39 chk(out[1] == 50, pass, fail, "up[1]=50 (interp)" as *u8) 40 chk(out[2] == 100, pass, fail, "up[2]=100" as *u8) 41 chk(out[3] == 100, pass, fail, "up[3]=100 (edge hold)" as *u8) 42 43 // ---- downsample 2x: [0,50,100,150] @2 -> @1 = [0,100] ---- 44 inb[0] = 0; inb[1] = 50; inb[2] = 100; inb[3] = 150 45 let nd: i64 = ar_resample_linear(inb, 4, 2, 1, out, 64) 46 chk(nd == 2, pass, fail, "downsample n_out=2" as *u8) 47 chk(out[0] == 0, pass, fail, "down[0]=0" as *u8) 48 chk(out[1] == 100, pass, fail, "down[1]=100" as *u8) 49 50 // ---- identity: same rate -> unchanged ---- 51 inb[0] = 10; inb[1] = 20; inb[2] = 30 52 let ni: i64 = ar_resample_linear(inb, 3, 8000, 8000, out, 64) 53 chk(ni == 3, pass, fail, "identity n_out=3" as *u8) 54 chk(out[0] == 10, pass, fail, "id[0]=10" as *u8) 55 chk(out[1] == 20, pass, fail, "id[1]=20" as *u8) 56 chk(out[2] == 30, pass, fail, "id[2]=30" as *u8) 57 58 // ---- out_len helper matches ---- 59 chk(ar_out_len(441, 44100, 48000) == 480, pass, fail, "out_len 44.1k->48k (441->480)" as *u8) 60 61 w("=== VERDICT pass=" as *u8); wn(pass[0]); w(" fail=" as *u8); wn(fail[0]); w(" ===\n" as *u8) 62 if fail[0] == 0 { return 0 } 63 return 1 64}