nx_audio_sfx_web.nx source
↩ module page · 79 lines · 3457 B
1// nx_audio_sfx_web.nx -- sovereign WEB-AUDIO SFX module: emits a self-contained
2// <script> exposing playCoin()/playLaser()/playExplosion()/playBeep(), each a
3// base64 WAV generated by the sovereign SFX library. Drop into econsim / any game
4// page and call on events. Composes sfx + wav + b64_encode (all sovereign).
5// license_tier: ORIGINAL.
6
7import "nx_syscalls.nx"
8import "nx_audio_mix.nx"
9import "nx_audio_synth.nx"
10import "nx_audio_fx.nx"
11import "nx_audio_sfx.nx"
12import "nx_audio_wav.nx"
13import "nx_base64.nx"
14const K_MAGIC_8000: i64 = 8000
15const K_MAGIC_4000: i64 = 4000
16const K_MAGIC_262144: i64 = 262144
17
18func sw_app(out: *u8, off: *i64, s: *u8) -> i64 {
19 var i: i64 = 0
20 while s[i] != (0 as u8) { out[off[0]] = s[i]; off[0] = off[0] + 1; i = i + 1 }
21 return 0
22}
23
24// buf already holds n SFX samples -> append `window.<name>=function(){P('<b64 wav>')};`
25func sw_emit_one(name: *u8, buf: *i64, n: i64, rate: i64, out: *u8, off: *i64) -> i64 {
26 let wavbuf: *u8 = sys_mmap(n * 4 + 128)
27 let wb: i64 = wav_render_stereo(buf, buf, n, rate, wavbuf)
28 let b64: *u8 = sys_mmap(wb * 2 + 64)
29 let bn: i64 = b64_encode(wavbuf, wb, b64)
30 sw_app(out, off, "window." as *u8)
31 sw_app(out, off, name)
32 sw_app(out, off, "=function(){P('" as *u8)
33 var i: i64 = 0
34 while i < bn { out[off[0]] = b64[i]; off[0] = off[0] + 1; i = i + 1 }
35 sw_app(out, off, "')};" as *u8)
36 return 0
37}
38
39// build the <script> SFX module into out; returns length
40func nx_sfx_js_module(out: *u8, out_cap: i64) -> i64 {
41 let off: *i64 = (sys_mmap(8)) as *i64
42 off[0] = 0
43 sw_app(out, off, "<script>(function(){function P(b){try{var a=new Audio('data:audio/wav;base64,'+b);a.play()}catch(e){}}" as *u8)
44 let rate: i64 = K_MAGIC_8000
45 let N: i64 = K_MAGIC_4000
46 let buf: *i64 = (sys_mmap(N * 8 + 64)) as *i64
47 sfx_coin(buf, N, rate); sw_emit_one("playCoin" as *u8, buf, N, rate, out, off)
48 sfx_laser(buf, N, rate); sw_emit_one("playLaser" as *u8, buf, N, rate, out, off)
49 sfx_explosion(buf, N, rate); sw_emit_one("playExplosion" as *u8, buf, N, rate, out, off)
50 sfx_beep(buf, N, rate); sw_emit_one("playBeep" as *u8, buf, N, rate, out, off)
51 sw_app(out, off, "})();</script>" as *u8)
52 out[off[0]] = 0 as u8
53 return off[0]
54}
55
56// a standalone demo page with buttons that play each SFX
57func nx_sfx_demo_page(out: *u8, out_cap: i64) -> i64 {
58 let off: *i64 = (sys_mmap(8)) as *i64
59 off[0] = 0
60 sw_app(out, off, "<!doctype html><html><head><meta charset=\"utf-8\"><title>Sovereign Game SFX</title></head><body><h1>Sovereign Game SFX</h1><p>Generated 100% in NishiLang. No FMOD.</p>" as *u8)
61 sw_app(out, off, "<button onclick=playCoin()>Coin</button> <button onclick=playLaser()>Laser</button> <button onclick=playExplosion()>Explosion</button> <button onclick=playBeep()>Beep</button>" as *u8)
62 let modoff: i64 = off[0]
63 let mlen: i64 = nx_sfx_js_module(out + modoff, out_cap - modoff)
64 off[0] = modoff + mlen
65 sw_app(out, off, "</body></html>\n" as *u8)
66 out[off[0]] = 0 as u8
67 return off[0]
68}
69
70func nx_sfx_demo_save(path: *u8) -> i64 {
71 let out: *u8 = sys_mmap(K_MAGIC_262144)
72 let len: i64 = nx_sfx_demo_page(out, K_MAGIC_262144)
73 let fd: i64 = sys_openat_wr(path, 420)
74 if fd < 0 { return 0 - 1 }
75 var wr: i64 = 0
76 while wr < len { let c: i64 = sys_write(fd, out + wr, len - wr); if c <= 0 { break } wr = wr + c }
77 sys_close(fd)
78 return wr
79}