Voxley / website · voxley.si / assets / site.js
| 1 | // The working preview in the hero: a scripted run of the Voxley recorder. |
| 2 | // Nothing here touches the microphone. The stage's data-phase and data-mode |
| 3 | // attributes drive the stylesheet, as they do on the other sites. |
| 4 | (function () { |
| 5 | var stage = document.querySelector('.stage'); |
| 6 | if (!stage) return; |
| 7 | |
| 8 | var SPOKEN = 'Um, so I just got out of the meeting with Dana and, uh, the launch is moving to Thursday. ' + |
| 9 | 'We need the release notes by Wednesday noon and I’ll, I’ll send the final build tonight.'; |
| 10 | var OUTPUT = { |
| 11 | verbatim: SPOKEN, |
| 12 | clean: 'I just got out of the meeting with Dana, and the launch is moving to Thursday. ' + |
| 13 | 'We need the release notes by Wednesday noon, and I’ll send the final build tonight.', |
| 14 | notes: '• Met with Dana: the launch is moving to Thursday\n' + |
| 15 | '• Release notes are due by Wednesday noon\n' + |
| 16 | '• I’ll send the final build tonight', |
| 17 | email: 'I just got out of the meeting with Dana, and the launch is moving to Thursday.\n\n' + |
| 18 | 'We need the release notes by Wednesday noon. I’ll send the final build tonight.' |
| 19 | }; |
| 20 | var STATUS = { listening: 'Listening…', transcribing: 'Transcribing…', refining: 'Polishing with Apple Intelligence…' }; |
| 21 | |
| 22 | var out = document.getElementById('demo-out'); |
| 23 | var live = document.getElementById('demo-live'); |
| 24 | var wave = document.getElementById('demo-wave'); |
| 25 | var play = document.getElementById('demo-play'); |
| 26 | var hint = document.getElementById('demo-hint'); |
| 27 | var announce = document.getElementById('demo-status'); |
| 28 | var modes = Array.prototype.slice.call(document.querySelectorAll('[data-mode-to]')); |
| 29 | var still = window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
| 30 | var words = SPOKEN.split(' '); |
| 31 | var run = 0; // Bumped on every start, so timers from an earlier run stop quietly. |
| 32 | var spoken = 0; // Words shown in the card so far. |
| 33 | var loud = 0; // How loud the stand-in voice is, 0 to 1. |
| 34 | |
| 35 | // The waveform follows RecorderWave.swift in the app, and fills the space between the |
| 36 | // microphone and Stop, fading at both ends, as the app does: 96 columns of 14 ms |
| 37 | // (1.34 seconds) scroll in from the right. Each column holds the lowest and highest |
| 38 | // sample, smoothed across five columns, drawn as a filled envelope with bright edges. |
| 39 | // The app reads the microphone. This page never does, so the samples are synthetic. |
| 40 | var COLUMNS = 96, HISTORY = COLUMNS + 20, COLUMN_SECONDS = .014, WEIGHTS = [1, 2, 3, 2, 1]; |
| 41 | function silence() { |
| 42 | var peaks = []; |
| 43 | for (var i = 0; i < HISTORY; i++) peaks.push({ low: 0, high: 0 }); |
| 44 | return peaks; |
| 45 | } |
| 46 | function height(amplitude) { |
| 47 | var audible = Math.min(1, Math.max(0, amplitude - .0025)); |
| 48 | return 1 - Math.exp(-2.52 * Math.pow(audible, .58)); |
| 49 | } |
| 50 | function smoothed(peaks, index) { |
| 51 | var low = 0, high = 0, total = 0; |
| 52 | for (var offset = -2; offset <= 2; offset++) { |
| 53 | var peak = peaks[index + offset]; |
| 54 | if (!peak) continue; |
| 55 | low += peak.low * WEIGHTS[offset + 2]; |
| 56 | high += peak.high * WEIGHTS[offset + 2]; |
| 57 | total += WEIGHTS[offset + 2]; |
| 58 | } |
| 59 | return { low: low / total, high: high / total }; |
| 60 | } |
| 61 | function curve(context, points) { |
| 62 | for (var i = 0; i < points.length - 1; i++) { |
| 63 | context.quadraticCurveTo(points[i].x, points[i].y, |
| 64 | (points[i].x + points[i + 1].x) / 2, (points[i].y + points[i + 1].y) / 2); |
| 65 | } |
| 66 | var last = points[points.length - 1]; |
| 67 | context.quadraticCurveTo(last.x, last.y, last.x, last.y); |
| 68 | } |
| 69 | function trace(canvas, peaks, rightEdge) { |
| 70 | var width = canvas.clientWidth, tall = canvas.clientHeight; |
| 71 | var scale = window.devicePixelRatio || 1; |
| 72 | if (canvas.width !== Math.round(width * scale)) { |
| 73 | canvas.width = Math.round(width * scale); |
| 74 | canvas.height = Math.round(tall * scale); |
| 75 | } |
| 76 | var context = canvas.getContext('2d'); |
| 77 | context.setTransform(scale, 0, 0, scale, 0, 0); |
| 78 | context.clearRect(0, 0, width, tall); |
| 79 | var center = tall / 2, spacing = width / (COLUMNS - 1), most = Math.max(0, center - 1); |
| 80 | context.strokeStyle = 'rgba(255, 255, 255, .18)'; |
| 81 | context.lineWidth = .5; |
| 82 | context.beginPath(); |
| 83 | context.moveTo(0, center); |
| 84 | context.lineTo(width, center); |
| 85 | context.stroke(); |
| 86 | |
| 87 | var upper = [], lower = []; |
| 88 | for (var i = 0; i < peaks.length; i++) { |
| 89 | var peak = smoothed(peaks, i), x = width + (i - rightEdge) * spacing; |
| 90 | upper.push({ x: x, y: center - height(peak.high) * most }); |
| 91 | lower.unshift({ x: x, y: center + height(-peak.low) * most }); |
| 92 | } |
| 93 | context.beginPath(); |
| 94 | context.moveTo(upper[0].x, upper[0].y); |
| 95 | curve(context, upper); |
| 96 | context.lineTo(lower[0].x, lower[0].y); |
| 97 | curve(context, lower); |
| 98 | context.closePath(); |
| 99 | context.fillStyle = 'rgba(255, 255, 255, .2)'; |
| 100 | context.fill(); |
| 101 | context.save(); |
| 102 | context.clip(); |
| 103 | context.beginPath(); |
| 104 | context.moveTo(upper[0].x, upper[0].y); |
| 105 | curve(context, upper); |
| 106 | context.moveTo(lower[0].x, lower[0].y); |
| 107 | curve(context, lower); |
| 108 | context.strokeStyle = 'rgba(255, 255, 255, .92)'; |
| 109 | context.lineWidth = 1.8; |
| 110 | context.stroke(); |
| 111 | context.restore(); |
| 112 | } |
| 113 | |
| 114 | // A stand-in for a voice: uneven bursts a syllable long, louder while a word is being shown. |
| 115 | function voice(t, level, seed) { |
| 116 | var syllable = Math.pow(Math.abs(Math.sin(t * 11 + seed) * Math.sin(t * 4.3 + seed * 2)), .8); |
| 117 | var amplitude = level * syllable * .5 + .003; |
| 118 | return { low: -amplitude * (.35 + .65 * Math.random()), high: amplitude * (.35 + .65 * Math.random()) }; |
| 119 | } |
| 120 | function sample(count, level, seed) { |
| 121 | var peaks = silence(); |
| 122 | for (var i = HISTORY - count; i < HISTORY; i++) peaks[i] = voice(i * COLUMN_SECONDS, level, seed); |
| 123 | return peaks; |
| 124 | } |
| 125 | |
| 126 | var peaks = sample(HISTORY, .8, 1), newest = null, written = 0; |
| 127 | trace(wave, peaks, HISTORY - 1); |
| 128 | Array.prototype.forEach.call(document.querySelectorAll('canvas[data-wave]'), function (canvas) { |
| 129 | trace(canvas, sample(HISTORY, .8, 3), HISTORY - 1); |
| 130 | }); |
| 131 | |
| 132 | function tick(id, began) { |
| 133 | if (id !== run || stage.dataset.phase !== 'recording') return; |
| 134 | var now = performance.now() / 1000, elapsed = now - began; |
| 135 | // Audio arrives in buffers, so columns are written a few at a time, as in the app. |
| 136 | while ((written + 3) * COLUMN_SECONDS <= elapsed) { |
| 137 | for (var n = 0; n < 3; n++) { |
| 138 | peaks.shift(); |
| 139 | peaks.push(voice(written * COLUMN_SECONDS, loud, 0)); |
| 140 | written++; |
| 141 | } |
| 142 | newest = began + (written - .5) * COLUMN_SECONDS; |
| 143 | loud *= .95; |
| 144 | } |
| 145 | var edge = HISTORY - 1; |
| 146 | if (newest !== null) edge = Math.min(edge, Math.max(COLUMNS - 1, edge + (now - .125 - newest) / COLUMN_SECONDS)); |
| 147 | trace(wave, peaks, edge); |
| 148 | requestAnimationFrame(function () { tick(id, began); }); |
| 149 | } |
| 150 | |
| 151 | function status(text) { |
| 152 | live.textContent = text; |
| 153 | live.classList.add('is-status'); |
| 154 | } |
| 155 | function later(id, ms, fn) { |
| 156 | setTimeout(function () { if (id === run) fn(); }, still ? Math.min(ms, 500) : ms); |
| 157 | } |
| 158 | function paste() { |
| 159 | out.textContent = OUTPUT[stage.dataset.mode]; |
| 160 | out.classList.remove('flash'); |
| 161 | void out.offsetWidth; |
| 162 | out.classList.add('flash'); |
| 163 | } |
| 164 | |
| 165 | function start() { |
| 166 | var id = ++run; |
| 167 | spoken = 0; |
| 168 | out.textContent = ''; |
| 169 | stage.dataset.phase = 'recording'; |
| 170 | status(STATUS.listening); |
| 171 | hint.textContent = 'Listening. Press again to stop.'; |
| 172 | announce.textContent = 'Listening.'; |
| 173 | if (still) { |
| 174 | later(id, 500, function () { finish(id); }); |
| 175 | return; |
| 176 | } |
| 177 | peaks = silence(); newest = null; written = 0; loud = 0; |
| 178 | tick(id, performance.now() / 1000); |
| 179 | later(id, 650, function () { speak(id); }); |
| 180 | } |
| 181 | function speak(id) { |
| 182 | if (id !== run || stage.dataset.phase !== 'recording') return; |
| 183 | if (spoken >= words.length) { later(id, 600, function () { finish(id); }); return; } |
| 184 | var word = words[spoken++]; |
| 185 | live.classList.remove('is-status'); |
| 186 | live.textContent = words.slice(0, spoken).join(' '); |
| 187 | loud = .55 + Math.random() * .45; |
| 188 | var pause = /[.,]$/.test(word) ? (/\.$/.test(word) ? 520 : 300) : 120 + word.length * 22; |
| 189 | later(id, pause, function () { speak(id); }); |
| 190 | } |
| 191 | function finish(id) { |
| 192 | if (id !== run || stage.dataset.phase !== 'recording') return; |
| 193 | stage.dataset.phase = 'transcribing'; |
| 194 | status(STATUS.transcribing); |
| 195 | later(id, 750, function () { |
| 196 | if (stage.dataset.mode === 'verbatim') { done(id); return; } |
| 197 | stage.dataset.phase = 'refining'; |
| 198 | status(STATUS.refining); |
| 199 | later(id, 1200, function () { done(id); }); |
| 200 | }); |
| 201 | } |
| 202 | function done() { |
| 203 | stage.dataset.phase = 'done'; |
| 204 | paste(); |
| 205 | hint.textContent = 'Pasted at the cursor. Press to dictate again, or try another mode.'; |
| 206 | announce.textContent = 'Pasted: ' + OUTPUT[stage.dataset.mode]; |
| 207 | } |
| 208 | |
| 209 | function toggle() { |
| 210 | if (stage.dataset.phase === 'recording' && run > 0) finish(run); |
| 211 | else if (stage.dataset.phase === 'transcribing' || stage.dataset.phase === 'refining') return; |
| 212 | else start(); |
| 213 | } |
| 214 | |
| 215 | play.addEventListener('click', toggle); |
| 216 | // ⌃⇧Space works here too, as it does in the app. |
| 217 | document.addEventListener('keydown', function (event) { |
| 218 | if (event.ctrlKey && event.shiftKey && !event.metaKey && !event.altKey && event.code === 'Space' && !event.repeat) { |
| 219 | event.preventDefault(); |
| 220 | toggle(); |
| 221 | } |
| 222 | }); |
| 223 | |
| 224 | modes.forEach(function (button) { |
| 225 | button.addEventListener('click', function () { |
| 226 | stage.dataset.mode = button.dataset.modeTo; |
| 227 | modes.forEach(function (b) { b.setAttribute('aria-pressed', String(b === button)); }); |
| 228 | if (stage.dataset.phase === 'done') paste(); |
| 229 | }); |
| 230 | }); |
| 231 | |
| 232 | // Play once when the preview scrolls into view. People who ask for less motion start it themselves. |
| 233 | if (!still && 'IntersectionObserver' in window) { |
| 234 | var seen = new IntersectionObserver(function (entries) { |
| 235 | if (entries[0].isIntersecting) { seen.disconnect(); if (run === 0) start(); } |
| 236 | }, { threshold: .6 }); |
| 237 | seen.observe(stage.querySelector('.desk')); |
| 238 | } |
| 239 | })(); |