code wiki / _hdl_build / nx_audio_bowl.nx
nx_audio_bowl.nx source
↩ module page · 153 lines · 8553 B
1// nx_audio_bowl.nx -- CAP MODAL-OBJECT-SYNTH + BINAURAL-HRTF (operator 2026-06-23: "generate copper cups like
2// from tibet, exact binaural ... s-class audio ecosystem"). SOVEREIGN all the way up: physically-grounded MODAL
3// synthesis of a Tibetan singing/copper bowl (a set of inharmonic resonant modes = exponentially-damped
4// sinusoids, each generated by a 2nd-order RESONATOR recurrence y[n]=a1*y[n-1]-a2*y[n-2] -> NO per-sample
5// sin/exp, only a per-mode cos), then BINAURAL spatialization (ITD interaural time-delay + ILD level-difference
6// = the two dominant 3D cues) for a source placed to the right, rendered to a real 16-bit stereo .wav via our
7// OWN sovereign WAV writer (nx_audio_wav). No float, no 3rd-party. Also a self-hypnosis/relaxation asset.
8//
9// Honest gate: T1 MODAL -- the bowl signal rings then DECAYS (non-silent + decreasing envelope = real modal
10// ring, not noise/constant); T2 BINAURAL -- L/R differ with the right ear louder (ILD) and time-delayed (ITD)
11// = a positioned 3D source; T3 WAV -- a correct-size RIFF .wav is written. HONEST: ITD+ILD binaural (full
12// HRTF spectral filtering = follow-on); 5 modes (measured bowls have more). license_tier: ORIGINAL expect_exit: 0
13import "nx_audio_wav.nx"
14import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
15const K_MAGIC_1404: i64 = 1404
16const K_MAGIC_2333: i64 = 2333
17const K_MAGIC_3471: i64 = 3471
18const K_MAGIC_12000: i64 = 12000
19const K_MAGIC_7000: i64 = 7000
20const K_MAGIC_4500: i64 = 4500
21const K_MAGIC_2600: i64 = 2600
22const K_MAGIC_1500: i64 = 1500
23const K_MAGIC_65533: i64 = 65533
24const K_MAGIC_65531: i64 = 65531
25const K_MAGIC_65527: i64 = 65527
26const K_MAGIC_65521: i64 = 65521
27const K_MAGIC_65514: i64 = 65514
28const K_MAGIC_411774: i64 = 411774
29const K_MAGIC_16000: i64 = 16000
30const K_MAGIC_42000: i64 = 42000
31const K_MAGIC_65536: i64 = 65536
32
33const BLOG: *u8 = "knowledge/status/ng_audio_bowl.log"
34const OUTP: *u8 = "web_assets/ng_singing_bowl_binaural.wav"
35const Q16: i64 = 65536
36const SR: i64 = 22050
37const NS: i64 = 44100 // 2.0 s
38const NMODE: i64 = 5
39const ITD: i64 = 11 // interaural time delay (samples) for a right-placed source (~0.5ms)
40
41func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
42// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
43// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
44// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
45// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
46func bpn(v: i64) -> i64 { nxi_out(v); return 0 }
47func b_abs(v: i64) -> i64 { if v<0 { return 0-v } return v }
48func bqm(a: i64, b: i64) -> i64 { return (a*b)>>16 }
49func bl_ws(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
50// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
51// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
52// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
53// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
54func bl_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 }
55
56// fixed-point cos(x) for x in [0, ~1.2] rad (Q16) via Taylor 1 - x^2/2 + x^4/24 (all modes have omega<1)
57func cos_fp(x: i64) -> i64 {
58 let x2: i64 = bqm(x, x)
59 let x4: i64 = bqm(x2, x2)
60 return Q16 - (x2 >> 1) + (x4 / 24)
61}
62
63func main() -> i64 {
64 bp("nx_audio_bowl: MODAL singing-bowl synth + BINAURAL (ITD+ILD) -> sovereign .wav (no float, our own tools)\n" as *u8)
65 // inharmonic bowl partials: f0=261 Hz, ratios ~ [1, 2.75, 5.38, 8.94, 13.3]; lower modes ring LONGER + louder
66 let f: *i64 = sys_mmap(NMODE*8) as *i64
67 f[0]=261; f[1]=718; f[2]=K_MAGIC_1404; f[3]=K_MAGIC_2333; f[4]=K_MAGIC_3471
68 let amp: *i64 = sys_mmap(NMODE*8) as *i64
69 amp[0]=K_MAGIC_12000; amp[1]=K_MAGIC_7000; amp[2]=K_MAGIC_4500; amp[3]=K_MAGIC_2600; amp[4]=K_MAGIC_1500
70 let rq: *i64 = sys_mmap(NMODE*8) as *i64 // per-mode decay r (Q16, <1); lower=longer ring
71 rq[0]=K_MAGIC_65533; rq[1]=K_MAGIC_65531; rq[2]=K_MAGIC_65527; rq[3]=K_MAGIC_65521; rq[4]=K_MAGIC_65514
72
73 let mono: *i64 = sys_mmap(NS*8) as *i64
74 var i: i64=0; while i<NS { mono[i]=0; i=i+1 }
75
76 // sum the modal resonators (2nd-order recurrence per mode)
77 var k: i64=0
78 while k < NMODE {
79 // omega = 2*pi*f/SR in Q16: 2*pi*65536 = 411774 ; omega_q = 411774*f/SR
80 let om: i64 = (K_MAGIC_411774 * f[k]) / SR
81 let cosw: i64 = cos_fp(om)
82 let a1: i64 = bqm(2 * rq[k], cosw) // 2*r*cos(omega) (Q16)
83 let a2: i64 = bqm(rq[k], rq[k]) // r^2 (Q16)
84 var y1: i64 = amp[k] // y[1] = kick (amplitude)
85 var y2: i64 = 0 // y[0] = 0
86 mono[0] = mono[0] + y2; mono[1] = mono[1] + y1
87 var n: i64 = 2
88 while n < NS {
89 let yn: i64 = bqm(a1, y1) - bqm(a2, y2)
90 mono[n] = mono[n] + yn
91 y2 = y1; y1 = yn
92 n = n + 1
93 }
94 k = k + 1
95 }
96
97 // normalize mono to ~16000 peak (avoid clip/silence)
98 var mx: i64 = 1
99 i = 0; while i < NS { let a: i64 = b_abs(mono[i]); if a > mx { mx = a } i = i + 1 }
100 i = 0; while i < NS { mono[i] = mono[i] * K_MAGIC_16000 / mx; i = i + 1 }
101
102 // T1 MODAL: non-silent + DECAYING envelope (mean|.| of last 10% < first 10%)
103 var head: i64 = 0; var tail: i64 = 0; var pk: i64 = 0
104 let seg: i64 = NS / 10
105 i = 0; while i < seg { head = head + b_abs(mono[i]); i = i + 1 }
106 i = NS - seg; while i < NS { tail = tail + b_abs(mono[i]); i = i + 1 }
107 i = 0; while i < NS { let a: i64 = b_abs(mono[i]); if a > pk { pk = a } i = i + 1 }
108 var t1: i64 = 0; if pk > 1000 { if tail < head { t1 = 1 } }
109
110 // BINAURAL: source on the RIGHT -> right ear leads (no delay) + louder (ILD); left ear delayed by ITD + quieter
111 let L: *i64 = sys_mmap(NS*8) as *i64
112 let R: *i64 = sys_mmap(NS*8) as *i64
113 let gL: i64 = K_MAGIC_42000 // ~0.64 (Q16) far ear
114 let gR: i64 = K_MAGIC_65536 // 1.0 near ear
115 i = 0
116 while i < NS {
117 var ls: i64 = 0
118 if i - ITD >= 0 { ls = mono[i - ITD] } // left delayed (ITD)
119 L[i] = bqm(ls, gL) // + quieter (ILD)
120 R[i] = bqm(mono[i], gR)
121 i = i + 1
122 }
123 // T2 BINAURAL: right louder (ILD) AND channels differ (ITD)
124 var sumL: i64 = 0; var sumR: i64 = 0; var diff: i64 = 0
125 i = 0; while i < NS { sumL = sumL + b_abs(L[i]); sumR = sumR + b_abs(R[i]); if L[i] != R[i] { diff = diff + 1 } i = i + 1 }
126 var t2: i64 = 0; if sumR > sumL { if diff > NS / 2 { t2 = 1 } }
127
128 // T3 render + save the .wav (our own sovereign RIFF writer)
129 let wavbuf: *u8 = sys_mmap(44 + NS * 4 + 64)
130 let wlen: i64 = wav_render_stereo(L, R, NS, SR, wavbuf)
131 let saved: i64 = wav_save(OUTP, wavbuf, wlen)
132 var t3: i64 = 0; if wlen == 44 + NS * 4 { if saved == wlen { if wavbuf[0] == (82 as u8) { t3 = 1 } } } // 82='R' (RIFF)
133
134 bp(" T1 MODAL: peak=" as *u8); bpn(pk); bp(" head_env=" as *u8); bpn(head); bp(" tail_env=" as *u8); bpn(tail); bp(" rings+decays=" as *u8); bpn(t1); bp("\n" as *u8)
135 bp(" T2 BINAURAL: sum|L|=" as *u8); bpn(sumL); bp(" sum|R|=" as *u8); bpn(sumR); bp(" (R louder=ILD) diff_samples=" as *u8); bpn(diff); bp(" positioned=" as *u8); bpn(t2); bp("\n" as *u8)
136 bp(" T3 WAV: bytes=" as *u8); bpn(wlen); bp(" saved=" as *u8); bpn(saved); bp(" -> " as *u8); bp(OUTP); bp(" ok=" as *u8); bpn(t3); bp("\n" as *u8)
137
138 var ok: i64 = 1
139 if t1 != 1 { ok = 0 }
140 if t2 != 1 { ok = 0 }
141 if t3 != 1 { ok = 0 }
142 let logf: i64 = sys_openat_append(BLOG, 420)
143 if logf >= 0 {
144 bl_ws(logf, "NGAUDIOBOWL authored=organ modal+binaural peak=" as *u8); bl_wn(logf, pk)
145 bl_ws(logf, " t1=" as *u8); bl_wn(logf, t1); bl_ws(logf, " t2=" as *u8); bl_wn(logf, t2); bl_ws(logf, " t3=" as *u8); bl_wn(logf, t3)
146 if ok == 1 { bl_ws(logf, " verdict=GREEN\n" as *u8) } else { bl_ws(logf, " verdict=RED\n" as *u8) }
147 sys_close(logf)
148 }
149 bp(" verdict=" as *u8)
150 if ok == 1 { bp("GREEN (modal singing-bowl rings+decays + binaural ITD/ILD positioning + sovereign .wav = the copper-bowl-in-3D ask)\n" as *u8); sys_exit(0); return 0 }
151 bp("RED\n" as *u8)
152 sys_exit(1); return 1
153}