aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / public / js / allen-audio.js
| 1 | /* |
| 2 | * allen-audio.js: Allen's startup sound, the one the iMac plays as its two |
| 3 | * black holes merge. Generated by om/allen-audio (Omega); do not edit. |
| 4 | * |
| 5 | * The sound is ../audio/allen-chirp.wav, written sample for sample by |
| 6 | * om/allen-audio/chirp.om, a port of aienux native/kernel/ui/chirp.c at |
| 7 | * da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c. It holds frames 545280 to 590400 |
| 8 | * of the night at 48000 Hz, 16-bit mono (the native left and right samples |
| 9 | * are equal): 11360 ms to 12300 ms of the night, the merger at 12000 ms. |
| 10 | * This script only plays it. |
| 11 | * |
| 12 | * window.AllenAudio |
| 13 | * enter(arg) call inside a click or key press. The only place an audio |
| 14 | * context is created or resumed. arg is a scene kind or |
| 15 | * { kind, nightMs }. The native source has one sound, the |
| 16 | * merger chirp of the two-hole night (kind 'night', alias |
| 17 | * 'home', the default); any other kind (the /allen/ tube |
| 18 | * included) has no native sound and resolves false without |
| 19 | * touching audio. nightMs is where the night is at the moment |
| 20 | * of the call (default 0: the night starts now). As on the |
| 21 | * iMac, the sound lands on the merger: |
| 22 | * before 11360 ms it waits, inside the chirp it starts from |
| 23 | * that moment, after 12300 ms nothing plays. While a sound |
| 24 | * is pending or playing, enter returns the same promise; |
| 25 | * after it ends or stops, the next gesture may enter again |
| 26 | * (a replay). Resolves true once the sound is scheduled, |
| 27 | * false when muted, unsupported, stopped or too late; never |
| 28 | * rejects. |
| 29 | * pause() suspends the audio context: its clock and the sound |
| 30 | * freeze together, as the scene's clock does. Done by |
| 31 | * itself when the page is hidden, so no chirp plays in the |
| 32 | * background. |
| 33 | * resume() resumes a context pause() suspended (the scene calls it |
| 34 | * when it runs again); never creates one, and does nothing |
| 35 | * while muted. |
| 36 | * stop() stops the sound and closes the audio context; done by |
| 37 | * itself on pagehide. Call it when the scene is destroyed |
| 38 | * or replayed. |
| 39 | * setMuted(b) stores the choice in localStorage 'allen.audio.muted' |
| 40 | * ('1' or '0'), the only key this script writes; muting |
| 41 | * stops the sound. |
| 42 | * muted() the stored choice. |
| 43 | * data-chirp-src on this script's tag points elsewhere (resolved against the |
| 44 | * page); by default the file sits beside this script's folder, so the same |
| 45 | * bytes work on aienos.com and on a localhost install. |
| 46 | */ |
| 47 | (function () { |
| 48 | 'use strict'; |
| 49 | var KEY = 'allen.audio.muted'; |
| 50 | var RATE = 48000; |
| 51 | var FRAMES = 45120; |
| 52 | var START_MS = 11360; |
| 53 | var MERGE_MS = 12000; |
| 54 | var END_MS = 12300; |
| 55 | |
| 56 | var me = document.currentScript; |
| 57 | var pick = me && me.getAttribute('data-chirp-src'); |
| 58 | var src = pick ? new URL(pick, location.href).href |
| 59 | : new URL('../audio/allen-chirp.wav', (me && me.src) || location.href).href; |
| 60 | |
| 61 | var ctx = null; |
| 62 | var node = null; |
| 63 | var entered = null; |
| 64 | var paused = false; |
| 65 | var pausedAt = 0; |
| 66 | var pausedMs = 0; |
| 67 | |
| 68 | function stored() { |
| 69 | try { return window.localStorage.getItem(KEY) === '1'; } catch (e) { return false; } |
| 70 | } |
| 71 | var isMuted = stored(); |
| 72 | |
| 73 | function close() { |
| 74 | var n = node; |
| 75 | var c = ctx; |
| 76 | node = null; |
| 77 | ctx = null; |
| 78 | entered = null; |
| 79 | paused = false; |
| 80 | if (n) { |
| 81 | n.onended = null; |
| 82 | try { n.stop(); } catch (e) { /* not started yet */ } |
| 83 | n.disconnect(); |
| 84 | } |
| 85 | if (c && c.close && c.state !== 'closed') c.close().then(null, function () {}); |
| 86 | } |
| 87 | |
| 88 | function word(v, at) { return v.getUint32(at, true); } |
| 89 | function half(v, at) { return v.getUint16(at, true); } |
| 90 | |
| 91 | /* The asset's exact header, or nothing: mono 16-bit PCM at RATE, FRAMES long. */ |
| 92 | function pcm(bytes) { |
| 93 | if (bytes.byteLength !== 44 + 2 * FRAMES) return null; |
| 94 | var v = new DataView(bytes); |
| 95 | if (word(v, 0) !== 0x46464952 || word(v, 4) !== 36 + 2 * FRAMES || word(v, 8) !== 0x45564157 || |
| 96 | word(v, 12) !== 0x20746d66 || word(v, 16) !== 16 || half(v, 20) !== 1 || half(v, 22) !== 1 || |
| 97 | word(v, 24) !== RATE || word(v, 28) !== 2 * RATE || half(v, 32) !== 2 || half(v, 34) !== 16 || |
| 98 | word(v, 36) !== 0x61746164 || word(v, 40) !== 2 * FRAMES) return null; |
| 99 | var out = new Float32Array(FRAMES); |
| 100 | for (var i = 0; i < FRAMES; i++) out[i] = v.getInt16(44 + 2 * i, true) / 32768; |
| 101 | return out; |
| 102 | } |
| 103 | |
| 104 | /* A silent frame started inside the gesture: some browsers unlock output only so. */ |
| 105 | function unlock(c) { |
| 106 | try { |
| 107 | var s = c.createBufferSource(); |
| 108 | s.buffer = c.createBuffer(1, 1, c.sampleRate); |
| 109 | s.connect(c.destination); |
| 110 | s.start(0); |
| 111 | } catch (e) { /* the real sound still tries */ } |
| 112 | } |
| 113 | |
| 114 | function play(c, samples, night) { |
| 115 | if (c !== ctx || isMuted || night >= END_MS) return false; |
| 116 | var buf = c.createBuffer(1, FRAMES, RATE); |
| 117 | buf.getChannelData(0).set(samples); |
| 118 | var n = c.createBufferSource(); |
| 119 | n.buffer = buf; |
| 120 | n.connect(c.destination); |
| 121 | n.onended = function () { if (node === n) close(); }; |
| 122 | node = n; |
| 123 | if (night < START_MS) n.start(c.currentTime + (START_MS - night) / 1000); |
| 124 | else n.start(0, (night - START_MS) / 1000); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | var KINDS = { night: 1, home: 1 }; |
| 129 | |
| 130 | function enter(arg) { |
| 131 | var opts = typeof arg === 'string' ? { kind: arg } : arg || {}; |
| 132 | if (!KINDS.hasOwnProperty(opts.kind || 'night')) return Promise.resolve(false); |
| 133 | if (entered) return entered; |
| 134 | var t0 = performance.now(); |
| 135 | pausedMs = 0; |
| 136 | var base = typeof opts.nightMs === 'number' && opts.nightMs >= 0 ? opts.nightMs : 0; |
| 137 | var AC = window.AudioContext || window.webkitAudioContext; |
| 138 | if (isMuted || !AC || !window.fetch || base >= END_MS) return Promise.resolve(false); |
| 139 | var c; |
| 140 | try { c = new AC(); } catch (e) { return Promise.resolve(false); } |
| 141 | ctx = c; |
| 142 | if (c.resume) c.resume().then(null, function () {}); |
| 143 | unlock(c); |
| 144 | entered = fetch(src, { credentials: 'same-origin' }) |
| 145 | .then(function (r) { |
| 146 | if (!r.ok) throw new Error('allen-chirp.wav ' + r.status); |
| 147 | return r.arrayBuffer(); |
| 148 | }) |
| 149 | .then(function (bytes) { |
| 150 | var samples = pcm(bytes); |
| 151 | var held = pausedMs + (paused ? performance.now() - pausedAt : 0); |
| 152 | var ok = samples ? play(c, samples, base + performance.now() - t0 - held) : false; |
| 153 | if (!ok && ctx === c) close(); |
| 154 | return ok; |
| 155 | }) |
| 156 | .then(null, function () { |
| 157 | if (ctx === c) close(); |
| 158 | return false; |
| 159 | }); |
| 160 | return entered; |
| 161 | } |
| 162 | |
| 163 | function pause() { |
| 164 | if (!ctx || paused) return; |
| 165 | paused = true; |
| 166 | pausedAt = performance.now(); |
| 167 | if (ctx.suspend) ctx.suspend().then(null, function () {}); |
| 168 | } |
| 169 | |
| 170 | function resume() { |
| 171 | if (!ctx || !paused || isMuted) return; |
| 172 | paused = false; |
| 173 | pausedMs += performance.now() - pausedAt; |
| 174 | if (ctx.resume) ctx.resume().then(null, function () {}); |
| 175 | } |
| 176 | |
| 177 | function setMuted(on) { |
| 178 | isMuted = !!on; |
| 179 | try { window.localStorage.setItem(KEY, isMuted ? '1' : '0'); } catch (e) { /* kept for this page */ } |
| 180 | if (isMuted) close(); |
| 181 | } |
| 182 | |
| 183 | document.addEventListener('visibilitychange', function () { |
| 184 | if (document.visibilityState === 'hidden') pause(); |
| 185 | }); |
| 186 | window.addEventListener('pagehide', close); |
| 187 | |
| 188 | window.addEventListener('storage', function (e) { |
| 189 | if (e.key !== KEY) return; |
| 190 | isMuted = e.newValue === '1'; |
| 191 | if (isMuted) close(); |
| 192 | }); |
| 193 | |
| 194 | window.AllenAudio = Object.freeze({ |
| 195 | enter: enter, |
| 196 | stop: close, |
| 197 | pause: pause, |
| 198 | resume: resume, |
| 199 | setMuted: setMuted, |
| 200 | muted: function () { return isMuted; }, |
| 201 | src: src, |
| 202 | kinds: Object.freeze(['night', 'home']), |
| 203 | timeline: Object.freeze({ rate: RATE, frames: FRAMES, startMs: START_MS, mergeMs: MERGE_MS, endMs: END_MS }) |
| 204 | }); |
| 205 | })(); |