code wiki / _hdl_build / nx_audio_beats_variants.nx
nx_audio_beats_variants.nx source
↩ module page · 66 lines · 3238 B
1// nx_audio_beats_variants.nx -- generate a SET of binaural-beat tracks (operator: "generate new options if they
2// tire of the current" -> give shuffle/new real variety). Each = two sustained pure tones (L=base, R=base+beat)
3// via an r=1 resonator -> the brain hears the BEAT frequency. delta (deep), alpha (relaxed focus), beta (alert).
4// Sovereign, no float, our own RIFF writer. Outputs web_assets/ng_beats_<name>.wav. license_tier: ORIGINAL expect_exit: 0
5import "nx_audio_wav.nx"
6const K_MAGIC_411774: i64 = 411774
7
8const Q16: i64 = 65536
9const SR: i64 = 22050
10const NS: i64 = 88200 // 4.0 s
11const AMP: i64 = 10000
12
13func vp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
14func vpn(v: i64) -> i64 {
15 let b: *u8 = sys_mmap(28); var x: i64=v
16 if x<0 { b[0]=45; sys_write(1,b,1); x=0-x }
17 if x==0 { b[0]=48; sys_write(1,b,1); return 0 }
18 var d: i64=0; var y: i64=x; while y>0 { d=d+1; y=y/10 }
19 var i: i64=d-1; y=x; while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
20 sys_write(1,b,d); return 0
21}
22func b_abs(v: i64) -> i64 { if v<0 { return 0-v } return v }
23func bqm(a: i64, b: i64) -> i64 { return (a*b)>>16 }
24func cos_fp(x: i64) -> i64 { let x2: i64=bqm(x,x); let x4: i64=bqm(x2,x2); return Q16 - (x2>>1) + (x4/24) }
25func sin_fp(x: i64) -> i64 { let x2: i64=bqm(x,x); let x3: i64=bqm(x2,x); return x - (x3/6) }
26
27func gen_tone(buf: *i64, f: i64) -> i64 {
28 let om: i64 = (K_MAGIC_411774 * f) / SR // 2*pi*f/SR in Q16
29 let a1: i64 = 2 * cos_fp(om)
30 var y2: i64 = 0
31 var y1: i64 = bqm(AMP, sin_fp(om))
32 buf[0] = 0; buf[1] = y1
33 var n: i64 = 2
34 while n < NS { let yn: i64 = bqm(a1, y1) - y2; buf[n] = yn; y2 = y1; y1 = yn; n = n + 1 }
35 let FN: i64 = SR / 3
36 var i: i64 = 0
37 while i < FN { buf[i] = buf[i] * i / FN; buf[NS - 1 - i] = buf[NS - 1 - i] * i / FN; i = i + 1 }
38 return 0
39}
40
41// generate one stereo beat track -> wav file. returns saved bytes (0 on fail).
42func gen_track(path: *u8, fbase: i64, fbeat: i64) -> i64 {
43 let L: *i64 = sys_mmap(NS*8) as *i64
44 let R: *i64 = sys_mmap(NS*8) as *i64
45 gen_tone(L, fbase)
46 gen_tone(R, fbase + fbeat)
47 let wavbuf: *u8 = sys_mmap(44 + NS*4 + 64)
48 let wlen: i64 = wav_render_stereo(L, R, NS, SR, wavbuf)
49 let saved: i64 = wav_save(path, wavbuf, wlen)
50 return saved
51}
52
53func main() -> i64 {
54 vp("nx_audio_beats_variants: generating delta/alpha/beta binaural beat tracks (sovereign .wav)\n" as *u8)
55 var ok: i64 = 0
56 let d: i64 = gen_track("web_assets/ng_beats_delta.wav\x00" as *u8, 200, 3)
57 vp(" delta (200/+3Hz deep): bytes=" as *u8); vpn(d); vp("\n" as *u8); if d > 44 { ok = ok + 1 }
58 let a: i64 = gen_track("web_assets/ng_beats_alpha.wav\x00" as *u8, 200, 10)
59 vp(" alpha (200/+10Hz relaxed focus): bytes=" as *u8); vpn(a); vp("\n" as *u8); if a > 44 { ok = ok + 1 }
60 let bt: i64 = gen_track("web_assets/ng_beats_beta.wav\x00" as *u8, 220, 18)
61 vp(" beta (220/+18Hz alert focus): bytes=" as *u8); vpn(bt); vp("\n" as *u8); if bt > 44 { ok = ok + 1 }
62 vp(" verdict=" as *u8)
63 if ok == 3 { vp("GREEN (3 new beat tracks generated)\n" as *u8); sys_exit(0); return 0 }
64 vp("RED\n" as *u8)
65 sys_exit(1); return 1
66}