interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / allen-audio / asset.om

31 lines · 9024 bytes Raw Blame History
1note Web half of Allen's startup sound; composed after chirp.om by check.sh.
2note fd 1: public/audio/allen-chirp.wav, frames FZERO..FEND of the night (45120 frames, 940 ms),
3note 16-bit mono at 48000 Hz, 90284 bytes. The native fill writes equal left and right samples, so
4note one channel keeps every sample. fd 3: public/js/allen-audio.js, the browser controller that
5note plays those bytes with WebAudio after a user gesture. Its text is the WebAudio glue only: no
6note synthesis happens in the browser.
7
8text jsdoc "/*\n * allen-audio.js: Allen's startup sound, the one the iMac plays as its two\n * black holes merge. Generated by om/allen-audio (Omega); do not edit.\n *\n * The sound is ../audio/allen-chirp.wav, written sample for sample by\n * om/allen-audio/chirp.om, a port of aienux native/kernel/ui/chirp.c at\n * da55660ea7337f2b6a42095fcd1fb6ddebe1cc7c. It holds frames 545280 to 590400\n * of the night at 48000 Hz, 16-bit mono (the native left and right samples\n * are equal): 11360 ms to 12300 ms of the night, the merger at 12000 ms.\n * This script only plays it.\n *\n * window.AllenAudio\n * enter(arg) call inside a click or key press. The only place an audio\n * context is created or resumed. arg is a scene kind or\n * { kind, nightMs }. The native source has one sound, the\n * merger chirp of the two-hole night (kind 'night', alias\n * 'home', the default); any other kind (the /allen/ tube\n * included) has no native sound and resolves false without\n * touching audio. nightMs is where the night is at the moment\n * of the call (default 0: the night starts now). As on the\n * iMac, the sound lands on the merger:\n * before 11360 ms it waits, inside the chirp it starts from\n * that moment, after 12300 ms nothing plays. While a sound\n * is pending or playing, enter returns the same promise;\n * after it ends or stops, the next gesture may enter again\n * (a replay). Resolves true once the sound is scheduled,\n * false when muted, unsupported, stopped or too late; never\n * rejects.\n * pause() suspends the audio context: its clock and the sound\n * freeze together, as the scene's clock does. Done by\n * itself when the page is hidden, so no chirp plays in the\n * background.\n * resume() resumes a context pause() suspended (the scene calls it\n * when it runs again); never creates one, and does nothing\n * while muted.\n * stop() stops the sound and closes the audio context; done by\n * itself on pagehide. Call it when the scene is destroyed\n * or replayed.\n * setMuted(b) stores the choice in localStorage 'allen.audio.muted'\n * ('1' or '0'), the only key this script writes; muting\n * stops the sound.\n * muted() the stored choice.\n * data-chirp-src on this script's tag points elsewhere (resolved against the\n * page); by default the file sits beside this script's folder, so the same\n * bytes work on aienos.com and on a localhost install.\n */\n(function () {\n 'use strict';\n var KEY = 'allen.audio.muted';\n var RATE = 48000;\n var FRAMES = 45120;\n var START_MS = 11360;\n var MERGE_MS = 12000;\n var END_MS = 12300;\n\n var me = document.currentScript;\n var pick = me && me.getAttribute('data-chirp-src');\n var src = pick ? new URL(pick, location.href).href\n : new URL('../audio/allen-chirp.wav', (me && me.src) || location.href).href;\n\n var ctx = null;\n var node = null;\n var entered = null;\n var paused = false;\n var pausedAt = 0;\n var pausedMs = 0;\n\n function stored() {\n try { return window.localStorage.getItem(KEY) === '1'; } catch (e) { return false; }\n }\n var isMuted = stored();\n\n function close() {\n var n = node;\n var c = ctx;\n node = null;\n ctx = null;\n entered = null;\n paused = false;\n if (n) {\n n.onended = null;\n try { n.stop(); } catch (e) { /* not started yet */ }\n n.disconnect();\n }\n if (c && c.close && c.state !== 'closed') c.close().then(null, function () {});\n }\n\n function word(v, at) { return v.getUint32(at, true); }\n function half(v, at) { return v.getUint16(at, true); }\n\n /* The asset's exact header, or nothing: mono 16-bit PCM at RATE, FRAMES long. */\n function pcm(bytes) {\n if (bytes.byteLength !== 44 + 2 * FRAMES) return null;\n var v = new DataView(bytes);\n if (word(v, 0) !== 0x46464952 || word(v, 4) !== 36 + 2 * FRAMES || word(v, 8) !== 0x45564157 ||\n word(v, 12) !== 0x20746d66 || word(v, 16) !== 16 || half(v, 20) !== 1 || half(v, 22) !== 1 ||\n word(v, 24) !== RATE || word(v, 28) !== 2 * RATE || half(v, 32) !== 2 || half(v, 34) !== 16 ||\n word(v, 36) !== 0x61746164 || word(v, 40) !== 2 * FRAMES) return null;\n var out = new Float32Array(FRAMES);\n for (var i = 0; i < FRAMES; i++) out[i] = v.getInt16(44 + 2 * i, true) / 32768;\n return out;\n }\n\n /* A silent frame started inside the gesture: some browsers unlock output only so. */\n function unlock(c) {\n try {\n var s = c.createBufferSource();\n s.buffer = c.createBuffer(1, 1, c.sampleRate);\n s.connect(c.destination);\n s.start(0);\n } catch (e) { /* the real sound still tries */ }\n }\n\n function play(c, samples, night) {\n if (c !== ctx || isMuted || night >= END_MS) return false;\n var buf = c.createBuffer(1, FRAMES, RATE);\n buf.getChannelData(0).set(samples);\n var n = c.createBufferSource();\n n.buffer = buf;\n n.connect(c.destination);\n n.onended = function () { if (node === n) close(); };\n node = n;\n if (night < START_MS) n.start(c.currentTime + (START_MS - night) / 1000);\n else n.start(0, (night - START_MS) / 1000);\n return true;\n }\n\n var KINDS = { night: 1, home: 1 };\n\n function enter(arg) {\n var opts = typeof arg === 'string' ? { kind: arg } : arg || {};\n if (!KINDS.hasOwnProperty(opts.kind || 'night')) return Promise.resolve(false);\n if (entered) return entered;\n var t0 = performance.now();\n pausedMs = 0;\n var base = typeof opts.nightMs === 'number' && opts.nightMs >= 0 ? opts.nightMs : 0;\n var AC = window.AudioContext || window.webkitAudioContext;\n if (isMuted || !AC || !window.fetch || base >= END_MS) return Promise.resolve(false);\n var c;\n try { c = new AC(); } catch (e) { return Promise.resolve(false); }\n ctx = c;\n if (c.resume) c.resume().then(null, function () {});\n unlock(c);\n entered = fetch(src, { credentials: 'same-origin' })\n .then(function (r) {\n if (!r.ok) throw new Error('allen-chirp.wav ' + r.status);\n return r.arrayBuffer();\n })\n .then(function (bytes) {\n var samples = pcm(bytes);\n var held = pausedMs + (paused ? performance.now() - pausedAt : 0);\n var ok = samples ? play(c, samples, base + performance.now() - t0 - held) : false;\n if (!ok && ctx === c) close();\n return ok;\n })\n .then(null, function () {\n if (ctx === c) close();\n return false;\n });\n return entered;\n }\n\n function pause() {\n if (!ctx || paused) return;\n paused = true;\n pausedAt = performance.now();\n if (ctx.suspend) ctx.suspend().then(null, function () {});\n }\n\n function resume() {\n if (!ctx || !paused || isMuted) return;\n paused = false;\n pausedMs += performance.now() - pausedAt;\n if (ctx.resume) ctx.resume().then(null, function () {});\n }\n\n function setMuted(on) {\n isMuted = !!on;\n try { window.localStorage.setItem(KEY, isMuted ? '1' : '0'); } catch (e) { /* kept for this page */ }\n if (isMuted) close();\n }\n\n document.addEventListener('visibilitychange', function () {\n if (document.visibilityState === 'hidden') pause();\n });\n window.addEventListener('pagehide', close);\n\n window.addEventListener('storage', function (e) {\n if (e.key !== KEY) return;\n isMuted = e.newValue === '1';\n if (isMuted) close();\n });\n\n window.AllenAudio = Object.freeze({\n enter: enter,\n stop: close,\n pause: pause,\n resume: resume,\n setMuted: setMuted,\n muted: function () { return isMuted; },\n src: src,\n kinds: Object.freeze(['night', 'home']),\n timeline: Object.freeze({ rate: RATE, frames: FRAMES, startMs: START_MS, mergeMs: MERGE_MS, endMs: END_MS })\n });\n})();\n"
9
10fn script to void
11for i ulong len jsdoc
12obyte get jsdoc i
13end
14end
15
16fn main to int
17setup
18put ofd 0 1
19owav 1 sub FEND FZERO
20f = add FZERO 0
21note 45120 frames, one pass each.
22while lt f FEND bound 45120
23osample sample f
24f = add f 1
25end
26oflush
27put ofd 0 3
28script
29oflush
30ret 0
31end