nx_audio_demo_test.nx source
↩ module page · 59 lines · 2851 B
1// nx_audio_demo_test.nx -- INTEGRATION gate: the 4 audio rungs compose into the
2// real signal path. Voice A (square) synthesized -> resampled 8->16 -> panned
3// hard-LEFT; voice B (saw) synthesized -> panned hard-RIGHT; both summed into one
4// stereo bus. Proves osc(R3) -> resample(R1) -> pan(R2) -> mix(R0) end to end,
5// all sovereign. expect_exit: 0.
6
7import "nx_syscalls.nx"
8import "nx_audio_mix.nx"
9import "nx_audio_resample.nx"
10import "nx_audio_pan.nx"
11import "nx_audio_osc.nx"
12
13func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func wn(v: i64) -> i64 {
15 let b: *u8 = sys_mmap(28); var m: i64 = v
16 if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
17 let t: *u8 = sys_mmap(28); var k: i64 = 0
18 if m == 0 { t[0] = 48 as u8; k = 1 }
19 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
20 var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 }
21 sys_write(1, b, k); return 0
22}
23func chk(cond: i64, pass: *i64, fail: *i64, label: *u8) -> i64 {
24 if cond == 1 { pass[0] = pass[0] + 1; w(" ok " as *u8); w(label); w("\n" as *u8) }
25 else { fail[0] = fail[0] + 1; w(" XX " as *u8); w(label); w("\n" as *u8) }
26 return 0
27}
28
29func main() -> i64 {
30 let pass: *i64 = (sys_mmap(8)) as *i64
31 let fail: *i64 = (sys_mmap(8)) as *i64
32 pass[0] = 0
33 fail[0] = 0
34 w("=== nx_audio_demo: osc -> resample -> pan -> mix (integration) ===\n" as *u8)
35
36 let vA: *i64 = (sys_mmap(256)) as *i64 // voice A: square
37 let vB: *i64 = (sys_mmap(256)) as *i64 // voice B: saw
38 let rA: *i64 = (sys_mmap(512)) as *i64 // A resampled
39 let L: *i64 = (sys_mmap(512)) as *i64 // stereo bus L (zeroed by mmap)
40 let R: *i64 = (sys_mmap(512)) as *i64 // stereo bus R
41
42 osc_square(vA, 16, 1, 8, 1000) // A starts at +1000
43 osc_saw(vB, 16, 1, 4, 1000) // B[0] = -1000
44 let nA: i64 = ar_resample_linear(vA, 16, 8, 16, rA, 512) // 8->16k = 2x -> 32 samples
45 ap_pan_into(rA, nA, 256, 0, L, R) // A hard-left -> L only
46 ap_pan_into(vB, 16, 256, 4, L, R) // B hard-right -> R only
47
48 chk(nA == 32, pass, fail, "resample 16@8 -> 32@16" as *u8)
49 chk(rA[0] == 1000, pass, fail, "resampled A[0] = 1000" as *u8)
50 chk(L[0] == 1000, pass, fail, "bus L[0] = voice A only (1000)" as *u8)
51 chk(R[0] == (0 - 1000), pass, fail, "bus R[0] = voice B only (-1000)" as *u8)
52 chk(R[0] != 0, pass, fail, "R non-silent (B present)" as *u8)
53 // isolation: A hard-left contributed nothing to R[0]; B hard-right nothing to L[0]
54 chk(L[0] == 1000, pass, fail, "no B leak into L (still 1000)" as *u8)
55
56 w("=== VERDICT pass=" as *u8); wn(pass[0]); w(" fail=" as *u8); wn(fail[0]); w(" ===\n" as *u8)
57 if fail[0] == 0 { return 0 }
58 return 1
59}