aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / public / js / allen-scene.js
| 1 | /* Generated from om/allen-scene/scene.om by pinned seeded Omega. |
| 2 | * Controller: Omega-generated JavaScript. Rendering/audio: unchanged native C, |
| 3 | * aien-dev/aienux@da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c, compiled to WASM. |
| 4 | * This source pin was staged, not hardware-confirmed by the available report. |
| 5 | * Web opening reuses native dusk; enter('birth') is the original binary merger. |
| 6 | * Catalogue/licences and native dependency boundary: om/allen-scene/provenance.txt. |
| 7 | * WolframLanguageEvaluator: 1000/60 -> 50/3 ms; (1000/60)/2 -> 25/3 ms; |
| 8 | * 48000 (12300-11360)/1000 -> 45120 frames (native chirp non-silent window). |
| 9 | */ |
| 10 | (() => { |
| 11 | 'use strict'; |
| 12 | const PIN = 'da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c'; |
| 13 | const mounted = new WeakMap(); |
| 14 | let compiled; |
| 15 | function module() { |
| 16 | if (!compiled) compiled = fetch('/allen/scene.wasm', {credentials:'omit'}) |
| 17 | .then(response => { if (!response.ok) throw new Error('Native scene unavailable'); return response.arrayBuffer(); }) |
| 18 | .then(bytes => WebAssembly.compile(bytes)).catch(error => { compiled = null; throw error; }); |
| 19 | return compiled; |
| 20 | } |
| 21 | function mount(canvas, options = {}) { |
| 22 | if (!(canvas instanceof HTMLCanvasElement)) throw new TypeError('AllenScene requires a canvas'); |
| 23 | if (mounted.has(canvas)) return mounted.get(canvas); |
| 24 | const compact = Boolean(options.compact), interactive = Boolean(options.interactive) && !compact; |
| 25 | const opening = options.opening === undefined ? (compact ? 'idle' : 'dusk') : options.opening; |
| 26 | if(opening !== 'idle' && opening !== 'dusk' && opening !== 'birth') throw new RangeError('Invalid Allen scene opening'); |
| 27 | const ctx = canvas.getContext('2d', {alpha:false}); |
| 28 | if (!ctx) throw new Error('Canvas rendering unavailable'); |
| 29 | const motion = matchMedia('(prefers-reduced-motion: reduce)'); |
| 30 | const cleanup = [], letters = Array.from({length:64}, () => ({char:0,start:0,slot:0})); |
| 31 | const wordTimes = new Float64Array(256); |
| 32 | let ex, image, width = 0, height = 0, disposed = false, failed = false; |
| 33 | let raf = 0, lastWall = 0, clock = 20000, kind = 0, modeValue = 0; |
| 34 | let visible = true, reduced = motion.matches, dirty = true, resizePending = true; |
| 35 | const connection = typeof navigator === 'object' ? navigator.connection : null; |
| 36 | let calm = options.calm === undefined ? Boolean(connection && connection.saveData) : Boolean(options.calm); |
| 37 | let quality = 0, overBudget = 0, underBudget = 0, qualityHold = 0, samples = 0; |
| 38 | const renderScales = [1, 3/4, 9/16, 9/16]; |
| 39 | const qualityNames = ['full-60','balanced-60','small-60','small-30']; |
| 40 | const frameBudget = 1000/60; |
| 41 | // Wolfram: 1000/60 -> 50/3 ms; budget/2 -> 25/3; 1280*{1,3/4,9/16} -> {1280,960,720}. |
| 42 | function still() { return reduced || compact || calm; } |
| 43 | function qualityLabel() { canvas.dataset.sceneQuality = reduced ? 'reduced' : compact ? 'compact' : calm ? 'calm' : qualityNames[quality]; } |
| 44 | function measureDraw(cost) { |
| 45 | samples++; |
| 46 | if(samples % 60 === 0) canvas.dataset.sceneDrawMs=String(cost); |
| 47 | if(qualityHold) { qualityHold--; return; } |
| 48 | overBudget=cost>frameBudget?overBudget+1:0; |
| 49 | underBudget=cost<frameBudget/2?underBudget+1:0; |
| 50 | const next=overBudget>=8?Math.min(3,quality+1):underBudget>=180?Math.max(0,quality-1):quality; |
| 51 | if(next!==quality) { |
| 52 | quality=next;qualityHold=60;overBudget=underBudget=0; |
| 53 | resizePending=dirty=true;canvas.dataset.sceneDrawMs=String(cost);qualityLabel(); |
| 54 | } |
| 55 | } |
| 56 | let letterHead = 0, wordRead = 0, wordWrite = 0, lastWord = -1000, speakUntil = 0, keyUntil = 0; |
| 57 | let ra = 113697000, dec = -72757000, fov = 80000, viewPending = false; |
| 58 | let dragId = null, dragX = 0, dragY = 0, pinchDistance = 0; |
| 59 | const touches = [{id:null,x:0,y:0},{id:null,x:0,y:0}]; |
| 60 | let ownsAudio = false, completeSent = false, resolveSize, rejectSize; |
| 61 | let pendingEnter = compact || opening === 'idle' ? 0 : opening === 'birth' ? 2 : 1, pendingStatic = false; |
| 62 | canvas.dataset.sceneSource = PIN; |
| 63 | canvas.dataset.sceneEngine = 'loading'; |
| 64 | qualityLabel(); |
| 65 | function listen(target, name, fn, opts) { |
| 66 | target.addEventListener(name, fn, opts); |
| 67 | cleanup.push(() => target.removeEventListener(name, fn, opts)); |
| 68 | } |
| 69 | function stopAudio() { |
| 70 | if (ownsAudio && window.AllenAudio) window.AllenAudio.stop(); |
| 71 | ownsAudio=false; |
| 72 | } |
| 73 | function awake() { return !disposed && !failed && !document.hidden && visible; } |
| 74 | function request() { |
| 75 | if (!raf && ex && awake() && (!still() || dirty)) raf = requestAnimationFrame(tick); |
| 76 | } |
| 77 | function size() { |
| 78 | const box = canvas.getBoundingClientRect(); |
| 79 | if (box.width <= 0 || box.height <= 0) return false; |
| 80 | // Policy bound, not a physical-resolution claim. Same aspect ratio, native design scaling. |
| 81 | const scale = Math.min(devicePixelRatio || 1, 1280 / Math.max(box.width,box.height))*renderScales[quality]; |
| 82 | const w = Math.max(64,Math.round(box.width*scale)), h = Math.max(64,Math.round(box.height*scale)); |
| 83 | resizePending = false; |
| 84 | if (w === width && h === height) return true; |
| 85 | width=w; height=h; canvas.width=w; canvas.height=h; |
| 86 | if (!ex.scene_init(w,h,compact?1:0)) throw new Error('Native scene size refused'); |
| 87 | image = new ImageData(new Uint8ClampedArray(ex.memory.buffer,ex.scene_pixels(),w*h*4),w,h); |
| 88 | ex.scene_enter(kind); |
| 89 | // Resize keeps phase and mode but does not manufacture the old frame history. |
| 90 | ex.scene_step(Math.floor(clock),modeValue,0); |
| 91 | viewPending=true; |
| 92 | if(resolveSize) {resolveSize();resolveSize=null;rejectSize=null;} |
| 93 | return true; |
| 94 | } |
| 95 | function reset(kindValue) { |
| 96 | kind=kindValue; clock=kind===0?20000:0; lastWall=0;completeSent=false; |
| 97 | wordRead=wordWrite=0; lastWord=-1000; speakUntil=keyUntil=0; modeValue=0; |
| 98 | for (const letter of letters) letter.char=0; |
| 99 | stopAudio(); |
| 100 | if (ex && width) ex.scene_enter(kind); |
| 101 | dirty=true; request(); |
| 102 | } |
| 103 | function render() { |
| 104 | if (!ex || !awake()) return; |
| 105 | if (resizePending && !size()) return; |
| 106 | if (!image) return; |
| 107 | if (viewPending && !(kind===1 && clock<4200)) { |
| 108 | if (!ex.scene_view(Math.round(ra),Math.round(dec),Math.round(fov))) throw new Error('Native sky view refused'); |
| 109 | viewPending=false; |
| 110 | } |
| 111 | const time=Math.floor(clock); |
| 112 | while(wordRead!==wordWrite && wordTimes[wordRead]<=clock) { |
| 113 | lastWord=wordTimes[wordRead];ex.scene_word(Math.floor(lastWord));wordRead=(wordRead+1)%wordTimes.length; |
| 114 | } |
| 115 | let currentMode=modeValue; |
| 116 | if(clock<speakUntil) currentMode=3; |
| 117 | else if(currentMode===3) currentMode=0; |
| 118 | if(currentMode===0 && clock<keyUntil) currentMode=1; |
| 119 | // Native allen_ui.c voice envelope (per-word exp decay, 200 ms). |
| 120 | const voice=clock<speakUntil && lastWord>=0?ex.scene_voice(time,Math.floor(lastWord)):0; |
| 121 | const shimmer=clock<speakUntil?256:0; |
| 122 | ex.scene_step(time,currentMode,reduced?0:voice); |
| 123 | ex.scene_draw(reduced?0:shimmer,reduced?0:voice); |
| 124 | if(!reduced && !compact) for(const letter of letters) { |
| 125 | if(!letter.char || clock<letter.start) continue; |
| 126 | const u=Math.min(1,(clock-letter.start)/900); |
| 127 | if(u>=1) {letter.char=0;continue;} |
| 128 | // Web enhancement: native glyphs lift from input to Allen. No HTML/text injection. |
| 129 | const ease=1-(1-u)*(1-u)*(1-u); |
| 130 | const x=width/2+(letter.slot-4)*width/90*(1-ease); |
| 131 | const y=height*(0.86+(0.4-0.86)*ease); |
| 132 | ex.scene_glyph(letter.char,Math.round(x),Math.round(y),Math.round(256*(1-u))); |
| 133 | } |
| 134 | ctx.putImageData(image,0,0); |
| 135 | canvas.dataset.scenePhase=kind===1 && clock<4200?'tube':kind===2 && clock<12300?'merger':clock<(kind===1?7200:15300)?'forming':'presence'; |
| 136 | canvas.dataset.sceneTime=String(time); |
| 137 | canvas.dataset.sceneEngine='native-wasm'; |
| 138 | dirty=false; |
| 139 | if(!completeSent && (kind===0 || clock>=(kind===1?7200:15300))) { |
| 140 | completeSent=true; |
| 141 | canvas.dispatchEvent(new CustomEvent('allen-scene-complete',{bubbles:true,detail:{kind:kind===2?'birth':kind===1?'dusk':'idle',time}})); |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | function error(err) { |
| 146 | failed=true;cancelAnimationFrame(raf);raf=0;stopAudio(); |
| 147 | if(rejectSize) {rejectSize(err);resolveSize=null;rejectSize=null;} |
| 148 | canvas.dataset.sceneEngine='unavailable'; |
| 149 | canvas.dispatchEvent(new CustomEvent('allen-scene-error',{bubbles:true,detail:{message:String(err.message||err)}})); |
| 150 | } |
| 151 | function tick(wall) { |
| 152 | raf=0; |
| 153 | if(!awake()) {lastWall=0;return;} |
| 154 | const dt=lastWall?wall-lastWall:0; |
| 155 | // Lowest level skips alternate 60 Hz callbacks; 25 ms tolerates timestamp jitter around 50/3 and 100/3 ms. |
| 156 | if(!still() && quality===3 && dt<25 && !dirty) {request();return;} |
| 157 | lastWall=wall; |
| 158 | if(!still()) clock+=dt; // source clock advances; native integrators bound their own catch-up |
| 159 | const sample=!still() && !resizePending && !viewPending; |
| 160 | const began=sample?performance.now():0; |
| 161 | let drawn=false; |
| 162 | try { drawn=render(); } catch(err) { error(err); } |
| 163 | if(sample && drawn) measureDraw(performance.now()-began); |
| 164 | if(drawn && !still()) request(); |
| 165 | } |
| 166 | function motionChanged() { |
| 167 | reduced=motion.matches;lastWall=0;cancelAnimationFrame(raf);raf=0;stopAudio(); |
| 168 | if(still()) { reset(0); pendingStatic=true; } |
| 169 | overBudget=underBudget=0;qualityLabel(); |
| 170 | dirty=true;request(); |
| 171 | } |
| 172 | function visibility() { |
| 173 | lastWall=0; |
| 174 | if(!awake()) {cancelAnimationFrame(raf);raf=0;if(ownsAudio && window.AllenAudio) window.AllenAudio.pause();} |
| 175 | else {if(ownsAudio && !still() && window.AllenAudio) void window.AllenAudio.resume();overBudget=underBudget=0;dirty=true;request();} |
| 176 | } |
| 177 | function changeView(dx,dy,zoom) { |
| 178 | if(!interactive || kind===1 && clock<4200) return; |
| 179 | ra=((ra+dx)%360000000+360000000)%360000000; |
| 180 | dec=Math.max(-89000000,Math.min(89000000,dec+dy)); |
| 181 | fov=Math.max(10000,Math.min(120000,fov*zoom)); |
| 182 | viewPending=dirty=true;request(); |
| 183 | } |
| 184 | function home() {ra=113697000;dec=-72757000;fov=80000;viewPending=dirty=true;request();} |
| 185 | const api = { |
| 186 | key(text) { |
| 187 | if(disposed) return; |
| 188 | text=String(text).slice(-64);keyUntil=clock+4000; |
| 189 | if(ex) ex.scene_key(Math.floor(clock)); |
| 190 | if(!reduced && !compact) for(let i=0;i<text.length;i++) { |
| 191 | const c=text.charCodeAt(i);if(c<32 || c>126) continue; |
| 192 | const letter=letters[letterHead];letterHead=(letterHead+1)%letters.length; |
| 193 | letter.char=c;letter.start=clock+i*35;letter.slot=i%9; |
| 194 | } |
| 195 | dirty=true;request(); |
| 196 | }, |
| 197 | speak(text) { |
| 198 | if(disposed) return; |
| 199 | text=String(text).slice(0,4096); |
| 200 | let offset=0;const start=Math.max(clock,speakUntil-300); |
| 201 | for(let i=0;i<text.length;i++) { |
| 202 | if(!/\s/.test(text[i]) && (i===0 || /\s/.test(text[i-1]))) { |
| 203 | const next=(wordWrite+1)%wordTimes.length; |
| 204 | if(next===wordRead) break; |
| 205 | wordTimes[wordWrite]=start+offset;wordWrite=next; |
| 206 | } |
| 207 | offset+=35; |
| 208 | } |
| 209 | speakUntil=Math.min(clock+30000,start+offset+300);dirty=true;request(); |
| 210 | }, |
| 211 | mode(name) { |
| 212 | const modes={idle:0,listening:1,listen:1,thinking:2,think:2,speaking:3,speak:3}; |
| 213 | if(Object.prototype.hasOwnProperty.call(modes,name)) {modeValue=modes[name];dirty=true;request();} |
| 214 | }, |
| 215 | enter(name='dusk') { |
| 216 | if(disposed) return; |
| 217 | const next=still() || name==='idle'?0:(name==='birth' || name==='boot' || name==='night')?2:1; |
| 218 | if(next===2) ownsAudio=false; // caller starts audio in this same gesture before entering |
| 219 | pendingEnter=next;reset(next); |
| 220 | if(next===2 && window.AllenAudio) { |
| 221 | ownsAudio=true; |
| 222 | // AudioPort is invoked by the caller's gesture; this instance only owns its pause/stop lifecycle. |
| 223 | } |
| 224 | if(ex && width) pendingEnter=null; |
| 225 | request(); |
| 226 | }, |
| 227 | home, |
| 228 | zoom(direction) { |
| 229 | if(direction!=='in' && direction!=='out') throw new RangeError('Scene zoom must be in or out'); |
| 230 | changeView(0,0,direction==='in'?1/1.1:1.1); |
| 231 | }, |
| 232 | calm(enabled) { |
| 233 | if(disposed) return; |
| 234 | calm=Boolean(enabled);lastWall=0;cancelAnimationFrame(raf);raf=0; |
| 235 | if(calm) reset(0); |
| 236 | overBudget=underBudget=0;qualityLabel();dirty=true;request(); |
| 237 | }, |
| 238 | frame(ms) { |
| 239 | if(!Number.isFinite(ms) || ms<0 || ms>86400000) throw new RangeError('Scene time outside one day'); |
| 240 | cancelAnimationFrame(raf);raf=0;lastWall=0; |
| 241 | if(reduced) clock=20000;else clock=ms; |
| 242 | dirty=true; |
| 243 | if(ex) {try {render();} catch(err) {error(err);}} |
| 244 | }, |
| 245 | destroy() { |
| 246 | if(disposed) return; |
| 247 | disposed=true;cancelAnimationFrame(raf);raf=0;stopAudio(); |
| 248 | if(rejectSize) {rejectSize(new Error('Scene destroyed before initialization'));resolveSize=null;rejectSize=null;} |
| 249 | for(const fn of cleanup) fn(); |
| 250 | mounted.delete(canvas);image=null;ex=null; |
| 251 | canvas.dataset.sceneEngine='destroyed'; |
| 252 | } |
| 253 | }; |
| 254 | mounted.set(canvas,api); |
| 255 | listen(document,'visibilitychange',visibility); |
| 256 | listen(motion,'change',motionChanged); |
| 257 | if(typeof ResizeObserver==='function') { |
| 258 | const resize=new ResizeObserver(() => {resizePending=dirty=true;request();});resize.observe(canvas);cleanup.push(() => resize.disconnect()); |
| 259 | } else listen(window,'resize',() => {resizePending=dirty=true;request();}); |
| 260 | if(typeof IntersectionObserver==='function') { |
| 261 | const observer=new IntersectionObserver(entries => {visible=entries[0].isIntersecting;visibility();});observer.observe(canvas);cleanup.push(() => observer.disconnect()); |
| 262 | } |
| 263 | if(interactive) { |
| 264 | const oldTouch=canvas.style.touchAction;canvas.style.touchAction='pinch-zoom';cleanup.push(() => {canvas.style.touchAction=oldTouch;}); |
| 265 | listen(canvas,'pointerdown',event => { |
| 266 | if(event.button!==0) return; |
| 267 | if(event.pointerType==='touch') { |
| 268 | const slot=touches.find(touch => touch.id===null); |
| 269 | if(!slot) return; |
| 270 | slot.id=event.pointerId;slot.x=event.clientX;slot.y=event.clientY; |
| 271 | if(touches[0].id!==null && touches[1].id!==null) pinchDistance=Math.hypot(touches[0].x-touches[1].x,touches[0].y-touches[1].y); |
| 272 | } |
| 273 | dragId=event.pointerId;dragX=event.clientX;dragY=event.clientY; |
| 274 | canvas.setPointerCapture(dragId);canvas.focus({preventScroll:true}); |
| 275 | }); |
| 276 | listen(canvas,'pointermove',event => { |
| 277 | if(event.pointerType==='touch') { |
| 278 | const touch=touches.find(touch => touch.id===event.pointerId); |
| 279 | if(!touch) return; |
| 280 | touch.x=event.clientX;touch.y=event.clientY; |
| 281 | if(touches[0].id!==null && touches[1].id!==null) { |
| 282 | const distance=Math.hypot(touches[0].x-touches[1].x,touches[0].y-touches[1].y); |
| 283 | if(distance>0 && pinchDistance>0) changeView(0,0,pinchDistance/distance); |
| 284 | pinchDistance=distance;return; |
| 285 | } |
| 286 | } |
| 287 | if(event.pointerId!==dragId) return; |
| 288 | const box=canvas.getBoundingClientRect(); |
| 289 | changeView(-(event.clientX-dragX)*fov*1000/Math.max(1,box.width),(event.clientY-dragY)*fov*1000/Math.max(1,box.width),1); |
| 290 | dragX=event.clientX;dragY=event.clientY; |
| 291 | }); |
| 292 | const release=event => { |
| 293 | for(const touch of touches) if(touch.id===event.pointerId) touch.id=null; |
| 294 | pinchDistance=0; |
| 295 | if(event.pointerId===dragId) { |
| 296 | dragId=null; |
| 297 | if(canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId); |
| 298 | const remaining=touches.find(touch => touch.id!==null); |
| 299 | if(remaining) {dragId=remaining.id;dragX=remaining.x;dragY=remaining.y;} |
| 300 | } |
| 301 | }; |
| 302 | listen(canvas,'pointerup',release);listen(canvas,'pointercancel',release);listen(canvas,'lostpointercapture',release); |
| 303 | listen(canvas,'wheel',event => {if(event.ctrlKey || event.metaKey) return;event.preventDefault();changeView(0,0,Math.pow(2,Math.max(-600,Math.min(600,event.deltaY))/600));},{passive:false}); |
| 304 | listen(canvas,'keydown',event => { |
| 305 | const step=fov*1000/20; |
| 306 | switch(event.key) { |
| 307 | case 'ArrowLeft':changeView(-step,0,1);break; |
| 308 | case 'ArrowRight':changeView(step,0,1);break; |
| 309 | case 'ArrowUp':changeView(0,step,1);break; |
| 310 | case 'ArrowDown':changeView(0,-step,1);break; |
| 311 | case '+':case '=':changeView(0,0,1/1.1);break; |
| 312 | case '-':changeView(0,0,1.1);break; |
| 313 | case 'Home':home();break; |
| 314 | default:return; |
| 315 | } |
| 316 | event.preventDefault(); |
| 317 | }); |
| 318 | } |
| 319 | api.ready=module().then(module => WebAssembly.instantiate(module,{})).then(async instance => { |
| 320 | if(disposed) return api; |
| 321 | ex=instance.exports; |
| 322 | const initial=still()?0:pendingEnter===null?0:pendingEnter; |
| 323 | // Preserve an audio gesture made before compilation completes. |
| 324 | const audioOwner=ownsAudio;ownsAudio=false;reset(initial);ownsAudio=audioOwner; |
| 325 | pendingEnter=null; |
| 326 | if(!size()) await new Promise((resolve,reject) => {resolveSize=resolve;rejectSize=reject;}); |
| 327 | if(disposed) return api; |
| 328 | if(still() || pendingStatic) {clock=20000;ex.scene_step(20000,0,0);pendingStatic=false;} |
| 329 | render();request(); |
| 330 | canvas.dispatchEvent(new CustomEvent('allen-scene-initialized',{bubbles:true,detail:{source:PIN}})); |
| 331 | return api; |
| 332 | }).catch(err => {if(!disposed) error(err);throw err;}); |
| 333 | // Expose the rejection to callers while avoiding an unhandled rejection for decorative mounts. |
| 334 | api.ready.catch(() => {}); |
| 335 | return api; |
| 336 | } |
| 337 | window.AllenScene=Object.freeze({mount,source:PIN}); |
| 338 | document.dispatchEvent(new CustomEvent('allen-scene-ready')); |
| 339 | })(); |