code wiki / _hdl_build / nx_audio_binaural_beats.nx

nx_audio_binaural_beats.nx source

↩ module page · 88 lines · 4608 B

1// nx_audio_binaural_beats.nx -- binaural BEATS generator (operator 2026-06-23: s-class audio ecosystem + s-class 2// self-hypnosis help). Two PURE SUSTAINED tones, one per ear at slightly different frequencies (L=200Hz, R=207Hz) 3// -> the brain perceives a 7Hz THETA beat = a classic relaxation / self-hypnosis induction aid (the audio 4// ecosystem powering hypnosis). Sustained sines via a 2nd-order resonator with r=1 (no decay); fade in/out to 5// avoid clicks; our own RIFF writer. Sovereign, no float. license_tier: ORIGINAL expect_exit: 0 6import "nx_audio_wav.nx" 7const K_MAGIC_411774: i64 = 411774 8 9const OUTP: *u8 = "web_assets/ng_binaural_beats_theta.wav" 10const Q16: i64 = 65536 11const SR: i64 = 22050 12const NS: i64 = 88200 // 4.0 s 13const FBASE: i64 = 200 // left ear Hz 14const FBEAT: i64 = 7 // beat Hz -> right ear = 207 Hz (theta, relaxation) 15const AMP: i64 = 10000 16 17func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func bpn(v: i64) -> i64 { 19 let b: *u8 = sys_mmap(28); var x: i64=v 20 if x<0 { b[0]=45; sys_write(1,b,1); x=0-x } 21 if x==0 { b[0]=48; sys_write(1,b,1); return 0 } 22 var d: i64=0; var y: i64=x; while y>0 { d=d+1; y=y/10 } 23 var i: i64=d-1; y=x; while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 } 24 sys_write(1,b,d); return 0 25} 26func b_abs(v: i64) -> i64 { if v<0 { return 0-v } return v } 27func bqm(a: i64, b: i64) -> i64 { return (a*b)>>16 } 28func cos_fp(x: i64) -> i64 { let x2: i64=bqm(x,x); let x4: i64=bqm(x2,x2); return Q16 - (x2>>1) + (x4/24) } 29func sin_fp(x: i64) -> i64 { let x2: i64=bqm(x,x); let x3: i64=bqm(x2,x); return x - (x3/6) } 30 31// fill buf with AMP*sin(omega*n) (omega=2*pi*f/SR) via a sustained resonator (r=1), then fade in/out. 32func gen_tone(buf: *i64, f: i64) -> i64 { 33 let om: i64 = (K_MAGIC_411774 * f) / SR // 2*pi*f/SR in Q16 34 let a1: i64 = 2 * cos_fp(om) // 2*cos(omega) (Q16); a2 = r^2 = 1.0 -> y[n-2] passes through 35 var y2: i64 = 0 36 var y1: i64 = bqm(AMP, sin_fp(om)) // y[1] = AMP*sin(omega) -> amplitude AMP 37 buf[0] = 0; buf[1] = y1 38 var n: i64 = 2 39 while n < NS { 40 let yn: i64 = bqm(a1, y1) - y2 // a2=1.0 so the y[n-2] term is exact 41 buf[n] = yn 42 y2 = y1; y1 = yn 43 n = n + 1 44 } 45 // fade in (first FN) + fade out (last FN) -- linear, avoids start/stop clicks 46 let FN: i64 = SR / 3 47 var i: i64 = 0 48 while i < FN { buf[i] = buf[i] * i / FN; buf[NS - 1 - i] = buf[NS - 1 - i] * i / FN; i = i + 1 } 49 return 0 50} 51func zero_cross(buf: *i64) -> i64 { 52 var c: i64 = 0; var n: i64 = 1 53 while n < NS { if buf[n - 1] < 0 { if buf[n] >= 0 { c = c + 1 } } n = n + 1 } 54 return c 55} 56 57func main() -> i64 { 58 bp("nx_audio_binaural_beats: L=" as *u8); bpn(FBASE); bp("Hz R=" as *u8); bpn(FBASE + FBEAT); bp("Hz -> " as *u8); bpn(FBEAT); bp("Hz theta beat (sovereign .wav)\n" as *u8) 59 let L: *i64 = sys_mmap(NS*8) as *i64 60 let R: *i64 = sys_mmap(NS*8) as *i64 61 gen_tone(L, FBASE) 62 gen_tone(R, FBASE + FBEAT) 63 64 // T1 both ears non-silent; T2 the two ears are at DIFFERENT frequencies (R has more zero-crossings) 65 var mxL: i64 = 0; var mxR: i64 = 0; var i: i64 = 0 66 while i < NS { let al: i64=b_abs(L[i]); let ar: i64=b_abs(R[i]); if al>mxL {mxL=al} if ar>mxR {mxR=ar} i=i+1 } 67 let zL: i64 = zero_cross(L); let zR: i64 = zero_cross(R) 68 var t1: i64 = 0; if mxL > 1000 { if mxR > 1000 { t1 = 1 } } 69 var t2: i64 = 0; if zR > zL { t2 = 1 } // 207Hz right oscillates faster than 200Hz left = the beat exists 70 71 let wavbuf: *u8 = sys_mmap(44 + NS*4 + 64) 72 let wlen: i64 = wav_render_stereo(L, R, NS, SR, wavbuf) 73 let saved: i64 = wav_save(OUTP, wavbuf, wlen) 74 var t3: i64 = 0; if wlen == 44 + NS*4 { if saved == wlen { t3 = 1 } } 75 76 bp(" T1 both-ears: peakL=" as *u8); bpn(mxL); bp(" peakR=" as *u8); bpn(mxR); bp(" ok=" as *u8); bpn(t1); bp("\n" as *u8) 77 bp(" T2 beat: zeroX L=" as *u8); bpn(zL); bp(" R=" as *u8); bpn(zR); bp(" (R faster=different-freq=beat) ok=" as *u8); bpn(t2); bp("\n" as *u8) 78 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) 79 80 var ok: i64 = 1 81 if t1 != 1 { ok = 0 } 82 if t2 != 1 { ok = 0 } 83 if t3 != 1 { ok = 0 } 84 bp(" verdict=" as *u8) 85 if ok == 1 { bp("GREEN (two sustained per-ear tones at different freqs = a binaural theta beat; sovereign .wav for self-hypnosis/relaxation)\n" as *u8); sys_exit(0); return 0 } 86 bp("RED\n" as *u8) 87 sys_exit(1); return 1 88}