// The working preview in the hero: a scripted run of the Voxley recorder. // Nothing here touches the microphone. The stage's data-phase and data-mode // attributes drive the stylesheet, as they do on the other sites. (function () { var stage = document.querySelector('.stage'); if (!stage) return; var SPOKEN = 'Um, so I just got out of the meeting with Dana and, uh, the launch is moving to Thursday. ' + 'We need the release notes by Wednesday noon and I’ll, I’ll send the final build tonight.'; var OUTPUT = { verbatim: SPOKEN, clean: 'I just got out of the meeting with Dana, and the launch is moving to Thursday. ' + 'We need the release notes by Wednesday noon, and I’ll send the final build tonight.', notes: '• Met with Dana: the launch is moving to Thursday\n' + '• Release notes are due by Wednesday noon\n' + '• I’ll send the final build tonight', email: 'I just got out of the meeting with Dana, and the launch is moving to Thursday.\n\n' + 'We need the release notes by Wednesday noon. I’ll send the final build tonight.' }; var STATUS = { listening: 'Listening…', transcribing: 'Transcribing…', refining: 'Polishing with Apple Intelligence…' }; var out = document.getElementById('demo-out'); var live = document.getElementById('demo-live'); var wave = document.getElementById('demo-wave'); var play = document.getElementById('demo-play'); var hint = document.getElementById('demo-hint'); var announce = document.getElementById('demo-status'); var modes = Array.prototype.slice.call(document.querySelectorAll('[data-mode-to]')); var still = window.matchMedia('(prefers-reduced-motion: reduce)').matches; var words = SPOKEN.split(' '); var run = 0; // Bumped on every start, so timers from an earlier run stop quietly. var spoken = 0; // Words shown in the card so far. var loud = 0; // How loud the stand-in voice is, 0 to 1. // The waveform follows RecorderWave.swift in the app, and fills the space between the // microphone and Stop, fading at both ends, as the app does: 96 columns of 14 ms // (1.34 seconds) scroll in from the right. Each column holds the lowest and highest // sample, smoothed across five columns, drawn as a filled envelope with bright edges. // The app reads the microphone. This page never does, so the samples are synthetic. var COLUMNS = 96, HISTORY = COLUMNS + 20, COLUMN_SECONDS = .014, WEIGHTS = [1, 2, 3, 2, 1]; function silence() { var peaks = []; for (var i = 0; i < HISTORY; i++) peaks.push({ low: 0, high: 0 }); return peaks; } function height(amplitude) { var audible = Math.min(1, Math.max(0, amplitude - .0025)); return 1 - Math.exp(-2.52 * Math.pow(audible, .58)); } function smoothed(peaks, index) { var low = 0, high = 0, total = 0; for (var offset = -2; offset <= 2; offset++) { var peak = peaks[index + offset]; if (!peak) continue; low += peak.low * WEIGHTS[offset + 2]; high += peak.high * WEIGHTS[offset + 2]; total += WEIGHTS[offset + 2]; } return { low: low / total, high: high / total }; } function curve(context, points) { for (var i = 0; i < points.length - 1; i++) { context.quadraticCurveTo(points[i].x, points[i].y, (points[i].x + points[i + 1].x) / 2, (points[i].y + points[i + 1].y) / 2); } var last = points[points.length - 1]; context.quadraticCurveTo(last.x, last.y, last.x, last.y); } function trace(canvas, peaks, rightEdge) { var width = canvas.clientWidth, tall = canvas.clientHeight; var scale = window.devicePixelRatio || 1; if (canvas.width !== Math.round(width * scale)) { canvas.width = Math.round(width * scale); canvas.height = Math.round(tall * scale); } var context = canvas.getContext('2d'); context.setTransform(scale, 0, 0, scale, 0, 0); context.clearRect(0, 0, width, tall); var center = tall / 2, spacing = width / (COLUMNS - 1), most = Math.max(0, center - 1); context.strokeStyle = 'rgba(255, 255, 255, .18)'; context.lineWidth = .5; context.beginPath(); context.moveTo(0, center); context.lineTo(width, center); context.stroke(); var upper = [], lower = []; for (var i = 0; i < peaks.length; i++) { var peak = smoothed(peaks, i), x = width + (i - rightEdge) * spacing; upper.push({ x: x, y: center - height(peak.high) * most }); lower.unshift({ x: x, y: center + height(-peak.low) * most }); } context.beginPath(); context.moveTo(upper[0].x, upper[0].y); curve(context, upper); context.lineTo(lower[0].x, lower[0].y); curve(context, lower); context.closePath(); context.fillStyle = 'rgba(255, 255, 255, .2)'; context.fill(); context.save(); context.clip(); context.beginPath(); context.moveTo(upper[0].x, upper[0].y); curve(context, upper); context.moveTo(lower[0].x, lower[0].y); curve(context, lower); context.strokeStyle = 'rgba(255, 255, 255, .92)'; context.lineWidth = 1.8; context.stroke(); context.restore(); } // A stand-in for a voice: uneven bursts a syllable long, louder while a word is being shown. function voice(t, level, seed) { var syllable = Math.pow(Math.abs(Math.sin(t * 11 + seed) * Math.sin(t * 4.3 + seed * 2)), .8); var amplitude = level * syllable * .5 + .003; return { low: -amplitude * (.35 + .65 * Math.random()), high: amplitude * (.35 + .65 * Math.random()) }; } function sample(count, level, seed) { var peaks = silence(); for (var i = HISTORY - count; i < HISTORY; i++) peaks[i] = voice(i * COLUMN_SECONDS, level, seed); return peaks; } var peaks = sample(HISTORY, .8, 1), newest = null, written = 0; trace(wave, peaks, HISTORY - 1); Array.prototype.forEach.call(document.querySelectorAll('canvas[data-wave]'), function (canvas) { trace(canvas, sample(HISTORY, .8, 3), HISTORY - 1); }); function tick(id, began) { if (id !== run || stage.dataset.phase !== 'recording') return; var now = performance.now() / 1000, elapsed = now - began; // Audio arrives in buffers, so columns are written a few at a time, as in the app. while ((written + 3) * COLUMN_SECONDS <= elapsed) { for (var n = 0; n < 3; n++) { peaks.shift(); peaks.push(voice(written * COLUMN_SECONDS, loud, 0)); written++; } newest = began + (written - .5) * COLUMN_SECONDS; loud *= .95; } var edge = HISTORY - 1; if (newest !== null) edge = Math.min(edge, Math.max(COLUMNS - 1, edge + (now - .125 - newest) / COLUMN_SECONDS)); trace(wave, peaks, edge); requestAnimationFrame(function () { tick(id, began); }); } function status(text) { live.textContent = text; live.classList.add('is-status'); } function later(id, ms, fn) { setTimeout(function () { if (id === run) fn(); }, still ? Math.min(ms, 500) : ms); } function paste() { out.textContent = OUTPUT[stage.dataset.mode]; out.classList.remove('flash'); void out.offsetWidth; out.classList.add('flash'); } function start() { var id = ++run; spoken = 0; out.textContent = ''; stage.dataset.phase = 'recording'; status(STATUS.listening); hint.textContent = 'Listening. Press again to stop.'; announce.textContent = 'Listening.'; if (still) { later(id, 500, function () { finish(id); }); return; } peaks = silence(); newest = null; written = 0; loud = 0; tick(id, performance.now() / 1000); later(id, 650, function () { speak(id); }); } function speak(id) { if (id !== run || stage.dataset.phase !== 'recording') return; if (spoken >= words.length) { later(id, 600, function () { finish(id); }); return; } var word = words[spoken++]; live.classList.remove('is-status'); live.textContent = words.slice(0, spoken).join(' '); loud = .55 + Math.random() * .45; var pause = /[.,]$/.test(word) ? (/\.$/.test(word) ? 520 : 300) : 120 + word.length * 22; later(id, pause, function () { speak(id); }); } function finish(id) { if (id !== run || stage.dataset.phase !== 'recording') return; stage.dataset.phase = 'transcribing'; status(STATUS.transcribing); later(id, 750, function () { if (stage.dataset.mode === 'verbatim') { done(id); return; } stage.dataset.phase = 'refining'; status(STATUS.refining); later(id, 1200, function () { done(id); }); }); } function done() { stage.dataset.phase = 'done'; paste(); hint.textContent = 'Pasted at the cursor. Press to dictate again, or try another mode.'; announce.textContent = 'Pasted: ' + OUTPUT[stage.dataset.mode]; } function toggle() { if (stage.dataset.phase === 'recording' && run > 0) finish(run); else if (stage.dataset.phase === 'transcribing' || stage.dataset.phase === 'refining') return; else start(); } play.addEventListener('click', toggle); // ⌃⇧Space works here too, as it does in the app. document.addEventListener('keydown', function (event) { if (event.ctrlKey && event.shiftKey && !event.metaKey && !event.altKey && event.code === 'Space' && !event.repeat) { event.preventDefault(); toggle(); } }); modes.forEach(function (button) { button.addEventListener('click', function () { stage.dataset.mode = button.dataset.modeTo; modes.forEach(function (b) { b.setAttribute('aria-pressed', String(b === button)); }); if (stage.dataset.phase === 'done') paste(); }); }); // Play once when the preview scrolls into view. People who ask for less motion start it themselves. if (!still && 'IntersectionObserver' in window) { var seen = new IntersectionObserver(function (entries) { if (entries[0].isIntersecting) { seen.disconnect(); if (run === 0) start(); } }, { threshold: .6 }); seen.observe(stage.querySelector('.desk')); } })();