@arraypress/waveform-player 1.24.0 → 1.24.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/waveform-player.cjs +57 -31
- package/dist/waveform-player.cjs.map +2 -2
- package/dist/waveform-player.esm.js +3 -3
- package/dist/waveform-player.esm.js.map +3 -3
- package/dist/waveform-player.js +57 -31
- package/dist/waveform-player.min.js +3 -3
- package/dist/waveform-player.min.js.map +3 -3
- package/index.d.ts +7 -0
- package/package.json +2 -1
- package/src/js/core.js +7 -5
- package/src/js/index.js +8 -3
- package/src/js/themes.js +119 -33
- package/src/js/utils.js +52 -12
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/js/utils.js", "../src/js/drawing.js", "../src/js/bpm.js", "../src/js/audio.js", "../src/js/themes.js", "../src/js/core.js", "../src/js/index.js"],
|
|
4
|
-
"sourcesContent": ["/**\n * @module utils\n * @description Utility functions for WaveformPlayer\n */\n\n/**\n * Default number of peak windows extracted per track. Shared by the runtime\n * peak extraction and `DEFAULT_OPTIONS.samples` so they can't silently drift,\n * and matched to @arraypress/waveform-gen's offline default (1800) for parity.\n * @type {number}\n */\nexport const DEFAULT_SAMPLES = 1800;\n\n/**\n * Largest value in an array, via a loop \u2014 NOT `Math.max(...arr)`, whose\n * argument spread throws `RangeError: Maximum call stack size exceeded` for\n * very large arrays (~1e5+ entries). Returns `-Infinity` for an empty array,\n * matching `Math.max()`.\n * @param {number[]} values - Numbers to scan.\n * @returns {number} The maximum value (`-Infinity` if empty).\n */\nexport function maxOf(values) {\n let max = -Infinity;\n for (let i = 0; i < values.length; i++) {\n if (values[i] > max) max = values[i];\n }\n return max;\n}\n\n/**\n * Escape a string for safe interpolation into HTML, preventing injection when\n * building markup with template strings. `null`/`undefined` become `''`.\n * @param {*} str - Value to escape.\n * @returns {string} HTML-escaped string.\n */\nexport function escapeHtml(str) {\n return String(str == null ? '' : str)\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n/**\n * Format a CSS length option (`buttonSize`, `buttonRadius`) for interpolation\n * into an inline `style` attribute. A number is treated as px; a string (e.g.\n * `'4rem'`) is used verbatim.\n *\n * The result is HTML-escaped: these values reach the markup from author-supplied\n * `data-*` attributes, so an unescaped quote would otherwise close the `style`\n * attribute and let arbitrary markup in.\n *\n * @param {number|string} value - Length option value.\n * @returns {string} Escaped CSS length, e.g. `36px` or `4rem`.\n */\nexport function formatCssLength(value) {\n return escapeHtml(typeof value === 'number' ? `${value}px` : value);\n}\n\n/**\n * Whether a URL is safe to navigate to (assign to `location.href`): allows only\n * `http`/`https` and relative URLs, rejecting `javascript:`, `data:`, `blob:`,\n * `vbscript:` and other script-bearing schemes.\n * @param {string} url - Candidate URL.\n * @returns {boolean} True if the URL uses a safe scheme.\n */\nexport function isSafeHref(url) {\n if (typeof url !== 'string' || url === '') return false;\n try {\n // Resolve relative URLs against a dummy http base; only the scheme matters.\n const u = new URL(url, 'http://localhost/');\n return u.protocol === 'http:' || u.protocol === 'https:';\n } catch (e) {\n return false;\n }\n}\n\n/**\n * Clamp a number to an inclusive range.\n * @param {number} value - Value to constrain.\n * @param {number} [min=0] - Lower bound.\n * @param {number} [max=1] - Upper bound.\n * @returns {number} `value` constrained to `[min, max]`.\n */\nexport function clamp(value, min = 0, max = 1) {\n return Math.max(min, Math.min(value, max));\n}\n\n/**\n * Read a boolean `data-*` flag. Returns `undefined` when the attribute is\n * absent (preserving the sparse-options contract) and otherwise compares the\n * raw value against the literal string `'true'`.\n * @param {string|undefined} value - Raw `dataset` value.\n * @returns {boolean|undefined} `true`/`false` when present, else `undefined`.\n */\nexport function parseBoolAttr(value) {\n return value === undefined ? undefined : value === 'true';\n}\n\n/**\n * A colour data-attribute may be a CSS colour string OR a JSON array of\n * gradient stops (e.g. '[\"#fafafa\",\"#71717a\"]'). Parse the array form;\n * otherwise pass the string straight through.\n * @param {string} value\n * @returns {string|string[]}\n */\nfunction parseColorValue(value) {\n if (typeof value === 'string' && value.trim().startsWith('[')) {\n try { return JSON.parse(value); } catch (e) { /* fall through to string */ }\n }\n return value;\n}\n\n/**\n * Read every recognised `data-*` attribute off a host element and translate it\n * into a plain options object suitable for `mergeOptions`.\n *\n * Only attributes that are actually present are copied, so the returned object\n * is sparse and never overrides defaults with `undefined`. Numeric attributes\n * are coerced with `parseInt`/`parseFloat`, boolean flags are compared against\n * the literal string `'true'`, and JSON-valued attributes (`markers`,\n * `playbackRates`) are parsed defensively \u2014 a parse failure is warned about and\n * the attribute is skipped rather than thrown.\n *\n * Several attributes are shorthand aliases of a canonical long form: `data-src`\n * \u2192 `url`, `data-style` \u2192 `waveformStyle`. When both are present the canonical\n * long form is applied last and therefore wins. `data-color` and `data-theme`\n * are retained as legacy aliases for `waveformColor` and `colorPreset`.\n * Colour attributes that accept gradients (`waveformColor`, `progressColor`)\n * are passed through {@link parseColorValue} so a JSON stop array is expanded.\n *\n * @param {HTMLElement} element - Host element whose `dataset` is inspected.\n * @returns {Object} Sparse options object containing only the attributes found.\n */\nexport function parseDataAttributes(element) {\n const options = {};\n\n // Set a boolean option only when its `data-*` attribute is present, so the\n // returned object stays sparse and never overrides a default with a value\n // the author didn't set. (`dataKey` differs from `optKey` only for showBPM.)\n const setBool = (optKey, dataKey = optKey) => {\n const v = parseBoolAttr(element.dataset[dataKey]);\n if (v !== undefined) options[optKey] = v;\n };\n\n // Read a present (non-empty) numeric attribute as an int (or float).\n const setNum = (optKey, dataKey = optKey, float = false) => {\n const raw = element.dataset[dataKey];\n if (raw) options[optKey] = float ? parseFloat(raw) : parseInt(raw, 10);\n };\n\n // Read a CSS length attribute: a bare number is px (data-button-size=\"64\"),\n // a unit string (e.g. \"4rem\") is kept verbatim for the stylesheet to resolve.\n const setLength = (optKey, dataKey = optKey) => {\n const raw = element.dataset[dataKey];\n if (!raw) return;\n options[optKey] = /^\\d+(\\.\\d+)?$/.test(raw.trim()) ? parseFloat(raw) : raw;\n };\n\n // Parse a JSON-valued attribute defensively \u2014 warn and skip on bad JSON.\n const setJson = (optKey, dataKey = optKey) => {\n const raw = element.dataset[dataKey];\n if (!raw) return;\n try { options[optKey] = JSON.parse(raw); }\n catch (e) { console.warn(`[WaveformPlayer] Invalid ${dataKey} JSON:`, e); }\n };\n\n // Core attributes. `data-src` is a shorthand alias for `data-url`;\n // the canonical long form wins if both are set.\n if (element.dataset.src) options.url = element.dataset.src;\n if (element.dataset.url) options.url = element.dataset.url;\n setNum('height');\n setNum('samples');\n if (element.dataset.preload) {\n options.preload = element.dataset.preload;\n }\n if (element.dataset.crossOrigin) {\n options.crossOrigin = element.dataset.crossOrigin;\n }\n if (element.dataset.audioMode) options.audioMode = element.dataset.audioMode;\n\n // Waveform style attributes. `data-style` is a shorthand alias for\n // `data-waveform-style`; the canonical long form wins if both are set.\n if (element.dataset.style) options.waveformStyle = element.dataset.style;\n if (element.dataset.waveformStyle) options.waveformStyle = element.dataset.waveformStyle;\n if (element.dataset.waveformGradient) options.waveformGradient = element.dataset.waveformGradient;\n setNum('barWidth');\n setNum('barSpacing');\n setNum('barRadius');\n if (element.dataset.buttonAlign) options.buttonAlign = element.dataset.buttonAlign;\n if (element.dataset.layout) options.layout = element.dataset.layout;\n if (element.dataset.buttonStyle) options.buttonStyle = element.dataset.buttonStyle;\n setLength('buttonSize');\n setLength('buttonRadius');\n\n // Color preset\n if (element.dataset.colorPreset) options.colorPreset = element.dataset.colorPreset;\n\n // Individual color customization\n if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);\n if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);\n\n // Legacy support for old attribute names\n if (element.dataset.color) options.waveformColor = element.dataset.color;\n if (element.dataset.theme) options.colorPreset = element.dataset.theme;\n\n // Feature flags\n setBool('autoplay');\n setBool('showControls');\n setBool('showInfo');\n setBool('showTime');\n setBool('showHoverTime');\n setBool('seekHandle');\n setBool('showBPM', 'showBpm');\n setNum('bpm');\n setBool('singlePlay');\n setBool('playOnSeek');\n\n // Content and metadata\n if (element.dataset.title) options.title = element.dataset.title;\n if (element.dataset.artist) options.artist = element.dataset.artist;\n if (element.dataset.album) options.album = element.dataset.album;\n if (element.dataset.artwork) options.artwork = element.dataset.artwork;\n if (element.dataset.artworkPosition) options.artworkPosition = element.dataset.artworkPosition;\n\n // Waveform data\n if (element.dataset.waveform) options.waveform = element.dataset.waveform;\n\n // Markers\n setJson('markers');\n\n // Playback controls\n setNum('playbackRate', 'playbackRate', true);\n setBool('showPlaybackSpeed');\n setJson('playbackRates');\n\n // Media Session API\n setBool('enableMediaSession');\n\n // Markers visibility\n setBool('showMarkers');\n\n // Accessibility\n setBool('accessibleSeek');\n if (element.dataset.seekLabel) options.seekLabel = element.dataset.seekLabel;\n if (element.dataset.seekValueText) options.seekValueText = element.dataset.seekValueText;\n if (element.dataset.errorText) options.errorText = element.dataset.errorText;\n\n // Localizable UI strings (screen-reader / lock-screen facing)\n if (element.dataset.playPauseLabel) options.playPauseLabel = element.dataset.playPauseLabel;\n if (element.dataset.speedLabel) options.speedLabel = element.dataset.speedLabel;\n if (element.dataset.artworkAlt) options.artworkAlt = element.dataset.artworkAlt;\n if (element.dataset.unknownTrackText) options.unknownTrackText = element.dataset.unknownTrackText;\n\n // Custom icons (raw SVG markup)\n if (element.dataset.playIcon) options.playIcon = element.dataset.playIcon;\n if (element.dataset.pauseIcon) options.pauseIcon = element.dataset.pauseIcon;\n\n return options;\n}\n\n/**\n * Interpolate a printf-style seek value-text template.\n *\n * Substitutes positional (`%1$s`, `%2$s`) and sequential (`%s`) placeholders so\n * a localized template resolves the same way `sprintf` would \u2014 including\n * templates that reorder the arguments for a given language. Placeholders with\n * no matching argument are left intact.\n * @param {string} template - Value-text template, e.g. `'%1$s of %2$s'`.\n * @param {...string} args - Replacement values, in order (current, duration).\n * @returns {string} The interpolated value text.\n */\nexport function formatSeekValueText(template, ...args) {\n let sequentialIndex = 0;\n return template.replace(/%(?:(\\d+)\\$)?s/g, (match, position) => {\n const index = position ? Number(position) - 1 : sequentialIndex++;\n return args[index] ?? match;\n });\n}\n\n/**\n * Format a duration as a clock string.\n *\n * Renders `M:SS` for durations under an hour and `H:MM:SS` for longer ones,\n * zero-padding the minutes and seconds. Falsy, `NaN`, or negative inputs are\n * treated as zero and return `'0:00'`.\n * @param {number} seconds - Time in seconds.\n * @returns {string} Formatted time, e.g. `'3:07'` or `'1:02:09'`.\n */\nexport function formatTime(seconds) {\n if (!seconds || isNaN(seconds) || seconds < 0) return '0:00';\n\n const hrs = Math.floor(seconds / 3600);\n const mins = Math.floor((seconds % 3600) / 60);\n const secs = Math.floor(seconds % 60);\n\n if (hrs > 0) {\n return `${hrs}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;\n }\n\n return `${mins}:${secs.toString().padStart(2, '0')}`;\n}\n\n/**\n * Monotonic per-process counter appended to every generated id to guarantee\n * uniqueness even when two ids hash from the same URL.\n * @type {number}\n * @private\n */\nlet idCounter = 0;\n\n/**\n * Generate a unique, DOM-safe ID from a URL.\n *\n * Uses a DJB2 hash of the FULL url (not a 10-char prefix) plus a process\n * counter, so same-host tracks don't collide in the instances map and\n * non-Latin1 / Unicode URLs don't throw (the old btoa() approach did both).\n * @param {string} url - Audio URL\n * @returns {string} Unique element-id-safe string\n */\nexport function generateId(url) {\n const str = url || 'audio';\n let hash = 5381;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) + hash + str.charCodeAt(i)) | 0;\n }\n return `wp_${(hash >>> 0).toString(36)}_${(idCounter++).toString(36)}`;\n}\n\n/**\n * Derive a human-readable title from an audio URL's filename.\n *\n * Takes the last path segment, drops the extension, replaces `-`/`_`\n * separators with spaces, and title-cases the first letter of each word.\n * Returns `'Audio'` for an empty or missing URL.\n * @param {string} url - Audio URL.\n * @returns {string} Extracted, prettified title.\n * @example\n * extractTitleFromUrl('https://cdn.example.com/my-cool_track.mp3'); // 'My Cool Track'\n */\nexport function extractTitleFromUrl(url) {\n if (!url) return 'Audio';\n\n const parts = url.split('/');\n const filename = parts[parts.length - 1];\n const name = filename.split('.')[0];\n\n // Clean up common separators\n return name\n .replace(/[-_]/g, ' ')\n .replace(/\\b\\w/g, l => l.toUpperCase());\n}\n\n/**\n * Perceived brightness (0\u2013255) of a CSS colour, via the luminance formula.\n * Pulls the numeric channels out of an `rgb()`/`rgba()` string.\n * @param {string} color - CSS colour string, e.g. `\"rgb(34, 34, 34)\"`.\n * @returns {number|null} Brightness 0\u2013255, or `null` if it can't be parsed.\n */\nexport function perceivedBrightness(color) {\n const rgb = typeof color === 'string' ? color.match(/\\d+/g) : null;\n if (!rgb || rgb.length < 3) return null;\n const [r, g, b] = rgb.map(Number);\n return (r * 299 + g * 587 + b * 114) / 1000;\n}\n\n/**\n * Shallow-merge option objects into a new object, last source winning.\n *\n * Keys whose value is `null` or `undefined` are skipped, so a later source can\n * leave an earlier value untouched by passing a nullish entry rather than\n * clobbering it. The inputs are never mutated.\n * @param {...Object} sources - Option objects merged left-to-right.\n * @returns {Object} A fresh object containing the merged, defined keys.\n */\nexport function mergeOptions(...sources) {\n const result = {};\n\n for (const source of sources) {\n for (const key in source) {\n if (source[key] !== null && source[key] !== undefined) {\n result[key] = source[key];\n }\n }\n }\n\n return result;\n}\n\n/**\n * Wrap a function so it only runs once calls stop arriving for `wait` ms.\n *\n * Each invocation resets the pending timer, so rapid bursts collapse into a\n * single trailing-edge call that receives the most recent arguments. The\n * wrapper itself returns nothing.\n * @param {Function} func - Function to debounce.\n * @param {number} wait - Idle period in milliseconds before `func` fires.\n * @returns {Function} Debounced wrapper forwarding its arguments to `func`.\n */\nexport function debounce(func, wait) {\n let timeout;\n\n return function executedFunction(...args) {\n const later = () => {\n clearTimeout(timeout);\n func(...args);\n };\n\n clearTimeout(timeout);\n timeout = setTimeout(later, wait);\n };\n}\n\n/**\n * Resize a waveform amplitude array to a target number of bars.\n *\n * Returns the original array unchanged when lengths already match, and an empty\n * array when either side is empty. When upsampling (target larger than source)\n * it linearly interpolates between neighbouring samples for a smooth result.\n * When downsampling it splits the source into evenly sized buckets and keeps\n * the peak (maximum) of each so transients survive the reduction; an empty\n * bucket falls back to its nearest-neighbour sample.\n * @param {number[]} data - Original amplitude samples.\n * @param {number} targetLength - Desired number of output bars.\n * @returns {number[]} Resampled amplitude array of length `targetLength`.\n */\nexport function resampleData(data, targetLength) {\n if (data.length === targetLength) return data;\n if (data.length === 0 || targetLength === 0) return [];\n\n const result = [];\n\n // If upsampling (target is larger than source)\n if (targetLength > data.length) {\n const ratio = (data.length - 1) / (targetLength - 1);\n\n for (let i = 0; i < targetLength; i++) {\n const index = i * ratio;\n const lower = Math.floor(index);\n const upper = Math.ceil(index);\n const fraction = index - lower;\n\n // Linear interpolation between samples\n if (upper >= data.length) {\n result.push(data[data.length - 1]);\n } else if (lower === upper) {\n result.push(data[lower]);\n } else {\n const value = data[lower] * (1 - fraction) + data[upper] * fraction;\n result.push(value);\n }\n }\n } else {\n // Downsampling (target is smaller than source)\n const bucketSize = data.length / targetLength;\n\n for (let i = 0; i < targetLength; i++) {\n const start = Math.floor(i * bucketSize);\n const end = Math.floor((i + 1) * bucketSize);\n\n // Find the maximum value in this bucket\n let max = 0;\n let count = 0;\n\n for (let j = start; j <= end && j < data.length; j++) {\n if (data[j] > max) {\n max = data[j];\n }\n count++;\n }\n\n // If no samples were found in this bucket, use nearest neighbor\n if (count === 0) {\n const nearestIndex = Math.min(Math.round(i * bucketSize), data.length - 1);\n max = data[nearestIndex];\n }\n\n result.push(max);\n }\n }\n\n return result;\n}", "/**\n * @module drawing\n * @description Core waveform drawing styles optimized for visual distinction at all sizes\n */\n\nimport {resampleData, clamp} from './utils.js';\n\n/**\n * Resolve a fill value that may be a CSS colour string OR an array of colour\n * stops (rendered as a canvas gradient). Bundle-light gradient support: pass\n * e.g. `waveformColor: ['#fafafa', '#71717a']`. A single-element array collapses\n * to that one colour; a multi-element array is spread evenly along the axis set\n * by `options.waveformGradient`:\n * - `'vertical'` (default) a canvas-height gradient (colour keyed to\n * absolute Y, so short bars occupy only the lower band).\n * - `'horizontal'` left -> right across the whole waveform (a hue sweep).\n * - `'diagonal'` top-left -> bottom-right.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context used to build the gradient.\n * @param {string|string[]} value - A CSS colour string, or an array of colour stops.\n * @param {HTMLCanvasElement} canvas - Canvas (provides the device-pixel span).\n * @param {Object} [options] - Drawing options; reads `waveformGradient`.\n * @returns {string|CanvasGradient} The original string, or a linear gradient.\n */\nfunction makeFill(ctx, value, canvas, options) {\n if (!Array.isArray(value)) return value;\n if (value.length < 2) return value[0];\n const w = canvas.width;\n const h = canvas.height;\n const dir = options && options.waveformGradient;\n const [x0, y0, x1, y1] =\n dir === 'horizontal' ? [0, 0, w, 0] :\n dir === 'diagonal' ? [0, 0, w, h] :\n [0, 0, 0, h];\n try {\n const grad = ctx.createLinearGradient(x0, y0, x1, y1);\n value.forEach((c, i) => grad.addColorStop(i / (value.length - 1), c));\n return grad;\n } catch (e) {\n return value[0]; // a bad stop shouldn't abort the whole render\n }\n}\n\n/**\n * Fill a bar rect, optionally with rounded caps (`barRadius`). Falls back to\n * a plain fillRect where `roundRect` is unavailable (older Safari) \u2014 square\n * bars, no error. Radii are clamped to half the rect's width/height so a\n * large `barRadius` never overflows a thin or short bar.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context (current fillStyle is used).\n * @param {number} x - Left edge of the bar in device pixels.\n * @param {number} y - Top edge of the bar in device pixels.\n * @param {number} w - Bar width in device pixels.\n * @param {number} h - Bar height in device pixels (may be negative for upward fills).\n * @param {number|number[]} radii - Corner radius (number, or [tl, tr, br, bl]).\n * @returns {void}\n */\nfunction fillBar(ctx, x, y, w, h, radii) {\n const any = Array.isArray(radii) ? radii.some(r => r > 0) : radii > 0;\n if (any && typeof ctx.roundRect === 'function') {\n const max = Math.min(w / 2, Math.abs(h) / 2);\n const clampR = (r) => clamp(r, 0, max);\n ctx.beginPath();\n ctx.roundRect(x, y, w, h, Array.isArray(radii) ? radii.map(clampR) : clampR(radii));\n ctx.fill();\n } else {\n ctx.fillRect(x, y, w, h);\n }\n}\n\n/**\n * Scale the configured `barRadius` into device pixels (scalar).\n * @private\n * @param {Object} options - Drawing options (`barRadius` in CSS pixels, defaults to 0).\n * @param {number} dpr - Device pixel ratio multiplier.\n * @returns {number} The bar corner radius in device pixels.\n */\nfunction barRadiusPx(options, dpr) {\n return (options.barRadius || 0) * dpr;\n}\n\n/**\n * Top-rounded corner radii for bottom-anchored bars: [tl, tr, br, bl].\n * Only the top two corners are rounded so bars sit flush on the baseline.\n * @private\n * @param {Object} options - Drawing options (supplies `barRadius`).\n * @param {number} dpr - Device pixel ratio multiplier.\n * @returns {number[]} Corner radii in device pixels as [tl, tr, br, bl].\n */\nfunction barRadii(options, dpr) {\n const r = barRadiusPx(options, dpr);\n return [r, r, 0, 0];\n}\n\n/**\n * Trace a horizontal rounded-capsule (stadium) path from `startX` to `endX`,\n * ready to fill. The end caps are semicircles of radius `barHeight / 2`.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context.\n * @param {number} startX - Left edge x (also the left cap centre).\n * @param {number} endX - Right edge x.\n * @param {number} centerY - Vertical centre of the capsule.\n * @param {number} barHeight - Capsule thickness in pixels.\n * @returns {void}\n */\nfunction capsulePath(ctx, startX, endX, centerY, barHeight) {\n const r = barHeight / 2;\n ctx.beginPath();\n ctx.moveTo(startX, centerY - r);\n ctx.lineTo(endX - r, centerY - r);\n ctx.arc(endX - r, centerY, r, -Math.PI / 2, Math.PI / 2);\n ctx.lineTo(startX, centerY + r);\n ctx.arc(startX, centerY, r, Math.PI / 2, -Math.PI / 2);\n ctx.closePath();\n}\n\n/**\n * Draw standard bars waveform - classic vertical bars anchored to the baseline.\n * Peaks are resampled to fit the available bar slots, drawn at 90% of canvas\n * height, then the progress portion is repainted in `progressColor` via a\n * left-anchored clip rect.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) that drives the colour overlay.\n * @param {Object} options - Drawing options: `barWidth`, `barSpacing`, `barRadius`,\n * `color`, `progressColor` (colour strings or gradient stop arrays).\n * @returns {void}\n */\nexport function drawBars(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = options.barWidth * dpr;\n const barSpacing = options.barSpacing * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const progressWidth = progress * canvas.width;\n const radii = barRadii(options, dpr);\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Draw all bars first\n ctx.fillStyle = baseFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n // Draw from bottom up, not centered\n const y = height - peakHeight;\n\n fillBar(ctx, x, y, barWidth, peakHeight, radii);\n }\n\n // Progress overlay\n ctx.save();\n ctx.beginPath();\n ctx.rect(0, 0, progressWidth, height);\n ctx.clip();\n\n ctx.fillStyle = progFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x > progressWidth) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n // Draw from bottom up, not centered\n const y = height - peakHeight;\n\n fillBar(ctx, x, y, barWidth, peakHeight, radii);\n }\n\n ctx.restore();\n}\n\n/**\n * Draw mirror/SoundCloud style waveform - symmetrical bars about the centre line.\n * Each peak is drawn twice (45% of height up and down) with the upper cap rounded\n * on top and the lower cap rounded on the bottom; the progress portion is then\n * repainted in `progressColor` through a left-anchored clip rect.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) that drives the colour overlay.\n * @param {Object} options - Drawing options: `barWidth`, `barSpacing`, `barRadius`,\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawMirror(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = options.barWidth * dpr;\n const barSpacing = options.barSpacing * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const centerY = height / 2;\n const progressWidth = progress * canvas.width;\n const r = barRadiusPx(options, dpr);\n const topRadii = [r, r, 0, 0]; // round the upper cap\n const botRadii = [0, 0, r, r]; // round the lower cap\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Draw all bars\n ctx.fillStyle = baseFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.45;\n\n fillBar(ctx, x, centerY - peakHeight, barWidth, peakHeight, topRadii);\n fillBar(ctx, x, centerY, barWidth, peakHeight, botRadii);\n }\n\n // Progress overlay\n ctx.save();\n ctx.beginPath();\n ctx.rect(0, 0, progressWidth, height);\n ctx.clip();\n\n ctx.fillStyle = progFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x > progressWidth) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.45;\n\n fillBar(ctx, x, centerY - peakHeight, barWidth, peakHeight, topRadii);\n fillBar(ctx, x, centerY, barWidth, peakHeight, botRadii);\n }\n\n ctx.restore();\n}\n\n/**\n * Draw line/oscilloscope style waveform - smooth flowing wave with glow.\n * Renders a faint oscilloscope grid (centre line + 10 vertical divisions), the\n * full waveform as a bezier-smoothed curve, then the played portion on top with\n * a coloured shadow glow. Peaks are modulated by a sine term so the line undulates\n * rather than reading as static bars.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1); the glowing curve is only drawn when > 0.\n * @param {Object} options - Drawing options: `color` (base wave), `progressColor` (played wave).\n * @returns {void}\n */\nexport function drawLine(ctx, canvas, peaks, progress, options) {\n const width = canvas.width;\n const height = canvas.height;\n const centerY = height / 2;\n const amplitude = height * 0.35;\n\n ctx.clearRect(0, 0, width, height);\n\n /**\n * Stroke a bezier-smoothed curve through the (optionally sine-modulated) peaks.\n * @private\n * @param {string} color - Stroke colour (and shadow colour when glowing).\n * @param {number} lineWidth - Stroke width in pixels.\n * @param {number} [endProgress=1] - Fraction (0-1) of the peaks to draw, left to right.\n * @param {boolean} [addGlow=false] - When true, applies a coloured shadow blur for a glow effect.\n * @returns {void}\n */\n const drawCurve = (color, lineWidth, endProgress = 1, addGlow = false) => {\n // strokeStyle accepts a gradient; shadowColor must be a solid string, so\n // collapse a stop array to its last stop for the glow.\n const stroke = makeFill(ctx, color, canvas, options);\n const solid = Array.isArray(color) ? color[color.length - 1] : color;\n if (addGlow) {\n ctx.shadowBlur = 12;\n ctx.shadowColor = solid;\n }\n\n ctx.strokeStyle = stroke;\n ctx.lineWidth = lineWidth;\n ctx.lineCap = 'round';\n ctx.lineJoin = 'round';\n\n ctx.beginPath();\n ctx.moveTo(0, centerY);\n\n const points = [];\n const samples = Math.floor(peaks.length * endProgress);\n\n // Calculate smoothed points\n for (let i = 0; i < samples; i++) {\n const x = (i / (peaks.length - 1)) * width;\n const peakValue = peaks[i];\n\n // Create a smooth wave motion\n const waveOffset = Math.sin(i * 0.1) * peakValue;\n const y = centerY + (waveOffset * amplitude);\n\n points.push({x, y});\n }\n\n // Draw smooth curve through points using bezier curves\n for (let i = 0; i < points.length - 1; i++) {\n const cp1x = points[i].x + (points[i + 1].x - points[i].x) * 0.5;\n const cp1y = points[i].y;\n const cp2x = points[i + 1].x - (points[i + 1].x - points[i].x) * 0.5;\n const cp2y = points[i + 1].y;\n\n ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, points[i + 1].x, points[i + 1].y);\n }\n\n ctx.stroke();\n\n if (addGlow) {\n ctx.shadowBlur = 0;\n }\n };\n\n // Draw subtle grid for oscilloscope feel\n ctx.strokeStyle = 'rgba(255, 255, 255, 0.03)';\n ctx.lineWidth = 0.5;\n\n // Horizontal center line\n ctx.beginPath();\n ctx.moveTo(0, centerY);\n ctx.lineTo(width, centerY);\n ctx.stroke();\n\n // Vertical grid lines\n for (let i = 0; i <= 10; i++) {\n const x = (width / 10) * i;\n ctx.beginPath();\n ctx.moveTo(x, 0);\n ctx.lineTo(x, height);\n ctx.stroke();\n }\n\n // Draw background wave\n drawCurve(options.color, 2, 1, false);\n\n // Draw progress with glow\n if (progress > 0) {\n drawCurve(options.progressColor, 3, progress, true);\n }\n}\n\n/**\n * Draw blocks/LED meter style waveform - segmented blocks growing from the centre.\n * Each bar's height is quantised into fixed-size blocks separated by gaps, drawn\n * symmetrically up and down from the centre line (the shared centre block is not\n * duplicated downward). Per-bar colour is chosen by comparing the bar's x against\n * the played width \u2014 there is no clip overlay here.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) used to pick each bar's colour.\n * @param {Object} options - Drawing options: `barWidth` (default 3), `barSpacing` (default 1),\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawBlocks(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = (options.barWidth || 3) * dpr;\n const barSpacing = (options.barSpacing || 1) * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const blockSize = 4 * dpr;\n const blockGap = 2 * dpr;\n const progressWidth = progress * canvas.width;\n const centerY = height / 2;\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n const blockCount = Math.floor(peakHeight / (blockSize + blockGap));\n\n ctx.fillStyle = x < progressWidth ? progFill : baseFill;\n\n // Draw blocks from center outward\n for (let j = 0; j < blockCount; j++) {\n const blockOffset = j * (blockSize + blockGap);\n\n // Upper blocks\n ctx.fillRect(x, centerY - blockOffset - blockSize, barWidth, blockSize);\n\n // Lower blocks (skip the center block)\n if (j > 0) {\n ctx.fillRect(x, centerY + blockOffset, barWidth, blockSize);\n }\n }\n }\n}\n\n/**\n * Draw dots style waveform - pairs of circular points mirrored about the centre.\n * For each sample a dot is drawn above and below the centre line at half the peak\n * height; dot radius scales with bar width but is floored at 1.5 device pixels.\n * Per-dot colour is chosen by comparing x against the played width (no clip overlay).\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) used to pick each dot's colour.\n * @param {Object} options - Drawing options: `barWidth` (default 2), `barSpacing` (default 3),\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawDots(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = (options.barWidth || 2) * dpr;\n const barSpacing = (options.barSpacing || 3) * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const dotRadius = Math.max(1.5 * dpr, barWidth / 2);\n const progressWidth = progress * canvas.width;\n const centerY = height / 2;\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing) + barWidth / 2;\n if (x > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n\n ctx.fillStyle = x < progressWidth ? progFill : baseFill;\n\n // Draw upper dot\n ctx.beginPath();\n ctx.arc(x, centerY - peakHeight / 2, dotRadius, 0, Math.PI * 2);\n ctx.fill();\n\n // Draw lower dot\n ctx.beginPath();\n ctx.arc(x, centerY + peakHeight / 2, dotRadius, 0, Math.PI * 2);\n ctx.fill();\n }\n}\n\n/**\n * Draw seekbar style - a simple rounded progress bar with no waveform.\n * Renders a pill-shaped background track, a clean pill-shaped filled portion\n * (clamped to at least one full pill width so it never collapses), and a\n * draggable circular handle/thumb at the playhead (clamped so it never clips\n * at the track ends).\n * The `peaks` argument is accepted for signature parity but is unused by this style.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Ignored; present to match the shared draw-function signature.\n * @param {number} progress - Playback progress (0-1); the fill and handle are only drawn when > 0.\n * @param {Object} options - Drawing options: `color` (track), `progressColor` (fill/glow/accent).\n * @returns {void}\n */\nexport function drawSeekbar(ctx, canvas, peaks, progress, options) {\n const width = canvas.width;\n const height = canvas.height;\n const centerY = height / 2;\n const barHeight = 4; // Height of the seekbar in pixels\n const borderRadius = barHeight / 2;\n const active = !!options.seekActive; // hovering or dragging the seekbar\n\n ctx.clearRect(0, 0, width, height);\n\n // Draw background track\n ctx.fillStyle = makeFill(ctx, options.color, canvas, options) || 'rgba(255, 255, 255, 0.2)';\n\n // Rounded background track\n capsulePath(ctx, borderRadius, width, centerY, barHeight);\n ctx.fill();\n\n // Draw progress \u2014 slightly dimmed at rest, full-bright on hover / drag.\n // With a monochrome palette there's no hue to swap (Spotify goes\n // white\u2192green), so the hover cue is a brightness lift plus the handle\n // appearing. The draggable handle is a DOM element\n // (`.waveform-seek-handle`), not drawn here \u2014 so it's always a perfect\n // circle and can grow on direct hover via CSS.\n if (progress > 0) {\n const progressWidth = Math.max(borderRadius * 2, progress * width);\n\n // Brightness lift on hover/drag is only meaningful when the handle is\n // enabled (the bar); a standalone seekbar stays at full progress.\n ctx.save();\n ctx.globalAlpha = options.seekHandle && !active ? 0.7 : 1;\n ctx.fillStyle = makeFill(ctx, options.progressColor, canvas, options) || 'rgba(255, 255, 255, 0.9)';\n capsulePath(ctx, borderRadius, progressWidth, centerY, barHeight);\n ctx.fill();\n ctx.restore();\n }\n}\n\n/**\n * Map of style names (and singular aliases) to their drawing functions.\n * Six visually distinct styles including a simple seekbar; keys are matched\n * against `options.waveformStyle` by {@link draw}.\n * @type {Object.<string, function(CanvasRenderingContext2D, HTMLCanvasElement, number[], number, Object): void>}\n */\nexport const DRAWING_STYLES = {\n 'bars': drawBars, // Classic vertical bars\n 'bar': drawBars,\n 'mirror': drawMirror, // SoundCloud-style symmetrical\n 'line': drawLine, // Smooth oscilloscope wave\n 'blocks': drawBlocks, // LED meter segmented\n 'block': drawBlocks,\n 'dots': drawDots, // Circular points\n 'dot': drawDots,\n 'seekbar': drawSeekbar // Simple progress bar (no waveform)\n};\n\n/**\n * Main drawing entry point that delegates to the style named by\n * `options.waveformStyle`, falling back to {@link drawBars} for unknown styles.\n * @param {CanvasRenderingContext2D} ctx - Canvas context\n * @param {HTMLCanvasElement} canvas - Canvas element\n * @param {number[]} peaks - Waveform peak data (0-1)\n * @param {number} progress - Progress (0-1)\n * @param {Object} options - Drawing options, including `waveformStyle` plus the\n * per-style fields (`barWidth`, `barSpacing`, `barRadius`, `color`, `progressColor`).\n * @returns {void}\n */\nexport function draw(ctx, canvas, peaks, progress, options) {\n const drawFunc = DRAWING_STYLES[options.waveformStyle] || drawBars;\n drawFunc(ctx, canvas, peaks, progress, options);\n}", "/**\n * @module bpm\n * @description BPM detection for audio analysis\n */\n\n/**\n * Estimate the tempo (beats per minute) of an audio buffer.\n *\n * Analyses the first (left/mono) channel by detecting onsets, measuring the\n * time between successive onsets, converting each interval to a tempo, and\n * histogramming those tempos into 3-BPM buckets (60-200 BPM) to find the most\n * common one. Octave errors are corrected by doubling very slow results and\n * halving very fast ones when a strong half/double bucket also exists, then a\n * fixed -1 BPM calibration offset is applied. Returns a 120 BPM fallback when\n * too few onsets are found, and null if analysis throws.\n *\n * @param {AudioBuffer} buffer - Decoded audio buffer to analyse; only channel 0 is read.\n * @returns {number|null} Detected tempo in BPM, 120 as a fallback when onsets are insufficient, or null on error.\n */\nexport function detectBPM(buffer) {\n try {\n const channelData = buffer.getChannelData(0);\n const sampleRate = buffer.sampleRate;\n const onsets = detectOnsets(channelData, sampleRate);\n\n if (onsets.length < 2) return 120;\n\n // Calculate intervals\n const intervals = [];\n for (let i = 1; i < onsets.length; i++) {\n intervals.push((onsets[i] - onsets[i - 1]) / sampleRate);\n }\n\n // Convert to tempos and group\n const tempoGroups = {};\n intervals.forEach(interval => {\n const tempo = 60 / interval;\n const bucket = Math.round(tempo / 3) * 3;\n if (bucket > 60 && bucket < 200) {\n tempoGroups[bucket] = (tempoGroups[bucket] || 0) + 1;\n }\n });\n\n // Find most common\n let maxCount = 0;\n let detectedBPM = 120;\n for (const [tempo, count] of Object.entries(tempoGroups)) {\n if (count > maxCount) {\n maxCount = count;\n detectedBPM = parseInt(tempo);\n }\n }\n\n // Handle tempo ambiguity\n if (detectedBPM < 70 && tempoGroups[detectedBPM * 2]) {\n detectedBPM *= 2;\n } else if (detectedBPM > 160 && tempoGroups[Math.round(detectedBPM / 2)]) {\n detectedBPM = Math.round(detectedBPM / 2);\n }\n\n return detectedBPM - 1; // Calibration offset\n } catch (e) {\n console.warn('[WaveformPlayer] BPM detection failed:', e);\n return null;\n }\n}\n\n/**\n * Detect onset sample positions (transients/beats) within a channel of audio.\n *\n * Slides a 2048-sample window (50% overlap via a half-window hop) across the\n * signal, computing the mean squared energy of each window. An onset is flagged\n * when the energy rise over the previous (smoothed) energy exceeds an adaptive\n * threshold and the window energy is above a noise floor, subject to a minimum\n * spacing of 150 ms so a single transient is not counted twice. The running\n * previousEnergy is exponentially smoothed (0.8 new / 0.2 old) to track the\n * local energy envelope.\n *\n * @param {Float32Array} channelData - PCM samples (normalised -1..1) for a single channel.\n * @param {number} sampleRate - Sample rate in Hz, used to derive the minimum onset spacing.\n * @returns {number[]} Ascending sample indices at which onsets were detected.\n * @private\n */\nfunction detectOnsets(channelData, sampleRate) {\n const windowSize = 2048;\n const hopSize = windowSize / 2;\n const onsets = [];\n let previousEnergy = 0;\n\n for (let i = 0; i < channelData.length - windowSize; i += hopSize) {\n let energy = 0;\n for (let j = i; j < i + windowSize; j++) {\n energy += channelData[j] * channelData[j];\n }\n energy = energy / windowSize;\n\n const energyDiff = energy - previousEnergy;\n const threshold = previousEnergy * 1.8 + 0.01;\n\n if (energyDiff > threshold && energy > 0.01) {\n const lastOnset = onsets[onsets.length - 1] || 0;\n const minDistance = sampleRate * 0.15;\n\n if (i - lastOnset > minDistance) {\n onsets.push(i);\n }\n }\n\n previousEnergy = energy * 0.8 + previousEnergy * 0.2;\n }\n\n return onsets;\n}", "/**\n * @module audio\n * @description Audio processing for WaveformPlayer\n */\n\nimport {detectBPM} from './bpm.js';\nimport {clamp, maxOf, DEFAULT_SAMPLES} from './utils.js';\n\n/**\n * Extract peaks from a decoded audio buffer for waveform visualization.\n *\n * Divides the buffer into `samples` equal-width windows and, within each\n * window, finds the largest absolute amplitude by inspecting every frame (so\n * transients are never missed and the shape is stable across sample counts \u2014\n * matching WaveformGen's offline output). Across multiple channels the\n * per-window peaks are merged by taking the loudest channel, then the whole\n * array is normalized so the maximum peak becomes 1 (a silent buffer is\n * returned unscaled).\n *\n * @param {AudioBuffer} buffer - Decoded audio buffer to analyse.\n * @param {number} [samples=1800] - Number of peak windows (output array length).\n * @returns {number[]} Array of `samples` normalized peak values in the 0-1 range.\n */\nexport function extractPeaks(buffer, samples = DEFAULT_SAMPLES) {\n const sampleSize = buffer.length / samples;\n const channels = buffer.numberOfChannels;\n const peaks = [];\n\n for (let c = 0; c < channels; c++) {\n const chan = buffer.getChannelData(c);\n\n for (let i = 0; i < samples; i++) {\n const start = ~~(i * sampleSize);\n const end = ~~(start + sampleSize);\n\n let min = 0;\n let max = 0;\n\n // Scan EVERY frame in the window. Previously this stepped by\n // sampleSize/10 (~10 samples/window) for speed, but that misses\n // transients and makes the shape change with the sample count.\n // decodeAudioData dominates the cost, so a full scan is worth it \u2014\n // and it now matches WaveformGen's offline output exactly.\n for (let j = start; j < end; j++) {\n const value = chan[j];\n if (value > max) max = value;\n if (value < min) min = value;\n }\n\n const peak = Math.max(Math.abs(max), Math.abs(min));\n\n if (c === 0 || peak > peaks[i]) {\n peaks[i] = peak;\n }\n }\n }\n\n // Normalize peaks\n const maxPeak = maxOf(peaks);\n return maxPeak > 0 ? peaks.map(peak => peak / maxPeak) : peaks;\n}\n\n/**\n * Generate waveform data by fetching and decoding an audio file at a URL.\n *\n * Fetches the URL, decodes it through a short-lived AudioContext, runs\n * {@link extractPeaks} followed by {@link normalizePeaks}, and optionally\n * detects the track's BPM. The AudioContext is created lazily and always\n * closed in the `finally` block so failed decodes never leak one (browsers\n * hard-cap the number of live contexts). Errors are logged and re-thrown so\n * callers can fall back to a placeholder waveform.\n *\n * @param {string} url - Audio file URL to fetch and decode.\n * @param {number} [samples=1800] - Number of peak windows to extract.\n * @param {boolean} [shouldDetectBPM=false] - Whether to run BPM detection on the decoded buffer.\n * @returns {Promise<{peaks: number[], bpm: (number|null)}>} Resolves with the\n * normalized peaks and the detected BPM (`null` when detection is disabled or fails).\n * @throws {Error} Re-throws any fetch/decode error after logging it.\n */\nexport async function generateWaveform(url, samples = DEFAULT_SAMPLES, shouldDetectBPM = false) {\n // Created lazily so the finally block can always close it \u2014 browsers\n // hard-cap live AudioContexts (~6 in Chrome), so leaking one per failed\n // decode would break every subsequent player on the page.\n let audioContext;\n try {\n const AudioCtx = window.AudioContext || /** @type {any} */ (window).webkitAudioContext;\n audioContext = new AudioCtx();\n const response = await fetch(url);\n const arrayBuffer = await response.arrayBuffer();\n const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);\n\n let peaks = extractPeaks(audioBuffer, samples);\n\n // Normalize peaks for consistent visualization\n peaks = normalizePeaks(peaks);\n\n let bpm = null;\n if (shouldDetectBPM) {\n bpm = detectBPM(audioBuffer); // synchronous \u2014 returns number|null\n }\n\n return {peaks, bpm};\n } finally {\n // Error (if any) propagates to the caller, which decides how to log /\n // recover; the context is always closed either way.\n if (audioContext) audioContext.close();\n }\n}\n\n/**\n * Generate a synthetic placeholder waveform for use before (or instead of)\n * real peak data is available.\n *\n * Each bar combines a random base height (0.3-0.8) with a slow sinusoidal\n * variation across the array, clamped to the 0.1-1 range so the result always\n * looks like a plausible waveform rather than pure noise.\n *\n * @param {number} [samples=1800] - Number of bars (output array length).\n * @returns {number[]} Array of `samples` pseudo-random peak values in the 0.1-1 range.\n */\nexport function generatePlaceholderWaveform(samples = DEFAULT_SAMPLES) {\n const data = [];\n for (let i = 0; i < samples; i++) {\n const base = Math.random() * 0.5 + 0.3;\n const variation = Math.sin(i / samples * Math.PI * 4) * 0.2;\n data.push(clamp(base + variation, 0.1, 1));\n }\n return data;\n}\n\n/**\n * Scale peak values so quiet tracks fill the available height consistently.\n *\n * Finds the loudest peak and, only when it is non-zero yet below `targetMax`,\n * scales every peak proportionally so the maximum lands on `targetMax`. Silent\n * arrays (max 0) and already-loud arrays (max above `targetMax`) are returned\n * untouched, so the function never amplifies clipping or divides by zero.\n *\n * @param {number[]} peaks - Peak values, typically in the 0-1 range.\n * @param {number} [targetMax=0.95] - Desired maximum peak after scaling.\n * @returns {number[]} The normalized peak array (the original array when no scaling is applied).\n * @private\n */\nfunction normalizePeaks(peaks, targetMax = 0.95) {\n const maxPeak = maxOf(peaks);\n\n // Don't normalize if already loud enough or silent\n if (maxPeak === 0 || maxPeak > targetMax) return peaks;\n\n // Scale all peaks proportionally\n const scaleFactor = targetMax / maxPeak;\n return peaks.map(peak => peak * scaleFactor);\n}", "/**\n * @module themes\n * @description Color presets and default options for WaveformPlayer\n */\n\nimport {perceivedBrightness, DEFAULT_SAMPLES} from './utils.js';\n\n/**\n * Does `<html>` or `<body>` explicitly signal the given colour scheme via a\n * known class name (`dark`, `dark-mode`, `theme-dark`) or theme attribute\n * (`data-theme`, and `data-color-scheme` on the root)?\n * @param {'dark'|'light'} scheme - Scheme to look for.\n * @returns {boolean} True if the page explicitly hints at `scheme`.\n * @private\n */\nfunction hasThemeHint(scheme) {\n const root = document.documentElement;\n const body = document.body;\n return (\n root.classList.contains(scheme) ||\n root.classList.contains(`${scheme}-mode`) ||\n root.classList.contains(`theme-${scheme}`) ||\n root.getAttribute('data-theme') === scheme ||\n root.getAttribute('data-color-scheme') === scheme ||\n body.classList.contains(scheme) ||\n body.classList.contains(`${scheme}-mode`) ||\n body.getAttribute('data-theme') === scheme\n );\n}\n\n/**\n * Detect the appropriate color scheme for the player from the surrounding page.\n *\n * Resolution order, first match wins:\n * 1. Explicit theme hints on `<html>`/`<body>` \u2014 class names\n * (`dark`, `dark-mode`, `theme-dark`, light equivalents) and data\n * attributes (`data-theme`, `data-color-scheme`).\n * 2. The page's computed `<body>` background colour, classified via\n * {@link perceivedBrightness} (>128 = light, <128 = dark; exactly 128\n * or unparseable is treated as ambiguous and falls through).\n * 3. The OS/browser `prefers-color-scheme` media query.\n * 4. Default fallback of `'dark'` (most audio players are dark).\n *\n * @returns {string} The detected scheme, either `'dark'` or `'light'`.\n */\nexport function detectColorScheme() {\n // 1. Explicit theme class names / data attributes win.\n if (hasThemeHint('dark')) return 'dark';\n if (hasThemeHint('light')) return 'light';\n\n // 2. Try to detect website's theme from background color\n try {\n const bodyBg = getComputedStyle(document.body).backgroundColor;\n const brightness = perceivedBrightness(bodyBg);\n\n // Clear determination: bright background = light theme. Exactly 128\n // (or unparseable) is ambiguous \u2014 fall through to the next method.\n if (brightness !== null) {\n if (brightness > 128) return 'light';\n if (brightness < 128) return 'dark';\n }\n } catch (e) {\n // If background detection fails, continue to next method\n }\n\n // 3. Check system preference\n if (window.matchMedia) {\n if (window.matchMedia('(prefers-color-scheme: dark)').matches) {\n return 'dark';\n }\n if (window.matchMedia('(prefers-color-scheme: light)').matches) {\n return 'light';\n }\n }\n\n // 4. Default fallback (most audio players are dark)\n return 'dark';\n}\n\n/**\n * Built-in colour presets keyed by scheme name.\n *\n * Each preset is a flat map of the player's themeable colour tokens\n * (waveform, progress, button, text, background, border). They are deliberately\n * simple translucent black/white values so they sit on any host background, and\n * any individual token can be overridden per-instance via the matching\n * `*Color` option in {@link DEFAULT_OPTIONS}.\n *\n * @type {Object<string, Object<string, string>>}\n * @property {Object<string, string>} dark Light-on-dark token set.\n * @property {Object<string, string>} light Dark-on-light token set.\n */\nexport const COLOR_PRESETS = {\n dark: {\n waveformColor: 'rgba(255, 255, 255, 0.3)',\n progressColor: 'rgba(255, 255, 255, 0.9)'\n },\n light: {\n waveformColor: 'rgba(0, 0, 0, 0.2)',\n progressColor: 'rgba(0, 0, 0, 0.8)'\n }\n};\n\n/**\n * Resolve a colour preset by name, falling back to auto-detection.\n *\n * When `presetName` names a known preset it is returned as-is; otherwise\n * (null, undefined, or an unrecognised name) the scheme is auto-detected via\n * {@link detectColorScheme} and the corresponding preset is returned.\n *\n * @param {string|null} presetName - Preset name (`'dark'` or `'light'`), or\n * null/invalid to trigger auto-detection.\n * @returns {Object<string, string>} The matching colour token map from\n * {@link COLOR_PRESETS}.\n */\nexport function getColorPreset(presetName) {\n // If explicitly set to a valid preset, use it\n if (presetName && COLOR_PRESETS[presetName]) {\n return COLOR_PRESETS[presetName];\n }\n\n // Auto-detect if not specified or invalid\n const detected = detectColorScheme();\n return COLOR_PRESETS[detected];\n}\n\n/**\n * Default option set for a {@link WaveformPlayer} instance.\n *\n * User-supplied options are merged over this object, so every supported option\n * is enumerated here with its baseline value. `null` colour tokens mean \"inherit\n * from the resolved {@link COLOR_PRESETS} preset\"; `null` content/callback\n * fields mean \"unset\". See the grouped inline comments for per-field notes,\n * notably the `audioMode` self/external distinction and the `accessibleSeek`\n * keyboard slider.\n *\n * @type {Object}\n */\nexport const DEFAULT_OPTIONS = {\n // Core settings\n url: '',\n height: 64,\n // Source peak resolution for LIVE decode (ignored when peaks are supplied).\n // The drawer resamples these to fit canvasWidth / (barWidth + barSpacing)\n // bars, so this is fidelity headroom, not the visible bar count. 1800 (the\n // SoundCloud/WaveformGen figure) keeps wide / high-DPI waveforms crisp; the\n // every-frame scan means a higher value costs no extra extraction time.\n samples: DEFAULT_SAMPLES,\n preload: 'metadata',\n // CORS mode for the underlying <audio> element. Left null by default so the\n // player behaves like a plain <audio> and never forces a CORS request \u2014\n // setting 'anonymous' here would break playback of media hosted on origins\n // that don't send Access-Control-Allow-Origin (default-config S3/CDN). The\n // playback element is never fed to createMediaElementSource, so it never\n // needs CORS-clean media; peak analysis uses a separate fetch() path with\n // its own placeholder fallback. Set 'anonymous' / 'use-credentials' only if\n // you specifically need it. See issue #18.\n crossOrigin: null,\n\n // Audio mode \u2014 'self' = player owns the <audio> element (default, current\n // behavior). 'external' = player is a visualization-only surface; no audio\n // element is created, play() dispatches `waveformplayer:request-play`\n // instead of calling audio.play(), and setPlayingState/setProgress are\n // expected to be driven by an external controller (e.g. WaveformBar).\n audioMode: 'self',\n\n // Playback\n playbackRate: 1,\n showPlaybackSpeed: false,\n playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2],\n\n // Layout Options\n buttonAlign: 'auto',\n // Player layout. 'default' = play button + waveform with a left-aligned\n // info row below. 'preview' = compact: the title is centered under the\n // waveform and the meta row (time / speed / BPM) is trimmed \u2014 ideal for\n // sample-pack sample previews and dense grids.\n layout: 'default',\n // Play/pause button style. 'circle' = bordered circle (default).\n // 'minimal' = a bare play/pause glyph with no circle \u2014 the look sample-pack\n // and beat stores use in their preview grids.\n buttonStyle: 'circle',\n // Play/pause button size. null = the stylesheet default (36px circle /\n // proportional minimal). A number is treated as px; a string (e.g. '4rem')\n // is used verbatim. Sets the `--wfp-btn-size` CSS var, which scales BOTH\n // styles \u2014 box and glyph \u2014 proportionally.\n buttonSize: null,\n // Play/pause button corner radius. null = the stylesheet default (50%, a\n // circle). A number is treated as px; a string (e.g. '8px') is used\n // verbatim. Set 0 for a square button. Sets the `--wfp-btn-radius` CSS var,\n // which shapes the 'circle' style's box \u2014 the bare 'minimal' glyph has no\n // box to round, so this is a no-op there.\n buttonRadius: null,\n\n // Default waveform style\n waveformStyle: 'mirror',\n barWidth: 2,\n barSpacing: 0,\n // Rounded bar caps (px). 0 = square; 1 = soft caps (default). Applies to bars/mirror.\n barRadius: 1,\n\n // Gradient axis when waveformColor/progressColor is an array of stops:\n // 'vertical' (top->bottom canvas gradient), 'horizontal' (hue sweep across the\n // waveform) or 'diagonal'. Ignored for single colours.\n waveformGradient: 'vertical',\n\n // Color preset: null = auto-detect, 'dark' = force dark, 'light' = force light\n colorPreset: null,\n\n // Canvas colours (null = inherit the resolved preset; arrays = gradient\n // stops, see waveformGradient). The DOM chrome (button, title, meta) is\n // themed via CSS variables (--wfp-button-color / --wfp-text-color /\n // --wfp-text-secondary-color), not JS options.\n waveformColor: null,\n progressColor: null,\n\n // Features\n autoplay: false,\n showControls: true,\n showInfo: true,\n showTime: true,\n showHoverTime: false,\n // Show a draggable circle handle + hover brightness-lift on the SEEKBAR\n // style only (it's meaningless on a waveform, where the fill-edge is the\n // playhead). Off by default; the bar turns it on. Drag-to-scrub works\n // regardless of this.\n seekHandle: false,\n showBPM: false,\n // Known BPM to display in the badge (with showBPM). Wins over auto-detection\n // \u2014 set it when peaks are pre-generated so the tempo still shows. null = auto.\n bpm: null,\n singlePlay: true,\n playOnSeek: true,\n enableMediaSession: true,\n\n // Markers\n markers: [],\n showMarkers: true,\n\n // Accessibility \u2014 expose the waveform as a keyboard-operable slider\n // (role=\"slider\" + ARIA value attributes + arrow/page/home/end seeking).\n // seekLabel sets the slider's accessible name; when null it falls back\n // to the track title, then 'Seek'.\n // seekValueText templates the slider's spoken aria-valuetext: %1$s is the\n // current time and %2$s the total duration (both formatted M:SS). When null\n // it falls back to '%1$s of %2$s'. Lets consumers localize the connective\n // text without reformatting the times.\n accessibleSeek: true,\n seekLabel: null,\n seekValueText: null,\n\n // Content\n title: null,\n artist: null,\n artwork: null,\n // Where the `artwork` image renders. 'info' = the info row beside the title\n // (needs showInfo). 'button' = the cover becomes the play/pause button,\n // which still works with the info row hidden. Only ONE placement ever\n // renders, so the same cover can't appear twice.\n //\n // 'button' is self-contained: the stylesheet drops the button's ring and\n // swaps its defaults for cover-sized ones (64px, 8px rounding), because the\n // transport defaults are wrong for artwork in both directions \u2014 36px\n // smudges a cover, and 50% crops square art into a circle. buttonSize /\n // buttonRadius still override it if you want a different tile.\n artworkPosition: 'info',\n album: '',\n\n // Message shown in the error state when audio fails to load.\n errorText: 'Unable to load audio',\n\n // Localizable UI strings (alongside seekLabel / seekValueText / errorText).\n // These are screen-reader / assistive-tech facing, so translate them for\n // non-English UIs. playPauseLabel/speedLabel are aria-labels; artworkAlt is\n // the cover image alt text; unknownTrackText is the Media Session (lock-\n // screen) title fallback used when no track title is set.\n playPauseLabel: 'Play/Pause',\n speedLabel: 'Playback speed',\n artworkAlt: 'Album artwork',\n unknownTrackText: 'Unknown Track',\n\n // Icons (SVG)\n playIcon: '<svg viewBox=\"0 0 24 24\" width=\"16\" height=\"16\"><path d=\"M8 5v14l11-7z\"/></svg>',\n pauseIcon: '<svg viewBox=\"0 0 24 24\" width=\"16\" height=\"16\"><path d=\"M6 4h4v16H6zM14 4h4v16h-4z\"/></svg>',\n\n // Callbacks\n onLoad: null,\n onPlay: null,\n onPause: null,\n onEnd: null,\n onError: null,\n onTimeUpdate: null,\n\n // Optional queue navigation \u2014 when set, the player registers Media Session\n // nexttrack/previoustrack handlers (lock-screen skip buttons). Called with\n // the player instance; wired by waveform-bar / -playlist.\n onNextTrack: null,\n onPreviousTrack: null\n};\n\n/**\n * Per-waveform-style geometry defaults.\n *\n * Maps each supported `waveformStyle` to its natural `barWidth`/`barSpacing`\n * (in px), used to seed bar geometry when the caller has not explicitly set\n * those options so each style renders at sensible proportions.\n *\n * @type {Object<string, {barWidth: number, barSpacing: number}>}\n */\nexport const STYLE_DEFAULTS = {\n bars: {barWidth: 3, barSpacing: 1},\n mirror: {barWidth: 2, barSpacing: 2},\n line: {barWidth: 2, barSpacing: 0},\n blocks: {barWidth: 4, barSpacing: 2},\n dots: {barWidth: 3, barSpacing: 3},\n seekbar: {barWidth: 1, barSpacing: 0}\n};", "/**\n * @module core\n * @description Main WaveformPlayer class\n */\n\nimport {draw} from './drawing.js';\nimport {generateWaveform, generatePlaceholderWaveform} from './audio.js';\nimport {\n formatTime,\n formatSeekValueText,\n extractTitleFromUrl,\n generateId,\n parseDataAttributes,\n mergeOptions,\n debounce,\n clamp,\n escapeHtml,\n formatCssLength,\n DEFAULT_SAMPLES\n} from './utils.js';\n\nimport {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS, detectColorScheme} from './themes.js';\n\n/**\n * Placeholder shown when an artwork URL fails to load (404 / broken) \u2014 a muted\n * music-note tile, so a missing cover degrades gracefully instead of showing the\n * browser's broken-image icon. Inline SVG data-URI (no font / network needed).\n */\nconst ARTWORK_FALLBACK = 'data:image/svg+xml,' + encodeURIComponent(\n '<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect width=\"24\" height=\"24\" rx=\"4\" fill=\"#71717a\" fill-opacity=\"0.15\"/><g fill=\"none\" stroke=\"#a1a1aa\" stroke-width=\"1.4\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><circle cx=\"8\" cy=\"17\" r=\"2.2\"/><circle cx=\"17\" cy=\"15\" r=\"2.2\"/><path d=\"M10.2 17V7l9-1.6v9\"/></g></svg>'\n);\n\n// Keyboard seek steps (seconds) for the accessible slider.\nconst SEEK_STEP_SECONDS = 5;\nconst SEEK_PAGE_SECONDS = 10;\n\n// Controls that own their own focus. A click landing on one of these (or a\n// descendant) must leave focus on the control, not pull it onto the player\n// container \u2014 otherwise activating the play button/slider steals focus onto the\n// wrapper, moving it off the control the user just operated.\nconst INTERACTIVE_ELEMENTS = 'button, a[href], input, [role=\"slider\"]';\n\n/**\n * WaveformPlayer - Modern audio player with waveform visualization\n * @class\n */\nexport class WaveformPlayer {\n /** @type {Map<string, WaveformPlayer>} */\n static instances = new Map();\n\n /** @type {WaveformPlayer|null} */\n static currentlyPlaying = null;\n\n /**\n * Create a new WaveformPlayer instance.\n *\n * Resolves the container, merges options (defaults < `data-*` attributes <\n * constructor options), applies the colour preset and style-specific\n * defaults, registers the instance in the static map, and kicks off\n * {@link WaveformPlayer#init}. A `waveformplayer:ready` event is dispatched\n * ~100ms later, once initialization has settled.\n *\n * @param {string|HTMLElement} container - Container element, or a CSS\n * selector resolved with `document.querySelector`.\n * @param {Object} [options={}] - Player options. Accepts the shorthand\n * aliases `style` (\u2192 `waveformStyle`) and `src` (\u2192 `url`); the canonical\n * names win if both are supplied.\n * @throws {Error} If the container element cannot be found.\n * @fires WaveformPlayer#waveformplayer:ready\n */\n constructor(container, options = {}) {\n // Resolve container\n this.container = typeof container === 'string'\n ? document.querySelector(container)\n : container;\n\n if (!this.container) {\n throw new Error('[WaveformPlayer] Container element not found');\n }\n\n // Parse data attributes if present\n const dataOptions = parseDataAttributes(this.container);\n\n // Shorthand option aliases \u2014 `style` -> `waveformStyle`, `src` -> `url`.\n // The canonical names still work and win if both are supplied.\n const userOptions = { ...options };\n if (userOptions.style && !userOptions.waveformStyle) userOptions.waveformStyle = userOptions.style;\n if (userOptions.src && !userOptions.url) userOptions.url = userOptions.src;\n\n // Merge options: defaults < data attributes < constructor options\n this.options = mergeOptions(DEFAULT_OPTIONS, dataOptions, userOptions);\n\n // Apply color preset (auto-detect if not specified)\n const preset = getColorPreset(this.options.colorPreset);\n\n // Track auto-theme state so the player can re-detect on a runtime light/\n // dark switch. Only colours the user LEFT UNSET follow the preset; an\n // explicit colorPreset or hand-set colour is never overridden.\n this._autoTheme = this.options.colorPreset == null || !COLOR_PRESETS[this.options.colorPreset];\n this._presetKeys = [];\n // Resolved scheme name for the CSS theme class (CSS provides the DOM\n // chrome colours via --wfp-* variables).\n this._scheme = (this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset]) ? this.options.colorPreset : detectColorScheme();\n\n // Apply preset colors only if individual colors aren't explicitly set\n for (const [key, value] of Object.entries(preset)) {\n if (this.options[key] === null || this.options[key] === undefined) {\n this.options[key] = value;\n this._presetKeys.push(key);\n }\n }\n\n // Apply style-specific defaults if not explicitly set\n const styleDefaults = STYLE_DEFAULTS[this.options.waveformStyle];\n if (styleDefaults) {\n if (dataOptions.barWidth === undefined && options.barWidth === undefined) {\n this.options.barWidth = styleDefaults.barWidth;\n }\n if (dataOptions.barSpacing === undefined && options.barSpacing === undefined) {\n this.options.barSpacing = styleDefaults.barSpacing;\n }\n }\n\n // Initialize state\n this.audio = null;\n this.canvas = null;\n this.ctx = null;\n this.waveformData = [];\n this.progress = 0;\n this._activeMarkerIndex = -1;\n this._markerLabelTimer = null;\n this.isPlaying = false;\n this.isLoading = false;\n this.hasError = false;\n this.updateTimer = null;\n this.resizeObserver = null;\n\n // All DOM/document listeners are registered with this signal so a\n // single abort() in destroy() tears every one of them down (the old\n // destroy left the document-click and container listeners attached).\n this._ac = new AbortController();\n\n // Generate unique ID\n this.id = this.container.id || generateId(this.options.url);\n\n // Add to instances\n WaveformPlayer.instances.set(this.id, this);\n\n // Re-detect the theme on runtime light/dark switches (shared watcher).\n WaveformPlayer._watchTheme();\n\n // Initialize\n this.init();\n\n // Dispatch ready event after initialization\n setTimeout(() => {\n this._emit('waveformplayer:ready', {player: this, url: this.options.url});\n }, 100);\n }\n\n /**\n * Build and dispatch a bubbling `waveformplayer:*` CustomEvent on the\n * container, returning the event so cancelable (request-*) events can have\n * their `defaultPrevented` checked. Single source of truth for the event\n * shape \u2014 every player event bubbles and carries the supplied detail.\n * @param {string} type - Full event type, e.g. `'waveformplayer:play'`.\n * @param {Object} detail - Event detail payload.\n * @param {boolean} [cancelable=false] - Whether the event is cancelable.\n * @returns {CustomEvent} The dispatched event.\n * @private\n */\n _emit(type, detail, cancelable = false) {\n const event = new CustomEvent(type, { bubbles: true, cancelable, detail });\n this.container.dispatchEvent(event);\n return event;\n }\n\n /**\n * External-mode seek request: dispatch a cancelable\n * `waveformplayer:request-seek` and, unless the controller calls\n * `preventDefault()`, optimistically advance the local progress overlay so\n * the canvas repaints at once. Shared by the keyboard slider and canvas click.\n * @param {number} percent - Target position as a 0..1 fraction.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n _requestSeek(percent) {\n const evt = this._emit('waveformplayer:request-seek', { ...this._buildTrackDetail(), percent }, true);\n if (!evt.defaultPrevented) {\n this.progress = percent;\n this.drawWaveform?.();\n }\n }\n\n // ============================================\n // Initialization\n // ============================================\n\n /**\n * Initialize the player: build the DOM, create the audio element (self\n * mode only), wire up the feature controls (speed, keyboard, accessible\n * seek), bind events, attach the resize observer, then size the canvas and\n * \u2014 if a `url` option was given \u2014 load it and optionally autoplay.\n * @private\n */\n init() {\n this.createDOM();\n this.createAudio();\n this.initPlaybackSpeed();\n this.initKeyboardControls();\n this.initSeekControl();\n this.bindEvents();\n this.setupResizeObserver();\n\n // Ensure proper sizing after DOM is ready\n requestAnimationFrame(() => {\n this.resizeCanvas();\n\n // Load audio if URL provided\n if (this.options.url) {\n this.load(this.options.url).then(() => {\n if (this.options.autoplay) {\n this.play()?.catch(() => {});\n }\n }).catch(error => {\n console.error('[WaveformPlayer] Failed to load audio:', error);\n });\n }\n });\n }\n\n /**\n * Build the player's DOM tree inside the container and cache element\n * references.\n *\n * Clears the container, resolves button alignment (`auto` \u2192 `bottom` for\n * the `bars` style, `center` otherwise), and conditionally renders the play\n * button, info row (artwork/title/artist), BPM badge, playback-speed\n * menu, and time display based on the relevant `show*` options. Caches the\n * canvas, controls, and text elements onto `this`, then sizes the canvas.\n * @private\n */\n createDOM() {\n // Clear container\n this.container.innerHTML = '';\n this.container.className = 'waveform-player';\n\n // Determine button alignment\n let buttonAlign = this.options.buttonAlign;\n if (buttonAlign === 'auto') {\n // Auto-align based on waveform style\n const style = this.options.waveformStyle;\n if (style === 'bars') {\n buttonAlign = 'bottom';\n } else {\n buttonAlign = 'center'; // blocks, mirror, line, dots, seekbar all center\n }\n }\n\n // Compact 'preview' layout: centered title under the waveform with the\n // meta row trimmed. Set via the `layout` option / data-layout=\"preview\".\n const isPreview = this.options.layout === 'preview';\n if (isPreview) {\n this.container.classList.add('waveform-layout-preview');\n }\n this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');\n\n // Button sizing/shape ride in on CSS vars so the stylesheet keeps the\n // fallbacks (36px, 50%) \u2014 an unset option emits no var at all.\n const buttonVars = [];\n if (this.options.buttonSize != null) {\n buttonVars.push(`--wfp-btn-size: ${formatCssLength(this.options.buttonSize)}`);\n }\n if (this.options.buttonRadius != null) {\n buttonVars.push(`--wfp-btn-radius: ${formatCssLength(this.options.buttonRadius)}`);\n }\n const buttonStyleAttr = buttonVars.length ? ` style=\"${buttonVars.join('; ')};\"` : '';\n\n // Artwork renders in exactly one place \u2014 the button or the info row \u2014\n // so the same cover can never appear twice. Button artwork is\n // decorative: the button already carries its own aria-label, so a\n // second accessible name here would just be noise.\n const artworkOnButton = this.options.artworkPosition === 'button' && this.options.artwork;\n const buttonArtworkHTML = artworkOnButton\n ? `<img class=\"waveform-btn-artwork\" src=\"${escapeHtml(this.options.artwork)}\" alt=\"\" aria-hidden=\"true\">`\n : '';\n\n // Build play button HTML (conditional)\n const buttonHTML = this.options.showControls ? `\n <button class=\"waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}${artworkOnButton ? ' waveform-btn-has-artwork' : ''}\" aria-label=\"${escapeHtml(this.options.playPauseLabel)}\"${buttonStyleAttr}>\n ${buttonArtworkHTML}\n <span class=\"waveform-icon-play\">${this.options.playIcon}</span>\n <span class=\"waveform-icon-pause\" style=\"display:none;\">${this.options.pauseIcon}</span>\n </button>\n ` : '';\n\n // Build info section HTML (conditional)\n const infoHTML = this.options.showInfo ? `\n <div class=\"waveform-info\">\n ${this.options.artworkPosition !== 'button' && this.options.artwork ? `\n <img class=\"waveform-artwork\" src=\"${escapeHtml(this.options.artwork)}\" alt=\"${escapeHtml(this.options.artworkAlt)}\" style=\"\n width: 40px;\n height: 40px;\n border-radius: 4px;\n object-fit: cover;\n flex-shrink: 0;\n \">\n ` : ''}\n <div class=\"waveform-text\">\n <span class=\"waveform-title\"></span>\n ${this.options.artist ? `<span class=\"waveform-artist\">${escapeHtml(this.options.artist)}</span>` : ''}\n </div>\n <div class=\"waveform-meta\" style=\"display: flex; align-items: center; gap: 1rem;\">\n ${this.options.showBPM ? `\n <span class=\"waveform-bpm\" style=\"display: none;\">\n <span class=\"bpm-value\">--</span> BPM\n </span>\n ` : ''}\n ${this.options.showPlaybackSpeed ? `\n <div class=\"waveform-speed\">\n <button class=\"speed-btn\" aria-label=\"${escapeHtml(this.options.speedLabel)}\" aria-haspopup=\"menu\" aria-expanded=\"false\">\n <span class=\"speed-value\">1x</span>\n </button>\n <div class=\"speed-menu\" role=\"menu\" aria-label=\"${escapeHtml(this.options.speedLabel)}\" style=\"display: none;\">\n ${this.options.playbackRates.map(rate =>\n `<button class=\"speed-option\" role=\"menuitemradio\" tabindex=\"-1\" aria-checked=\"false\" data-rate=\"${rate}\">${rate}x</button>`\n ).join('')}\n </div>\n </div>\n ` : ''}\n ${this.options.showTime ? `\n <span class=\"waveform-time\">\n <span class=\"time-current\">0:00</span> / <span class=\"time-total\">0:00</span>\n </span>\n ` : ''}\n </div>\n </div>\n ` : '';\n\n // Create HTML structure\n this.container.innerHTML = `\n <div class=\"waveform-player-inner\">\n <div class=\"waveform-body\">\n <div class=\"waveform-track waveform-align-${buttonAlign}\">\n ${buttonHTML}\n \n <div class=\"waveform-container\">\n <canvas></canvas>\n <div class=\"waveform-markers\"></div>\n <div class=\"waveform-loading\" style=\"display:none;\"></div>\n <div class=\"waveform-error\" style=\"display:none;\" role=\"alert\">\n <span class=\"waveform-error-text\">${escapeHtml(this.options.errorText)}</span>\n </div>\n </div>\n </div>\n \n ${infoHTML}\n </div>\n </div>\n`;\n\n // Get references\n this.playBtn = this.container.querySelector('.waveform-btn');\n this.canvas = this.container.querySelector('canvas');\n this.ctx = this.canvas.getContext('2d');\n this.titleEl = this.container.querySelector('.waveform-title');\n this.artistEl = this.container.querySelector('.waveform-artist');\n // One reference for either placement \u2014 artworkPosition is structural\n // (like showInfo/buttonStyle) and fixed for the player's lifetime, so\n // the image only ever has one home.\n this.artworkEl = this.container.querySelector('.waveform-artwork, .waveform-btn-artwork');\n this.bindArtworkFallback(this.artworkEl);\n this.currentTimeEl = this.container.querySelector('.time-current');\n this.totalTimeEl = this.container.querySelector('.time-total');\n this.bpmEl = this.container.querySelector('.waveform-bpm');\n this.bpmValueEl = this.container.querySelector('.bpm-value');\n this.loadingEl = this.container.querySelector('.waveform-loading');\n this.errorEl = this.container.querySelector('.waveform-error');\n this.markersContainer = this.container.querySelector('.waveform-markers');\n this.speedBtn = this.container.querySelector('.speed-btn');\n this.speedMenu = this.container.querySelector('.speed-menu');\n\n // Set canvas size\n this.resizeCanvas();\n\n // Show a caller-supplied BPM immediately (no audio decode required).\n this.updateBPMDisplay();\n }\n\n /**\n * Bind the artwork error fallback to an image element.\n *\n * @param {HTMLImageElement|null} img - Artwork image element.\n * @private\n */\n bindArtworkFallback(img) {\n if (!img) return;\n\n // Fall back to the placeholder tile if the cover URL fails to load.\n img.addEventListener('error', () => {\n if (!img.src.startsWith('data:')) img.src = ARTWORK_FALLBACK;\n }, {signal: this._ac.signal});\n }\n\n /**\n * Create an artwork image element matching the initial player markup.\n *\n * @returns {HTMLImageElement} Artwork image element.\n * @private\n */\n createArtworkElement() {\n const img = document.createElement('img');\n img.className = 'waveform-artwork';\n img.style.width = '40px';\n img.style.height = '40px';\n img.style.borderRadius = '4px';\n img.style.objectFit = 'cover';\n img.style.flexShrink = '0';\n this.bindArtworkFallback(img);\n return img;\n }\n\n /**\n * Create a play-button artwork image matching the initial player markup.\n *\n * Decorative by design \u2014 the button already has an accessible name from its\n * `aria-label`, so the cover is hidden from assistive tech rather than\n * given a competing one. Sizing and clipping are the stylesheet's job.\n *\n * @returns {HTMLImageElement} Play-button artwork image element.\n * @private\n */\n createButtonArtworkElement() {\n const img = document.createElement('img');\n img.className = 'waveform-btn-artwork';\n img.alt = '';\n img.setAttribute('aria-hidden', 'true');\n this.bindArtworkFallback(img);\n return img;\n }\n\n /**\n * Create an artist text element matching the initial player markup.\n *\n * @returns {HTMLSpanElement} Artist text element.\n * @private\n */\n createArtistElement() {\n const span = document.createElement('span');\n span.className = 'waveform-artist';\n return span;\n }\n\n /**\n * Reconcile artist metadata and markup for the current track.\n *\n * @param {string|null} artist - Artist text, or a falsy value to remove it.\n * @private\n */\n syncArtist(artist) {\n this.options.artist = artist || null;\n\n if (!this.options.showInfo) return;\n\n if (!artist) {\n this.artistEl?.remove();\n this.artistEl = null;\n return;\n }\n\n if (!this.artistEl) {\n const titleEl = this.container.querySelector('.waveform-title');\n if (!titleEl) return;\n this.artistEl = this.createArtistElement();\n titleEl.after(this.artistEl);\n }\n\n this.artistEl.textContent = artist;\n this.artistEl.style.display = '';\n }\n\n /**\n * Reconcile the play button's artwork image (`artworkPosition: 'button'`).\n *\n * Shares {@link bindArtworkFallback} with the info-row image, so a broken\n * cover degrades to the same placeholder tile in either placement. The\n * `waveform-btn-has-artwork` class is what turns on the scrim + glyph\n * treatment, so it tracks the image's presence exactly.\n *\n * @param {string|null} artwork - Artwork image URL, or a falsy value to remove it.\n * @private\n */\n syncButtonArtwork(artwork) {\n // Nothing to hang the cover on when the transport is hidden.\n if (!this.playBtn) return;\n\n if (!artwork) {\n this.artworkEl?.remove();\n this.artworkEl = null;\n this.playBtn.classList.remove('waveform-btn-has-artwork');\n return;\n }\n\n if (!this.artworkEl) {\n this.artworkEl = this.createButtonArtworkElement();\n this.playBtn.prepend(this.artworkEl);\n }\n\n this.artworkEl.src = artwork;\n this.playBtn.classList.add('waveform-btn-has-artwork');\n }\n\n /**\n * Reconcile artwork metadata and markup for the current track.\n *\n * The image lives in one of two homes depending on `artworkPosition` \u2014 the\n * info row (default) or the play button \u2014 and only ever one of them, so a\n * cover can't render twice.\n *\n * @param {string|null} artwork - Artwork image URL, or a falsy value to remove artwork.\n * @param {string} artworkAlt - Artwork alt text. Info-row placement only:\n * button artwork is decorative and defers to the button's own label.\n * @private\n */\n syncArtwork(artwork, artworkAlt = '') {\n this.options.artwork = artwork || null;\n this.options.artworkAlt = artworkAlt || '';\n\n if (this.options.artworkPosition === 'button') {\n this.syncButtonArtwork(this.options.artwork);\n return;\n }\n\n if (!this.options.showInfo) return;\n\n if (!artwork) {\n this.artworkEl?.remove();\n this.artworkEl = null;\n return;\n }\n\n if (!this.artworkEl) {\n const textEl = this.container.querySelector('.waveform-text');\n if (!textEl) return;\n this.artworkEl = this.createArtworkElement();\n textEl.before(this.artworkEl);\n }\n\n this.artworkEl.src = artwork;\n this.artworkEl.alt = artworkAlt || '';\n }\n\n /**\n * Create audio element\n * @private\n *\n * No-op in `audioMode: 'external'` \u2014 the player has no audio of its\n * own; an external controller (e.g. WaveformBar) owns playback and\n * pushes state in via setPlayingState() / setProgress(). The\n * `this.audio` field stays null in that mode; downstream code must\n * null-check it.\n */\n createAudio() {\n if (this.options.audioMode === 'external') {\n this.audio = null;\n return;\n }\n this.audio = new Audio();\n this.audio.preload = this.options.preload || 'metadata';\n // Only set crossOrigin when explicitly opted in. Setting it forces a\n // CORS request that breaks playback of media on origins without\n // Access-Control-Allow-Origin; the player never needs CORS-clean media\n // for playback (no createMediaElementSource). See issue #18.\n if (this.options.crossOrigin) {\n this.audio.crossOrigin = this.options.crossOrigin;\n }\n }\n\n // ============================================\n // Feature Initialization\n // ============================================\n\n /**\n * Apply the configured initial playback rate to the audio element (self\n * mode only) and, when `showPlaybackSpeed` is enabled, wire up the speed\n * menu UI via {@link WaveformPlayer#initSpeedControls}.\n * @private\n */\n initPlaybackSpeed() {\n // External mode has no <audio> element, so the speed control\n // doesn't apply locally \u2014 the external controller (e.g.\n // WaveformBar) owns playback rate. Skip the audio init but\n // still bind the speed control UI in case the controller\n // wants to mirror rate changes via events later.\n if (this.audio && this.options.playbackRate && this.options.playbackRate !== 1) {\n this.audio.playbackRate = this.options.playbackRate;\n }\n\n // Initialize speed control UI if enabled\n if (this.options.showPlaybackSpeed) {\n this.initSpeedControls();\n }\n }\n\n /**\n * Wire up the playback-speed menu: toggle it open on the speed button,\n * close it on outside click or Escape (keeping `aria-expanded` on the\n * trigger in sync), and apply the chosen rate when a `.speed-option` is\n * clicked, returning focus to the trigger. All listeners are registered against the\n * instance `AbortController` signal so {@link WaveformPlayer#destroy} tears\n * them down. No-op if the speed elements are absent.\n * @private\n */\n initSpeedControls() {\n const speedBtn = this.container.querySelector('.speed-btn');\n const speedMenu = this.container.querySelector('.speed-menu');\n\n if (!speedBtn || !speedMenu) return;\n\n const options = () => Array.from(speedMenu.querySelectorAll('.speed-option'));\n const isOpen = () => speedMenu.style.display !== 'none';\n\n // Open/close the menu (a proper role=\"menu\"): keep aria-expanded in sync\n // and, on open, move focus into the menu \u2014 the checked rate, else the\n // first option \u2014 so arrow keys drive it.\n const setSpeedMenu = (open) => {\n speedMenu.style.display = open ? 'block' : 'none';\n speedBtn.setAttribute('aria-expanded', open ? 'true' : 'false');\n if (open) {\n const opts = options();\n (opts.find((o) => o.getAttribute('aria-checked') === 'true') || opts[0])?.focus();\n }\n };\n\n const focusOption = (i) => {\n const opts = options();\n if (opts.length) opts[(i + opts.length) % opts.length].focus();\n };\n\n const selectOption = (opt) => {\n this.setPlaybackRate(parseFloat(opt.dataset.rate));\n setSpeedMenu(false);\n speedBtn.focus();\n };\n\n // Toggle from the trigger.\n speedBtn.addEventListener('click', (e) => {\n e.stopPropagation();\n setSpeedMenu(!isOpen());\n }, {signal: this._ac.signal});\n\n // Close when clicking outside.\n document.addEventListener('click', () => setSpeedMenu(false), {signal: this._ac.signal});\n\n // Pointer + native-button (Enter/Space on an option) selection.\n speedMenu.addEventListener('click', (e) => {\n e.stopPropagation();\n const opt = e.target.closest('.speed-option');\n if (opt) selectOption(opt);\n }, {signal: this._ac.signal});\n\n // Keyboard menu: arrows/Home/End roam, Enter/Space select (via the\n // native button click above), Escape/Tab close. ArrowDown/Up on the\n // trigger opens and focuses the first/checked option.\n speedBtn.closest('.waveform-speed')?.addEventListener('keydown', (e) => {\n const opts = options();\n const i = opts.indexOf(document.activeElement);\n if (!isOpen()) {\n if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && document.activeElement === speedBtn) {\n e.preventDefault();\n setSpeedMenu(true);\n }\n return;\n }\n switch (e.key) {\n case 'ArrowDown': e.preventDefault(); focusOption(i < 0 ? 0 : i + 1); break;\n case 'ArrowUp': e.preventDefault(); focusOption(i < 0 ? opts.length - 1 : i - 1); break;\n case 'Home': e.preventDefault(); focusOption(0); break;\n case 'End': e.preventDefault(); focusOption(opts.length - 1); break;\n case 'Escape': e.preventDefault(); setSpeedMenu(false); speedBtn.focus(); break;\n case 'Tab': setSpeedMenu(false); break;\n }\n }, {signal: this._ac.signal});\n\n // Set initial UI state\n this.updateSpeedUI();\n }\n\n /**\n * Enable keyboard transport controls on the container.\n *\n * The container is focusable only after it is clicked (it carries\n * `tabindex=\"-1\"` until then, and clicking steals focus from sibling\n * players). While focused it handles: digits 0-9 (seek to that tenth of\n * the track), Space (toggle play), and \u2014 in self mode only, since\n * `this.audio` is null in external mode \u2014 arrow keys (seek \u00B15s, volume\n * \u00B10.1) and `m`/`M` (mute). Listeners use the instance abort signal.\n * @private\n */\n initKeyboardControls() {\n // Make container focusable but not in tab order by default\n this.container.setAttribute('tabindex', '-1');\n\n // Only activate keyboard controls when explicitly focused (clicked).\n // Skip when the click landed on an interactive control (play button,\n // link, slider, input): those own their focus, so pulling it onto the\n // container would move focus off the control the user just activated.\n this.container.addEventListener('click', (e) => {\n if (e.target.closest(INTERACTIVE_ELEMENTS)) return;\n // Remove focus from all other players\n WaveformPlayer.getAllInstances().forEach(player => {\n if (player !== this) {\n player.container.setAttribute('tabindex', '-1');\n }\n });\n // Make this one focusable\n this.container.setAttribute('tabindex', '0');\n this.container.focus();\n }, {signal: this._ac.signal});\n\n // Keyboard events. In external mode `this.audio` is null, so\n // seek/volume/mute keys are no-ops (the external controller\n // owns those). Space (togglePlay) still works because togglePlay\n // routes through the request-play/pause events.\n this.container.addEventListener('keydown', (e) => {\n if (document.activeElement !== this.container) return;\n\n const key = e.key;\n const hasAudio = !!this.audio;\n const currentTime = hasAudio ? this.audio.currentTime : 0;\n\n // Handle number keys 0-9 for seeking\n if (hasAudio && key >= '0' && key <= '9') {\n e.preventDefault();\n this.seekToPercent(parseInt(key) / 10);\n return;\n }\n\n // Handle other keys. Space always works (dispatches\n // request-play in external mode); audio-bound keys only\n // when we own the <audio> element.\n const actions = {\n ' ': () => this.togglePlay(),\n };\n if (hasAudio) {\n actions['ArrowLeft'] = () => this.seekTo(clamp(currentTime - 5, 0, this.audio.duration));\n actions['ArrowRight'] = () => this.seekTo(clamp(currentTime + 5, 0, this.audio.duration));\n actions['ArrowUp'] = () => this.setVolume(clamp(this.audio.volume + 0.1));\n actions['ArrowDown'] = () => this.setVolume(clamp(this.audio.volume - 0.1));\n actions['m'] = actions['M'] = () => this.audio.muted = !this.audio.muted;\n }\n\n if (actions[key]) {\n e.preventDefault();\n actions[key]();\n }\n }, {signal: this._ac.signal});\n }\n\n /**\n * Expose the waveform as an accessible, keyboard-operable slider.\n *\n * Adds role=\"slider\" + ARIA value attributes to the waveform surface,\n * makes it focusable in the tab order, and handles the standard slider\n * keys (arrows, Page Up/Down, Home/End) to seek. Works in both self and\n * external audio modes. Opt out with `accessibleSeek: false`.\n * @private\n */\n initSeekControl() {\n if (!this.options.accessibleSeek) return;\n\n this.seekEl = this.container.querySelector('.waveform-container');\n if (!this.seekEl) return;\n\n this.seekEl.setAttribute('role', 'slider');\n this.seekEl.setAttribute('tabindex', '0');\n this.seekEl.setAttribute('aria-valuemin', '0');\n this.applySeekLabel();\n this.updateSeekAccessibility();\n\n this.seekEl.addEventListener('keydown', (e) => {\n // Space toggles play/pause even while the waveform slider is focused \u2014\n // it's a media control, so play/pause should work here too, not only\n // when the player root is the active element (restores the pre-a11y\n // Space path reported in #10).\n if (e.key === ' ' || e.key === 'Spacebar') {\n e.preventDefault();\n e.stopPropagation();\n this.togglePlay();\n return;\n }\n\n const duration = this.getSeekDuration();\n if (!duration) return;\n\n const current = this.getSeekCurrentTime();\n let target;\n switch (e.key) {\n case 'ArrowLeft':\n case 'ArrowDown':\n target = current - SEEK_STEP_SECONDS;\n break;\n case 'ArrowRight':\n case 'ArrowUp':\n target = current + SEEK_STEP_SECONDS;\n break;\n case 'PageDown':\n target = current - SEEK_PAGE_SECONDS;\n break;\n case 'PageUp':\n target = current + SEEK_PAGE_SECONDS;\n break;\n case 'Home':\n target = 0;\n break;\n case 'End':\n target = duration;\n break;\n default:\n return;\n }\n\n // Prevent page scroll and stop the container-level keydown\n // handler from also seeking (it would double-fire / change\n // volume on the vertical arrows).\n e.preventDefault();\n e.stopPropagation();\n this.seekToSeconds(target);\n }, {signal: this._ac.signal});\n }\n\n /**\n * Total seekable duration in seconds, regardless of audio mode.\n * @returns {number}\n * @private\n */\n getSeekDuration() {\n if (this.options.audioMode === 'external') {\n return this._extDuration || 0;\n }\n return this.audio && Number.isFinite(this.audio.duration)\n ? this.audio.duration\n : 0;\n }\n\n /**\n * Current playback position in seconds, regardless of audio mode.\n * @returns {number}\n * @private\n */\n getSeekCurrentTime() {\n if (this.options.audioMode === 'external') {\n return this.progress * (this._extDuration || 0);\n }\n return this.audio && Number.isFinite(this.audio.currentTime)\n ? this.audio.currentTime\n : 0;\n }\n\n /**\n * Seek the slider to an absolute time, clamped to the track length.\n *\n * In self mode this defers to {@link WaveformPlayer#seekTo}. In external\n * mode it dispatches a cancelable `waveformplayer:request-seek` event with\n * the target percentage; if the controller doesn't `preventDefault()`, the\n * local progress/visual is updated optimistically. Either way the ARIA\n * slider values are refreshed.\n * @param {number} seconds - Target time in seconds.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n seekToSeconds(seconds) {\n const duration = this.getSeekDuration();\n if (!duration) return;\n\n const clamped = clamp(seconds, 0, duration);\n\n if (this.options.audioMode === 'external') {\n this._requestSeek(clamped / duration);\n this.updateSeekAccessibility();\n return;\n }\n\n // seekTo() calls updateProgress(), which refreshes the ARIA values.\n this.seekTo(clamped);\n }\n\n /**\n * Set the slider's accessible name from `seekLabel`, falling back to the\n * track title, then a generic 'Seek'. No-op if the slider isn't present.\n * @param {string} [title=this.options.title] - Track title to fall back to\n * when `seekLabel` is not set.\n * @private\n */\n applySeekLabel(title = this.options.title) {\n if (!this.seekEl) return;\n const label = this.options.seekLabel || title || 'Seek';\n this.seekEl.setAttribute('aria-label', label);\n }\n\n /**\n * Keep the slider's ARIA value attributes in sync with playback.\n * @private\n */\n updateSeekAccessibility() {\n if (!this.seekEl) return;\n\n const duration = this.getSeekDuration();\n const current = Math.min(this.getSeekCurrentTime(), duration);\n\n this.seekEl.setAttribute('aria-valuemax', String(Math.round(duration)));\n this.seekEl.setAttribute('aria-valuenow', String(Math.round(current)));\n this.seekEl.setAttribute(\n 'aria-valuetext',\n formatSeekValueText(\n this.options.seekValueText || '%1$s of %2$s',\n formatTime(current),\n formatTime(duration)\n )\n );\n }\n\n /**\n * Initialize Media Session API for system media controls\n * @private\n */\n initMediaSession() {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession) return;\n // Skip Media Session in external mode \u2014 the controller (e.g.\n // WaveformBar) owns audio playback and registers its own Media\n // Session handlers; ours would conflict with its.\n if (!this.audio) return;\n\n this._applyMediaMetadata();\n\n // Set up action handlers\n navigator.mediaSession.setActionHandler('play', () => this.play());\n navigator.mediaSession.setActionHandler('pause', () => this.pause());\n navigator.mediaSession.setActionHandler('seekbackward', () => {\n this.seekTo(clamp(this.audio.currentTime - 10, 0, this.audio.duration));\n });\n navigator.mediaSession.setActionHandler('seekforward', () => {\n this.seekTo(clamp(this.audio.currentTime + 10, 0, this.audio.duration));\n });\n navigator.mediaSession.setActionHandler('seekto', (details) => {\n if (details.seekTime !== null) {\n this.seekTo(details.seekTime);\n }\n });\n\n // Optional queue navigation \u2014 lights up the lock-screen skip-track\n // buttons. Registered only when a host (bar / playlist) supplies a\n // callback; a null handler hides the button.\n const onNext = this.options.onNextTrack, onPrev = this.options.onPreviousTrack;\n try { navigator.mediaSession.setActionHandler('nexttrack', typeof onNext === 'function' ? () => onNext(this) : null); } catch (e) { /* unsupported */ }\n try { navigator.mediaSession.setActionHandler('previoustrack', typeof onPrev === 'function' ? () => onPrev(this) : null); } catch (e) { /* unsupported */ }\n }\n\n /**\n * Publish the current track's Media Session metadata (lock-screen /\n * Now-Playing title, artist, album, artwork). Idempotent \u2014 safe to re-call.\n * @private\n */\n _applyMediaMetadata() {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession) return;\n navigator.mediaSession.metadata = new MediaMetadata({\n title: this.options.title || this.options.unknownTrackText,\n artist: this.options.artist || '',\n album: this.options.album || '',\n artwork: this.options.artwork ? [\n {src: this.options.artwork, sizes: '512x512', type: 'image/jpeg'}\n ] : []\n });\n }\n\n /**\n * Sync Media Session playback state + scrubber position on play/pause. iOS\n * ignores metadata set before the media is active, so we re-assert it on\n * play \u2014 that's what fixes the blank lock-screen card on mobile.\n * @param {'playing'|'paused'} state\n * @private\n */\n _updateMediaSession(state) {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession || !this.audio) return;\n try {\n // Claim the (global, singleton) Media Session for THIS player on play \u2014\n // (re)register our action handlers + metadata so the lock-screen controls\n // drive the player you're actually listening to, not the last one loaded.\n if (state === 'playing') this.initMediaSession();\n navigator.mediaSession.playbackState = state;\n const d = this.audio.duration;\n if (navigator.mediaSession.setPositionState && d && isFinite(d)) {\n navigator.mediaSession.setPositionState({\n duration: d,\n playbackRate: this.audio.playbackRate || 1,\n position: clamp(this.audio.currentTime, 0, d),\n });\n }\n } catch (e) { /* Media Session is best-effort */ }\n }\n\n // ============================================\n // Event Binding\n // ============================================\n\n /**\n * Bind the core interaction listeners: play-button click, the `<audio>`\n * media events (self mode only \u2014 external mode is fed state via\n * {@link WaveformPlayer#setPlayingState}/{@link WaveformPlayer#setProgress}),\n * canvas click-to-seek, and a debounced window-resize redraw.\n * @private\n */\n bindEvents() {\n // Play button (only if controls are shown). In external mode\n // togglePlay() dispatches the request-play/pause events so the\n // controller can decide what to do; the click still goes through\n // here.\n if (this.playBtn) {\n this.playBtn.addEventListener('click', () => this.togglePlay());\n }\n\n // Audio events \u2014 only when we own an <audio> element. External\n // mode receives state via setPlayingState() / setProgress() from\n // the controller, so we have nothing to listen to here.\n if (this.audio) {\n this.audio.addEventListener('loadstart', () => this.setLoading(true));\n this.audio.addEventListener('loadedmetadata', () => this.onMetadataLoaded());\n this.audio.addEventListener('canplay', () => this.setLoading(false));\n this.audio.addEventListener('play', () => this.onPlay());\n this.audio.addEventListener('pause', () => this.onPause());\n this.audio.addEventListener('ended', () => this.onEnded());\n this.audio.addEventListener('error', (e) => this.onError(e));\n }\n\n // Canvas interactions \u2014 click + drag to seek. pointerdown starts a\n // scrub (with pointer capture so moves keep flowing if the cursor\n // leaves the canvas), pointermove updates it while held, pointerup /\n // pointercancel end it. The click handler stays for keyboard/synthetic\n // compatibility. In external mode each seek dispatches a cancelable\n // `waveformplayer:request-seek` so the controller positions its audio.\n this.canvas.addEventListener('click', (e) => this.handleCanvasClick(e));\n this._dragging = false;\n this._seekHover = false;\n this._handleNear = false;\n\n // Hover state \u2014 reveals the handle + lifts the seekbar brightness. A\n // redraw is needed because the canvas has no CSS :hover of its own.\n this.canvas.addEventListener('pointerenter', () => {\n this._seekHover = true;\n this.drawWaveform();\n this._updateSeekHandle();\n });\n this.canvas.addEventListener('pointerleave', () => {\n this._seekHover = false;\n this._handleNear = false;\n if (!this._dragging) this._hideHoverTip();\n this.drawWaveform();\n this._updateSeekHandle();\n });\n\n // Drag = a VISUAL scrub: the playhead previews the cursor while the\n // audio keeps playing untouched (a per-move seek glitches the audio and\n // fights the progress loop). The real seek is committed once, on\n // release. updateProgress() bails while _dragging so playback can't drag\n // the handle off the cursor.\n this.canvas.addEventListener('pointerdown', (e) => {\n if (e.pointerType === 'mouse' && e.button !== 0) return; // primary / touch only\n this._dragging = true;\n try { this.canvas.setPointerCapture(e.pointerId); } catch (err) { /* unsupported */ }\n this._scrubTo(e.clientX);\n });\n this.canvas.addEventListener('pointermove', (e) => {\n if (this._dragging) {\n this._scrubTo(e.clientX);\n return;\n }\n // Hover (not dragging): grow the handle when the cursor is over it.\n const rect = this.canvas.getBoundingClientRect();\n if (rect.width) {\n this._handleNear = Math.abs((e.clientX - rect.left) - this.progress * rect.width) <= 10;\n this._updateSeekHandle();\n }\n });\n const endDrag = (e) => {\n if (!this._dragging) return;\n this._dragging = false;\n // The browser fires a click after pointerup on a tap/short drag \u2014\n // flag it so handleCanvasClick ignores it (a second seek from the\n // synthetic click's coordinate can snap the playhead, e.g. to 0).\n this._suppressClick = true;\n try { this.canvas.releasePointerCapture(e.pointerId); } catch (err) { /* ignore */ }\n this._seekFromPointer(e.clientX); // commit the seek now \u2014 audio jumps once\n // Hide the drag tooltip on release, unless hover-time keeps it (or\n // the seekbar handle is being hovered), to avoid a hide/show flicker.\n if (!this._seekHover && !this.options.showHoverTime) this._hideHoverTip();\n this._updateSeekHandle();\n };\n this.canvas.addEventListener('pointerup', endDrag);\n this.canvas.addEventListener('pointercancel', endDrag);\n\n // Hover-time tooltip over the waveform (when showHoverTime is on).\n this.setupHoverTime();\n // Draggable seek handle (DOM circle, shown on hover / drag).\n this.setupSeekHandle();\n\n // Window resize - store handler for cleanup\n this.resizeHandler = debounce(() => this.resizeCanvas(), 100);\n window.addEventListener('resize', this.resizeHandler);\n }\n\n /**\n * Observe the canvas's parent element for size changes and re-fit the\n * canvas on each one. No-op where `ResizeObserver` is unavailable.\n * @private\n */\n setupResizeObserver() {\n if ('ResizeObserver' in window) {\n this.resizeObserver = new ResizeObserver(() => {\n this.resizeCanvas();\n });\n\n if (this.canvas?.parentElement) {\n this.resizeObserver.observe(this.canvas.parentElement);\n }\n }\n }\n\n // ============================================\n // Audio Loading\n // ============================================\n\n /**\n * Load an audio source: set the title, fetch/generate the waveform peaks,\n * draw them, render markers, and initialise Media Session.\n *\n * In self mode the `<audio>` src is assigned and the method awaits\n * `loadedmetadata` before proceeding. In external mode there is no audio\n * element, so the src/metadata step is skipped and only the visualization\n * is built (duration/time come from the controller via\n * {@link WaveformPlayer#setProgress}). Peaks come from the `waveform`\n * option when provided, otherwise they are decoded from the audio; a\n * decode failure falls back to a placeholder waveform. The `onLoad`\n * callback fires on success.\n * @param {string} url - Audio URL.\n * @returns {Promise<void>} Resolves once loading settles (errors are caught\n * internally and surfaced through {@link WaveformPlayer#onError}).\n */\n async load(url) {\n try {\n this.setLoading(true);\n this.progress = 0;\n this.hasError = false;\n this.container.classList.remove('waveform-is-placeholder');\n\n // In external mode we don't own an <audio> element \u2014 skip\n // src assignment + metadata-wait, but still generate the\n // waveform peaks so the canvas can render the visualization.\n // Duration / current time come from the external controller\n // via setProgress().\n if (this.audio) {\n // Set audio source\n this.audio.src = url;\n\n // Wait for metadata to load\n await new Promise((resolve, reject) => {\n const metadataHandler = () => {\n this.audio.removeEventListener('loadedmetadata', metadataHandler);\n this.audio.removeEventListener('error', errorHandler);\n resolve();\n };\n const errorHandler = (e) => {\n this.audio.removeEventListener('loadedmetadata', metadataHandler);\n this.audio.removeEventListener('error', errorHandler);\n reject(e);\n };\n this.audio.addEventListener('loadedmetadata', metadataHandler);\n this.audio.addEventListener('error', errorHandler);\n });\n }\n\n // Set title\n const title = this.options.title || extractTitleFromUrl(url);\n if (this.titleEl) {\n this.titleEl.textContent = title;\n }\n // Keep the seek slider's accessible name in sync with the track.\n this.applySeekLabel(title);\n\n // Load or generate waveform\n if (this.options.waveform) {\n this.setWaveformData(this.options.waveform);\n } else {\n // Generate waveform\n try {\n const result = await generateWaveform(url, this.options.samples, this.options.showBPM);\n this.waveformData = result.peaks;\n\n // Store BPM if detected\n if (result.bpm) {\n this.detectedBPM = result.bpm;\n this.updateBPMDisplay();\n }\n } catch (error) {\n console.warn('[WaveformPlayer] Using placeholder waveform:', error);\n this.waveformData = generatePlaceholderWaveform(this.options.samples);\n this.container.classList.add('waveform-is-placeholder');\n }\n }\n\n this.drawWaveform();\n this.renderMarkers();\n\n // Fire callback\n if (this.options.onLoad) {\n this.options.onLoad(this);\n }\n } catch (error) {\n // onError() is the single funnel for surfacing + logging errors.\n this.onError(error);\n } finally {\n this.setLoading(false);\n }\n }\n\n /**\n * Swap the player to a new track at runtime.\n *\n * Pauses any current playback, fully resets the audio element (self mode),\n * clears error/marker/progress state, merges the new metadata into\n * `this.options`, updates the artist/artwork DOM, then calls\n * {@link WaveformPlayer#load}. Auto-plays the new track unless\n * `options.autoplay === false`.\n * @param {string} url - Audio URL.\n * @param {string|null} [title=null] - Track title; keeps the existing\n * title when null.\n * @param {string|null} [artist=null] - Track artist; pass `''` to hide\n * the artist row, or null to keep the existing one.\n * @param {Object} [options={}] - Additional options to merge (e.g.\n * `preload`, `artwork`, `artworkAlt`, `markers`, `autoplay`).\n * @returns {Promise<void>}\n */\n async loadTrack(url, title = null, artist = null, options = {}) {\n const hasArtworkOption = Object.prototype.hasOwnProperty.call(options, 'artwork');\n const hasArtworkAltOption = Object.prototype.hasOwnProperty.call(options, 'artworkAlt');\n\n // Stop current playback and clear state\n if (this.isPlaying) {\n this.pause();\n }\n\n // Reset audio element completely (only when we own one)\n if (this.audio) {\n this.audio.src = '';\n this.audio.load();\n }\n\n // Clear any errors\n this.hasError = false;\n if (this.errorEl) {\n this.errorEl.style.display = 'none';\n }\n if (this.canvas) {\n this.canvas.style.opacity = '1';\n }\n if (this.playBtn) {\n this.playBtn.disabled = false;\n }\n\n // Reset state\n this.progress = 0;\n this.waveformData = [];\n\n // Update options (including preload if specified)\n this.options = mergeOptions(this.options, {\n url,\n title: title === null ? this.options.title : title,\n artist: artist === null ? this.options.artist : artist,\n ...options\n });\n if (hasArtworkOption) {\n this.options.artwork = options.artwork || null;\n }\n if (hasArtworkAltOption) {\n this.options.artworkAlt = options.artworkAlt || '';\n } else if (hasArtworkOption) {\n this.options.artworkAlt = this.options.artwork ? DEFAULT_OPTIONS.artworkAlt : '';\n }\n\n // Apply preload setting if it was changed\n if (options.preload && this.audio) {\n this.audio.preload = options.preload;\n }\n\n // Apply crossOrigin if it was changed for this track\n if (options.crossOrigin && this.audio) {\n this.audio.crossOrigin = options.crossOrigin;\n }\n\n // Update artist only when explicitly provided. A null artist keeps the\n // existing artist, while an empty string removes it.\n if (artist !== null) {\n this.syncArtist(artist);\n }\n\n // Update artwork when explicitly provided. The caller can pass an empty\n // value to remove existing artwork from the in-place player.\n if (hasArtworkOption || hasArtworkAltOption) {\n this.syncArtwork(\n hasArtworkOption ? options.artwork : this.options.artwork,\n hasArtworkAltOption ? options.artworkAlt : this.options.artworkAlt\n );\n }\n\n // Clear or update markers\n this.options.markers = options.markers || [];\n\n // Reset the waveform to the NEW track's peaks, or null to regenerate\n // from the URL. mergeOptions() above keeps the previous track's\n // this.options.waveform when the caller passes none, and load() does\n // `if (this.options.waveform) setWaveformData(...)` \u2014 so without this\n // reset a track loaded without peaks would redraw the PREVIOUS track's\n // waveform (audio changes, visualization doesn't).\n this.options.waveform = options.waveform || null;\n\n // Load the new track\n await this.load(url);\n\n // Auto-play the new track unless the caller opted out \u2014 lets a\n // controller load/restore/enqueue without forcing playback.\n if (options.autoplay !== false) {\n this.play()?.catch(() => {});\n }\n }\n\n // ============================================\n // Visualization\n // ============================================\n\n /**\n * Normalise externally-supplied waveform data into `this.waveformData` and\n * redraw.\n *\n * Accepts several shapes: a `.json` URL (fetched async; peaks and any\n * embedded `markers` are applied on resolve), a JSON-encoded array string,\n * a comma-separated number string, or a plain number array. Malformed\n * input degrades to an empty array rather than throwing.\n * @param {string|number[]} data - Peaks as an array, a JSON/CSV string, or\n * a URL to a `.json` peaks file.\n * @private\n */\n setWaveformData(data) {\n // URL to JSON file \u2014 fetch peaks and maybe markers\n if (typeof data === 'string' && data.trim().endsWith('.json')) {\n fetch(data.trim())\n .then(r => r.json())\n .then(json => {\n this.waveformData = Array.isArray(json) ? json : (json.peaks || []);\n if (json.markers && !this.options.markers?.length) {\n this.options.markers = json.markers;\n this.renderMarkers();\n }\n this.drawWaveform();\n })\n .catch(() => {});\n return;\n }\n\n if (typeof data === 'string') {\n try {\n const parsed = JSON.parse(data);\n this.waveformData = Array.isArray(parsed) ? parsed : [];\n } catch {\n this.waveformData = data.split(',').map(Number);\n }\n } else {\n this.waveformData = Array.isArray(data) ? data : [];\n }\n this.drawWaveform();\n }\n\n /**\n * Render the current waveform + progress to the canvas via the shared\n * {@link draw} routine, passing the resolved style and colours. No-op\n * before the context exists or while there is no peak data.\n * @private\n */\n drawWaveform() {\n if (!this.ctx || this.waveformData.length === 0) return;\n\n draw(this.ctx, this.canvas, this.waveformData, this.progress, {\n ...this.options,\n waveformStyle: this.options.waveformStyle || 'bars',\n color: this.options.waveformColor,\n progressColor: this.options.progressColor,\n seekActive: this._seekHover || this._dragging\n });\n }\n\n /**\n * Re-fit the canvas backing store to its parent's width and the configured\n * height, scaled by the device pixel ratio for crisp rendering, then\n * redraw. Guards against running after destruction.\n * @private\n */\n resizeCanvas() {\n // Guard against calls after destruction\n if (!this.canvas || this.isDestroying) {\n return;\n }\n\n const dpr = window.devicePixelRatio || 1;\n const rect = this.canvas.parentElement.getBoundingClientRect();\n\n this.canvas.width = rect.width * dpr;\n this.canvas.height = this.options.height * dpr;\n this.canvas.parentElement.style.height = this.options.height + 'px';\n\n this.drawWaveform();\n }\n\n /**\n * Render the configured cue markers as positioned, clickable buttons over\n * the waveform.\n *\n * Clears any existing markers first, then bails out unless `showMarkers` is\n * on, markers exist, and a duration is known (via the mode-agnostic\n * {@link WaveformPlayer#getSeekDuration}). Each marker is placed by its\n * time-as-percentage, carries a tooltip and ARIA label, and seeks on click\n * (also starting playback when `playOnSeek` is set and currently paused).\n * Markers past the track duration are skipped with a warning.\n * @private\n */\n renderMarkers() {\n if (!this.markersContainer) return;\n\n // Always clear existing markers first\n this.markersContainer.innerHTML = '';\n // Re-sync the active marker on the next progress tick (the DOM was wiped).\n this._activeMarkerIndex = -1;\n clearTimeout(this._markerLabelTimer);\n\n if (!this.options.showMarkers || !this.options.markers?.length) return;\n\n // Duration may come from the <audio> (self mode) or the external\n // controller (external mode) \u2014 use the mode-agnostic accessor.\n const duration = this.getSeekDuration();\n if (!duration) {\n return;\n }\n\n // Add each marker\n this.options.markers.forEach((marker, index) => {\n // Skip markers that are beyond the audio duration\n if (marker.time > duration) {\n console.warn(`[WaveformPlayer] Marker \"${marker.label}\" at ${marker.time}s exceeds audio duration of ${duration}s`);\n return;\n }\n\n const position = (marker.time / duration) * 100;\n\n const markerEl = document.createElement('button');\n markerEl.className = 'waveform-marker';\n markerEl.style.left = `${position}%`;\n markerEl.style.backgroundColor = marker.color || 'rgba(255, 255, 255, 0.5)';\n markerEl.setAttribute('aria-label', marker.label);\n markerEl.setAttribute('data-time', marker.time);\n\n // Tooltip\n const tooltip = document.createElement('span');\n tooltip.className = 'waveform-marker-tooltip';\n tooltip.textContent = marker.label;\n markerEl.appendChild(tooltip);\n\n // Click to seek\n markerEl.addEventListener('click', (e) => {\n e.stopPropagation();\n this.seekTo(marker.time);\n if (this.options.playOnSeek && !this.isPlaying) {\n this.play();\n }\n });\n\n this.markersContainer.appendChild(markerEl);\n });\n }\n\n /**\n * Highlight the marker at `index` (toggling an `active` class) and clear\n * the rest. Pass `null` to clear all. Lets an external controller (e.g. a\n * DJ bar) reflect the current section without reaching into the player's\n * private marker DOM.\n * @param {number|null} index - Marker index to activate, or `null` to clear.\n */\n setActiveMarker(index) {\n if (!this.markersContainer) return;\n const markers = this.markersContainer.querySelectorAll('.waveform-marker');\n markers.forEach((el, i) => el.classList.toggle('active', i === index));\n }\n\n /**\n * Highlight the marker the playhead has most recently passed \u2014 the rendered\n * marker with the greatest `time` \u2264 the current time \u2014 and reveal its label.\n * Driven from the progress loop; a no-op when there are no markers. Reuses\n * {@link WaveformPlayer#setActiveMarker}, and only re-applies when the active\n * marker actually changes.\n * @private\n */\n updateActiveMarker() {\n if (!this.markersContainer) return;\n const els = this.markersContainer.querySelectorAll('.waveform-marker');\n if (!els.length) return;\n const dur = this.getSeekDuration();\n const t = dur ? this.progress * dur : 0;\n let active = -1;\n let best = -Infinity;\n els.forEach((el, i) => {\n const mt = parseFloat(el.getAttribute('data-time'));\n if (Number.isFinite(mt) && mt <= t + 0.05 && mt > best) {\n best = mt;\n active = i;\n }\n });\n if (active !== this._activeMarkerIndex) {\n this._activeMarkerIndex = active;\n this.setActiveMarker(active);\n // Flash the newly-active marker's label, then let it fade \u2014 the\n // highlight stays. Override via `.waveform-marker.show-label` CSS.\n clearTimeout(this._markerLabelTimer);\n els.forEach((el, i) => el.classList.toggle('show-label', i === active));\n if (active >= 0) {\n this._markerLabelTimer = setTimeout(() => {\n this.markersContainer\n ?.querySelectorAll('.waveform-marker')\n .forEach((el) => el.classList.remove('show-label'));\n }, 2500);\n }\n }\n }\n\n /**\n * Build the time tooltip that follows the pointer over the waveform. The\n * element is always created so **drag-scrub** can show the time you're\n * dragging to; the *hover* reveal is gated on `showHoverTime`. Works in both\n * self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).\n * @private\n */\n setupHoverTime() {\n if (!this.seekEl) return;\n const tip = document.createElement('div');\n tip.className = 'waveform-hover-time';\n tip.setAttribute('aria-hidden', 'true');\n this.seekEl.appendChild(tip);\n this.hoverTimeEl = tip;\n\n // Without showHoverTime the tip is drag-only \u2014 no hover listeners.\n if (!this.options.showHoverTime) return;\n\n // Hover updates the tip; the drag handlers also call _updateHoverTip so\n // the time follows the playhead while scrubbing (Spotify-style). The\n // guard avoids a double-update when a drag is in progress (the canvas\n // pointermove already drives it then).\n this.seekEl.addEventListener('pointermove', (e) => {\n if (!this._dragging) this._updateHoverTip(e.clientX);\n });\n this.seekEl.addEventListener('pointerleave', () => {\n if (!this._dragging) this._hideHoverTip();\n });\n }\n\n /**\n * Position + fill the hover-time tooltip for a given client-X. Shared by\n * hover and drag-scrub. No-op when the tooltip isn't enabled\n * (`showHoverTime` off).\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n */\n _updateHoverTip(clientX) {\n const tip = this.hoverTimeEl;\n if (!tip) return;\n const dur = this.getSeekDuration();\n if (!dur) { tip.style.opacity = '0'; return; }\n const rect = this.canvas.getBoundingClientRect();\n const pct = clamp((clientX - rect.left) / rect.width);\n tip.textContent = formatTime(pct * dur);\n tip.style.left = (pct * 100) + '%';\n tip.style.opacity = '1';\n }\n\n /** Hide the hover-time tooltip. @private */\n _hideHoverTip() {\n if (this.hoverTimeEl) this.hoverTimeEl.style.opacity = '0';\n }\n\n /**\n * Visually preview a seek during a drag \u2014 moves the playhead + handle +\n * tooltip to the cursor WITHOUT touching the audio (playback continues; the\n * real seek is committed on release).\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n */\n _scrubTo(clientX) {\n const rect = this.canvas.getBoundingClientRect();\n if (!rect.width) return;\n this.progress = clamp((clientX - rect.left) / rect.width);\n this.drawWaveform();\n this._updateSeekHandle();\n // Live time readout while scrubbing (Spotify-style): the audio keeps\n // playing at its old position; the display shows where release lands.\n const dur = this.getSeekDuration();\n if (dur && this.currentTimeEl) {\n this.currentTimeEl.textContent = formatTime(this.progress * dur);\n // The current-time already shows the target \u2014 don't also pop the\n // hover tooltip with the same value (that duplicate is the bug).\n this._hideHoverTip();\n } else {\n // No current-time readout to fall back on \u2014 keep the tooltip as the\n // only scrub feedback.\n this._updateHoverTip(clientX);\n }\n }\n\n /**\n * Create the draggable seek handle \u2014 a DOM circle over the playhead, shown\n * only on hover / drag (Spotify-style). Pointer-events are off so it never\n * blocks the canvas scrub; size + visibility are CSS.\n * @private\n */\n setupSeekHandle() {\n // Only the seekbar style \u2014 on a real waveform the fill-edge already is\n // the playhead, so a floating dot is just noise.\n if (!this.options.seekHandle || this.options.waveformStyle !== 'seekbar' || !this.seekEl) return;\n const h = document.createElement('div');\n h.className = 'waveform-seek-handle';\n h.setAttribute('aria-hidden', 'true');\n this.seekEl.appendChild(h);\n this.seekHandleEl = h;\n }\n\n /**\n * Position the seek handle at the current progress and toggle its\n * visibility (hover / drag) + expanded (drag / direct-hover) classes.\n * @private\n */\n _updateSeekHandle() {\n const h = this.seekHandleEl;\n if (!h) return;\n h.style.left = (this.progress * 100) + '%';\n h.classList.toggle('is-visible', this._seekHover || this._dragging);\n h.classList.toggle('is-active', this._dragging || this._handleNear);\n }\n\n // ============================================\n // Event Handlers\n // ============================================\n\n /**\n * Seek to the clicked horizontal position on the waveform canvas.\n *\n * Converts the click X into a 0..1 percentage. In external mode it\n * dispatches a cancelable `waveformplayer:request-seek` event (updating the\n * local visual optimistically unless the controller vetoes it); in self\n * mode it seeks the owned `<audio>` via\n * {@link WaveformPlayer#seekToPercent}.\n * @param {MouseEvent} event - The canvas click event.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n handleCanvasClick(event) {\n // Ignore the synthetic click that trails a pointer drag (see endDrag).\n if (this._suppressClick) { this._suppressClick = false; return; }\n this._seekFromPointer(event.clientX);\n }\n\n /**\n * Seek to a horizontal client-X position over the canvas \u2014 shared by\n * click and drag. In external mode the player has no audio of its own,\n * so it dispatches a cancelable `waveformplayer:request-seek` with the\n * target percentage (updating the local visual optimistically); in self\n * mode it seeks the owned `<audio>` via\n * {@link WaveformPlayer#seekToPercent}.\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n _seekFromPointer(clientX) {\n const rect = this.canvas.getBoundingClientRect();\n if (!rect.width) return;\n const targetPercent = clamp((clientX - rect.left) / rect.width);\n\n if (this.options.audioMode === 'external') {\n this._requestSeek(targetPercent);\n return;\n }\n\n if (!this.audio || !this.audio.duration) return;\n this.seekToPercent(targetPercent);\n }\n\n /**\n * Toggle the loading state: show/hide the spinner overlay and set\n * `aria-busy` on the accessible seek slider so assistive tech knows the\n * player is fetching/decoding.\n * @param {boolean} loading - True while audio is loading.\n * @private\n */\n setLoading(loading) {\n this.isLoading = loading;\n if (this.loadingEl) {\n this.loadingEl.style.display = loading ? 'block' : 'none';\n }\n // Let assistive tech know the player is busy fetching/decoding.\n if (this.seekEl) {\n this.seekEl.setAttribute('aria-busy', loading ? 'true' : 'false');\n }\n }\n\n /**\n * `loadedmetadata` handler (self mode): write the total-time display, now\n * that duration is known re-render markers, and publish duration to the\n * accessible seek slider. No-op during destruction.\n * @private\n */\n onMetadataLoaded() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n if (this.totalTimeEl) {\n this.totalTimeEl.textContent = formatTime(this.audio.duration);\n }\n // Re-render markers when duration is known\n this.renderMarkers();\n // Duration is now known \u2014 publish it to the accessible slider.\n this.updateSeekAccessibility();\n }\n\n /**\n * Reflect play/pause state on the transport button: toggle the `playing`\n * class and swap the play/pause icon visibility. The single source of\n * truth shared by `onPlay`, `onPause`, and the external-mode\n * `setPlayingState` pump so they can't drift. No-op without a button.\n * @param {boolean} isPlaying - Whether playback is active.\n * @private\n */\n setPlayButtonState(isPlaying) {\n if (!this.playBtn) return;\n this.playBtn.classList.toggle('playing', isPlaying);\n const playIcon = this.playBtn.querySelector('.waveform-icon-play');\n const pauseIcon = this.playBtn.querySelector('.waveform-icon-pause');\n if (playIcon) playIcon.style.display = isPlaying ? 'none' : 'flex';\n if (pauseIcon) pauseIcon.style.display = isPlaying ? 'flex' : 'none';\n }\n\n /**\n * `play` handler (self mode): set the playing flag, swap the button to its\n * pause icon, start the smooth progress loop, dispatch\n * `waveformplayer:play`, and fire the `onPlay` callback. No-op during\n * destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:play\n */\n onPlay() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n this.isPlaying = true;\n\n this.setPlayButtonState(true);\n\n this.startSmoothUpdate();\n\n // iOS drops Media Session metadata set before playback begins \u2014 re-assert\n // metadata + playback state now that we're actually playing.\n this._updateMediaSession('playing');\n\n // Dispatch play event\n this._emit('waveformplayer:play', {player: this, url: this.options.url});\n\n if (this.options.onPlay) {\n this.options.onPlay(this);\n }\n }\n\n /**\n * `pause` handler (self mode): clear the playing flag, swap the button back\n * to its play icon, stop the smooth progress loop, dispatch\n * `waveformplayer:pause`, and fire the `onPause` callback. No-op during\n * destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:pause\n */\n onPause() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n this.isPlaying = false;\n\n this.setPlayButtonState(false);\n\n this.stopSmoothUpdate();\n\n this._updateMediaSession('paused');\n\n // Dispatch pause event\n this._emit('waveformplayer:pause', {player: this, url: this.options.url});\n\n if (this.options.onPause) {\n this.options.onPause(this);\n }\n }\n\n /**\n * `ended` handler (self mode): reset progress and `currentTime` to the\n * start, redraw, reset the time display, dispatch `waveformplayer:ended`\n * (carrying the final time), run {@link WaveformPlayer#onPause}, and fire\n * the `onEnd` callback. No-op during destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:ended\n */\n onEnded() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n const duration = this.audio.duration;\n\n this.progress = 0;\n this.audio.currentTime = 0;\n this.drawWaveform();\n\n // Reset time display\n if (this.currentTimeEl) {\n this.currentTimeEl.textContent = '0:00';\n }\n\n // Dispatch ended event \u2014 carries the final time so listeners (e.g.\n // analytics) don't have to reach into player.audio.\n this._emit('waveformplayer:ended', {player: this, url: this.options.url, currentTime: duration, duration});\n\n this.onPause();\n\n if (this.options.onEnd) {\n this.options.onEnd(this);\n }\n }\n\n /**\n * `error` handler: set the error flag, hide the spinner, reveal the error\n * overlay, dim the canvas, disable the play button, and fire the `onError`\n * callback. No-op during destruction.\n * @param {Event|Error} error - The audio error event, or an Error thrown\n * during loading.\n * @private\n */\n onError(error) {\n // Ignore errors during destruction\n if (this.isDestroying) return;\n\n console.error('[WaveformPlayer] Audio error:', error);\n this.hasError = true;\n this.setLoading(false);\n\n if (this.errorEl) {\n this.errorEl.style.display = 'flex';\n }\n\n if (this.canvas) {\n this.canvas.style.opacity = '0.2';\n }\n\n if (this.playBtn) {\n this.playBtn.disabled = true;\n }\n\n if (this.options.onError) {\n this.options.onError(error, this);\n }\n }\n\n // ============================================\n // Progress Updates\n // ============================================\n\n /**\n * Start the `requestAnimationFrame` loop that drives smooth progress\n * updates while playing (self mode only \u2014 external mode is redrawn by\n * controller {@link WaveformPlayer#setProgress} pushes). Cancels any\n * existing loop first so it's safe to call repeatedly.\n * @private\n */\n startSmoothUpdate() {\n this.stopSmoothUpdate();\n\n const update = () => {\n // In external mode the canvas redraws are driven by\n // setProgress() pushes from the controller \u2014 no internal\n // RAF needed. Self-mode keeps the smooth-update loop.\n if (this.isPlaying && this.audio && this.audio.duration) {\n this.updateProgress();\n this.updateTimer = requestAnimationFrame(update);\n }\n };\n\n this.updateTimer = requestAnimationFrame(update);\n }\n\n /**\n * Cancel the smooth-update animation frame, if one is scheduled.\n * @private\n */\n stopSmoothUpdate() {\n if (this.updateTimer) {\n cancelAnimationFrame(this.updateTimer);\n this.updateTimer = null;\n }\n }\n\n /**\n * Recompute progress from the owned `<audio>` clock and reflect it\n * everywhere (self mode only \u2014 external mode uses\n * {@link WaveformPlayer#setProgress}).\n *\n * Redraws the canvas when progress moves meaningfully, updates the\n * current-time display, dispatches `waveformplayer:timeupdate`, fires the\n * `onTimeUpdate` callback, and refreshes the accessible slider values.\n * @private\n * @fires WaveformPlayer#waveformplayer:timeupdate\n */\n updateProgress() {\n // Self-mode only \u2014 external mode receives progress via\n // setProgress() from the controller and never calls this.\n if (!this.audio || !this.audio.duration) return;\n // While dragging, the playhead previews the cursor \u2014 don't let playback\n // move it (the audio keeps playing; the seek commits on release).\n if (this._dragging) return;\n\n const newProgress = this.audio.currentTime / this.audio.duration;\n\n if (Math.abs(newProgress - this.progress) > 0.001) {\n this.progress = newProgress;\n this.drawWaveform();\n this._updateSeekHandle();\n }\n\n if (this.currentTimeEl) {\n this.currentTimeEl.textContent = formatTime(this.audio.currentTime);\n }\n\n // Dispatch timeupdate event\n this._emit('waveformplayer:timeupdate', {\n player: this,\n currentTime: this.audio.currentTime,\n duration: this.audio.duration,\n progress: this.progress,\n url: this.options.url\n });\n\n if (this.options.onTimeUpdate) {\n this.options.onTimeUpdate(this.audio.currentTime, this.audio.duration, this);\n }\n\n this.updateActiveMarker();\n this.updateSeekAccessibility();\n }\n\n // ============================================\n // UI Updates\n // ============================================\n\n /**\n * Show the detected BPM in the badge, once a value has been detected.\n * @private\n */\n updateBPMDisplay() {\n // A caller-supplied `bpm` wins over auto-detection \u2014 useful when peaks\n // are pre-generated (so the audio is never decoded) but the BPM is known\n // anyway, e.g. sample-pack previews where the tempo is in the metadata.\n const bpm = this.options.bpm || this.detectedBPM;\n if (this.bpmEl && this.bpmValueEl && bpm) {\n this.bpmValueEl.textContent = Math.round(bpm);\n this.bpmEl.style.display = 'inline-flex';\n }\n }\n\n /**\n * Re-detect the page theme and re-apply auto colours + redraw, so an\n * auto-themed player adapts to a runtime light/dark switch (not just the\n * value present on load). No-op when the player uses an explicit\n * `colorPreset` or hand-set colours. Driven by the shared theme watcher.\n * @public\n */\n refreshTheme() {\n if (!this._autoTheme) return;\n this._scheme = detectColorScheme();\n const preset = getColorPreset(this.options.colorPreset);\n for (const key of (this._presetKeys || [])) {\n if (key in preset) this.options[key] = preset[key];\n }\n this._applyThemeColors();\n }\n\n /**\n * Sync the theme class (CSS drives the DOM chrome colours via `--wfp-*`\n * variables) and redraw the canvas with the resolved waveform colours.\n * @private\n */\n _applyThemeColors() {\n this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');\n if (this.canvas) this.drawWaveform();\n }\n\n /**\n * Lazily install ONE shared watcher that re-detects the theme for every\n * auto-themed instance when the document theme changes \u2014 a class/attribute\n * flip on `<html>`/`<body>` (Tailwind `dark`, `data-theme`,\n * `data-color-scheme`) or an OS `prefers-color-scheme` change. Event-driven\n * (MutationObserver + matchMedia), never a timer.\n * @private\n */\n static _watchTheme() {\n if (WaveformPlayer._themeWatch || typeof document === 'undefined') return;\n const refresh = () => requestAnimationFrame(() => {\n WaveformPlayer.instances.forEach((p) => {\n try { p.refreshTheme(); } catch (e) { /* ignore */ }\n });\n });\n const opts = {attributes: true, attributeFilter: ['class', 'data-theme', 'data-color-scheme', 'style']};\n const obs = new MutationObserver(refresh);\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n let mq = null;\n try {\n mq = window.matchMedia('(prefers-color-scheme: dark)');\n mq.addEventListener('change', refresh);\n } catch (e) { /* no matchMedia */ }\n WaveformPlayer._themeWatch = {obs, mq, refresh};\n }\n\n /**\n * Sync the speed control's label and the menu's active-option highlight to\n * the audio element's current `playbackRate`. No-op in external mode (no\n * owned `<audio>`), which also avoids reading `playbackRate` before the\n * element exists.\n * @private\n */\n updateSpeedUI() {\n // External mode owns no <audio>; nothing to reflect (and reading\n // this.audio.playbackRate here would throw during construction).\n if (!this.audio) return;\n\n const speedValue = this.container.querySelector('.speed-value');\n if (speedValue) {\n const rate = this.audio.playbackRate;\n speedValue.textContent = rate === 1 ? '1x' : `${rate}x`;\n }\n\n // Update active state in menu\n this.container.querySelectorAll('.speed-option').forEach(btn => {\n const on = parseFloat(btn.dataset.rate) === this.audio.playbackRate;\n btn.classList.toggle('active', on);\n btn.setAttribute('aria-checked', on ? 'true' : 'false');\n });\n }\n\n // ============================================\n // Public API\n // ============================================\n\n /**\n * Play audio.\n *\n * In `audioMode: 'self'` (default): calls the underlying <audio>\n * element's play(). Returns the promise from HTMLMediaElement.play().\n *\n * In `audioMode: 'external'`: dispatches a cancelable\n * `waveformplayer:request-play` event with the track metadata and\n * does NOT touch any audio element. Returns `undefined`. An external\n * controller (e.g. WaveformBar) listens for this event and starts\n * playback on its own audio source, then pushes state back via\n * setPlayingState() / setProgress(). Calling preventDefault() on\n * the event lets the controller veto the play (state is unchanged).\n *\n * When `singlePlay` is enabled, any other currently-playing instance is\n * paused first.\n *\n * @return {Promise|undefined} The promise from `HTMLMediaElement.play()` in\n * self mode; `undefined` in external mode.\n * @fires WaveformPlayer#waveformplayer:request-play\n */\n play() {\n if (this.options.singlePlay && WaveformPlayer.currentlyPlaying &&\n WaveformPlayer.currentlyPlaying !== this) {\n WaveformPlayer.currentlyPlaying.pause();\n }\n\n if (this.options.audioMode === 'external') {\n const evt = this._emit('waveformplayer:request-play', this._buildTrackDetail(), true);\n // If the controller cancels (preventDefault), don't claim\n // \"currentlyPlaying\" \u2014 the controller didn't accept the play.\n if (!evt.defaultPrevented) {\n WaveformPlayer.currentlyPlaying = this;\n }\n return undefined;\n }\n\n WaveformPlayer.currentlyPlaying = this;\n return this.audio.play();\n }\n\n /**\n * Pause audio.\n *\n * In `audioMode: 'external'`, dispatches `waveformplayer:request-pause`\n * (cancelable) and does NOT touch any audio element. See play().\n *\n * @fires WaveformPlayer#waveformplayer:request-pause\n */\n pause() {\n if (WaveformPlayer.currentlyPlaying === this) {\n WaveformPlayer.currentlyPlaying = null;\n }\n if (this.options.audioMode === 'external') {\n this._emit('waveformplayer:request-pause', this._buildTrackDetail(), true);\n return;\n }\n this.audio.pause();\n }\n\n /**\n * Build the track detail object dispatched by request-play /\n * request-pause events in external audio mode. Mirrors the shape\n * WaveformBar.play() accepts so a controller can forward it\n * directly: `WaveformBar.play(event.detail)`.\n *\n * @private\n * @return {{url:string,title:?string,artist:?string,artwork:?string,player:WaveformPlayer}}\n */\n _buildTrackDetail() {\n return {\n url: this.options.url,\n title: this.options.title,\n artist: this.options.artist,\n artwork: this.options.artwork,\n markers: this.options.markers,\n waveform: this.options.waveform,\n id: this.id,\n player: this\n };\n }\n\n /**\n * External-mode state pump: flip the play/pause visual state without\n * touching audio. Mirrors what onPlay()/onPause() do but skips the\n * audio-element interactions. Safe to call repeatedly \u2014 idempotent.\n *\n * Only dispatches `waveformplayer:play`/`waveformplayer:pause` (and runs\n * the matching callback) on an actual transition, starting/stopping the\n * smooth-update loop accordingly.\n *\n * @param {boolean} playing - True to enter the playing state, false to\n * enter the paused state.\n * @fires WaveformPlayer#waveformplayer:play\n * @fires WaveformPlayer#waveformplayer:pause\n */\n setPlayingState(playing) {\n const wasPlaying = this.isPlaying;\n this.isPlaying = !!playing;\n this.setPlayButtonState(this.isPlaying);\n if (this.isPlaying && !wasPlaying) {\n this.startSmoothUpdate?.();\n this._emit('waveformplayer:play', {player: this, url: this.options.url});\n if (this.options.onPlay) this.options.onPlay(this);\n } else if (!this.isPlaying && wasPlaying) {\n this.stopSmoothUpdate?.();\n this._emit('waveformplayer:pause', {player: this, url: this.options.url});\n if (this.options.onPause) this.options.onPause(this);\n }\n }\n\n /**\n * External-mode state pump: update the visualization's progress\n * from an external clock (e.g. WaveformBar's audio element's\n * timeupdate). Drives the canvas redraw + the time displays.\n *\n * Redraws the canvas, updates the current/total time displays, stores the\n * external duration for the accessible slider, dispatches\n * `waveformplayer:timeupdate`, runs `onTimeUpdate`, and synthesizes a\n * one-shot `waveformplayer:ended` (with `onEnd`) when progress reaches the\n * end. No-op for a non-positive duration.\n *\n * @param {number} currentTime - Current playback position in seconds.\n * @param {number} duration - Total track duration in seconds.\n * @fires WaveformPlayer#waveformplayer:timeupdate\n * @fires WaveformPlayer#waveformplayer:ended\n */\n setProgress(currentTime, duration) {\n if (!duration || duration <= 0) return;\n this.progress = clamp(currentTime / duration);\n // Mirror the existing display update code so callers don't have\n // to know which DOM elements live where.\n if (this.currentTimeEl) this.currentTimeEl.textContent = formatTime(currentTime);\n // Publish the duration unconditionally \u2014 the accessible seek slider\n // and keyboard seeking read getSeekDuration()/_extDuration even when\n // there's no time display to update.\n this._extDuration = duration;\n if (this.totalTimeEl && (!this.totalTimeEl.dataset._extSet || this.totalTimeEl.dataset._extDur !== String(duration))) {\n this.totalTimeEl.textContent = formatTime(duration);\n this.totalTimeEl.dataset._extSet = '1';\n this.totalTimeEl.dataset._extDur = String(duration);\n }\n this.drawWaveform?.();\n this.updateActiveMarker();\n this._emit('waveformplayer:timeupdate', {player: this, currentTime, duration, progress: this.progress, url: this.options.url});\n // Same (currentTime, duration, player) signature as self mode \u2014 the\n // arg order used to be swapped here, which made one shared handler\n // impossible across audioModes.\n if (this.options.onTimeUpdate) this.options.onTimeUpdate(currentTime, duration, this);\n\n // External mode has no <audio> 'ended' event \u2014 synthesize one when the\n // controller's progress reaches the end (fires once per playthrough).\n if (this.progress >= 1) {\n if (!this._extEnded) {\n this._extEnded = true;\n this._emit('waveformplayer:ended', {player: this, url: this.options.url, currentTime: duration, duration});\n if (this.options.onEnd) this.options.onEnd(this);\n }\n } else {\n this._extEnded = false;\n }\n\n this.updateSeekAccessibility();\n }\n\n /**\n * Toggle between play and pause based on the current `isPlaying` state.\n * Works in both audio modes (in external mode it routes through the\n * request-play/pause events).\n */\n togglePlay() {\n if (this.isPlaying) {\n this.pause();\n } else {\n this.play();\n }\n }\n\n /**\n * Seek the owned `<audio>` element to an absolute time, clamped to\n * `[0, duration]`, and refresh progress. Self mode only \u2014 a no-op when\n * there is no audio element or duration. External-mode keyboard/click\n * seeks go through {@link WaveformPlayer#seekToSeconds} instead.\n * @param {number} seconds - Target time in seconds.\n */\n seekTo(seconds) {\n if (this.audio && this.audio.duration) {\n this.audio.currentTime = clamp(seconds, 0, this.audio.duration);\n this.updateProgress();\n }\n }\n\n /**\n * Seek the owned `<audio>` element to a fraction of the track, clamped to\n * `[0, 1]`, and refresh progress. Self mode only \u2014 a no-op without an audio\n * element or duration.\n * @param {number} percent - Position as a fraction from 0 to 1.\n */\n seekToPercent(percent) {\n if (this.audio && this.audio.duration) {\n this.audio.currentTime = this.audio.duration * clamp(percent);\n this.updateProgress();\n }\n }\n\n /**\n * Set the owned `<audio>` element's volume, clamped to `[0, 1]`. Self mode\n * only \u2014 a no-op in external mode where the controller owns volume.\n * @param {number} volume - Volume from 0 (silent) to 1 (full).\n */\n setVolume(volume) {\n // Coerce + guard: a non-finite value (e.g. from a bad config or stale\n // storage) must not propagate NaN into audio.volume (which throws).\n const v = Number(volume);\n if (this.audio && Number.isFinite(v)) {\n this.audio.volume = clamp(v);\n }\n }\n\n /**\n * Set the owned `<audio>` element's playback rate (clamped to 0.5\u20132),\n * persist it onto `this.options.playbackRate`, and refresh the speed UI.\n * Self mode only \u2014 a no-op in external mode.\n * @param {number} rate - Desired playback rate; clamped to the 0.5\u20132 range.\n */\n setPlaybackRate(rate) {\n if (!this.audio) return;\n\n const clampedRate = clamp(rate, 0.5, 2);\n this.audio.playbackRate = clampedRate;\n this.options.playbackRate = clampedRate;\n\n this.updateSpeedUI();\n }\n\n /**\n * Tear down the player and release all resources.\n *\n * Flags destruction (so in-flight handlers bail), dispatches\n * `waveformplayer:destroy`, stops playback and the animation loop, aborts\n * every listener registered on the instance signal, disconnects the resize\n * observer, removes the window-resize handler, drops the instance from the\n * static map and `currentlyPlaying`, resets/releases the audio element, and\n * empties the container.\n * @fires WaveformPlayer#waveformplayer:destroy\n */\n destroy() {\n // Set a flag to indicate we're destroying\n this.isDestroying = true;\n\n // Let listeners (analytics, controllers) release their references\n // before teardown \u2014 the symmetric counterpart to waveformplayer:ready.\n this._emit('waveformplayer:destroy', {player: this, url: this.options.url});\n\n // Stop playback and animations\n this.pause();\n this.stopSmoothUpdate();\n clearTimeout(this._markerLabelTimer);\n\n // Tear down every document/container/seek listener in one shot.\n this._ac?.abort();\n\n // Disconnect observer\n if (this.resizeObserver) {\n this.resizeObserver.disconnect();\n this.resizeObserver = null;\n }\n\n // Remove window resize listener\n if (this.resizeHandler) {\n window.removeEventListener('resize', this.resizeHandler);\n this.resizeHandler = null;\n }\n\n // Remove from instances map\n WaveformPlayer.instances.delete(this.id);\n\n // Clear current playing reference if it's this instance\n if (WaveformPlayer.currentlyPlaying === this) {\n WaveformPlayer.currentlyPlaying = null;\n }\n\n // Properly clean up audio element\n if (this.audio) {\n this.audio.pause();\n this.audio.src = '';\n this.audio.load(); // Reset the audio element\n this.audio = null;\n }\n\n // Clear the container\n this.container.innerHTML = '';\n\n // Clear all references\n this.canvas = null;\n this.ctx = null;\n this.playBtn = null;\n this.waveformData = [];\n }\n\n // ============================================\n // Static Methods\n // ============================================\n\n /**\n * Get player instance by ID, element, or element ID\n * @param {string|HTMLElement} idOrElement - Player ID, element, or element ID\n * @returns {WaveformPlayer|undefined}\n */\n static getInstance(idOrElement) {\n if (typeof idOrElement === 'string') {\n const instance = this.instances.get(idOrElement);\n if (instance) return instance;\n\n const element = document.getElementById(idOrElement);\n if (element) {\n return Array.from(this.instances.values()).find(p => p.container === element);\n }\n }\n\n if (idOrElement instanceof HTMLElement) {\n return Array.from(this.instances.values()).find(p => p.container === idOrElement);\n }\n\n return undefined;\n }\n\n /**\n * Get all player instances\n * @returns {WaveformPlayer[]}\n */\n static getAllInstances() {\n return Array.from(this.instances.values());\n }\n\n /**\n * Destroy all player instances\n */\n static destroyAll() {\n this.instances.forEach(player => player.destroy());\n this.instances.clear();\n }\n\n /**\n * Generate waveform data from audio URL\n * @static\n * @param {string} url - Audio URL\n * @param {number} samples - Number of samples\n * @returns {Promise<number[]>} Waveform peak data\n */\n static async generateWaveformData(url, samples = DEFAULT_SAMPLES) {\n try {\n const result = await generateWaveform(url, samples);\n return result.peaks;\n } catch (error) {\n console.error('[WaveformPlayer] Failed to generate waveform:', error);\n throw error;\n }\n }\n\n /**\n * Derive a peaks-JSON URL from an audio URL by swapping the\n * extension. Strict counterpart to `generateWaveformData()`:\n * `generateWaveformData` decodes the audio at runtime,\n * `getPeaksUrl` assumes you generated the peaks at build time\n * (e.g. with `@arraypress/waveform-gen`) and stored the JSON\n * alongside the audio file.\n *\n * Use the result as the `waveform` option \u2014 the player detects\n * the `.json` suffix, `fetch()`es the file, and skips the Web\n * Audio decode pass entirely. Big perf win on catalogues with\n * many tracks (saves ~1-5s decode per file on slow connections).\n *\n * Recognised extensions: mp3, wav, ogg, flac, m4a, aac.\n * Preserves query strings + URL fragments. Returns `undefined`\n * for unrecognised inputs so callers can pass through\n * unconditionally:\n *\n * new WaveformPlayer('#el', {\n * url: track.audioUrl,\n * waveform: WaveformPlayer.getPeaksUrl(track.audioUrl),\n * });\n *\n * @static\n * @param {string|undefined|null} audioUrl - Audio file URL.\n * @returns {string|undefined} Peaks JSON URL, or `undefined`\n * when the input is empty / has no recognised audio extension.\n *\n * @example\n * WaveformPlayer.getPeaksUrl('/audio/track.mp3')\n * // '/audio/track.json'\n *\n * WaveformPlayer.getPeaksUrl('/audio/track.wav?v=2')\n * // '/audio/track.json?v=2'\n *\n * WaveformPlayer.getPeaksUrl(undefined)\n * // undefined\n */\n static getPeaksUrl(audioUrl) {\n if (!audioUrl) return undefined;\n const swapped = audioUrl.replace(\n /\\.(mp3|wav|ogg|flac|m4a|aac)(\\?[^#]*)?(#.*)?$/i,\n '.json$2$3'\n );\n /* Nothing changed \u2192 unrecognised extension, return undefined\n * so callers know to fall back to live decoding. */\n return swapped === audioUrl ? undefined : swapped;\n }\n\n}\n", "/**\n * @module index\n * @description Public entry point for the WaveformPlayer library.\n *\n * Wires together the runtime surfaces for the player: it re-exports the\n * {@link WaveformPlayer} class (default and named), exposes a static\n * `WaveformPlayer.init` hook, scans the DOM for declarative `[data-waveform-player]`\n * markup and auto-instantiates a player for each match, and attaches the class\n * to `window` for plain `<script>`/CDN usage. Loading this module is enough to\n * make any markup-driven players on the page come alive once the DOM is ready.\n */\n\n// Import the main class\nimport {WaveformPlayer} from './core.js';\nimport {formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes} from './utils.js';\n\n// Expose a small set of pure helpers as a single source of truth so consumers\n// (e.g. @arraypress/waveform-bar, @arraypress/waveform-playlist) can reuse them\n// instead of shipping divergent copies. `parseDataAttributes` lets wrappers read\n// the player's full `data-*` option surface off a host element without\n// re-implementing (and drifting from) the contract. Attached to the class so\n// it's reachable from the IIFE global too.\nWaveformPlayer.utils = {formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes};\n\n/**\n * Whether we're running in a browser (vs. SSR / Node), where `window` and\n * `document` are available. Guards the auto-init and global-attach steps.\n * @returns {boolean}\n */\nconst isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined';\n\n/**\n * Scan the document for declarative player markup and instantiate one\n * {@link WaveformPlayer} per matching element.\n *\n * Finds every element carrying the `data-waveform-player` attribute and, for\n * each one not already initialized, constructs a player from it (the constructor\n * reads the element's `data-*` attributes for configuration). Each successfully\n * initialized element is flagged with `data-waveform-initialized=\"true\"` so\n * repeat calls are idempotent and never double-initialize the same element.\n * Construction errors are caught and logged so one broken element cannot abort\n * the rest of the scan. A no-op in non-DOM environments (e.g. SSR).\n *\n * @returns {void}\n */\nfunction autoInit() {\n if (!isBrowser()) return;\n\n const elements = document.querySelectorAll('[data-waveform-player]');\n\n elements.forEach(element => {\n if (element.dataset.waveformInitialized === 'true') return;\n\n try {\n new WaveformPlayer(element);\n element.dataset.waveformInitialized = 'true';\n } catch (error) {\n console.error('[WaveformPlayer] Failed to initialize:', error, element);\n }\n });\n}\n\n// Initialize when DOM is ready: defer until DOMContentLoaded if the document is\n// still parsing, otherwise run the scan immediately on import.\nif (isBrowser()) {\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', autoInit);\n } else {\n autoInit();\n }\n}\n\n/**\n * Static re-scan hook.\n *\n * Exposes {@link autoInit} as `WaveformPlayer.init` so callers can manually\n * (re-)scan the DOM after dynamically injecting `[data-waveform-player]` markup.\n * Already-initialized elements are skipped on subsequent calls.\n *\n * @type {typeof autoInit}\n */\nWaveformPlayer.init = autoInit;\n\n// For CDN/browser usage: expose the class as a global so it is reachable from a\n// plain <script> tag without an ES module loader.\nif (isBrowser()) {\n window.WaveformPlayer = WaveformPlayer;\n}\n\n/**\n * The {@link WaveformPlayer} class.\n * @type {typeof WaveformPlayer}\n */\nexport default WaveformPlayer;\n\n// Named exports\nexport {WaveformPlayer};"],
|
|
5
|
-
"mappings": "AAqBO,SAASA,EAAMC,EAAQ,CAC1B,IAAIC,EAAM,KACV,QAAS,EAAI,EAAG,EAAID,EAAO,OAAQ,IAC3BA,EAAO,CAAC,EAAIC,IAAKA,EAAMD,EAAO,CAAC,GAEvC,OAAOC,CACX,CAQO,SAASC,EAAWC,EAAK,CAC5B,OAAO,OAAOA,GAAc,EAAQ,EAC/B,QAAQ,KAAM,OAAO,EACrB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,MAAM,EACpB,QAAQ,KAAM,QAAQ,EACtB,QAAQ,KAAM,OAAO,CAC9B,CAcO,SAASC,EAAgBC,EAAO,CACnC,OAAOH,EAAW,OAAOG,GAAU,SAAW,GAAGA,CAAK,KAAOA,CAAK,CACtE,CASO,SAASC,EAAWC,EAAK,CAC5B,GAAI,OAAOA,GAAQ,UAAYA,IAAQ,GAAI,MAAO,GAClD,GAAI,CAEA,IAAMC,EAAI,IAAI,IAAID,EAAK,mBAAmB,EAC1C,OAAOC,EAAE,WAAa,SAAWA,EAAE,WAAa,QACpD,MAAY,CACR,MAAO,EACX,CACJ,CASO,SAASC,EAAMJ,EAAOK,EAAM,EAAGT,EAAM,EAAG,CAC3C,OAAO,KAAK,IAAIS,EAAK,KAAK,IAAIL,EAAOJ,CAAG,CAAC,CAC7C,CASO,SAASU,GAAcN,EAAO,CACjC,OAAOA,IAAU,OAAY,OAAYA,IAAU,MACvD,CASA,SAASO,EAAgBP,EAAO,CAC5B,GAAI,OAAOA,GAAU,UAAYA,EAAM,KAAK,EAAE,WAAW,GAAG,EACxD,GAAI,CAAE,OAAO,KA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A,EAAQ,QAAQ,sBAAwB,OAE5C,GAAI,CACA,IAAIR,EAAeQ,CAAO,EAC1BA,EAAQ,QAAQ,oBAAsB,MAC1C,OAASC,EAAO,CACZ,QAAQ,MAAM,yCAA0CA,EAAOD,CAAO,CAC1E,CACJ,CAAC,CACL,CAIIF,EAAU,IACN,SAAS,aAAe,UACxB,SAAS,iBAAiB,mBAAoBC,CAAQ,EAEtDA,EAAS,GAajBP,EAAe,KAAOO,EAIlBD,EAAU,IACV,OAAO,eAAiBN,GAO5B,IAAOU,GAAQV",
|
|
6
|
-
"names": ["maxOf", "values", "max", "escapeHtml", "str", "formatCssLength", "value", "isSafeHref", "url", "u", "clamp", "min", "parseBoolAttr", "parseColorValue", "parseDataAttributes", "element", "options", "setBool", "optKey", "dataKey", "v", "setNum", "float", "raw", "setLength", "setJson", "e", "formatSeekValueText", "template", "args", "sequentialIndex", "match", "position", "index", "formatTime", "seconds", "hrs", "mins", "secs", "idCounter", "generateId", "hash", "i", "extractTitleFromUrl", "parts", "l", "
|
|
4
|
+
"sourcesContent": ["/**\n * @module utils\n * @description Utility functions for WaveformPlayer\n */\n\n/**\n * Default number of peak windows extracted per track. Shared by the runtime\n * peak extraction and `DEFAULT_OPTIONS.samples` so they can't silently drift,\n * and matched to @arraypress/waveform-gen's offline default (1800) for parity.\n * @type {number}\n */\nexport const DEFAULT_SAMPLES = 1800;\n\n/**\n * Largest value in an array, via a loop \u2014 NOT `Math.max(...arr)`, whose\n * argument spread throws `RangeError: Maximum call stack size exceeded` for\n * very large arrays (~1e5+ entries). Returns `-Infinity` for an empty array,\n * matching `Math.max()`.\n * @param {number[]} values - Numbers to scan.\n * @returns {number} The maximum value (`-Infinity` if empty).\n */\nexport function maxOf(values) {\n let max = -Infinity;\n for (let i = 0; i < values.length; i++) {\n if (values[i] > max) max = values[i];\n }\n return max;\n}\n\n/**\n * Escape a string for safe interpolation into HTML, preventing injection when\n * building markup with template strings. `null`/`undefined` become `''`.\n * @param {*} str - Value to escape.\n * @returns {string} HTML-escaped string.\n */\nexport function escapeHtml(str) {\n return String(str == null ? '' : str)\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"')\n .replace(/'/g, ''');\n}\n\n/**\n * Format a CSS length option (`buttonSize`, `buttonRadius`) for interpolation\n * into an inline `style` attribute. A number is treated as px; a string (e.g.\n * `'4rem'`) is used verbatim.\n *\n * The result is HTML-escaped: these values reach the markup from author-supplied\n * `data-*` attributes, so an unescaped quote would otherwise close the `style`\n * attribute and let arbitrary markup in.\n *\n * @param {number|string} value - Length option value.\n * @returns {string} Escaped CSS length, e.g. `36px` or `4rem`.\n */\nexport function formatCssLength(value) {\n return escapeHtml(typeof value === 'number' ? `${value}px` : value);\n}\n\n/**\n * Whether a URL is safe to navigate to (assign to `location.href`): allows only\n * `http`/`https` and relative URLs, rejecting `javascript:`, `data:`, `blob:`,\n * `vbscript:` and other script-bearing schemes.\n * @param {string} url - Candidate URL.\n * @returns {boolean} True if the URL uses a safe scheme.\n */\nexport function isSafeHref(url) {\n if (typeof url !== 'string' || url === '') return false;\n try {\n // Resolve relative URLs against a dummy http base; only the scheme matters.\n const u = new URL(url, 'http://localhost/');\n return u.protocol === 'http:' || u.protocol === 'https:';\n } catch (e) {\n return false;\n }\n}\n\n/**\n * Clamp a number to an inclusive range.\n * @param {number} value - Value to constrain.\n * @param {number} [min=0] - Lower bound.\n * @param {number} [max=1] - Upper bound.\n * @returns {number} `value` constrained to `[min, max]`.\n */\nexport function clamp(value, min = 0, max = 1) {\n return Math.max(min, Math.min(value, max));\n}\n\n/**\n * Read a boolean `data-*` flag. Returns `undefined` when the attribute is\n * absent (preserving the sparse-options contract) and otherwise compares the\n * raw value against the literal string `'true'`.\n * @param {string|undefined} value - Raw `dataset` value.\n * @returns {boolean|undefined} `true`/`false` when present, else `undefined`.\n */\nexport function parseBoolAttr(value) {\n return value === undefined ? undefined : value === 'true';\n}\n\n/**\n * A colour data-attribute may be a CSS colour string OR a JSON array of\n * gradient stops (e.g. '[\"#fafafa\",\"#71717a\"]'). Parse the array form;\n * otherwise pass the string straight through.\n * @param {string} value\n * @returns {string|string[]}\n */\nfunction parseColorValue(value) {\n if (typeof value === 'string' && value.trim().startsWith('[')) {\n try { return JSON.parse(value); } catch (e) { /* fall through to string */ }\n }\n return value;\n}\n\n/**\n * Read every recognised `data-*` attribute off a host element and translate it\n * into a plain options object suitable for `mergeOptions`.\n *\n * Only attributes that are actually present are copied, so the returned object\n * is sparse and never overrides defaults with `undefined`. Numeric attributes\n * are coerced with `parseInt`/`parseFloat`, boolean flags are compared against\n * the literal string `'true'`, and JSON-valued attributes (`markers`,\n * `playbackRates`) are parsed defensively \u2014 a parse failure is warned about and\n * the attribute is skipped rather than thrown.\n *\n * Several attributes are shorthand aliases of a canonical long form: `data-src`\n * \u2192 `url`, `data-style` \u2192 `waveformStyle`. When both are present the canonical\n * long form is applied last and therefore wins. `data-color` and `data-theme`\n * are retained as legacy aliases for `waveformColor` and `colorPreset`.\n * Colour attributes that accept gradients (`waveformColor`, `progressColor`)\n * are passed through {@link parseColorValue} so a JSON stop array is expanded.\n *\n * @param {HTMLElement} element - Host element whose `dataset` is inspected.\n * @returns {Object} Sparse options object containing only the attributes found.\n */\nexport function parseDataAttributes(element) {\n const options = {};\n\n // Set a boolean option only when its `data-*` attribute is present, so the\n // returned object stays sparse and never overrides a default with a value\n // the author didn't set. (`dataKey` differs from `optKey` only for showBPM.)\n const setBool = (optKey, dataKey = optKey) => {\n const v = parseBoolAttr(element.dataset[dataKey]);\n if (v !== undefined) options[optKey] = v;\n };\n\n // Read a present (non-empty) numeric attribute as an int (or float).\n const setNum = (optKey, dataKey = optKey, float = false) => {\n const raw = element.dataset[dataKey];\n if (raw) options[optKey] = float ? parseFloat(raw) : parseInt(raw, 10);\n };\n\n // Read a CSS length attribute: a bare number is px (data-button-size=\"64\"),\n // a unit string (e.g. \"4rem\") is kept verbatim for the stylesheet to resolve.\n const setLength = (optKey, dataKey = optKey) => {\n const raw = element.dataset[dataKey];\n if (!raw) return;\n options[optKey] = /^\\d+(\\.\\d+)?$/.test(raw.trim()) ? parseFloat(raw) : raw;\n };\n\n // Parse a JSON-valued attribute defensively \u2014 warn and skip on bad JSON.\n const setJson = (optKey, dataKey = optKey) => {\n const raw = element.dataset[dataKey];\n if (!raw) return;\n try { options[optKey] = JSON.parse(raw); }\n catch (e) { console.warn(`[WaveformPlayer] Invalid ${dataKey} JSON:`, e); }\n };\n\n // Core attributes. `data-src` is a shorthand alias for `data-url`;\n // the canonical long form wins if both are set.\n if (element.dataset.src) options.url = element.dataset.src;\n if (element.dataset.url) options.url = element.dataset.url;\n setNum('height');\n setNum('samples');\n if (element.dataset.preload) {\n options.preload = element.dataset.preload;\n }\n if (element.dataset.crossOrigin) {\n options.crossOrigin = element.dataset.crossOrigin;\n }\n if (element.dataset.audioMode) options.audioMode = element.dataset.audioMode;\n\n // Waveform style attributes. `data-style` is a shorthand alias for\n // `data-waveform-style`; the canonical long form wins if both are set.\n if (element.dataset.style) options.waveformStyle = element.dataset.style;\n if (element.dataset.waveformStyle) options.waveformStyle = element.dataset.waveformStyle;\n if (element.dataset.waveformGradient) options.waveformGradient = element.dataset.waveformGradient;\n setNum('barWidth');\n setNum('barSpacing');\n setNum('barRadius');\n if (element.dataset.buttonAlign) options.buttonAlign = element.dataset.buttonAlign;\n if (element.dataset.layout) options.layout = element.dataset.layout;\n if (element.dataset.buttonStyle) options.buttonStyle = element.dataset.buttonStyle;\n setLength('buttonSize');\n setLength('buttonRadius');\n\n // Color preset\n if (element.dataset.colorPreset) options.colorPreset = element.dataset.colorPreset;\n\n // Individual color customization\n if (element.dataset.waveformColor) options.waveformColor = parseColorValue(element.dataset.waveformColor);\n if (element.dataset.progressColor) options.progressColor = parseColorValue(element.dataset.progressColor);\n\n // Legacy support for old attribute names\n if (element.dataset.color) options.waveformColor = element.dataset.color;\n if (element.dataset.theme) options.colorPreset = element.dataset.theme;\n\n // Feature flags\n setBool('autoplay');\n setBool('showControls');\n setBool('showInfo');\n setBool('showTime');\n setBool('showHoverTime');\n setBool('seekHandle');\n setBool('showBPM', 'showBpm');\n setNum('bpm');\n setBool('singlePlay');\n setBool('playOnSeek');\n\n // Content and metadata\n if (element.dataset.title) options.title = element.dataset.title;\n if (element.dataset.artist) options.artist = element.dataset.artist;\n if (element.dataset.album) options.album = element.dataset.album;\n if (element.dataset.artwork) options.artwork = element.dataset.artwork;\n if (element.dataset.artworkPosition) options.artworkPosition = element.dataset.artworkPosition;\n\n // Waveform data\n if (element.dataset.waveform) options.waveform = element.dataset.waveform;\n\n // Markers\n setJson('markers');\n\n // Playback controls\n setNum('playbackRate', 'playbackRate', true);\n setBool('showPlaybackSpeed');\n setJson('playbackRates');\n\n // Media Session API\n setBool('enableMediaSession');\n\n // Markers visibility\n setBool('showMarkers');\n\n // Accessibility\n setBool('accessibleSeek');\n if (element.dataset.seekLabel) options.seekLabel = element.dataset.seekLabel;\n if (element.dataset.seekValueText) options.seekValueText = element.dataset.seekValueText;\n if (element.dataset.errorText) options.errorText = element.dataset.errorText;\n\n // Localizable UI strings (screen-reader / lock-screen facing)\n if (element.dataset.playPauseLabel) options.playPauseLabel = element.dataset.playPauseLabel;\n if (element.dataset.speedLabel) options.speedLabel = element.dataset.speedLabel;\n if (element.dataset.artworkAlt) options.artworkAlt = element.dataset.artworkAlt;\n if (element.dataset.unknownTrackText) options.unknownTrackText = element.dataset.unknownTrackText;\n\n // Custom icons (raw SVG markup)\n if (element.dataset.playIcon) options.playIcon = element.dataset.playIcon;\n if (element.dataset.pauseIcon) options.pauseIcon = element.dataset.pauseIcon;\n\n return options;\n}\n\n/**\n * Interpolate a printf-style seek value-text template.\n *\n * Substitutes positional (`%1$s`, `%2$s`) and sequential (`%s`) placeholders so\n * a localized template resolves the same way `sprintf` would \u2014 including\n * templates that reorder the arguments for a given language. Placeholders with\n * no matching argument are left intact.\n * @param {string} template - Value-text template, e.g. `'%1$s of %2$s'`.\n * @param {...string} args - Replacement values, in order (current, duration).\n * @returns {string} The interpolated value text.\n */\nexport function formatSeekValueText(template, ...args) {\n let sequentialIndex = 0;\n return template.replace(/%(?:(\\d+)\\$)?s/g, (match, position) => {\n const index = position ? Number(position) - 1 : sequentialIndex++;\n return args[index] ?? match;\n });\n}\n\n/**\n * Format a duration as a clock string.\n *\n * Renders `M:SS` for durations under an hour and `H:MM:SS` for longer ones,\n * zero-padding the minutes and seconds. Falsy, non-numeric, non-finite, or\n * negative inputs are treated as zero and return `'0:00'`.\n *\n * `Infinity` matters here, and an `isNaN` check does not catch it: a streamed or\n * unseekable source reports `audio.duration === Infinity`, and `Infinity % 3600`\n * is `NaN`, so the old guard rendered a literal `'Infinity:NaN:NaN'` into the UI.\n * @param {number} seconds - Time in seconds.\n * @returns {string} Formatted time, e.g. `'3:07'` or `'1:02:09'`.\n */\nexport function formatTime(seconds) {\n const total = Number(seconds);\n if (!total || !Number.isFinite(total) || total < 0) return '0:00';\n\n const hrs = Math.floor(total / 3600);\n const mins = Math.floor((total % 3600) / 60);\n const secs = Math.floor(total % 60);\n\n if (hrs > 0) {\n return `${hrs}:${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;\n }\n\n return `${mins}:${secs.toString().padStart(2, '0')}`;\n}\n\n/**\n * Monotonic per-process counter appended to every generated id to guarantee\n * uniqueness even when two ids hash from the same URL.\n * @type {number}\n * @private\n */\nlet idCounter = 0;\n\n/**\n * Generate a unique, DOM-safe ID from a URL.\n *\n * Uses a DJB2 hash of the FULL url (not a 10-char prefix) plus a process\n * counter, so same-host tracks don't collide in the instances map and\n * non-Latin1 / Unicode URLs don't throw (the old btoa() approach did both).\n * @param {string} url - Audio URL\n * @returns {string} Unique element-id-safe string\n */\nexport function generateId(url) {\n const str = url || 'audio';\n let hash = 5381;\n for (let i = 0; i < str.length; i++) {\n hash = ((hash << 5) + hash + str.charCodeAt(i)) | 0;\n }\n return `wp_${(hash >>> 0).toString(36)}_${(idCounter++).toString(36)}`;\n}\n\n/**\n * Derive a human-readable title from an audio URL's filename.\n *\n * Takes the last path segment, drops the extension, replaces `-`/`_`\n * separators with spaces, and title-cases the first letter of each word.\n * Returns `'Audio'` for an empty or missing URL.\n * @param {string} url - Audio URL.\n * @returns {string} Extracted, prettified title.\n * @example\n * extractTitleFromUrl('https://cdn.example.com/my-cool_track.mp3'); // 'My Cool Track'\n */\nexport function extractTitleFromUrl(url) {\n if (!url) return 'Audio';\n\n const parts = url.split('/');\n const filename = parts[parts.length - 1];\n const name = filename.split('.')[0];\n\n // Clean up common separators\n return name\n .replace(/[-_]/g, ' ')\n .replace(/\\b\\w/g, l => l.toUpperCase());\n}\n\n/**\n * Parse a CSS `rgb()`/`rgba()` colour into numeric channels.\n *\n * Handles both serializations a browser can hand back from `getComputedStyle`:\n * the legacy comma form (`rgb(34, 34, 34)`, `rgba(34, 34, 34, 0.5)`) and the\n * modern space/slash form (`rgb(34 34 34 / 50%)`). Anything else \u2014 the\n * `transparent` keyword, a wide-gamut `color(srgb \u2026)`, an empty string \u2014 is\n * reported as `null` so callers treat it as *unknown* rather than as a colour.\n *\n * @param {string} color - CSS colour string.\n * @returns {{r: number, g: number, b: number, a: number}|null} Channels 0\u2013255\n * with alpha 0\u20131, or `null` when the string isn't an rgb()/rgba() colour.\n */\nexport function parseColor(color) {\n if (typeof color !== 'string') return null;\n\n const m = color.match(/rgba?\\(\\s*([\\d.]+)\\s*[,\\s]\\s*([\\d.]+)\\s*[,\\s]\\s*([\\d.]+)\\s*(?:[,/]\\s*([\\d.]+)(%?))?/i);\n if (!m) return null;\n\n const r = Number(m[1]), g = Number(m[2]), b = Number(m[3]);\n if (!Number.isFinite(r) || !Number.isFinite(g) || !Number.isFinite(b)) return null;\n\n let a = m[4] === undefined ? 1 : Number(m[4]);\n if (!Number.isFinite(a)) return null;\n if (m[5] === '%') a /= 100;\n\n return {r, g, b, a: clamp(a, 0, 1)};\n}\n\n/**\n * Perceived brightness (0\u2013255) of a CSS colour, via the luminance formula.\n *\n * A fully transparent colour returns `null`, not `0`. That distinction matters:\n * `rgba(0, 0, 0, 0)` is the computed background of *every* element the page\n * never paints, so scoring it as pure black made auto-theme detection classify\n * unpainted (i.e. white) pages as dark \u2014 see issue #21. Transparent means \"this\n * tells you nothing\", which is a different answer from \"this is black\".\n *\n * @param {string} color - CSS colour string, e.g. `\"rgb(34, 34, 34)\"`.\n * @returns {number|null} Brightness 0\u2013255, or `null` if it can't be parsed or\n * is fully transparent.\n */\nexport function perceivedBrightness(color) {\n const c = parseColor(color);\n if (!c || c.a <= 0) return null;\n return (c.r * 299 + c.g * 587 + c.b * 114) / 1000;\n}\n\n/**\n * Shallow-merge option objects into a new object, last source winning.\n *\n * Keys whose value is `null` or `undefined` are skipped, so a later source can\n * leave an earlier value untouched by passing a nullish entry rather than\n * clobbering it. The inputs are never mutated.\n * @param {...Object} sources - Option objects merged left-to-right.\n * @returns {Object} A fresh object containing the merged, defined keys.\n */\nexport function mergeOptions(...sources) {\n const result = {};\n\n for (const source of sources) {\n for (const key in source) {\n if (source[key] !== null && source[key] !== undefined) {\n result[key] = source[key];\n }\n }\n }\n\n return result;\n}\n\n/**\n * Wrap a function so it only runs once calls stop arriving for `wait` ms.\n *\n * Each invocation resets the pending timer, so rapid bursts collapse into a\n * single trailing-edge call that receives the most recent arguments. The\n * wrapper itself returns nothing.\n * @param {Function} func - Function to debounce.\n * @param {number} wait - Idle period in milliseconds before `func` fires.\n * @returns {Function} Debounced wrapper forwarding its arguments to `func`.\n */\nexport function debounce(func, wait) {\n let timeout;\n\n return function executedFunction(...args) {\n const later = () => {\n clearTimeout(timeout);\n func(...args);\n };\n\n clearTimeout(timeout);\n timeout = setTimeout(later, wait);\n };\n}\n\n/**\n * Resize a waveform amplitude array to a target number of bars.\n *\n * Returns the original array unchanged when lengths already match, and an empty\n * array when either side is empty. When upsampling (target larger than source)\n * it linearly interpolates between neighbouring samples for a smooth result.\n * When downsampling it splits the source into evenly sized buckets and keeps\n * the peak (maximum) of each so transients survive the reduction; an empty\n * bucket falls back to its nearest-neighbour sample.\n * @param {number[]} data - Original amplitude samples.\n * @param {number} targetLength - Desired number of output bars.\n * @returns {number[]} Resampled amplitude array of length `targetLength`.\n */\nexport function resampleData(data, targetLength) {\n if (data.length === targetLength) return data;\n if (data.length === 0 || targetLength === 0) return [];\n\n const result = [];\n\n // If upsampling (target is larger than source)\n if (targetLength > data.length) {\n const ratio = (data.length - 1) / (targetLength - 1);\n\n for (let i = 0; i < targetLength; i++) {\n const index = i * ratio;\n const lower = Math.floor(index);\n const upper = Math.ceil(index);\n const fraction = index - lower;\n\n // Linear interpolation between samples\n if (upper >= data.length) {\n result.push(data[data.length - 1]);\n } else if (lower === upper) {\n result.push(data[lower]);\n } else {\n const value = data[lower] * (1 - fraction) + data[upper] * fraction;\n result.push(value);\n }\n }\n } else {\n // Downsampling (target is smaller than source)\n const bucketSize = data.length / targetLength;\n\n for (let i = 0; i < targetLength; i++) {\n const start = Math.floor(i * bucketSize);\n const end = Math.floor((i + 1) * bucketSize);\n\n // Find the maximum value in this bucket\n let max = 0;\n let count = 0;\n\n for (let j = start; j <= end && j < data.length; j++) {\n if (data[j] > max) {\n max = data[j];\n }\n count++;\n }\n\n // If no samples were found in this bucket, use nearest neighbor\n if (count === 0) {\n const nearestIndex = Math.min(Math.round(i * bucketSize), data.length - 1);\n max = data[nearestIndex];\n }\n\n result.push(max);\n }\n }\n\n return result;\n}", "/**\n * @module drawing\n * @description Core waveform drawing styles optimized for visual distinction at all sizes\n */\n\nimport {resampleData, clamp} from './utils.js';\n\n/**\n * Resolve a fill value that may be a CSS colour string OR an array of colour\n * stops (rendered as a canvas gradient). Bundle-light gradient support: pass\n * e.g. `waveformColor: ['#fafafa', '#71717a']`. A single-element array collapses\n * to that one colour; a multi-element array is spread evenly along the axis set\n * by `options.waveformGradient`:\n * - `'vertical'` (default) a canvas-height gradient (colour keyed to\n * absolute Y, so short bars occupy only the lower band).\n * - `'horizontal'` left -> right across the whole waveform (a hue sweep).\n * - `'diagonal'` top-left -> bottom-right.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context used to build the gradient.\n * @param {string|string[]} value - A CSS colour string, or an array of colour stops.\n * @param {HTMLCanvasElement} canvas - Canvas (provides the device-pixel span).\n * @param {Object} [options] - Drawing options; reads `waveformGradient`.\n * @returns {string|CanvasGradient} The original string, or a linear gradient.\n */\nfunction makeFill(ctx, value, canvas, options) {\n if (!Array.isArray(value)) return value;\n if (value.length < 2) return value[0];\n const w = canvas.width;\n const h = canvas.height;\n const dir = options && options.waveformGradient;\n const [x0, y0, x1, y1] =\n dir === 'horizontal' ? [0, 0, w, 0] :\n dir === 'diagonal' ? [0, 0, w, h] :\n [0, 0, 0, h];\n try {\n const grad = ctx.createLinearGradient(x0, y0, x1, y1);\n value.forEach((c, i) => grad.addColorStop(i / (value.length - 1), c));\n return grad;\n } catch (e) {\n return value[0]; // a bad stop shouldn't abort the whole render\n }\n}\n\n/**\n * Fill a bar rect, optionally with rounded caps (`barRadius`). Falls back to\n * a plain fillRect where `roundRect` is unavailable (older Safari) \u2014 square\n * bars, no error. Radii are clamped to half the rect's width/height so a\n * large `barRadius` never overflows a thin or short bar.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context (current fillStyle is used).\n * @param {number} x - Left edge of the bar in device pixels.\n * @param {number} y - Top edge of the bar in device pixels.\n * @param {number} w - Bar width in device pixels.\n * @param {number} h - Bar height in device pixels (may be negative for upward fills).\n * @param {number|number[]} radii - Corner radius (number, or [tl, tr, br, bl]).\n * @returns {void}\n */\nfunction fillBar(ctx, x, y, w, h, radii) {\n const any = Array.isArray(radii) ? radii.some(r => r > 0) : radii > 0;\n if (any && typeof ctx.roundRect === 'function') {\n const max = Math.min(w / 2, Math.abs(h) / 2);\n const clampR = (r) => clamp(r, 0, max);\n ctx.beginPath();\n ctx.roundRect(x, y, w, h, Array.isArray(radii) ? radii.map(clampR) : clampR(radii));\n ctx.fill();\n } else {\n ctx.fillRect(x, y, w, h);\n }\n}\n\n/**\n * Scale the configured `barRadius` into device pixels (scalar).\n * @private\n * @param {Object} options - Drawing options (`barRadius` in CSS pixels, defaults to 0).\n * @param {number} dpr - Device pixel ratio multiplier.\n * @returns {number} The bar corner radius in device pixels.\n */\nfunction barRadiusPx(options, dpr) {\n return (options.barRadius || 0) * dpr;\n}\n\n/**\n * Top-rounded corner radii for bottom-anchored bars: [tl, tr, br, bl].\n * Only the top two corners are rounded so bars sit flush on the baseline.\n * @private\n * @param {Object} options - Drawing options (supplies `barRadius`).\n * @param {number} dpr - Device pixel ratio multiplier.\n * @returns {number[]} Corner radii in device pixels as [tl, tr, br, bl].\n */\nfunction barRadii(options, dpr) {\n const r = barRadiusPx(options, dpr);\n return [r, r, 0, 0];\n}\n\n/**\n * Trace a horizontal rounded-capsule (stadium) path from `startX` to `endX`,\n * ready to fill. The end caps are semicircles of radius `barHeight / 2`.\n * @private\n * @param {CanvasRenderingContext2D} ctx - Canvas context.\n * @param {number} startX - Left edge x (also the left cap centre).\n * @param {number} endX - Right edge x.\n * @param {number} centerY - Vertical centre of the capsule.\n * @param {number} barHeight - Capsule thickness in pixels.\n * @returns {void}\n */\nfunction capsulePath(ctx, startX, endX, centerY, barHeight) {\n const r = barHeight / 2;\n ctx.beginPath();\n ctx.moveTo(startX, centerY - r);\n ctx.lineTo(endX - r, centerY - r);\n ctx.arc(endX - r, centerY, r, -Math.PI / 2, Math.PI / 2);\n ctx.lineTo(startX, centerY + r);\n ctx.arc(startX, centerY, r, Math.PI / 2, -Math.PI / 2);\n ctx.closePath();\n}\n\n/**\n * Draw standard bars waveform - classic vertical bars anchored to the baseline.\n * Peaks are resampled to fit the available bar slots, drawn at 90% of canvas\n * height, then the progress portion is repainted in `progressColor` via a\n * left-anchored clip rect.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) that drives the colour overlay.\n * @param {Object} options - Drawing options: `barWidth`, `barSpacing`, `barRadius`,\n * `color`, `progressColor` (colour strings or gradient stop arrays).\n * @returns {void}\n */\nexport function drawBars(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = options.barWidth * dpr;\n const barSpacing = options.barSpacing * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const progressWidth = progress * canvas.width;\n const radii = barRadii(options, dpr);\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Draw all bars first\n ctx.fillStyle = baseFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n // Draw from bottom up, not centered\n const y = height - peakHeight;\n\n fillBar(ctx, x, y, barWidth, peakHeight, radii);\n }\n\n // Progress overlay\n ctx.save();\n ctx.beginPath();\n ctx.rect(0, 0, progressWidth, height);\n ctx.clip();\n\n ctx.fillStyle = progFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x > progressWidth) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n // Draw from bottom up, not centered\n const y = height - peakHeight;\n\n fillBar(ctx, x, y, barWidth, peakHeight, radii);\n }\n\n ctx.restore();\n}\n\n/**\n * Draw mirror/SoundCloud style waveform - symmetrical bars about the centre line.\n * Each peak is drawn twice (45% of height up and down) with the upper cap rounded\n * on top and the lower cap rounded on the bottom; the progress portion is then\n * repainted in `progressColor` through a left-anchored clip rect.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) that drives the colour overlay.\n * @param {Object} options - Drawing options: `barWidth`, `barSpacing`, `barRadius`,\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawMirror(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = options.barWidth * dpr;\n const barSpacing = options.barSpacing * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const centerY = height / 2;\n const progressWidth = progress * canvas.width;\n const r = barRadiusPx(options, dpr);\n const topRadii = [r, r, 0, 0]; // round the upper cap\n const botRadii = [0, 0, r, r]; // round the lower cap\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n // Draw all bars\n ctx.fillStyle = baseFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.45;\n\n fillBar(ctx, x, centerY - peakHeight, barWidth, peakHeight, topRadii);\n fillBar(ctx, x, centerY, barWidth, peakHeight, botRadii);\n }\n\n // Progress overlay\n ctx.save();\n ctx.beginPath();\n ctx.rect(0, 0, progressWidth, height);\n ctx.clip();\n\n ctx.fillStyle = progFill;\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x > progressWidth) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.45;\n\n fillBar(ctx, x, centerY - peakHeight, barWidth, peakHeight, topRadii);\n fillBar(ctx, x, centerY, barWidth, peakHeight, botRadii);\n }\n\n ctx.restore();\n}\n\n/**\n * Draw line/oscilloscope style waveform - smooth flowing wave with glow.\n * Renders a faint oscilloscope grid (centre line + 10 vertical divisions), the\n * full waveform as a bezier-smoothed curve, then the played portion on top with\n * a coloured shadow glow. Peaks are modulated by a sine term so the line undulates\n * rather than reading as static bars.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1); the glowing curve is only drawn when > 0.\n * @param {Object} options - Drawing options: `color` (base wave), `progressColor` (played wave).\n * @returns {void}\n */\nexport function drawLine(ctx, canvas, peaks, progress, options) {\n const width = canvas.width;\n const height = canvas.height;\n const centerY = height / 2;\n const amplitude = height * 0.35;\n\n ctx.clearRect(0, 0, width, height);\n\n /**\n * Stroke a bezier-smoothed curve through the (optionally sine-modulated) peaks.\n * @private\n * @param {string} color - Stroke colour (and shadow colour when glowing).\n * @param {number} lineWidth - Stroke width in pixels.\n * @param {number} [endProgress=1] - Fraction (0-1) of the peaks to draw, left to right.\n * @param {boolean} [addGlow=false] - When true, applies a coloured shadow blur for a glow effect.\n * @returns {void}\n */\n const drawCurve = (color, lineWidth, endProgress = 1, addGlow = false) => {\n // strokeStyle accepts a gradient; shadowColor must be a solid string, so\n // collapse a stop array to its last stop for the glow.\n const stroke = makeFill(ctx, color, canvas, options);\n const solid = Array.isArray(color) ? color[color.length - 1] : color;\n if (addGlow) {\n ctx.shadowBlur = 12;\n ctx.shadowColor = solid;\n }\n\n ctx.strokeStyle = stroke;\n ctx.lineWidth = lineWidth;\n ctx.lineCap = 'round';\n ctx.lineJoin = 'round';\n\n ctx.beginPath();\n ctx.moveTo(0, centerY);\n\n const points = [];\n const samples = Math.floor(peaks.length * endProgress);\n\n // Calculate smoothed points\n for (let i = 0; i < samples; i++) {\n const x = (i / (peaks.length - 1)) * width;\n const peakValue = peaks[i];\n\n // Create a smooth wave motion\n const waveOffset = Math.sin(i * 0.1) * peakValue;\n const y = centerY + (waveOffset * amplitude);\n\n points.push({x, y});\n }\n\n // Draw smooth curve through points using bezier curves\n for (let i = 0; i < points.length - 1; i++) {\n const cp1x = points[i].x + (points[i + 1].x - points[i].x) * 0.5;\n const cp1y = points[i].y;\n const cp2x = points[i + 1].x - (points[i + 1].x - points[i].x) * 0.5;\n const cp2y = points[i + 1].y;\n\n ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, points[i + 1].x, points[i + 1].y);\n }\n\n ctx.stroke();\n\n if (addGlow) {\n ctx.shadowBlur = 0;\n }\n };\n\n // Draw subtle grid for oscilloscope feel\n ctx.strokeStyle = 'rgba(255, 255, 255, 0.03)';\n ctx.lineWidth = 0.5;\n\n // Horizontal center line\n ctx.beginPath();\n ctx.moveTo(0, centerY);\n ctx.lineTo(width, centerY);\n ctx.stroke();\n\n // Vertical grid lines\n for (let i = 0; i <= 10; i++) {\n const x = (width / 10) * i;\n ctx.beginPath();\n ctx.moveTo(x, 0);\n ctx.lineTo(x, height);\n ctx.stroke();\n }\n\n // Draw background wave\n drawCurve(options.color, 2, 1, false);\n\n // Draw progress with glow\n if (progress > 0) {\n drawCurve(options.progressColor, 3, progress, true);\n }\n}\n\n/**\n * Draw blocks/LED meter style waveform - segmented blocks growing from the centre.\n * Each bar's height is quantised into fixed-size blocks separated by gaps, drawn\n * symmetrically up and down from the centre line (the shared centre block is not\n * duplicated downward). Per-bar colour is chosen by comparing the bar's x against\n * the played width \u2014 there is no clip overlay here.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) used to pick each bar's colour.\n * @param {Object} options - Drawing options: `barWidth` (default 3), `barSpacing` (default 1),\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawBlocks(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = (options.barWidth || 3) * dpr;\n const barSpacing = (options.barSpacing || 1) * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const blockSize = 4 * dpr;\n const blockGap = 2 * dpr;\n const progressWidth = progress * canvas.width;\n const centerY = height / 2;\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing);\n if (x + barWidth > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n const blockCount = Math.floor(peakHeight / (blockSize + blockGap));\n\n ctx.fillStyle = x < progressWidth ? progFill : baseFill;\n\n // Draw blocks from center outward\n for (let j = 0; j < blockCount; j++) {\n const blockOffset = j * (blockSize + blockGap);\n\n // Upper blocks\n ctx.fillRect(x, centerY - blockOffset - blockSize, barWidth, blockSize);\n\n // Lower blocks (skip the center block)\n if (j > 0) {\n ctx.fillRect(x, centerY + blockOffset, barWidth, blockSize);\n }\n }\n }\n}\n\n/**\n * Draw dots style waveform - pairs of circular points mirrored about the centre.\n * For each sample a dot is drawn above and below the centre line at half the peak\n * height; dot radius scales with bar width but is floored at 1.5 device pixels.\n * Per-dot colour is chosen by comparing x against the played width (no clip overlay).\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Normalised waveform peak values (0-1).\n * @param {number} progress - Playback progress (0-1) used to pick each dot's colour.\n * @param {Object} options - Drawing options: `barWidth` (default 2), `barSpacing` (default 3),\n * `color`, `progressColor`.\n * @returns {void}\n */\nexport function drawDots(ctx, canvas, peaks, progress, options) {\n const dpr = window.devicePixelRatio || 1;\n const barWidth = (options.barWidth || 2) * dpr;\n const barSpacing = (options.barSpacing || 3) * dpr;\n const barCount = Math.floor(canvas.width / (barWidth + barSpacing));\n const resampledPeaks = resampleData(peaks, barCount);\n const height = canvas.height;\n const dotRadius = Math.max(1.5 * dpr, barWidth / 2);\n const progressWidth = progress * canvas.width;\n const centerY = height / 2;\n const baseFill = makeFill(ctx, options.color, canvas, options);\n const progFill = makeFill(ctx, options.progressColor, canvas, options);\n\n ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n for (let i = 0; i < resampledPeaks.length; i++) {\n const x = i * (barWidth + barSpacing) + barWidth / 2;\n if (x > canvas.width) break;\n\n const peakHeight = resampledPeaks[i] * height * 0.9;\n\n ctx.fillStyle = x < progressWidth ? progFill : baseFill;\n\n // Draw upper dot\n ctx.beginPath();\n ctx.arc(x, centerY - peakHeight / 2, dotRadius, 0, Math.PI * 2);\n ctx.fill();\n\n // Draw lower dot\n ctx.beginPath();\n ctx.arc(x, centerY + peakHeight / 2, dotRadius, 0, Math.PI * 2);\n ctx.fill();\n }\n}\n\n/**\n * Draw seekbar style - a simple rounded progress bar with no waveform.\n * Renders a pill-shaped background track, a clean pill-shaped filled portion\n * (clamped to at least one full pill width so it never collapses), and a\n * draggable circular handle/thumb at the playhead (clamped so it never clips\n * at the track ends).\n * The `peaks` argument is accepted for signature parity but is unused by this style.\n * @param {CanvasRenderingContext2D} ctx - Canvas 2D context to draw into.\n * @param {HTMLCanvasElement} canvas - Canvas element (provides device-pixel dimensions).\n * @param {number[]} peaks - Ignored; present to match the shared draw-function signature.\n * @param {number} progress - Playback progress (0-1); the fill and handle are only drawn when > 0.\n * @param {Object} options - Drawing options: `color` (track), `progressColor` (fill/glow/accent).\n * @returns {void}\n */\nexport function drawSeekbar(ctx, canvas, peaks, progress, options) {\n const width = canvas.width;\n const height = canvas.height;\n const centerY = height / 2;\n const barHeight = 4; // Height of the seekbar in pixels\n const borderRadius = barHeight / 2;\n const active = !!options.seekActive; // hovering or dragging the seekbar\n\n ctx.clearRect(0, 0, width, height);\n\n // Draw background track\n ctx.fillStyle = makeFill(ctx, options.color, canvas, options) || 'rgba(255, 255, 255, 0.2)';\n\n // Rounded background track\n capsulePath(ctx, borderRadius, width, centerY, barHeight);\n ctx.fill();\n\n // Draw progress \u2014 slightly dimmed at rest, full-bright on hover / drag.\n // With a monochrome palette there's no hue to swap (Spotify goes\n // white\u2192green), so the hover cue is a brightness lift plus the handle\n // appearing. The draggable handle is a DOM element\n // (`.waveform-seek-handle`), not drawn here \u2014 so it's always a perfect\n // circle and can grow on direct hover via CSS.\n if (progress > 0) {\n const progressWidth = Math.max(borderRadius * 2, progress * width);\n\n // Brightness lift on hover/drag is only meaningful when the handle is\n // enabled (the bar); a standalone seekbar stays at full progress.\n ctx.save();\n ctx.globalAlpha = options.seekHandle && !active ? 0.7 : 1;\n ctx.fillStyle = makeFill(ctx, options.progressColor, canvas, options) || 'rgba(255, 255, 255, 0.9)';\n capsulePath(ctx, borderRadius, progressWidth, centerY, barHeight);\n ctx.fill();\n ctx.restore();\n }\n}\n\n/**\n * Map of style names (and singular aliases) to their drawing functions.\n * Six visually distinct styles including a simple seekbar; keys are matched\n * against `options.waveformStyle` by {@link draw}.\n * @type {Object.<string, function(CanvasRenderingContext2D, HTMLCanvasElement, number[], number, Object): void>}\n */\nexport const DRAWING_STYLES = {\n 'bars': drawBars, // Classic vertical bars\n 'bar': drawBars,\n 'mirror': drawMirror, // SoundCloud-style symmetrical\n 'line': drawLine, // Smooth oscilloscope wave\n 'blocks': drawBlocks, // LED meter segmented\n 'block': drawBlocks,\n 'dots': drawDots, // Circular points\n 'dot': drawDots,\n 'seekbar': drawSeekbar // Simple progress bar (no waveform)\n};\n\n/**\n * Main drawing entry point that delegates to the style named by\n * `options.waveformStyle`, falling back to {@link drawBars} for unknown styles.\n * @param {CanvasRenderingContext2D} ctx - Canvas context\n * @param {HTMLCanvasElement} canvas - Canvas element\n * @param {number[]} peaks - Waveform peak data (0-1)\n * @param {number} progress - Progress (0-1)\n * @param {Object} options - Drawing options, including `waveformStyle` plus the\n * per-style fields (`barWidth`, `barSpacing`, `barRadius`, `color`, `progressColor`).\n * @returns {void}\n */\nexport function draw(ctx, canvas, peaks, progress, options) {\n const drawFunc = DRAWING_STYLES[options.waveformStyle] || drawBars;\n drawFunc(ctx, canvas, peaks, progress, options);\n}", "/**\n * @module bpm\n * @description BPM detection for audio analysis\n */\n\n/**\n * Estimate the tempo (beats per minute) of an audio buffer.\n *\n * Analyses the first (left/mono) channel by detecting onsets, measuring the\n * time between successive onsets, converting each interval to a tempo, and\n * histogramming those tempos into 3-BPM buckets (60-200 BPM) to find the most\n * common one. Octave errors are corrected by doubling very slow results and\n * halving very fast ones when a strong half/double bucket also exists, then a\n * fixed -1 BPM calibration offset is applied. Returns a 120 BPM fallback when\n * too few onsets are found, and null if analysis throws.\n *\n * @param {AudioBuffer} buffer - Decoded audio buffer to analyse; only channel 0 is read.\n * @returns {number|null} Detected tempo in BPM, 120 as a fallback when onsets are insufficient, or null on error.\n */\nexport function detectBPM(buffer) {\n try {\n const channelData = buffer.getChannelData(0);\n const sampleRate = buffer.sampleRate;\n const onsets = detectOnsets(channelData, sampleRate);\n\n if (onsets.length < 2) return 120;\n\n // Calculate intervals\n const intervals = [];\n for (let i = 1; i < onsets.length; i++) {\n intervals.push((onsets[i] - onsets[i - 1]) / sampleRate);\n }\n\n // Convert to tempos and group\n const tempoGroups = {};\n intervals.forEach(interval => {\n const tempo = 60 / interval;\n const bucket = Math.round(tempo / 3) * 3;\n if (bucket > 60 && bucket < 200) {\n tempoGroups[bucket] = (tempoGroups[bucket] || 0) + 1;\n }\n });\n\n // Find most common\n let maxCount = 0;\n let detectedBPM = 120;\n for (const [tempo, count] of Object.entries(tempoGroups)) {\n if (count > maxCount) {\n maxCount = count;\n detectedBPM = parseInt(tempo);\n }\n }\n\n // Handle tempo ambiguity\n if (detectedBPM < 70 && tempoGroups[detectedBPM * 2]) {\n detectedBPM *= 2;\n } else if (detectedBPM > 160 && tempoGroups[Math.round(detectedBPM / 2)]) {\n detectedBPM = Math.round(detectedBPM / 2);\n }\n\n return detectedBPM - 1; // Calibration offset\n } catch (e) {\n console.warn('[WaveformPlayer] BPM detection failed:', e);\n return null;\n }\n}\n\n/**\n * Detect onset sample positions (transients/beats) within a channel of audio.\n *\n * Slides a 2048-sample window (50% overlap via a half-window hop) across the\n * signal, computing the mean squared energy of each window. An onset is flagged\n * when the energy rise over the previous (smoothed) energy exceeds an adaptive\n * threshold and the window energy is above a noise floor, subject to a minimum\n * spacing of 150 ms so a single transient is not counted twice. The running\n * previousEnergy is exponentially smoothed (0.8 new / 0.2 old) to track the\n * local energy envelope.\n *\n * @param {Float32Array} channelData - PCM samples (normalised -1..1) for a single channel.\n * @param {number} sampleRate - Sample rate in Hz, used to derive the minimum onset spacing.\n * @returns {number[]} Ascending sample indices at which onsets were detected.\n * @private\n */\nfunction detectOnsets(channelData, sampleRate) {\n const windowSize = 2048;\n const hopSize = windowSize / 2;\n const onsets = [];\n let previousEnergy = 0;\n\n for (let i = 0; i < channelData.length - windowSize; i += hopSize) {\n let energy = 0;\n for (let j = i; j < i + windowSize; j++) {\n energy += channelData[j] * channelData[j];\n }\n energy = energy / windowSize;\n\n const energyDiff = energy - previousEnergy;\n const threshold = previousEnergy * 1.8 + 0.01;\n\n if (energyDiff > threshold && energy > 0.01) {\n const lastOnset = onsets[onsets.length - 1] || 0;\n const minDistance = sampleRate * 0.15;\n\n if (i - lastOnset > minDistance) {\n onsets.push(i);\n }\n }\n\n previousEnergy = energy * 0.8 + previousEnergy * 0.2;\n }\n\n return onsets;\n}", "/**\n * @module audio\n * @description Audio processing for WaveformPlayer\n */\n\nimport {detectBPM} from './bpm.js';\nimport {clamp, maxOf, DEFAULT_SAMPLES} from './utils.js';\n\n/**\n * Extract peaks from a decoded audio buffer for waveform visualization.\n *\n * Divides the buffer into `samples` equal-width windows and, within each\n * window, finds the largest absolute amplitude by inspecting every frame (so\n * transients are never missed and the shape is stable across sample counts \u2014\n * matching WaveformGen's offline output). Across multiple channels the\n * per-window peaks are merged by taking the loudest channel, then the whole\n * array is normalized so the maximum peak becomes 1 (a silent buffer is\n * returned unscaled).\n *\n * @param {AudioBuffer} buffer - Decoded audio buffer to analyse.\n * @param {number} [samples=1800] - Number of peak windows (output array length).\n * @returns {number[]} Array of `samples` normalized peak values in the 0-1 range.\n */\nexport function extractPeaks(buffer, samples = DEFAULT_SAMPLES) {\n const sampleSize = buffer.length / samples;\n const channels = buffer.numberOfChannels;\n const peaks = [];\n\n for (let c = 0; c < channels; c++) {\n const chan = buffer.getChannelData(c);\n\n for (let i = 0; i < samples; i++) {\n const start = ~~(i * sampleSize);\n const end = ~~(start + sampleSize);\n\n let min = 0;\n let max = 0;\n\n // Scan EVERY frame in the window. Previously this stepped by\n // sampleSize/10 (~10 samples/window) for speed, but that misses\n // transients and makes the shape change with the sample count.\n // decodeAudioData dominates the cost, so a full scan is worth it \u2014\n // and it now matches WaveformGen's offline output exactly.\n for (let j = start; j < end; j++) {\n const value = chan[j];\n if (value > max) max = value;\n if (value < min) min = value;\n }\n\n const peak = Math.max(Math.abs(max), Math.abs(min));\n\n if (c === 0 || peak > peaks[i]) {\n peaks[i] = peak;\n }\n }\n }\n\n // Normalize peaks\n const maxPeak = maxOf(peaks);\n return maxPeak > 0 ? peaks.map(peak => peak / maxPeak) : peaks;\n}\n\n/**\n * Generate waveform data by fetching and decoding an audio file at a URL.\n *\n * Fetches the URL, decodes it through a short-lived AudioContext, runs\n * {@link extractPeaks} followed by {@link normalizePeaks}, and optionally\n * detects the track's BPM. The AudioContext is created lazily and always\n * closed in the `finally` block so failed decodes never leak one (browsers\n * hard-cap the number of live contexts). Errors are logged and re-thrown so\n * callers can fall back to a placeholder waveform.\n *\n * @param {string} url - Audio file URL to fetch and decode.\n * @param {number} [samples=1800] - Number of peak windows to extract.\n * @param {boolean} [shouldDetectBPM=false] - Whether to run BPM detection on the decoded buffer.\n * @returns {Promise<{peaks: number[], bpm: (number|null)}>} Resolves with the\n * normalized peaks and the detected BPM (`null` when detection is disabled or fails).\n * @throws {Error} Re-throws any fetch/decode error after logging it.\n */\nexport async function generateWaveform(url, samples = DEFAULT_SAMPLES, shouldDetectBPM = false) {\n // Created lazily so the finally block can always close it \u2014 browsers\n // hard-cap live AudioContexts (~6 in Chrome), so leaking one per failed\n // decode would break every subsequent player on the page.\n let audioContext;\n try {\n const AudioCtx = window.AudioContext || /** @type {any} */ (window).webkitAudioContext;\n audioContext = new AudioCtx();\n const response = await fetch(url);\n const arrayBuffer = await response.arrayBuffer();\n const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);\n\n let peaks = extractPeaks(audioBuffer, samples);\n\n // Normalize peaks for consistent visualization\n peaks = normalizePeaks(peaks);\n\n let bpm = null;\n if (shouldDetectBPM) {\n bpm = detectBPM(audioBuffer); // synchronous \u2014 returns number|null\n }\n\n return {peaks, bpm};\n } finally {\n // Error (if any) propagates to the caller, which decides how to log /\n // recover; the context is always closed either way.\n if (audioContext) audioContext.close();\n }\n}\n\n/**\n * Generate a synthetic placeholder waveform for use before (or instead of)\n * real peak data is available.\n *\n * Each bar combines a random base height (0.3-0.8) with a slow sinusoidal\n * variation across the array, clamped to the 0.1-1 range so the result always\n * looks like a plausible waveform rather than pure noise.\n *\n * @param {number} [samples=1800] - Number of bars (output array length).\n * @returns {number[]} Array of `samples` pseudo-random peak values in the 0.1-1 range.\n */\nexport function generatePlaceholderWaveform(samples = DEFAULT_SAMPLES) {\n const data = [];\n for (let i = 0; i < samples; i++) {\n const base = Math.random() * 0.5 + 0.3;\n const variation = Math.sin(i / samples * Math.PI * 4) * 0.2;\n data.push(clamp(base + variation, 0.1, 1));\n }\n return data;\n}\n\n/**\n * Scale peak values so quiet tracks fill the available height consistently.\n *\n * Finds the loudest peak and, only when it is non-zero yet below `targetMax`,\n * scales every peak proportionally so the maximum lands on `targetMax`. Silent\n * arrays (max 0) and already-loud arrays (max above `targetMax`) are returned\n * untouched, so the function never amplifies clipping or divides by zero.\n *\n * @param {number[]} peaks - Peak values, typically in the 0-1 range.\n * @param {number} [targetMax=0.95] - Desired maximum peak after scaling.\n * @returns {number[]} The normalized peak array (the original array when no scaling is applied).\n * @private\n */\nfunction normalizePeaks(peaks, targetMax = 0.95) {\n const maxPeak = maxOf(peaks);\n\n // Don't normalize if already loud enough or silent\n if (maxPeak === 0 || maxPeak > targetMax) return peaks;\n\n // Scale all peaks proportionally\n const scaleFactor = targetMax / maxPeak;\n return peaks.map(peak => peak * scaleFactor);\n}", "/**\n * @module themes\n * @description Color presets and default options for WaveformPlayer\n */\n\nimport {parseColor, perceivedBrightness, DEFAULT_SAMPLES} from './utils.js';\n\n/**\n * Brightness (0\u2013255) above which a backdrop counts as light.\n * @type {number}\n * @private\n */\nconst LIGHT_THRESHOLD = 128;\n\n/**\n * Does `<html>` or `<body>` explicitly signal the given colour scheme via a\n * known class name (`dark`, `dark-mode`, `theme-dark`) or theme attribute\n * (`data-theme`, and `data-color-scheme` on the root)?\n * @param {'dark'|'light'} scheme - Scheme to look for.\n * @returns {boolean} True if the page explicitly hints at `scheme`.\n * @private\n */\nfunction hasThemeHint(scheme) {\n const root = document.documentElement;\n const body = document.body;\n return (\n root.classList.contains(scheme) ||\n root.classList.contains(`${scheme}-mode`) ||\n root.classList.contains(`theme-${scheme}`) ||\n root.getAttribute('data-theme') === scheme ||\n root.getAttribute('data-color-scheme') === scheme ||\n body.classList.contains(scheme) ||\n body.classList.contains(`${scheme}-mode`) ||\n body.getAttribute('data-theme') === scheme\n );\n}\n\n/**\n * Collect what the page paints behind `el`.\n *\n * Walks `el` and its ancestors up to `<html>`, alpha-compositing each element's\n * background *colour* front-to-back. Walking the chain (rather than reading\n * `<body>` alone) is what makes real layouts work: plenty of pages paint their\n * background on a wrapper `<div>` or on `<html>` instead of `<body>`, and a\n * player sitting inside a dark card on an otherwise light page should theme to\n * the card.\n *\n * Compositing rather than taking the first colour found means translucent\n * layers \u2014 an `rgba(0, 0, 0, 0.04)` tint over white, say \u2014 score as what the eye\n * sees instead of as their own colour. Brightness is linear in R/G/B, so\n * blending per-layer brightnesses is equivalent to blending the colours.\n *\n * Background *images* and gradients can't be scored from CSS, so a page painted\n * only that way contributes no colour here and is classified by\n * {@link detectCanvasScheme} instead.\n *\n * @param {Element} el - Element to start from (the player's container).\n * @returns {{sum: number, alpha: number}} Premultiplied brightness, and how much\n * of the stack is opaque (0\u20131).\n * @private\n */\nfunction collectBackdrop(el) {\n let sum = 0;\n let alpha = 0;\n\n for (let node = el; node && node.nodeType === 1 && alpha < 0.995; node = node.parentElement) {\n const c = parseColor(getComputedStyle(node).backgroundColor);\n if (!c || c.a <= 0) continue;\n\n // Each layer only shows through however much is still see-through.\n const weight = c.a * (1 - alpha);\n sum += ((c.r * 299 + c.g * 587 + c.b * 114) / 1000) * weight;\n alpha += weight;\n }\n\n return {sum, alpha};\n}\n\n/**\n * Scheme of the canvas \u2014 what is visible wherever the page paints no background\n * colour of its own.\n *\n * Read from the page's *resolved text colour*. `color` initially resolves to the\n * `canvastext` system colour, which browsers flip to white exactly when the\n * document's used colour-scheme is dark, so it reports the palette the page is\n * actually designed for. It is also the only signal that sees the\n * `<meta name=\"color-scheme\" content=\"light dark\">` form \u2014 Chrome reports\n * `normal` for the computed `color-scheme` property there, so reading that\n * instead would miss the exact setup in #21.\n *\n * Deliberately NOT `prefers-color-scheme`. That describes the **user**, not the\n * page, and what browsers paint behind an unpainted page under a dark\n * preference turns out to be engine-specific \u2014 measured by screenshotting the\n * same page in both engines: Chromium composites a dark `#121212` base\n * background, WebKit keeps it white. Since no scripted signal can tell those\n * apart, we believe the page instead: black text means it was designed light,\n * so it gets the light preset. That is right in WebKit, and in Chromium it only\n * loses on a page whose own text is already unreadable against that same dark\n * base background. The media query survives purely as a last resort for\n * environments with no resolvable text colour.\n *\n * @returns {'dark'|'light'} Scheme of the canvas behind the page.\n * @private\n */\nfunction detectCanvasScheme() {\n // Light text \u21D2 the page is painted for a dark canvas, and vice versa.\n const text = perceivedBrightness(getComputedStyle(document.body).color);\n if (text !== null) return text > LIGHT_THRESHOLD ? 'dark' : 'light';\n\n if (window.matchMedia) {\n if (window.matchMedia('(prefers-color-scheme: dark)').matches) return 'dark';\n if (window.matchMedia('(prefers-color-scheme: light)').matches) return 'light';\n }\n\n // Knowing nothing at all keeps the historic default \u2014 most audio players\n // are dark.\n return 'dark';\n}\n\n/**\n * Detect the appropriate color scheme for the player from the surrounding page.\n *\n * Resolution order, first match wins:\n * 1. Explicit theme hints on `<html>`/`<body>` \u2014 class names\n * (`dark`, `dark-mode`, `theme-dark`, light equivalents) and data\n * attributes (`data-theme`, `data-color-scheme`).\n * 2. The backdrop actually visible behind the player \u2014 every background from\n * `el` up through `<html>` ({@link collectBackdrop}), composited over the\n * canvas ({@link detectCanvasScheme}). >128 = light, <128 = dark.\n * 3. The canvas scheme alone, for the knife-edge case where the composite\n * lands exactly on the threshold.\n *\n * @param {Element} [el] - The player's container, so a player inside a themed\n * card picks up that card. Defaults to `<body>`.\n * @returns {string} The detected scheme, either `'dark'` or `'light'`.\n */\nexport function detectColorScheme(el) {\n // 1. Explicit theme class names / data attributes win.\n if (hasThemeHint('dark')) return 'dark';\n if (hasThemeHint('light')) return 'light';\n\n try {\n // 2. What the player is actually sitting on.\n const start = (el && el.nodeType === 1) ? el : document.body;\n const {sum, alpha} = collectBackdrop(start);\n\n // Whatever transparency is left over after the page's own layers shows\n // the canvas through.\n const canvas = detectCanvasScheme();\n const brightness = sum + (canvas === 'dark' ? 0 : 255) * (1 - alpha);\n\n if (brightness > LIGHT_THRESHOLD) return 'light';\n if (brightness < LIGHT_THRESHOLD) return 'dark';\n\n // 3. Dead-centre grey tells us nothing; go with the canvas.\n return canvas;\n } catch (e) {\n // No usable computed styles (SSR shim, detached document) \u2014 most audio\n // players are dark, so that stays the fallback.\n return 'dark';\n }\n}\n\n/**\n * Built-in colour presets keyed by scheme name.\n *\n * Each preset is a flat map of the player's themeable colour tokens\n * (waveform, progress, button, text, background, border). They are deliberately\n * simple translucent black/white values so they sit on any host background, and\n * any individual token can be overridden per-instance via the matching\n * `*Color` option in {@link DEFAULT_OPTIONS}.\n *\n * @type {Object<string, Object<string, string>>}\n * @property {Object<string, string>} dark Light-on-dark token set.\n * @property {Object<string, string>} light Dark-on-light token set.\n */\nexport const COLOR_PRESETS = {\n dark: {\n waveformColor: 'rgba(255, 255, 255, 0.3)',\n progressColor: 'rgba(255, 255, 255, 0.9)'\n },\n light: {\n waveformColor: 'rgba(0, 0, 0, 0.2)',\n progressColor: 'rgba(0, 0, 0, 0.8)'\n }\n};\n\n/**\n * Resolve a colour preset by name, falling back to auto-detection.\n *\n * When `presetName` names a known preset it is returned as-is; otherwise\n * (null, undefined, or an unrecognised name) the scheme is auto-detected via\n * {@link detectColorScheme} and the corresponding preset is returned.\n *\n * @param {string|null} presetName - Preset name (`'dark'` or `'light'`), or\n * null/invalid to trigger auto-detection.\n * @param {Element} [el] - Container passed through to {@link detectColorScheme}\n * so detection reads the backdrop behind this player, not just `<body>`.\n * @returns {Object<string, string>} The matching colour token map from\n * {@link COLOR_PRESETS}.\n */\nexport function getColorPreset(presetName, el) {\n // If explicitly set to a valid preset, use it\n if (presetName && COLOR_PRESETS[presetName]) {\n return COLOR_PRESETS[presetName];\n }\n\n // Auto-detect if not specified or invalid\n const detected = detectColorScheme(el);\n return COLOR_PRESETS[detected];\n}\n\n/**\n * Default option set for a {@link WaveformPlayer} instance.\n *\n * User-supplied options are merged over this object, so every supported option\n * is enumerated here with its baseline value. `null` colour tokens mean \"inherit\n * from the resolved {@link COLOR_PRESETS} preset\"; `null` content/callback\n * fields mean \"unset\". See the grouped inline comments for per-field notes,\n * notably the `audioMode` self/external distinction and the `accessibleSeek`\n * keyboard slider.\n *\n * @type {Object}\n */\nexport const DEFAULT_OPTIONS = {\n // Core settings\n url: '',\n height: 64,\n // Source peak resolution for LIVE decode (ignored when peaks are supplied).\n // The drawer resamples these to fit canvasWidth / (barWidth + barSpacing)\n // bars, so this is fidelity headroom, not the visible bar count. 1800 (the\n // SoundCloud/WaveformGen figure) keeps wide / high-DPI waveforms crisp; the\n // every-frame scan means a higher value costs no extra extraction time.\n samples: DEFAULT_SAMPLES,\n preload: 'metadata',\n // CORS mode for the underlying <audio> element. Left null by default so the\n // player behaves like a plain <audio> and never forces a CORS request \u2014\n // setting 'anonymous' here would break playback of media hosted on origins\n // that don't send Access-Control-Allow-Origin (default-config S3/CDN). The\n // playback element is never fed to createMediaElementSource, so it never\n // needs CORS-clean media; peak analysis uses a separate fetch() path with\n // its own placeholder fallback. Set 'anonymous' / 'use-credentials' only if\n // you specifically need it. See issue #18.\n crossOrigin: null,\n\n // Audio mode \u2014 'self' = player owns the <audio> element (default, current\n // behavior). 'external' = player is a visualization-only surface; no audio\n // element is created, play() dispatches `waveformplayer:request-play`\n // instead of calling audio.play(), and setPlayingState/setProgress are\n // expected to be driven by an external controller (e.g. WaveformBar).\n audioMode: 'self',\n\n // Playback\n playbackRate: 1,\n showPlaybackSpeed: false,\n playbackRates: [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2],\n\n // Layout Options\n buttonAlign: 'auto',\n // Player layout. 'default' = play button + waveform with a left-aligned\n // info row below. 'preview' = compact: the title is centered under the\n // waveform and the meta row (time / speed / BPM) is trimmed \u2014 ideal for\n // sample-pack sample previews and dense grids.\n layout: 'default',\n // Play/pause button style. 'circle' = bordered circle (default).\n // 'minimal' = a bare play/pause glyph with no circle \u2014 the look sample-pack\n // and beat stores use in their preview grids.\n buttonStyle: 'circle',\n // Play/pause button size. null = the stylesheet default (36px circle /\n // proportional minimal). A number is treated as px; a string (e.g. '4rem')\n // is used verbatim. Sets the `--wfp-btn-size` CSS var, which scales BOTH\n // styles \u2014 box and glyph \u2014 proportionally.\n buttonSize: null,\n // Play/pause button corner radius. null = the stylesheet default (50%, a\n // circle). A number is treated as px; a string (e.g. '8px') is used\n // verbatim. Set 0 for a square button. Sets the `--wfp-btn-radius` CSS var,\n // which shapes the 'circle' style's box \u2014 the bare 'minimal' glyph has no\n // box to round, so this is a no-op there.\n buttonRadius: null,\n\n // Default waveform style\n waveformStyle: 'mirror',\n barWidth: 2,\n barSpacing: 0,\n // Rounded bar caps (px). 0 = square; 1 = soft caps (default). Applies to bars/mirror.\n barRadius: 1,\n\n // Gradient axis when waveformColor/progressColor is an array of stops:\n // 'vertical' (top->bottom canvas gradient), 'horizontal' (hue sweep across the\n // waveform) or 'diagonal'. Ignored for single colours.\n waveformGradient: 'vertical',\n\n // Color preset: null = auto-detect, 'dark' = force dark, 'light' = force light\n colorPreset: null,\n\n // Canvas colours (null = inherit the resolved preset; arrays = gradient\n // stops, see waveformGradient). The DOM chrome (button, title, meta) is\n // themed via CSS variables (--wfp-button-color / --wfp-text-color /\n // --wfp-text-secondary-color), not JS options.\n waveformColor: null,\n progressColor: null,\n\n // Features\n autoplay: false,\n showControls: true,\n showInfo: true,\n showTime: true,\n showHoverTime: false,\n // Show a draggable circle handle + hover brightness-lift on the SEEKBAR\n // style only (it's meaningless on a waveform, where the fill-edge is the\n // playhead). Off by default; the bar turns it on. Drag-to-scrub works\n // regardless of this.\n seekHandle: false,\n showBPM: false,\n // Known BPM to display in the badge (with showBPM). Wins over auto-detection\n // \u2014 set it when peaks are pre-generated so the tempo still shows. null = auto.\n bpm: null,\n singlePlay: true,\n playOnSeek: true,\n enableMediaSession: true,\n\n // Markers\n markers: [],\n showMarkers: true,\n\n // Accessibility \u2014 expose the waveform as a keyboard-operable slider\n // (role=\"slider\" + ARIA value attributes + arrow/page/home/end seeking).\n // seekLabel sets the slider's accessible name; when null it falls back\n // to the track title, then 'Seek'.\n // seekValueText templates the slider's spoken aria-valuetext: %1$s is the\n // current time and %2$s the total duration (both formatted M:SS). When null\n // it falls back to '%1$s of %2$s'. Lets consumers localize the connective\n // text without reformatting the times.\n accessibleSeek: true,\n seekLabel: null,\n seekValueText: null,\n\n // Content\n title: null,\n artist: null,\n artwork: null,\n // Where the `artwork` image renders. 'info' = the info row beside the title\n // (needs showInfo). 'button' = the cover becomes the play/pause button,\n // which still works with the info row hidden. Only ONE placement ever\n // renders, so the same cover can't appear twice.\n //\n // 'button' is self-contained: the stylesheet drops the button's ring and\n // swaps its defaults for cover-sized ones (64px, 8px rounding), because the\n // transport defaults are wrong for artwork in both directions \u2014 36px\n // smudges a cover, and 50% crops square art into a circle. buttonSize /\n // buttonRadius still override it if you want a different tile.\n artworkPosition: 'info',\n album: '',\n\n // Message shown in the error state when audio fails to load.\n errorText: 'Unable to load audio',\n\n // Localizable UI strings (alongside seekLabel / seekValueText / errorText).\n // These are screen-reader / assistive-tech facing, so translate them for\n // non-English UIs. playPauseLabel/speedLabel are aria-labels; artworkAlt is\n // the cover image alt text; unknownTrackText is the Media Session (lock-\n // screen) title fallback used when no track title is set.\n playPauseLabel: 'Play/Pause',\n speedLabel: 'Playback speed',\n artworkAlt: 'Album artwork',\n unknownTrackText: 'Unknown Track',\n\n // Icons (SVG)\n playIcon: '<svg viewBox=\"0 0 24 24\" width=\"16\" height=\"16\"><path d=\"M8 5v14l11-7z\"/></svg>',\n pauseIcon: '<svg viewBox=\"0 0 24 24\" width=\"16\" height=\"16\"><path d=\"M6 4h4v16H6zM14 4h4v16h-4z\"/></svg>',\n\n // Callbacks\n onLoad: null,\n onPlay: null,\n onPause: null,\n onEnd: null,\n onError: null,\n onTimeUpdate: null,\n\n // Optional queue navigation \u2014 when set, the player registers Media Session\n // nexttrack/previoustrack handlers (lock-screen skip buttons). Called with\n // the player instance; wired by waveform-bar / -playlist.\n onNextTrack: null,\n onPreviousTrack: null\n};\n\n/**\n * Per-waveform-style geometry defaults.\n *\n * Maps each supported `waveformStyle` to its natural `barWidth`/`barSpacing`\n * (in px), used to seed bar geometry when the caller has not explicitly set\n * those options so each style renders at sensible proportions.\n *\n * @type {Object<string, {barWidth: number, barSpacing: number}>}\n */\nexport const STYLE_DEFAULTS = {\n bars: {barWidth: 3, barSpacing: 1},\n mirror: {barWidth: 2, barSpacing: 2},\n line: {barWidth: 2, barSpacing: 0},\n blocks: {barWidth: 4, barSpacing: 2},\n dots: {barWidth: 3, barSpacing: 3},\n seekbar: {barWidth: 1, barSpacing: 0}\n};", "/**\n * @module core\n * @description Main WaveformPlayer class\n */\n\nimport {draw} from './drawing.js';\nimport {generateWaveform, generatePlaceholderWaveform} from './audio.js';\nimport {\n formatTime,\n formatSeekValueText,\n extractTitleFromUrl,\n generateId,\n parseDataAttributes,\n mergeOptions,\n debounce,\n clamp,\n escapeHtml,\n formatCssLength,\n DEFAULT_SAMPLES\n} from './utils.js';\n\nimport {DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS, detectColorScheme} from './themes.js';\n\n/**\n * Placeholder shown when an artwork URL fails to load (404 / broken) \u2014 a muted\n * music-note tile, so a missing cover degrades gracefully instead of showing the\n * browser's broken-image icon. Inline SVG data-URI (no font / network needed).\n */\nconst ARTWORK_FALLBACK = 'data:image/svg+xml,' + encodeURIComponent(\n '<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 24 24\"><rect width=\"24\" height=\"24\" rx=\"4\" fill=\"#71717a\" fill-opacity=\"0.15\"/><g fill=\"none\" stroke=\"#a1a1aa\" stroke-width=\"1.4\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><circle cx=\"8\" cy=\"17\" r=\"2.2\"/><circle cx=\"17\" cy=\"15\" r=\"2.2\"/><path d=\"M10.2 17V7l9-1.6v9\"/></g></svg>'\n);\n\n// Keyboard seek steps (seconds) for the accessible slider.\nconst SEEK_STEP_SECONDS = 5;\nconst SEEK_PAGE_SECONDS = 10;\n\n// Controls that own their own focus. A click landing on one of these (or a\n// descendant) must leave focus on the control, not pull it onto the player\n// container \u2014 otherwise activating the play button/slider steals focus onto the\n// wrapper, moving it off the control the user just operated.\nconst INTERACTIVE_ELEMENTS = 'button, a[href], input, [role=\"slider\"]';\n\n/**\n * WaveformPlayer - Modern audio player with waveform visualization\n * @class\n */\nexport class WaveformPlayer {\n /** @type {Map<string, WaveformPlayer>} */\n static instances = new Map();\n\n /** @type {WaveformPlayer|null} */\n static currentlyPlaying = null;\n\n /**\n * Create a new WaveformPlayer instance.\n *\n * Resolves the container, merges options (defaults < `data-*` attributes <\n * constructor options), applies the colour preset and style-specific\n * defaults, registers the instance in the static map, and kicks off\n * {@link WaveformPlayer#init}. A `waveformplayer:ready` event is dispatched\n * ~100ms later, once initialization has settled.\n *\n * @param {string|HTMLElement} container - Container element, or a CSS\n * selector resolved with `document.querySelector`.\n * @param {Object} [options={}] - Player options. Accepts the shorthand\n * aliases `style` (\u2192 `waveformStyle`) and `src` (\u2192 `url`); the canonical\n * names win if both are supplied.\n * @throws {Error} If the container element cannot be found.\n * @fires WaveformPlayer#waveformplayer:ready\n */\n constructor(container, options = {}) {\n // Resolve container\n this.container = typeof container === 'string'\n ? document.querySelector(container)\n : container;\n\n if (!this.container) {\n throw new Error('[WaveformPlayer] Container element not found');\n }\n\n // Parse data attributes if present\n const dataOptions = parseDataAttributes(this.container);\n\n // Shorthand option aliases \u2014 `style` -> `waveformStyle`, `src` -> `url`.\n // The canonical names still work and win if both are supplied.\n const userOptions = { ...options };\n if (userOptions.style && !userOptions.waveformStyle) userOptions.waveformStyle = userOptions.style;\n if (userOptions.src && !userOptions.url) userOptions.url = userOptions.src;\n\n // Merge options: defaults < data attributes < constructor options\n this.options = mergeOptions(DEFAULT_OPTIONS, dataOptions, userOptions);\n\n // Apply color preset (auto-detect if not specified). Detection reads\n // the backdrop behind THIS container, so a player dropped into a themed\n // card matches the card rather than the page.\n const preset = getColorPreset(this.options.colorPreset, this.container);\n\n // Track auto-theme state so the player can re-detect on a runtime light/\n // dark switch. Only colours the user LEFT UNSET follow the preset; an\n // explicit colorPreset or hand-set colour is never overridden.\n this._autoTheme = this.options.colorPreset == null || !COLOR_PRESETS[this.options.colorPreset];\n this._presetKeys = [];\n // Resolved scheme name for the CSS theme class (CSS provides the DOM\n // chrome colours via --wfp-* variables).\n this._scheme = (this.options.colorPreset && COLOR_PRESETS[this.options.colorPreset]) ? this.options.colorPreset : detectColorScheme(this.container);\n\n // Apply preset colors only if individual colors aren't explicitly set\n for (const [key, value] of Object.entries(preset)) {\n if (this.options[key] === null || this.options[key] === undefined) {\n this.options[key] = value;\n this._presetKeys.push(key);\n }\n }\n\n // Apply style-specific defaults if not explicitly set\n const styleDefaults = STYLE_DEFAULTS[this.options.waveformStyle];\n if (styleDefaults) {\n if (dataOptions.barWidth === undefined && options.barWidth === undefined) {\n this.options.barWidth = styleDefaults.barWidth;\n }\n if (dataOptions.barSpacing === undefined && options.barSpacing === undefined) {\n this.options.barSpacing = styleDefaults.barSpacing;\n }\n }\n\n // Initialize state\n this.audio = null;\n this.canvas = null;\n this.ctx = null;\n this.waveformData = [];\n this.progress = 0;\n this._activeMarkerIndex = -1;\n this._markerLabelTimer = null;\n this.isPlaying = false;\n this.isLoading = false;\n this.hasError = false;\n this.updateTimer = null;\n this.resizeObserver = null;\n\n // All DOM/document listeners are registered with this signal so a\n // single abort() in destroy() tears every one of them down (the old\n // destroy left the document-click and container listeners attached).\n this._ac = new AbortController();\n\n // Generate unique ID\n this.id = this.container.id || generateId(this.options.url);\n\n // Add to instances\n WaveformPlayer.instances.set(this.id, this);\n\n // Re-detect the theme on runtime light/dark switches (shared watcher).\n WaveformPlayer._watchTheme();\n\n // Initialize\n this.init();\n\n // Dispatch ready event after initialization\n setTimeout(() => {\n this._emit('waveformplayer:ready', {player: this, url: this.options.url});\n }, 100);\n }\n\n /**\n * Build and dispatch a bubbling `waveformplayer:*` CustomEvent on the\n * container, returning the event so cancelable (request-*) events can have\n * their `defaultPrevented` checked. Single source of truth for the event\n * shape \u2014 every player event bubbles and carries the supplied detail.\n * @param {string} type - Full event type, e.g. `'waveformplayer:play'`.\n * @param {Object} detail - Event detail payload.\n * @param {boolean} [cancelable=false] - Whether the event is cancelable.\n * @returns {CustomEvent} The dispatched event.\n * @private\n */\n _emit(type, detail, cancelable = false) {\n const event = new CustomEvent(type, { bubbles: true, cancelable, detail });\n this.container.dispatchEvent(event);\n return event;\n }\n\n /**\n * External-mode seek request: dispatch a cancelable\n * `waveformplayer:request-seek` and, unless the controller calls\n * `preventDefault()`, optimistically advance the local progress overlay so\n * the canvas repaints at once. Shared by the keyboard slider and canvas click.\n * @param {number} percent - Target position as a 0..1 fraction.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n _requestSeek(percent) {\n const evt = this._emit('waveformplayer:request-seek', { ...this._buildTrackDetail(), percent }, true);\n if (!evt.defaultPrevented) {\n this.progress = percent;\n this.drawWaveform?.();\n }\n }\n\n // ============================================\n // Initialization\n // ============================================\n\n /**\n * Initialize the player: build the DOM, create the audio element (self\n * mode only), wire up the feature controls (speed, keyboard, accessible\n * seek), bind events, attach the resize observer, then size the canvas and\n * \u2014 if a `url` option was given \u2014 load it and optionally autoplay.\n * @private\n */\n init() {\n this.createDOM();\n this.createAudio();\n this.initPlaybackSpeed();\n this.initKeyboardControls();\n this.initSeekControl();\n this.bindEvents();\n this.setupResizeObserver();\n\n // Ensure proper sizing after DOM is ready\n requestAnimationFrame(() => {\n this.resizeCanvas();\n\n // Load audio if URL provided\n if (this.options.url) {\n this.load(this.options.url).then(() => {\n if (this.options.autoplay) {\n this.play()?.catch(() => {});\n }\n }).catch(error => {\n console.error('[WaveformPlayer] Failed to load audio:', error);\n });\n }\n });\n }\n\n /**\n * Build the player's DOM tree inside the container and cache element\n * references.\n *\n * Clears the container, resolves button alignment (`auto` \u2192 `bottom` for\n * the `bars` style, `center` otherwise), and conditionally renders the play\n * button, info row (artwork/title/artist), BPM badge, playback-speed\n * menu, and time display based on the relevant `show*` options. Caches the\n * canvas, controls, and text elements onto `this`, then sizes the canvas.\n * @private\n */\n createDOM() {\n // Clear container\n this.container.innerHTML = '';\n this.container.className = 'waveform-player';\n\n // Determine button alignment\n let buttonAlign = this.options.buttonAlign;\n if (buttonAlign === 'auto') {\n // Auto-align based on waveform style\n const style = this.options.waveformStyle;\n if (style === 'bars') {\n buttonAlign = 'bottom';\n } else {\n buttonAlign = 'center'; // blocks, mirror, line, dots, seekbar all center\n }\n }\n\n // Compact 'preview' layout: centered title under the waveform with the\n // meta row trimmed. Set via the `layout` option / data-layout=\"preview\".\n const isPreview = this.options.layout === 'preview';\n if (isPreview) {\n this.container.classList.add('waveform-layout-preview');\n }\n this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');\n\n // Button sizing/shape ride in on CSS vars so the stylesheet keeps the\n // fallbacks (36px, 50%) \u2014 an unset option emits no var at all.\n const buttonVars = [];\n if (this.options.buttonSize != null) {\n buttonVars.push(`--wfp-btn-size: ${formatCssLength(this.options.buttonSize)}`);\n }\n if (this.options.buttonRadius != null) {\n buttonVars.push(`--wfp-btn-radius: ${formatCssLength(this.options.buttonRadius)}`);\n }\n const buttonStyleAttr = buttonVars.length ? ` style=\"${buttonVars.join('; ')};\"` : '';\n\n // Artwork renders in exactly one place \u2014 the button or the info row \u2014\n // so the same cover can never appear twice. Button artwork is\n // decorative: the button already carries its own aria-label, so a\n // second accessible name here would just be noise.\n const artworkOnButton = this.options.artworkPosition === 'button' && this.options.artwork;\n const buttonArtworkHTML = artworkOnButton\n ? `<img class=\"waveform-btn-artwork\" src=\"${escapeHtml(this.options.artwork)}\" alt=\"\" aria-hidden=\"true\">`\n : '';\n\n // Build play button HTML (conditional)\n const buttonHTML = this.options.showControls ? `\n <button class=\"waveform-btn${this.options.buttonStyle === 'minimal' ? ' waveform-btn-minimal' : ''}${artworkOnButton ? ' waveform-btn-has-artwork' : ''}\" aria-label=\"${escapeHtml(this.options.playPauseLabel)}\"${buttonStyleAttr}>\n ${buttonArtworkHTML}\n <span class=\"waveform-icon-play\">${this.options.playIcon}</span>\n <span class=\"waveform-icon-pause\" style=\"display:none;\">${this.options.pauseIcon}</span>\n </button>\n ` : '';\n\n // Build info section HTML (conditional)\n const infoHTML = this.options.showInfo ? `\n <div class=\"waveform-info\">\n ${this.options.artworkPosition !== 'button' && this.options.artwork ? `\n <img class=\"waveform-artwork\" src=\"${escapeHtml(this.options.artwork)}\" alt=\"${escapeHtml(this.options.artworkAlt)}\" style=\"\n width: 40px;\n height: 40px;\n border-radius: 4px;\n object-fit: cover;\n flex-shrink: 0;\n \">\n ` : ''}\n <div class=\"waveform-text\">\n <span class=\"waveform-title\"></span>\n ${this.options.artist ? `<span class=\"waveform-artist\">${escapeHtml(this.options.artist)}</span>` : ''}\n </div>\n <div class=\"waveform-meta\" style=\"display: flex; align-items: center; gap: 1rem;\">\n ${this.options.showBPM ? `\n <span class=\"waveform-bpm\" style=\"display: none;\">\n <span class=\"bpm-value\">--</span> BPM\n </span>\n ` : ''}\n ${this.options.showPlaybackSpeed ? `\n <div class=\"waveform-speed\">\n <button class=\"speed-btn\" aria-label=\"${escapeHtml(this.options.speedLabel)}\" aria-haspopup=\"menu\" aria-expanded=\"false\">\n <span class=\"speed-value\">1x</span>\n </button>\n <div class=\"speed-menu\" role=\"menu\" aria-label=\"${escapeHtml(this.options.speedLabel)}\" style=\"display: none;\">\n ${this.options.playbackRates.map(rate =>\n `<button class=\"speed-option\" role=\"menuitemradio\" tabindex=\"-1\" aria-checked=\"false\" data-rate=\"${rate}\">${rate}x</button>`\n ).join('')}\n </div>\n </div>\n ` : ''}\n ${this.options.showTime ? `\n <span class=\"waveform-time\">\n <span class=\"time-current\">0:00</span> / <span class=\"time-total\">0:00</span>\n </span>\n ` : ''}\n </div>\n </div>\n ` : '';\n\n // Create HTML structure\n this.container.innerHTML = `\n <div class=\"waveform-player-inner\">\n <div class=\"waveform-body\">\n <div class=\"waveform-track waveform-align-${buttonAlign}\">\n ${buttonHTML}\n \n <div class=\"waveform-container\">\n <canvas></canvas>\n <div class=\"waveform-markers\"></div>\n <div class=\"waveform-loading\" style=\"display:none;\"></div>\n <div class=\"waveform-error\" style=\"display:none;\" role=\"alert\">\n <span class=\"waveform-error-text\">${escapeHtml(this.options.errorText)}</span>\n </div>\n </div>\n </div>\n \n ${infoHTML}\n </div>\n </div>\n`;\n\n // Get references\n this.playBtn = this.container.querySelector('.waveform-btn');\n this.canvas = this.container.querySelector('canvas');\n this.ctx = this.canvas.getContext('2d');\n this.titleEl = this.container.querySelector('.waveform-title');\n this.artistEl = this.container.querySelector('.waveform-artist');\n // One reference for either placement \u2014 artworkPosition is structural\n // (like showInfo/buttonStyle) and fixed for the player's lifetime, so\n // the image only ever has one home.\n this.artworkEl = this.container.querySelector('.waveform-artwork, .waveform-btn-artwork');\n this.bindArtworkFallback(this.artworkEl);\n this.currentTimeEl = this.container.querySelector('.time-current');\n this.totalTimeEl = this.container.querySelector('.time-total');\n this.bpmEl = this.container.querySelector('.waveform-bpm');\n this.bpmValueEl = this.container.querySelector('.bpm-value');\n this.loadingEl = this.container.querySelector('.waveform-loading');\n this.errorEl = this.container.querySelector('.waveform-error');\n this.markersContainer = this.container.querySelector('.waveform-markers');\n this.speedBtn = this.container.querySelector('.speed-btn');\n this.speedMenu = this.container.querySelector('.speed-menu');\n\n // Set canvas size\n this.resizeCanvas();\n\n // Show a caller-supplied BPM immediately (no audio decode required).\n this.updateBPMDisplay();\n }\n\n /**\n * Bind the artwork error fallback to an image element.\n *\n * @param {HTMLImageElement|null} img - Artwork image element.\n * @private\n */\n bindArtworkFallback(img) {\n if (!img) return;\n\n // Fall back to the placeholder tile if the cover URL fails to load.\n img.addEventListener('error', () => {\n if (!img.src.startsWith('data:')) img.src = ARTWORK_FALLBACK;\n }, {signal: this._ac.signal});\n }\n\n /**\n * Create an artwork image element matching the initial player markup.\n *\n * @returns {HTMLImageElement} Artwork image element.\n * @private\n */\n createArtworkElement() {\n const img = document.createElement('img');\n img.className = 'waveform-artwork';\n img.style.width = '40px';\n img.style.height = '40px';\n img.style.borderRadius = '4px';\n img.style.objectFit = 'cover';\n img.style.flexShrink = '0';\n this.bindArtworkFallback(img);\n return img;\n }\n\n /**\n * Create a play-button artwork image matching the initial player markup.\n *\n * Decorative by design \u2014 the button already has an accessible name from its\n * `aria-label`, so the cover is hidden from assistive tech rather than\n * given a competing one. Sizing and clipping are the stylesheet's job.\n *\n * @returns {HTMLImageElement} Play-button artwork image element.\n * @private\n */\n createButtonArtworkElement() {\n const img = document.createElement('img');\n img.className = 'waveform-btn-artwork';\n img.alt = '';\n img.setAttribute('aria-hidden', 'true');\n this.bindArtworkFallback(img);\n return img;\n }\n\n /**\n * Create an artist text element matching the initial player markup.\n *\n * @returns {HTMLSpanElement} Artist text element.\n * @private\n */\n createArtistElement() {\n const span = document.createElement('span');\n span.className = 'waveform-artist';\n return span;\n }\n\n /**\n * Reconcile artist metadata and markup for the current track.\n *\n * @param {string|null} artist - Artist text, or a falsy value to remove it.\n * @private\n */\n syncArtist(artist) {\n this.options.artist = artist || null;\n\n if (!this.options.showInfo) return;\n\n if (!artist) {\n this.artistEl?.remove();\n this.artistEl = null;\n return;\n }\n\n if (!this.artistEl) {\n const titleEl = this.container.querySelector('.waveform-title');\n if (!titleEl) return;\n this.artistEl = this.createArtistElement();\n titleEl.after(this.artistEl);\n }\n\n this.artistEl.textContent = artist;\n this.artistEl.style.display = '';\n }\n\n /**\n * Reconcile the play button's artwork image (`artworkPosition: 'button'`).\n *\n * Shares {@link bindArtworkFallback} with the info-row image, so a broken\n * cover degrades to the same placeholder tile in either placement. The\n * `waveform-btn-has-artwork` class is what turns on the scrim + glyph\n * treatment, so it tracks the image's presence exactly.\n *\n * @param {string|null} artwork - Artwork image URL, or a falsy value to remove it.\n * @private\n */\n syncButtonArtwork(artwork) {\n // Nothing to hang the cover on when the transport is hidden.\n if (!this.playBtn) return;\n\n if (!artwork) {\n this.artworkEl?.remove();\n this.artworkEl = null;\n this.playBtn.classList.remove('waveform-btn-has-artwork');\n return;\n }\n\n if (!this.artworkEl) {\n this.artworkEl = this.createButtonArtworkElement();\n this.playBtn.prepend(this.artworkEl);\n }\n\n this.artworkEl.src = artwork;\n this.playBtn.classList.add('waveform-btn-has-artwork');\n }\n\n /**\n * Reconcile artwork metadata and markup for the current track.\n *\n * The image lives in one of two homes depending on `artworkPosition` \u2014 the\n * info row (default) or the play button \u2014 and only ever one of them, so a\n * cover can't render twice.\n *\n * @param {string|null} artwork - Artwork image URL, or a falsy value to remove artwork.\n * @param {string} artworkAlt - Artwork alt text. Info-row placement only:\n * button artwork is decorative and defers to the button's own label.\n * @private\n */\n syncArtwork(artwork, artworkAlt = '') {\n this.options.artwork = artwork || null;\n this.options.artworkAlt = artworkAlt || '';\n\n if (this.options.artworkPosition === 'button') {\n this.syncButtonArtwork(this.options.artwork);\n return;\n }\n\n if (!this.options.showInfo) return;\n\n if (!artwork) {\n this.artworkEl?.remove();\n this.artworkEl = null;\n return;\n }\n\n if (!this.artworkEl) {\n const textEl = this.container.querySelector('.waveform-text');\n if (!textEl) return;\n this.artworkEl = this.createArtworkElement();\n textEl.before(this.artworkEl);\n }\n\n this.artworkEl.src = artwork;\n this.artworkEl.alt = artworkAlt || '';\n }\n\n /**\n * Create audio element\n * @private\n *\n * No-op in `audioMode: 'external'` \u2014 the player has no audio of its\n * own; an external controller (e.g. WaveformBar) owns playback and\n * pushes state in via setPlayingState() / setProgress(). The\n * `this.audio` field stays null in that mode; downstream code must\n * null-check it.\n */\n createAudio() {\n if (this.options.audioMode === 'external') {\n this.audio = null;\n return;\n }\n this.audio = new Audio();\n this.audio.preload = this.options.preload || 'metadata';\n // Only set crossOrigin when explicitly opted in. Setting it forces a\n // CORS request that breaks playback of media on origins without\n // Access-Control-Allow-Origin; the player never needs CORS-clean media\n // for playback (no createMediaElementSource). See issue #18.\n if (this.options.crossOrigin) {\n this.audio.crossOrigin = this.options.crossOrigin;\n }\n }\n\n // ============================================\n // Feature Initialization\n // ============================================\n\n /**\n * Apply the configured initial playback rate to the audio element (self\n * mode only) and, when `showPlaybackSpeed` is enabled, wire up the speed\n * menu UI via {@link WaveformPlayer#initSpeedControls}.\n * @private\n */\n initPlaybackSpeed() {\n // External mode has no <audio> element, so the speed control\n // doesn't apply locally \u2014 the external controller (e.g.\n // WaveformBar) owns playback rate. Skip the audio init but\n // still bind the speed control UI in case the controller\n // wants to mirror rate changes via events later.\n if (this.audio && this.options.playbackRate && this.options.playbackRate !== 1) {\n this.audio.playbackRate = this.options.playbackRate;\n }\n\n // Initialize speed control UI if enabled\n if (this.options.showPlaybackSpeed) {\n this.initSpeedControls();\n }\n }\n\n /**\n * Wire up the playback-speed menu: toggle it open on the speed button,\n * close it on outside click or Escape (keeping `aria-expanded` on the\n * trigger in sync), and apply the chosen rate when a `.speed-option` is\n * clicked, returning focus to the trigger. All listeners are registered against the\n * instance `AbortController` signal so {@link WaveformPlayer#destroy} tears\n * them down. No-op if the speed elements are absent.\n * @private\n */\n initSpeedControls() {\n const speedBtn = this.container.querySelector('.speed-btn');\n const speedMenu = this.container.querySelector('.speed-menu');\n\n if (!speedBtn || !speedMenu) return;\n\n const options = () => Array.from(speedMenu.querySelectorAll('.speed-option'));\n const isOpen = () => speedMenu.style.display !== 'none';\n\n // Open/close the menu (a proper role=\"menu\"): keep aria-expanded in sync\n // and, on open, move focus into the menu \u2014 the checked rate, else the\n // first option \u2014 so arrow keys drive it.\n const setSpeedMenu = (open) => {\n speedMenu.style.display = open ? 'block' : 'none';\n speedBtn.setAttribute('aria-expanded', open ? 'true' : 'false');\n if (open) {\n const opts = options();\n (opts.find((o) => o.getAttribute('aria-checked') === 'true') || opts[0])?.focus();\n }\n };\n\n const focusOption = (i) => {\n const opts = options();\n if (opts.length) opts[(i + opts.length) % opts.length].focus();\n };\n\n const selectOption = (opt) => {\n this.setPlaybackRate(parseFloat(opt.dataset.rate));\n setSpeedMenu(false);\n speedBtn.focus();\n };\n\n // Toggle from the trigger.\n speedBtn.addEventListener('click', (e) => {\n e.stopPropagation();\n setSpeedMenu(!isOpen());\n }, {signal: this._ac.signal});\n\n // Close when clicking outside.\n document.addEventListener('click', () => setSpeedMenu(false), {signal: this._ac.signal});\n\n // Pointer + native-button (Enter/Space on an option) selection.\n speedMenu.addEventListener('click', (e) => {\n e.stopPropagation();\n const opt = e.target.closest('.speed-option');\n if (opt) selectOption(opt);\n }, {signal: this._ac.signal});\n\n // Keyboard menu: arrows/Home/End roam, Enter/Space select (via the\n // native button click above), Escape/Tab close. ArrowDown/Up on the\n // trigger opens and focuses the first/checked option.\n speedBtn.closest('.waveform-speed')?.addEventListener('keydown', (e) => {\n const opts = options();\n const i = opts.indexOf(document.activeElement);\n if (!isOpen()) {\n if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && document.activeElement === speedBtn) {\n e.preventDefault();\n setSpeedMenu(true);\n }\n return;\n }\n switch (e.key) {\n case 'ArrowDown': e.preventDefault(); focusOption(i < 0 ? 0 : i + 1); break;\n case 'ArrowUp': e.preventDefault(); focusOption(i < 0 ? opts.length - 1 : i - 1); break;\n case 'Home': e.preventDefault(); focusOption(0); break;\n case 'End': e.preventDefault(); focusOption(opts.length - 1); break;\n case 'Escape': e.preventDefault(); setSpeedMenu(false); speedBtn.focus(); break;\n case 'Tab': setSpeedMenu(false); break;\n }\n }, {signal: this._ac.signal});\n\n // Set initial UI state\n this.updateSpeedUI();\n }\n\n /**\n * Enable keyboard transport controls on the container.\n *\n * The container is focusable only after it is clicked (it carries\n * `tabindex=\"-1\"` until then, and clicking steals focus from sibling\n * players). While focused it handles: digits 0-9 (seek to that tenth of\n * the track), Space (toggle play), and \u2014 in self mode only, since\n * `this.audio` is null in external mode \u2014 arrow keys (seek \u00B15s, volume\n * \u00B10.1) and `m`/`M` (mute). Listeners use the instance abort signal.\n * @private\n */\n initKeyboardControls() {\n // Make container focusable but not in tab order by default\n this.container.setAttribute('tabindex', '-1');\n\n // Only activate keyboard controls when explicitly focused (clicked).\n // Skip when the click landed on an interactive control (play button,\n // link, slider, input): those own their focus, so pulling it onto the\n // container would move focus off the control the user just activated.\n this.container.addEventListener('click', (e) => {\n if (e.target.closest(INTERACTIVE_ELEMENTS)) return;\n // Remove focus from all other players\n WaveformPlayer.getAllInstances().forEach(player => {\n if (player !== this) {\n player.container.setAttribute('tabindex', '-1');\n }\n });\n // Make this one focusable\n this.container.setAttribute('tabindex', '0');\n this.container.focus();\n }, {signal: this._ac.signal});\n\n // Keyboard events. In external mode `this.audio` is null, so\n // seek/volume/mute keys are no-ops (the external controller\n // owns those). Space (togglePlay) still works because togglePlay\n // routes through the request-play/pause events.\n this.container.addEventListener('keydown', (e) => {\n if (document.activeElement !== this.container) return;\n\n const key = e.key;\n const hasAudio = !!this.audio;\n const currentTime = hasAudio ? this.audio.currentTime : 0;\n\n // Handle number keys 0-9 for seeking\n if (hasAudio && key >= '0' && key <= '9') {\n e.preventDefault();\n this.seekToPercent(parseInt(key) / 10);\n return;\n }\n\n // Handle other keys. Space always works (dispatches\n // request-play in external mode); audio-bound keys only\n // when we own the <audio> element.\n const actions = {\n ' ': () => this.togglePlay(),\n };\n if (hasAudio) {\n actions['ArrowLeft'] = () => this.seekTo(clamp(currentTime - 5, 0, this.audio.duration));\n actions['ArrowRight'] = () => this.seekTo(clamp(currentTime + 5, 0, this.audio.duration));\n actions['ArrowUp'] = () => this.setVolume(clamp(this.audio.volume + 0.1));\n actions['ArrowDown'] = () => this.setVolume(clamp(this.audio.volume - 0.1));\n actions['m'] = actions['M'] = () => this.audio.muted = !this.audio.muted;\n }\n\n if (actions[key]) {\n e.preventDefault();\n actions[key]();\n }\n }, {signal: this._ac.signal});\n }\n\n /**\n * Expose the waveform as an accessible, keyboard-operable slider.\n *\n * Adds role=\"slider\" + ARIA value attributes to the waveform surface,\n * makes it focusable in the tab order, and handles the standard slider\n * keys (arrows, Page Up/Down, Home/End) to seek. Works in both self and\n * external audio modes. Opt out with `accessibleSeek: false`.\n * @private\n */\n initSeekControl() {\n if (!this.options.accessibleSeek) return;\n\n this.seekEl = this.container.querySelector('.waveform-container');\n if (!this.seekEl) return;\n\n this.seekEl.setAttribute('role', 'slider');\n this.seekEl.setAttribute('tabindex', '0');\n this.seekEl.setAttribute('aria-valuemin', '0');\n this.applySeekLabel();\n this.updateSeekAccessibility();\n\n this.seekEl.addEventListener('keydown', (e) => {\n // Space toggles play/pause even while the waveform slider is focused \u2014\n // it's a media control, so play/pause should work here too, not only\n // when the player root is the active element (restores the pre-a11y\n // Space path reported in #10).\n if (e.key === ' ' || e.key === 'Spacebar') {\n e.preventDefault();\n e.stopPropagation();\n this.togglePlay();\n return;\n }\n\n const duration = this.getSeekDuration();\n if (!duration) return;\n\n const current = this.getSeekCurrentTime();\n let target;\n switch (e.key) {\n case 'ArrowLeft':\n case 'ArrowDown':\n target = current - SEEK_STEP_SECONDS;\n break;\n case 'ArrowRight':\n case 'ArrowUp':\n target = current + SEEK_STEP_SECONDS;\n break;\n case 'PageDown':\n target = current - SEEK_PAGE_SECONDS;\n break;\n case 'PageUp':\n target = current + SEEK_PAGE_SECONDS;\n break;\n case 'Home':\n target = 0;\n break;\n case 'End':\n target = duration;\n break;\n default:\n return;\n }\n\n // Prevent page scroll and stop the container-level keydown\n // handler from also seeking (it would double-fire / change\n // volume on the vertical arrows).\n e.preventDefault();\n e.stopPropagation();\n this.seekToSeconds(target);\n }, {signal: this._ac.signal});\n }\n\n /**\n * Total seekable duration in seconds, regardless of audio mode.\n * @returns {number}\n * @private\n */\n getSeekDuration() {\n if (this.options.audioMode === 'external') {\n return this._extDuration || 0;\n }\n return this.audio && Number.isFinite(this.audio.duration)\n ? this.audio.duration\n : 0;\n }\n\n /**\n * Current playback position in seconds, regardless of audio mode.\n * @returns {number}\n * @private\n */\n getSeekCurrentTime() {\n if (this.options.audioMode === 'external') {\n return this.progress * (this._extDuration || 0);\n }\n return this.audio && Number.isFinite(this.audio.currentTime)\n ? this.audio.currentTime\n : 0;\n }\n\n /**\n * Seek the slider to an absolute time, clamped to the track length.\n *\n * In self mode this defers to {@link WaveformPlayer#seekTo}. In external\n * mode it dispatches a cancelable `waveformplayer:request-seek` event with\n * the target percentage; if the controller doesn't `preventDefault()`, the\n * local progress/visual is updated optimistically. Either way the ARIA\n * slider values are refreshed.\n * @param {number} seconds - Target time in seconds.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n seekToSeconds(seconds) {\n const duration = this.getSeekDuration();\n if (!duration) return;\n\n const clamped = clamp(seconds, 0, duration);\n\n if (this.options.audioMode === 'external') {\n this._requestSeek(clamped / duration);\n this.updateSeekAccessibility();\n return;\n }\n\n // seekTo() calls updateProgress(), which refreshes the ARIA values.\n this.seekTo(clamped);\n }\n\n /**\n * Set the slider's accessible name from `seekLabel`, falling back to the\n * track title, then a generic 'Seek'. No-op if the slider isn't present.\n * @param {string} [title=this.options.title] - Track title to fall back to\n * when `seekLabel` is not set.\n * @private\n */\n applySeekLabel(title = this.options.title) {\n if (!this.seekEl) return;\n const label = this.options.seekLabel || title || 'Seek';\n this.seekEl.setAttribute('aria-label', label);\n }\n\n /**\n * Keep the slider's ARIA value attributes in sync with playback.\n * @private\n */\n updateSeekAccessibility() {\n if (!this.seekEl) return;\n\n const duration = this.getSeekDuration();\n const current = Math.min(this.getSeekCurrentTime(), duration);\n\n this.seekEl.setAttribute('aria-valuemax', String(Math.round(duration)));\n this.seekEl.setAttribute('aria-valuenow', String(Math.round(current)));\n this.seekEl.setAttribute(\n 'aria-valuetext',\n formatSeekValueText(\n this.options.seekValueText || '%1$s of %2$s',\n formatTime(current),\n formatTime(duration)\n )\n );\n }\n\n /**\n * Initialize Media Session API for system media controls\n * @private\n */\n initMediaSession() {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession) return;\n // Skip Media Session in external mode \u2014 the controller (e.g.\n // WaveformBar) owns audio playback and registers its own Media\n // Session handlers; ours would conflict with its.\n if (!this.audio) return;\n\n this._applyMediaMetadata();\n\n // Set up action handlers\n navigator.mediaSession.setActionHandler('play', () => this.play());\n navigator.mediaSession.setActionHandler('pause', () => this.pause());\n navigator.mediaSession.setActionHandler('seekbackward', () => {\n this.seekTo(clamp(this.audio.currentTime - 10, 0, this.audio.duration));\n });\n navigator.mediaSession.setActionHandler('seekforward', () => {\n this.seekTo(clamp(this.audio.currentTime + 10, 0, this.audio.duration));\n });\n navigator.mediaSession.setActionHandler('seekto', (details) => {\n if (details.seekTime !== null) {\n this.seekTo(details.seekTime);\n }\n });\n\n // Optional queue navigation \u2014 lights up the lock-screen skip-track\n // buttons. Registered only when a host (bar / playlist) supplies a\n // callback; a null handler hides the button.\n const onNext = this.options.onNextTrack, onPrev = this.options.onPreviousTrack;\n try { navigator.mediaSession.setActionHandler('nexttrack', typeof onNext === 'function' ? () => onNext(this) : null); } catch (e) { /* unsupported */ }\n try { navigator.mediaSession.setActionHandler('previoustrack', typeof onPrev === 'function' ? () => onPrev(this) : null); } catch (e) { /* unsupported */ }\n }\n\n /**\n * Publish the current track's Media Session metadata (lock-screen /\n * Now-Playing title, artist, album, artwork). Idempotent \u2014 safe to re-call.\n * @private\n */\n _applyMediaMetadata() {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession) return;\n navigator.mediaSession.metadata = new MediaMetadata({\n title: this.options.title || this.options.unknownTrackText,\n artist: this.options.artist || '',\n album: this.options.album || '',\n artwork: this.options.artwork ? [\n {src: this.options.artwork, sizes: '512x512', type: 'image/jpeg'}\n ] : []\n });\n }\n\n /**\n * Sync Media Session playback state + scrubber position on play/pause. iOS\n * ignores metadata set before the media is active, so we re-assert it on\n * play \u2014 that's what fixes the blank lock-screen card on mobile.\n * @param {'playing'|'paused'} state\n * @private\n */\n _updateMediaSession(state) {\n if (!('mediaSession' in navigator) || !this.options.enableMediaSession || !this.audio) return;\n try {\n // Claim the (global, singleton) Media Session for THIS player on play \u2014\n // (re)register our action handlers + metadata so the lock-screen controls\n // drive the player you're actually listening to, not the last one loaded.\n if (state === 'playing') this.initMediaSession();\n navigator.mediaSession.playbackState = state;\n const d = this.audio.duration;\n if (navigator.mediaSession.setPositionState && d && isFinite(d)) {\n navigator.mediaSession.setPositionState({\n duration: d,\n playbackRate: this.audio.playbackRate || 1,\n position: clamp(this.audio.currentTime, 0, d),\n });\n }\n } catch (e) { /* Media Session is best-effort */ }\n }\n\n // ============================================\n // Event Binding\n // ============================================\n\n /**\n * Bind the core interaction listeners: play-button click, the `<audio>`\n * media events (self mode only \u2014 external mode is fed state via\n * {@link WaveformPlayer#setPlayingState}/{@link WaveformPlayer#setProgress}),\n * canvas click-to-seek, and a debounced window-resize redraw.\n * @private\n */\n bindEvents() {\n // Play button (only if controls are shown). In external mode\n // togglePlay() dispatches the request-play/pause events so the\n // controller can decide what to do; the click still goes through\n // here.\n if (this.playBtn) {\n this.playBtn.addEventListener('click', () => this.togglePlay());\n }\n\n // Audio events \u2014 only when we own an <audio> element. External\n // mode receives state via setPlayingState() / setProgress() from\n // the controller, so we have nothing to listen to here.\n if (this.audio) {\n this.audio.addEventListener('loadstart', () => this.setLoading(true));\n this.audio.addEventListener('loadedmetadata', () => this.onMetadataLoaded());\n this.audio.addEventListener('canplay', () => this.setLoading(false));\n this.audio.addEventListener('play', () => this.onPlay());\n this.audio.addEventListener('pause', () => this.onPause());\n this.audio.addEventListener('ended', () => this.onEnded());\n this.audio.addEventListener('error', (e) => this.onError(e));\n }\n\n // Canvas interactions \u2014 click + drag to seek. pointerdown starts a\n // scrub (with pointer capture so moves keep flowing if the cursor\n // leaves the canvas), pointermove updates it while held, pointerup /\n // pointercancel end it. The click handler stays for keyboard/synthetic\n // compatibility. In external mode each seek dispatches a cancelable\n // `waveformplayer:request-seek` so the controller positions its audio.\n this.canvas.addEventListener('click', (e) => this.handleCanvasClick(e));\n this._dragging = false;\n this._seekHover = false;\n this._handleNear = false;\n\n // Hover state \u2014 reveals the handle + lifts the seekbar brightness. A\n // redraw is needed because the canvas has no CSS :hover of its own.\n this.canvas.addEventListener('pointerenter', () => {\n this._seekHover = true;\n this.drawWaveform();\n this._updateSeekHandle();\n });\n this.canvas.addEventListener('pointerleave', () => {\n this._seekHover = false;\n this._handleNear = false;\n if (!this._dragging) this._hideHoverTip();\n this.drawWaveform();\n this._updateSeekHandle();\n });\n\n // Drag = a VISUAL scrub: the playhead previews the cursor while the\n // audio keeps playing untouched (a per-move seek glitches the audio and\n // fights the progress loop). The real seek is committed once, on\n // release. updateProgress() bails while _dragging so playback can't drag\n // the handle off the cursor.\n this.canvas.addEventListener('pointerdown', (e) => {\n if (e.pointerType === 'mouse' && e.button !== 0) return; // primary / touch only\n this._dragging = true;\n try { this.canvas.setPointerCapture(e.pointerId); } catch (err) { /* unsupported */ }\n this._scrubTo(e.clientX);\n });\n this.canvas.addEventListener('pointermove', (e) => {\n if (this._dragging) {\n this._scrubTo(e.clientX);\n return;\n }\n // Hover (not dragging): grow the handle when the cursor is over it.\n const rect = this.canvas.getBoundingClientRect();\n if (rect.width) {\n this._handleNear = Math.abs((e.clientX - rect.left) - this.progress * rect.width) <= 10;\n this._updateSeekHandle();\n }\n });\n const endDrag = (e) => {\n if (!this._dragging) return;\n this._dragging = false;\n // The browser fires a click after pointerup on a tap/short drag \u2014\n // flag it so handleCanvasClick ignores it (a second seek from the\n // synthetic click's coordinate can snap the playhead, e.g. to 0).\n this._suppressClick = true;\n try { this.canvas.releasePointerCapture(e.pointerId); } catch (err) { /* ignore */ }\n this._seekFromPointer(e.clientX); // commit the seek now \u2014 audio jumps once\n // Hide the drag tooltip on release, unless hover-time keeps it (or\n // the seekbar handle is being hovered), to avoid a hide/show flicker.\n if (!this._seekHover && !this.options.showHoverTime) this._hideHoverTip();\n this._updateSeekHandle();\n };\n this.canvas.addEventListener('pointerup', endDrag);\n this.canvas.addEventListener('pointercancel', endDrag);\n\n // Hover-time tooltip over the waveform (when showHoverTime is on).\n this.setupHoverTime();\n // Draggable seek handle (DOM circle, shown on hover / drag).\n this.setupSeekHandle();\n\n // Window resize - store handler for cleanup\n this.resizeHandler = debounce(() => this.resizeCanvas(), 100);\n window.addEventListener('resize', this.resizeHandler);\n }\n\n /**\n * Observe the canvas's parent element for size changes and re-fit the\n * canvas on each one. No-op where `ResizeObserver` is unavailable.\n * @private\n */\n setupResizeObserver() {\n if ('ResizeObserver' in window) {\n this.resizeObserver = new ResizeObserver(() => {\n this.resizeCanvas();\n });\n\n if (this.canvas?.parentElement) {\n this.resizeObserver.observe(this.canvas.parentElement);\n }\n }\n }\n\n // ============================================\n // Audio Loading\n // ============================================\n\n /**\n * Load an audio source: set the title, fetch/generate the waveform peaks,\n * draw them, render markers, and initialise Media Session.\n *\n * In self mode the `<audio>` src is assigned and the method awaits\n * `loadedmetadata` before proceeding. In external mode there is no audio\n * element, so the src/metadata step is skipped and only the visualization\n * is built (duration/time come from the controller via\n * {@link WaveformPlayer#setProgress}). Peaks come from the `waveform`\n * option when provided, otherwise they are decoded from the audio; a\n * decode failure falls back to a placeholder waveform. The `onLoad`\n * callback fires on success.\n * @param {string} url - Audio URL.\n * @returns {Promise<void>} Resolves once loading settles (errors are caught\n * internally and surfaced through {@link WaveformPlayer#onError}).\n */\n async load(url) {\n try {\n this.setLoading(true);\n this.progress = 0;\n this.hasError = false;\n this.container.classList.remove('waveform-is-placeholder');\n\n // In external mode we don't own an <audio> element \u2014 skip\n // src assignment + metadata-wait, but still generate the\n // waveform peaks so the canvas can render the visualization.\n // Duration / current time come from the external controller\n // via setProgress().\n if (this.audio) {\n // Set audio source\n this.audio.src = url;\n\n // Wait for metadata to load\n await new Promise((resolve, reject) => {\n const metadataHandler = () => {\n this.audio.removeEventListener('loadedmetadata', metadataHandler);\n this.audio.removeEventListener('error', errorHandler);\n resolve();\n };\n const errorHandler = (e) => {\n this.audio.removeEventListener('loadedmetadata', metadataHandler);\n this.audio.removeEventListener('error', errorHandler);\n reject(e);\n };\n this.audio.addEventListener('loadedmetadata', metadataHandler);\n this.audio.addEventListener('error', errorHandler);\n });\n }\n\n // Set title\n const title = this.options.title || extractTitleFromUrl(url);\n if (this.titleEl) {\n this.titleEl.textContent = title;\n }\n // Keep the seek slider's accessible name in sync with the track.\n this.applySeekLabel(title);\n\n // Load or generate waveform\n if (this.options.waveform) {\n this.setWaveformData(this.options.waveform);\n } else {\n // Generate waveform\n try {\n const result = await generateWaveform(url, this.options.samples, this.options.showBPM);\n this.waveformData = result.peaks;\n\n // Store BPM if detected\n if (result.bpm) {\n this.detectedBPM = result.bpm;\n this.updateBPMDisplay();\n }\n } catch (error) {\n console.warn('[WaveformPlayer] Using placeholder waveform:', error);\n this.waveformData = generatePlaceholderWaveform(this.options.samples);\n this.container.classList.add('waveform-is-placeholder');\n }\n }\n\n this.drawWaveform();\n this.renderMarkers();\n\n // Fire callback\n if (this.options.onLoad) {\n this.options.onLoad(this);\n }\n } catch (error) {\n // onError() is the single funnel for surfacing + logging errors.\n this.onError(error);\n } finally {\n this.setLoading(false);\n }\n }\n\n /**\n * Swap the player to a new track at runtime.\n *\n * Pauses any current playback, fully resets the audio element (self mode),\n * clears error/marker/progress state, merges the new metadata into\n * `this.options`, updates the artist/artwork DOM, then calls\n * {@link WaveformPlayer#load}. Auto-plays the new track unless\n * `options.autoplay === false`.\n * @param {string} url - Audio URL.\n * @param {string|null} [title=null] - Track title; keeps the existing\n * title when null.\n * @param {string|null} [artist=null] - Track artist; pass `''` to hide\n * the artist row, or null to keep the existing one.\n * @param {Object} [options={}] - Additional options to merge (e.g.\n * `preload`, `artwork`, `artworkAlt`, `markers`, `autoplay`).\n * @returns {Promise<void>}\n */\n async loadTrack(url, title = null, artist = null, options = {}) {\n const hasArtworkOption = Object.prototype.hasOwnProperty.call(options, 'artwork');\n const hasArtworkAltOption = Object.prototype.hasOwnProperty.call(options, 'artworkAlt');\n\n // Stop current playback and clear state\n if (this.isPlaying) {\n this.pause();\n }\n\n // Reset audio element completely (only when we own one)\n if (this.audio) {\n this.audio.src = '';\n this.audio.load();\n }\n\n // Clear any errors\n this.hasError = false;\n if (this.errorEl) {\n this.errorEl.style.display = 'none';\n }\n if (this.canvas) {\n this.canvas.style.opacity = '1';\n }\n if (this.playBtn) {\n this.playBtn.disabled = false;\n }\n\n // Reset state\n this.progress = 0;\n this.waveformData = [];\n\n // Update options (including preload if specified)\n this.options = mergeOptions(this.options, {\n url,\n title: title === null ? this.options.title : title,\n artist: artist === null ? this.options.artist : artist,\n ...options\n });\n if (hasArtworkOption) {\n this.options.artwork = options.artwork || null;\n }\n if (hasArtworkAltOption) {\n this.options.artworkAlt = options.artworkAlt || '';\n } else if (hasArtworkOption) {\n this.options.artworkAlt = this.options.artwork ? DEFAULT_OPTIONS.artworkAlt : '';\n }\n\n // Apply preload setting if it was changed\n if (options.preload && this.audio) {\n this.audio.preload = options.preload;\n }\n\n // Apply crossOrigin if it was changed for this track\n if (options.crossOrigin && this.audio) {\n this.audio.crossOrigin = options.crossOrigin;\n }\n\n // Update artist only when explicitly provided. A null artist keeps the\n // existing artist, while an empty string removes it.\n if (artist !== null) {\n this.syncArtist(artist);\n }\n\n // Update artwork when explicitly provided. The caller can pass an empty\n // value to remove existing artwork from the in-place player.\n if (hasArtworkOption || hasArtworkAltOption) {\n this.syncArtwork(\n hasArtworkOption ? options.artwork : this.options.artwork,\n hasArtworkAltOption ? options.artworkAlt : this.options.artworkAlt\n );\n }\n\n // Clear or update markers\n this.options.markers = options.markers || [];\n\n // Reset the waveform to the NEW track's peaks, or null to regenerate\n // from the URL. mergeOptions() above keeps the previous track's\n // this.options.waveform when the caller passes none, and load() does\n // `if (this.options.waveform) setWaveformData(...)` \u2014 so without this\n // reset a track loaded without peaks would redraw the PREVIOUS track's\n // waveform (audio changes, visualization doesn't).\n this.options.waveform = options.waveform || null;\n\n // Load the new track\n await this.load(url);\n\n // Auto-play the new track unless the caller opted out \u2014 lets a\n // controller load/restore/enqueue without forcing playback.\n if (options.autoplay !== false) {\n this.play()?.catch(() => {});\n }\n }\n\n // ============================================\n // Visualization\n // ============================================\n\n /**\n * Normalise externally-supplied waveform data into `this.waveformData` and\n * redraw.\n *\n * Accepts several shapes: a `.json` URL (fetched async; peaks and any\n * embedded `markers` are applied on resolve), a JSON-encoded array string,\n * a comma-separated number string, or a plain number array. Malformed\n * input degrades to an empty array rather than throwing.\n * @param {string|number[]} data - Peaks as an array, a JSON/CSV string, or\n * a URL to a `.json` peaks file.\n * @private\n */\n setWaveformData(data) {\n // URL to JSON file \u2014 fetch peaks and maybe markers\n if (typeof data === 'string' && data.trim().endsWith('.json')) {\n fetch(data.trim())\n .then(r => r.json())\n .then(json => {\n this.waveformData = Array.isArray(json) ? json : (json.peaks || []);\n if (json.markers && !this.options.markers?.length) {\n this.options.markers = json.markers;\n this.renderMarkers();\n }\n this.drawWaveform();\n })\n .catch(() => {});\n return;\n }\n\n if (typeof data === 'string') {\n try {\n const parsed = JSON.parse(data);\n this.waveformData = Array.isArray(parsed) ? parsed : [];\n } catch {\n this.waveformData = data.split(',').map(Number);\n }\n } else {\n this.waveformData = Array.isArray(data) ? data : [];\n }\n this.drawWaveform();\n }\n\n /**\n * Render the current waveform + progress to the canvas via the shared\n * {@link draw} routine, passing the resolved style and colours. No-op\n * before the context exists or while there is no peak data.\n * @private\n */\n drawWaveform() {\n if (!this.ctx || this.waveformData.length === 0) return;\n\n draw(this.ctx, this.canvas, this.waveformData, this.progress, {\n ...this.options,\n waveformStyle: this.options.waveformStyle || 'bars',\n color: this.options.waveformColor,\n progressColor: this.options.progressColor,\n seekActive: this._seekHover || this._dragging\n });\n }\n\n /**\n * Re-fit the canvas backing store to its parent's width and the configured\n * height, scaled by the device pixel ratio for crisp rendering, then\n * redraw. Guards against running after destruction.\n * @private\n */\n resizeCanvas() {\n // Guard against calls after destruction\n if (!this.canvas || this.isDestroying) {\n return;\n }\n\n const dpr = window.devicePixelRatio || 1;\n const rect = this.canvas.parentElement.getBoundingClientRect();\n\n this.canvas.width = rect.width * dpr;\n this.canvas.height = this.options.height * dpr;\n this.canvas.parentElement.style.height = this.options.height + 'px';\n\n this.drawWaveform();\n }\n\n /**\n * Render the configured cue markers as positioned, clickable buttons over\n * the waveform.\n *\n * Clears any existing markers first, then bails out unless `showMarkers` is\n * on, markers exist, and a duration is known (via the mode-agnostic\n * {@link WaveformPlayer#getSeekDuration}). Each marker is placed by its\n * time-as-percentage, carries a tooltip and ARIA label, and seeks on click\n * (also starting playback when `playOnSeek` is set and currently paused).\n * Markers past the track duration are skipped with a warning.\n * @private\n */\n renderMarkers() {\n if (!this.markersContainer) return;\n\n // Always clear existing markers first\n this.markersContainer.innerHTML = '';\n // Re-sync the active marker on the next progress tick (the DOM was wiped).\n this._activeMarkerIndex = -1;\n clearTimeout(this._markerLabelTimer);\n\n if (!this.options.showMarkers || !this.options.markers?.length) return;\n\n // Duration may come from the <audio> (self mode) or the external\n // controller (external mode) \u2014 use the mode-agnostic accessor.\n const duration = this.getSeekDuration();\n if (!duration) {\n return;\n }\n\n // Add each marker\n this.options.markers.forEach((marker, index) => {\n // Skip markers that are beyond the audio duration\n if (marker.time > duration) {\n console.warn(`[WaveformPlayer] Marker \"${marker.label}\" at ${marker.time}s exceeds audio duration of ${duration}s`);\n return;\n }\n\n const position = (marker.time / duration) * 100;\n\n const markerEl = document.createElement('button');\n markerEl.className = 'waveform-marker';\n markerEl.style.left = `${position}%`;\n markerEl.style.backgroundColor = marker.color || 'rgba(255, 255, 255, 0.5)';\n markerEl.setAttribute('aria-label', marker.label);\n markerEl.setAttribute('data-time', marker.time);\n\n // Tooltip\n const tooltip = document.createElement('span');\n tooltip.className = 'waveform-marker-tooltip';\n tooltip.textContent = marker.label;\n markerEl.appendChild(tooltip);\n\n // Click to seek\n markerEl.addEventListener('click', (e) => {\n e.stopPropagation();\n this.seekTo(marker.time);\n if (this.options.playOnSeek && !this.isPlaying) {\n this.play();\n }\n });\n\n this.markersContainer.appendChild(markerEl);\n });\n }\n\n /**\n * Highlight the marker at `index` (toggling an `active` class) and clear\n * the rest. Pass `null` to clear all. Lets an external controller (e.g. a\n * DJ bar) reflect the current section without reaching into the player's\n * private marker DOM.\n * @param {number|null} index - Marker index to activate, or `null` to clear.\n */\n setActiveMarker(index) {\n if (!this.markersContainer) return;\n const markers = this.markersContainer.querySelectorAll('.waveform-marker');\n markers.forEach((el, i) => el.classList.toggle('active', i === index));\n }\n\n /**\n * Highlight the marker the playhead has most recently passed \u2014 the rendered\n * marker with the greatest `time` \u2264 the current time \u2014 and reveal its label.\n * Driven from the progress loop; a no-op when there are no markers. Reuses\n * {@link WaveformPlayer#setActiveMarker}, and only re-applies when the active\n * marker actually changes.\n * @private\n */\n updateActiveMarker() {\n if (!this.markersContainer) return;\n const els = this.markersContainer.querySelectorAll('.waveform-marker');\n if (!els.length) return;\n const dur = this.getSeekDuration();\n const t = dur ? this.progress * dur : 0;\n let active = -1;\n let best = -Infinity;\n els.forEach((el, i) => {\n const mt = parseFloat(el.getAttribute('data-time'));\n if (Number.isFinite(mt) && mt <= t + 0.05 && mt > best) {\n best = mt;\n active = i;\n }\n });\n if (active !== this._activeMarkerIndex) {\n this._activeMarkerIndex = active;\n this.setActiveMarker(active);\n // Flash the newly-active marker's label, then let it fade \u2014 the\n // highlight stays. Override via `.waveform-marker.show-label` CSS.\n clearTimeout(this._markerLabelTimer);\n els.forEach((el, i) => el.classList.toggle('show-label', i === active));\n if (active >= 0) {\n this._markerLabelTimer = setTimeout(() => {\n this.markersContainer\n ?.querySelectorAll('.waveform-marker')\n .forEach((el) => el.classList.remove('show-label'));\n }, 2500);\n }\n }\n }\n\n /**\n * Build the time tooltip that follows the pointer over the waveform. The\n * element is always created so **drag-scrub** can show the time you're\n * dragging to; the *hover* reveal is gated on `showHoverTime`. Works in both\n * self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).\n * @private\n */\n setupHoverTime() {\n if (!this.seekEl) return;\n const tip = document.createElement('div');\n tip.className = 'waveform-hover-time';\n tip.setAttribute('aria-hidden', 'true');\n this.seekEl.appendChild(tip);\n this.hoverTimeEl = tip;\n\n // Without showHoverTime the tip is drag-only \u2014 no hover listeners.\n if (!this.options.showHoverTime) return;\n\n // Hover updates the tip; the drag handlers also call _updateHoverTip so\n // the time follows the playhead while scrubbing (Spotify-style). The\n // guard avoids a double-update when a drag is in progress (the canvas\n // pointermove already drives it then).\n this.seekEl.addEventListener('pointermove', (e) => {\n if (!this._dragging) this._updateHoverTip(e.clientX);\n });\n this.seekEl.addEventListener('pointerleave', () => {\n if (!this._dragging) this._hideHoverTip();\n });\n }\n\n /**\n * Position + fill the hover-time tooltip for a given client-X. Shared by\n * hover and drag-scrub. No-op when the tooltip isn't enabled\n * (`showHoverTime` off).\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n */\n _updateHoverTip(clientX) {\n const tip = this.hoverTimeEl;\n if (!tip) return;\n const dur = this.getSeekDuration();\n if (!dur) { tip.style.opacity = '0'; return; }\n const rect = this.canvas.getBoundingClientRect();\n const pct = clamp((clientX - rect.left) / rect.width);\n tip.textContent = formatTime(pct * dur);\n tip.style.left = (pct * 100) + '%';\n tip.style.opacity = '1';\n }\n\n /** Hide the hover-time tooltip. @private */\n _hideHoverTip() {\n if (this.hoverTimeEl) this.hoverTimeEl.style.opacity = '0';\n }\n\n /**\n * Visually preview a seek during a drag \u2014 moves the playhead + handle +\n * tooltip to the cursor WITHOUT touching the audio (playback continues; the\n * real seek is committed on release).\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n */\n _scrubTo(clientX) {\n const rect = this.canvas.getBoundingClientRect();\n if (!rect.width) return;\n this.progress = clamp((clientX - rect.left) / rect.width);\n this.drawWaveform();\n this._updateSeekHandle();\n // Live time readout while scrubbing (Spotify-style): the audio keeps\n // playing at its old position; the display shows where release lands.\n const dur = this.getSeekDuration();\n if (dur && this.currentTimeEl) {\n this.currentTimeEl.textContent = formatTime(this.progress * dur);\n // The current-time already shows the target \u2014 don't also pop the\n // hover tooltip with the same value (that duplicate is the bug).\n this._hideHoverTip();\n } else {\n // No current-time readout to fall back on \u2014 keep the tooltip as the\n // only scrub feedback.\n this._updateHoverTip(clientX);\n }\n }\n\n /**\n * Create the draggable seek handle \u2014 a DOM circle over the playhead, shown\n * only on hover / drag (Spotify-style). Pointer-events are off so it never\n * blocks the canvas scrub; size + visibility are CSS.\n * @private\n */\n setupSeekHandle() {\n // Only the seekbar style \u2014 on a real waveform the fill-edge already is\n // the playhead, so a floating dot is just noise.\n if (!this.options.seekHandle || this.options.waveformStyle !== 'seekbar' || !this.seekEl) return;\n const h = document.createElement('div');\n h.className = 'waveform-seek-handle';\n h.setAttribute('aria-hidden', 'true');\n this.seekEl.appendChild(h);\n this.seekHandleEl = h;\n }\n\n /**\n * Position the seek handle at the current progress and toggle its\n * visibility (hover / drag) + expanded (drag / direct-hover) classes.\n * @private\n */\n _updateSeekHandle() {\n const h = this.seekHandleEl;\n if (!h) return;\n h.style.left = (this.progress * 100) + '%';\n h.classList.toggle('is-visible', this._seekHover || this._dragging);\n h.classList.toggle('is-active', this._dragging || this._handleNear);\n }\n\n // ============================================\n // Event Handlers\n // ============================================\n\n /**\n * Seek to the clicked horizontal position on the waveform canvas.\n *\n * Converts the click X into a 0..1 percentage. In external mode it\n * dispatches a cancelable `waveformplayer:request-seek` event (updating the\n * local visual optimistically unless the controller vetoes it); in self\n * mode it seeks the owned `<audio>` via\n * {@link WaveformPlayer#seekToPercent}.\n * @param {MouseEvent} event - The canvas click event.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n handleCanvasClick(event) {\n // Ignore the synthetic click that trails a pointer drag (see endDrag).\n if (this._suppressClick) { this._suppressClick = false; return; }\n this._seekFromPointer(event.clientX);\n }\n\n /**\n * Seek to a horizontal client-X position over the canvas \u2014 shared by\n * click and drag. In external mode the player has no audio of its own,\n * so it dispatches a cancelable `waveformplayer:request-seek` with the\n * target percentage (updating the local visual optimistically); in self\n * mode it seeks the owned `<audio>` via\n * {@link WaveformPlayer#seekToPercent}.\n * @param {number} clientX - Pointer client-X coordinate.\n * @private\n * @fires WaveformPlayer#waveformplayer:request-seek\n */\n _seekFromPointer(clientX) {\n const rect = this.canvas.getBoundingClientRect();\n if (!rect.width) return;\n const targetPercent = clamp((clientX - rect.left) / rect.width);\n\n if (this.options.audioMode === 'external') {\n this._requestSeek(targetPercent);\n return;\n }\n\n if (!this.audio || !this.audio.duration) return;\n this.seekToPercent(targetPercent);\n }\n\n /**\n * Toggle the loading state: show/hide the spinner overlay and set\n * `aria-busy` on the accessible seek slider so assistive tech knows the\n * player is fetching/decoding.\n * @param {boolean} loading - True while audio is loading.\n * @private\n */\n setLoading(loading) {\n this.isLoading = loading;\n if (this.loadingEl) {\n this.loadingEl.style.display = loading ? 'block' : 'none';\n }\n // Let assistive tech know the player is busy fetching/decoding.\n if (this.seekEl) {\n this.seekEl.setAttribute('aria-busy', loading ? 'true' : 'false');\n }\n }\n\n /**\n * `loadedmetadata` handler (self mode): write the total-time display, now\n * that duration is known re-render markers, and publish duration to the\n * accessible seek slider. No-op during destruction.\n * @private\n */\n onMetadataLoaded() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n if (this.totalTimeEl) {\n this.totalTimeEl.textContent = formatTime(this.audio.duration);\n }\n // Re-render markers when duration is known\n this.renderMarkers();\n // Duration is now known \u2014 publish it to the accessible slider.\n this.updateSeekAccessibility();\n }\n\n /**\n * Reflect play/pause state on the transport button: toggle the `playing`\n * class and swap the play/pause icon visibility. The single source of\n * truth shared by `onPlay`, `onPause`, and the external-mode\n * `setPlayingState` pump so they can't drift. No-op without a button.\n * @param {boolean} isPlaying - Whether playback is active.\n * @private\n */\n setPlayButtonState(isPlaying) {\n if (!this.playBtn) return;\n this.playBtn.classList.toggle('playing', isPlaying);\n const playIcon = this.playBtn.querySelector('.waveform-icon-play');\n const pauseIcon = this.playBtn.querySelector('.waveform-icon-pause');\n if (playIcon) playIcon.style.display = isPlaying ? 'none' : 'flex';\n if (pauseIcon) pauseIcon.style.display = isPlaying ? 'flex' : 'none';\n }\n\n /**\n * `play` handler (self mode): set the playing flag, swap the button to its\n * pause icon, start the smooth progress loop, dispatch\n * `waveformplayer:play`, and fire the `onPlay` callback. No-op during\n * destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:play\n */\n onPlay() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n this.isPlaying = true;\n\n this.setPlayButtonState(true);\n\n this.startSmoothUpdate();\n\n // iOS drops Media Session metadata set before playback begins \u2014 re-assert\n // metadata + playback state now that we're actually playing.\n this._updateMediaSession('playing');\n\n // Dispatch play event\n this._emit('waveformplayer:play', {player: this, url: this.options.url});\n\n if (this.options.onPlay) {\n this.options.onPlay(this);\n }\n }\n\n /**\n * `pause` handler (self mode): clear the playing flag, swap the button back\n * to its play icon, stop the smooth progress loop, dispatch\n * `waveformplayer:pause`, and fire the `onPause` callback. No-op during\n * destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:pause\n */\n onPause() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n this.isPlaying = false;\n\n this.setPlayButtonState(false);\n\n this.stopSmoothUpdate();\n\n this._updateMediaSession('paused');\n\n // Dispatch pause event\n this._emit('waveformplayer:pause', {player: this, url: this.options.url});\n\n if (this.options.onPause) {\n this.options.onPause(this);\n }\n }\n\n /**\n * `ended` handler (self mode): reset progress and `currentTime` to the\n * start, redraw, reset the time display, dispatch `waveformplayer:ended`\n * (carrying the final time), run {@link WaveformPlayer#onPause}, and fire\n * the `onEnd` callback. No-op during destruction.\n * @private\n * @fires WaveformPlayer#waveformplayer:ended\n */\n onEnded() {\n // Ignore during destruction\n if (this.isDestroying) return;\n\n const duration = this.audio.duration;\n\n this.progress = 0;\n this.audio.currentTime = 0;\n this.drawWaveform();\n\n // Reset time display\n if (this.currentTimeEl) {\n this.currentTimeEl.textContent = '0:00';\n }\n\n // Dispatch ended event \u2014 carries the final time so listeners (e.g.\n // analytics) don't have to reach into player.audio.\n this._emit('waveformplayer:ended', {player: this, url: this.options.url, currentTime: duration, duration});\n\n this.onPause();\n\n if (this.options.onEnd) {\n this.options.onEnd(this);\n }\n }\n\n /**\n * `error` handler: set the error flag, hide the spinner, reveal the error\n * overlay, dim the canvas, disable the play button, and fire the `onError`\n * callback. No-op during destruction.\n * @param {Event|Error} error - The audio error event, or an Error thrown\n * during loading.\n * @private\n */\n onError(error) {\n // Ignore errors during destruction\n if (this.isDestroying) return;\n\n console.error('[WaveformPlayer] Audio error:', error);\n this.hasError = true;\n this.setLoading(false);\n\n if (this.errorEl) {\n this.errorEl.style.display = 'flex';\n }\n\n if (this.canvas) {\n this.canvas.style.opacity = '0.2';\n }\n\n if (this.playBtn) {\n this.playBtn.disabled = true;\n }\n\n if (this.options.onError) {\n this.options.onError(error, this);\n }\n }\n\n // ============================================\n // Progress Updates\n // ============================================\n\n /**\n * Start the `requestAnimationFrame` loop that drives smooth progress\n * updates while playing (self mode only \u2014 external mode is redrawn by\n * controller {@link WaveformPlayer#setProgress} pushes). Cancels any\n * existing loop first so it's safe to call repeatedly.\n * @private\n */\n startSmoothUpdate() {\n this.stopSmoothUpdate();\n\n const update = () => {\n // In external mode the canvas redraws are driven by\n // setProgress() pushes from the controller \u2014 no internal\n // RAF needed. Self-mode keeps the smooth-update loop.\n if (this.isPlaying && this.audio && this.audio.duration) {\n this.updateProgress();\n this.updateTimer = requestAnimationFrame(update);\n }\n };\n\n this.updateTimer = requestAnimationFrame(update);\n }\n\n /**\n * Cancel the smooth-update animation frame, if one is scheduled.\n * @private\n */\n stopSmoothUpdate() {\n if (this.updateTimer) {\n cancelAnimationFrame(this.updateTimer);\n this.updateTimer = null;\n }\n }\n\n /**\n * Recompute progress from the owned `<audio>` clock and reflect it\n * everywhere (self mode only \u2014 external mode uses\n * {@link WaveformPlayer#setProgress}).\n *\n * Redraws the canvas when progress moves meaningfully, updates the\n * current-time display, dispatches `waveformplayer:timeupdate`, fires the\n * `onTimeUpdate` callback, and refreshes the accessible slider values.\n * @private\n * @fires WaveformPlayer#waveformplayer:timeupdate\n */\n updateProgress() {\n // Self-mode only \u2014 external mode receives progress via\n // setProgress() from the controller and never calls this.\n if (!this.audio || !this.audio.duration) return;\n // While dragging, the playhead previews the cursor \u2014 don't let playback\n // move it (the audio keeps playing; the seek commits on release).\n if (this._dragging) return;\n\n const newProgress = this.audio.currentTime / this.audio.duration;\n\n if (Math.abs(newProgress - this.progress) > 0.001) {\n this.progress = newProgress;\n this.drawWaveform();\n this._updateSeekHandle();\n }\n\n if (this.currentTimeEl) {\n this.currentTimeEl.textContent = formatTime(this.audio.currentTime);\n }\n\n // Dispatch timeupdate event\n this._emit('waveformplayer:timeupdate', {\n player: this,\n currentTime: this.audio.currentTime,\n duration: this.audio.duration,\n progress: this.progress,\n url: this.options.url\n });\n\n if (this.options.onTimeUpdate) {\n this.options.onTimeUpdate(this.audio.currentTime, this.audio.duration, this);\n }\n\n this.updateActiveMarker();\n this.updateSeekAccessibility();\n }\n\n // ============================================\n // UI Updates\n // ============================================\n\n /**\n * Show the detected BPM in the badge, once a value has been detected.\n * @private\n */\n updateBPMDisplay() {\n // A caller-supplied `bpm` wins over auto-detection \u2014 useful when peaks\n // are pre-generated (so the audio is never decoded) but the BPM is known\n // anyway, e.g. sample-pack previews where the tempo is in the metadata.\n const bpm = this.options.bpm || this.detectedBPM;\n if (this.bpmEl && this.bpmValueEl && bpm) {\n this.bpmValueEl.textContent = Math.round(bpm);\n this.bpmEl.style.display = 'inline-flex';\n }\n }\n\n /**\n * Re-detect the page theme and re-apply auto colours + redraw, so an\n * auto-themed player adapts to a runtime light/dark switch (not just the\n * value present on load). No-op when the player uses an explicit\n * `colorPreset` or hand-set colours. Driven by the shared theme watcher.\n * @public\n */\n refreshTheme() {\n if (!this._autoTheme) return;\n this._scheme = detectColorScheme(this.container);\n const preset = getColorPreset(this.options.colorPreset, this.container);\n for (const key of (this._presetKeys || [])) {\n if (key in preset) this.options[key] = preset[key];\n }\n this._applyThemeColors();\n }\n\n /**\n * Sync the theme class (CSS drives the DOM chrome colours via `--wfp-*`\n * variables) and redraw the canvas with the resolved waveform colours.\n * @private\n */\n _applyThemeColors() {\n this.container.classList.toggle('waveform-theme-light', this._scheme === 'light');\n if (this.canvas) this.drawWaveform();\n }\n\n /**\n * Lazily install ONE shared watcher that re-detects the theme for every\n * auto-themed instance when the document theme changes \u2014 a class/attribute\n * flip on `<html>`/`<body>` (Tailwind `dark`, `data-theme`,\n * `data-color-scheme`) or an OS `prefers-color-scheme` change. Event-driven\n * (MutationObserver + matchMedia), never a timer.\n * @private\n */\n static _watchTheme() {\n if (WaveformPlayer._themeWatch || typeof document === 'undefined') return;\n const refresh = () => requestAnimationFrame(() => {\n WaveformPlayer.instances.forEach((p) => {\n try { p.refreshTheme(); } catch (e) { /* ignore */ }\n });\n });\n const opts = {attributes: true, attributeFilter: ['class', 'data-theme', 'data-color-scheme', 'style']};\n const obs = new MutationObserver(refresh);\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n let mq = null;\n try {\n mq = window.matchMedia('(prefers-color-scheme: dark)');\n mq.addEventListener('change', refresh);\n } catch (e) { /* no matchMedia */ }\n WaveformPlayer._themeWatch = {obs, mq, refresh};\n }\n\n /**\n * Sync the speed control's label and the menu's active-option highlight to\n * the audio element's current `playbackRate`. No-op in external mode (no\n * owned `<audio>`), which also avoids reading `playbackRate` before the\n * element exists.\n * @private\n */\n updateSpeedUI() {\n // External mode owns no <audio>; nothing to reflect (and reading\n // this.audio.playbackRate here would throw during construction).\n if (!this.audio) return;\n\n const speedValue = this.container.querySelector('.speed-value');\n if (speedValue) {\n const rate = this.audio.playbackRate;\n speedValue.textContent = rate === 1 ? '1x' : `${rate}x`;\n }\n\n // Update active state in menu\n this.container.querySelectorAll('.speed-option').forEach(btn => {\n const on = parseFloat(btn.dataset.rate) === this.audio.playbackRate;\n btn.classList.toggle('active', on);\n btn.setAttribute('aria-checked', on ? 'true' : 'false');\n });\n }\n\n // ============================================\n // Public API\n // ============================================\n\n /**\n * Play audio.\n *\n * In `audioMode: 'self'` (default): calls the underlying <audio>\n * element's play(). Returns the promise from HTMLMediaElement.play().\n *\n * In `audioMode: 'external'`: dispatches a cancelable\n * `waveformplayer:request-play` event with the track metadata and\n * does NOT touch any audio element. Returns `undefined`. An external\n * controller (e.g. WaveformBar) listens for this event and starts\n * playback on its own audio source, then pushes state back via\n * setPlayingState() / setProgress(). Calling preventDefault() on\n * the event lets the controller veto the play (state is unchanged).\n *\n * When `singlePlay` is enabled, any other currently-playing instance is\n * paused first.\n *\n * @return {Promise|undefined} The promise from `HTMLMediaElement.play()` in\n * self mode; `undefined` in external mode.\n * @fires WaveformPlayer#waveformplayer:request-play\n */\n play() {\n if (this.options.singlePlay && WaveformPlayer.currentlyPlaying &&\n WaveformPlayer.currentlyPlaying !== this) {\n WaveformPlayer.currentlyPlaying.pause();\n }\n\n if (this.options.audioMode === 'external') {\n const evt = this._emit('waveformplayer:request-play', this._buildTrackDetail(), true);\n // If the controller cancels (preventDefault), don't claim\n // \"currentlyPlaying\" \u2014 the controller didn't accept the play.\n if (!evt.defaultPrevented) {\n WaveformPlayer.currentlyPlaying = this;\n }\n return undefined;\n }\n\n WaveformPlayer.currentlyPlaying = this;\n return this.audio.play();\n }\n\n /**\n * Pause audio.\n *\n * In `audioMode: 'external'`, dispatches `waveformplayer:request-pause`\n * (cancelable) and does NOT touch any audio element. See play().\n *\n * @fires WaveformPlayer#waveformplayer:request-pause\n */\n pause() {\n if (WaveformPlayer.currentlyPlaying === this) {\n WaveformPlayer.currentlyPlaying = null;\n }\n if (this.options.audioMode === 'external') {\n this._emit('waveformplayer:request-pause', this._buildTrackDetail(), true);\n return;\n }\n this.audio.pause();\n }\n\n /**\n * Build the track detail object dispatched by request-play /\n * request-pause events in external audio mode. Mirrors the shape\n * WaveformBar.play() accepts so a controller can forward it\n * directly: `WaveformBar.play(event.detail)`.\n *\n * @private\n * @return {{url:string,title:?string,artist:?string,artwork:?string,player:WaveformPlayer}}\n */\n _buildTrackDetail() {\n return {\n url: this.options.url,\n title: this.options.title,\n artist: this.options.artist,\n artwork: this.options.artwork,\n markers: this.options.markers,\n waveform: this.options.waveform,\n id: this.id,\n player: this\n };\n }\n\n /**\n * External-mode state pump: flip the play/pause visual state without\n * touching audio. Mirrors what onPlay()/onPause() do but skips the\n * audio-element interactions. Safe to call repeatedly \u2014 idempotent.\n *\n * Only dispatches `waveformplayer:play`/`waveformplayer:pause` (and runs\n * the matching callback) on an actual transition, starting/stopping the\n * smooth-update loop accordingly.\n *\n * @param {boolean} playing - True to enter the playing state, false to\n * enter the paused state.\n * @fires WaveformPlayer#waveformplayer:play\n * @fires WaveformPlayer#waveformplayer:pause\n */\n setPlayingState(playing) {\n const wasPlaying = this.isPlaying;\n this.isPlaying = !!playing;\n this.setPlayButtonState(this.isPlaying);\n if (this.isPlaying && !wasPlaying) {\n this.startSmoothUpdate?.();\n this._emit('waveformplayer:play', {player: this, url: this.options.url});\n if (this.options.onPlay) this.options.onPlay(this);\n } else if (!this.isPlaying && wasPlaying) {\n this.stopSmoothUpdate?.();\n this._emit('waveformplayer:pause', {player: this, url: this.options.url});\n if (this.options.onPause) this.options.onPause(this);\n }\n }\n\n /**\n * External-mode state pump: update the visualization's progress\n * from an external clock (e.g. WaveformBar's audio element's\n * timeupdate). Drives the canvas redraw + the time displays.\n *\n * Redraws the canvas, updates the current/total time displays, stores the\n * external duration for the accessible slider, dispatches\n * `waveformplayer:timeupdate`, runs `onTimeUpdate`, and synthesizes a\n * one-shot `waveformplayer:ended` (with `onEnd`) when progress reaches the\n * end. No-op for a non-positive duration.\n *\n * @param {number} currentTime - Current playback position in seconds.\n * @param {number} duration - Total track duration in seconds.\n * @fires WaveformPlayer#waveformplayer:timeupdate\n * @fires WaveformPlayer#waveformplayer:ended\n */\n setProgress(currentTime, duration) {\n if (!duration || duration <= 0) return;\n this.progress = clamp(currentTime / duration);\n // Mirror the existing display update code so callers don't have\n // to know which DOM elements live where.\n if (this.currentTimeEl) this.currentTimeEl.textContent = formatTime(currentTime);\n // Publish the duration unconditionally \u2014 the accessible seek slider\n // and keyboard seeking read getSeekDuration()/_extDuration even when\n // there's no time display to update.\n this._extDuration = duration;\n if (this.totalTimeEl && (!this.totalTimeEl.dataset._extSet || this.totalTimeEl.dataset._extDur !== String(duration))) {\n this.totalTimeEl.textContent = formatTime(duration);\n this.totalTimeEl.dataset._extSet = '1';\n this.totalTimeEl.dataset._extDur = String(duration);\n }\n this.drawWaveform?.();\n this.updateActiveMarker();\n this._emit('waveformplayer:timeupdate', {player: this, currentTime, duration, progress: this.progress, url: this.options.url});\n // Same (currentTime, duration, player) signature as self mode \u2014 the\n // arg order used to be swapped here, which made one shared handler\n // impossible across audioModes.\n if (this.options.onTimeUpdate) this.options.onTimeUpdate(currentTime, duration, this);\n\n // External mode has no <audio> 'ended' event \u2014 synthesize one when the\n // controller's progress reaches the end (fires once per playthrough).\n if (this.progress >= 1) {\n if (!this._extEnded) {\n this._extEnded = true;\n this._emit('waveformplayer:ended', {player: this, url: this.options.url, currentTime: duration, duration});\n if (this.options.onEnd) this.options.onEnd(this);\n }\n } else {\n this._extEnded = false;\n }\n\n this.updateSeekAccessibility();\n }\n\n /**\n * Toggle between play and pause based on the current `isPlaying` state.\n * Works in both audio modes (in external mode it routes through the\n * request-play/pause events).\n */\n togglePlay() {\n if (this.isPlaying) {\n this.pause();\n } else {\n this.play();\n }\n }\n\n /**\n * Seek the owned `<audio>` element to an absolute time, clamped to\n * `[0, duration]`, and refresh progress. Self mode only \u2014 a no-op when\n * there is no audio element or duration. External-mode keyboard/click\n * seeks go through {@link WaveformPlayer#seekToSeconds} instead.\n * @param {number} seconds - Target time in seconds.\n */\n seekTo(seconds) {\n if (this.audio && this.audio.duration) {\n this.audio.currentTime = clamp(seconds, 0, this.audio.duration);\n this.updateProgress();\n }\n }\n\n /**\n * Seek the owned `<audio>` element to a fraction of the track, clamped to\n * `[0, 1]`, and refresh progress. Self mode only \u2014 a no-op without an audio\n * element or duration.\n * @param {number} percent - Position as a fraction from 0 to 1.\n */\n seekToPercent(percent) {\n if (this.audio && this.audio.duration) {\n this.audio.currentTime = this.audio.duration * clamp(percent);\n this.updateProgress();\n }\n }\n\n /**\n * Set the owned `<audio>` element's volume, clamped to `[0, 1]`. Self mode\n * only \u2014 a no-op in external mode where the controller owns volume.\n * @param {number} volume - Volume from 0 (silent) to 1 (full).\n */\n setVolume(volume) {\n // Coerce + guard: a non-finite value (e.g. from a bad config or stale\n // storage) must not propagate NaN into audio.volume (which throws).\n const v = Number(volume);\n if (this.audio && Number.isFinite(v)) {\n this.audio.volume = clamp(v);\n }\n }\n\n /**\n * Set the owned `<audio>` element's playback rate (clamped to 0.5\u20132),\n * persist it onto `this.options.playbackRate`, and refresh the speed UI.\n * Self mode only \u2014 a no-op in external mode.\n * @param {number} rate - Desired playback rate; clamped to the 0.5\u20132 range.\n */\n setPlaybackRate(rate) {\n if (!this.audio) return;\n\n const clampedRate = clamp(rate, 0.5, 2);\n this.audio.playbackRate = clampedRate;\n this.options.playbackRate = clampedRate;\n\n this.updateSpeedUI();\n }\n\n /**\n * Tear down the player and release all resources.\n *\n * Flags destruction (so in-flight handlers bail), dispatches\n * `waveformplayer:destroy`, stops playback and the animation loop, aborts\n * every listener registered on the instance signal, disconnects the resize\n * observer, removes the window-resize handler, drops the instance from the\n * static map and `currentlyPlaying`, resets/releases the audio element, and\n * empties the container.\n * @fires WaveformPlayer#waveformplayer:destroy\n */\n destroy() {\n // Set a flag to indicate we're destroying\n this.isDestroying = true;\n\n // Let listeners (analytics, controllers) release their references\n // before teardown \u2014 the symmetric counterpart to waveformplayer:ready.\n this._emit('waveformplayer:destroy', {player: this, url: this.options.url});\n\n // Stop playback and animations\n this.pause();\n this.stopSmoothUpdate();\n clearTimeout(this._markerLabelTimer);\n\n // Tear down every document/container/seek listener in one shot.\n this._ac?.abort();\n\n // Disconnect observer\n if (this.resizeObserver) {\n this.resizeObserver.disconnect();\n this.resizeObserver = null;\n }\n\n // Remove window resize listener\n if (this.resizeHandler) {\n window.removeEventListener('resize', this.resizeHandler);\n this.resizeHandler = null;\n }\n\n // Remove from instances map\n WaveformPlayer.instances.delete(this.id);\n\n // Clear current playing reference if it's this instance\n if (WaveformPlayer.currentlyPlaying === this) {\n WaveformPlayer.currentlyPlaying = null;\n }\n\n // Properly clean up audio element\n if (this.audio) {\n this.audio.pause();\n this.audio.src = '';\n this.audio.load(); // Reset the audio element\n this.audio = null;\n }\n\n // Clear the container\n this.container.innerHTML = '';\n\n // Clear all references\n this.canvas = null;\n this.ctx = null;\n this.playBtn = null;\n this.waveformData = [];\n }\n\n // ============================================\n // Static Methods\n // ============================================\n\n /**\n * Get player instance by ID, element, or element ID\n * @param {string|HTMLElement} idOrElement - Player ID, element, or element ID\n * @returns {WaveformPlayer|undefined}\n */\n static getInstance(idOrElement) {\n if (typeof idOrElement === 'string') {\n const instance = this.instances.get(idOrElement);\n if (instance) return instance;\n\n const element = document.getElementById(idOrElement);\n if (element) {\n return Array.from(this.instances.values()).find(p => p.container === element);\n }\n }\n\n if (idOrElement instanceof HTMLElement) {\n return Array.from(this.instances.values()).find(p => p.container === idOrElement);\n }\n\n return undefined;\n }\n\n /**\n * Get all player instances\n * @returns {WaveformPlayer[]}\n */\n static getAllInstances() {\n return Array.from(this.instances.values());\n }\n\n /**\n * Destroy all player instances\n */\n static destroyAll() {\n this.instances.forEach(player => player.destroy());\n this.instances.clear();\n }\n\n /**\n * Generate waveform data from audio URL\n * @static\n * @param {string} url - Audio URL\n * @param {number} samples - Number of samples\n * @returns {Promise<number[]>} Waveform peak data\n */\n static async generateWaveformData(url, samples = DEFAULT_SAMPLES) {\n try {\n const result = await generateWaveform(url, samples);\n return result.peaks;\n } catch (error) {\n console.error('[WaveformPlayer] Failed to generate waveform:', error);\n throw error;\n }\n }\n\n /**\n * Derive a peaks-JSON URL from an audio URL by swapping the\n * extension. Strict counterpart to `generateWaveformData()`:\n * `generateWaveformData` decodes the audio at runtime,\n * `getPeaksUrl` assumes you generated the peaks at build time\n * (e.g. with `@arraypress/waveform-gen`) and stored the JSON\n * alongside the audio file.\n *\n * Use the result as the `waveform` option \u2014 the player detects\n * the `.json` suffix, `fetch()`es the file, and skips the Web\n * Audio decode pass entirely. Big perf win on catalogues with\n * many tracks (saves ~1-5s decode per file on slow connections).\n *\n * Recognised extensions: mp3, wav, ogg, flac, m4a, aac.\n * Preserves query strings + URL fragments. Returns `undefined`\n * for unrecognised inputs so callers can pass through\n * unconditionally:\n *\n * new WaveformPlayer('#el', {\n * url: track.audioUrl,\n * waveform: WaveformPlayer.getPeaksUrl(track.audioUrl),\n * });\n *\n * @static\n * @param {string|undefined|null} audioUrl - Audio file URL.\n * @returns {string|undefined} Peaks JSON URL, or `undefined`\n * when the input is empty / has no recognised audio extension.\n *\n * @example\n * WaveformPlayer.getPeaksUrl('/audio/track.mp3')\n * // '/audio/track.json'\n *\n * WaveformPlayer.getPeaksUrl('/audio/track.wav?v=2')\n * // '/audio/track.json?v=2'\n *\n * WaveformPlayer.getPeaksUrl(undefined)\n * // undefined\n */\n static getPeaksUrl(audioUrl) {\n if (!audioUrl) return undefined;\n const swapped = audioUrl.replace(\n /\\.(mp3|wav|ogg|flac|m4a|aac)(\\?[^#]*)?(#.*)?$/i,\n '.json$2$3'\n );\n /* Nothing changed \u2192 unrecognised extension, return undefined\n * so callers know to fall back to live decoding. */\n return swapped === audioUrl ? undefined : swapped;\n }\n\n}\n", "/**\n * @module index\n * @description Public entry point for the WaveformPlayer library.\n *\n * Wires together the runtime surfaces for the player: it re-exports the\n * {@link WaveformPlayer} class (default and named), exposes a static\n * `WaveformPlayer.init` hook, scans the DOM for declarative `[data-waveform-player]`\n * markup and auto-instantiates a player for each match, and attaches the class\n * to `window` for plain `<script>`/CDN usage. Loading this module is enough to\n * make any markup-driven players on the page come alive once the DOM is ready.\n */\n\n// Import the main class\nimport {WaveformPlayer} from './core.js';\nimport {formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes} from './utils.js';\nimport {detectColorScheme} from './themes.js';\n\n// Expose a small set of pure helpers as a single source of truth so consumers\n// (e.g. @arraypress/waveform-bar, @arraypress/waveform-playlist) can reuse them\n// instead of shipping divergent copies. `parseDataAttributes` lets wrappers read\n// the player's full `data-*` option surface off a host element without\n// re-implementing (and drifting from) the contract. `detectColorScheme` is here\n// for the same reason: the bar used to carry its own copy of the heuristic and\n// the two drifted (both shipped the transparent-background bug of #21, fixed\n// independently). Attached to the class so it's reachable from the IIFE global\n// too \u2014 which is how the bar reaches it, since it depends on this package as a\n// runtime peer rather than importing the module.\nWaveformPlayer.utils = {formatTime, extractTitleFromUrl, escapeHtml, isSafeHref, parseDataAttributes, detectColorScheme};\n\n/**\n * Whether we're running in a browser (vs. SSR / Node), where `window` and\n * `document` are available. Guards the auto-init and global-attach steps.\n * @returns {boolean}\n */\nconst isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined';\n\n/**\n * Scan the document for declarative player markup and instantiate one\n * {@link WaveformPlayer} per matching element.\n *\n * Finds every element carrying the `data-waveform-player` attribute and, for\n * each one not already initialized, constructs a player from it (the constructor\n * reads the element's `data-*` attributes for configuration). Each successfully\n * initialized element is flagged with `data-waveform-initialized=\"true\"` so\n * repeat calls are idempotent and never double-initialize the same element.\n * Construction errors are caught and logged so one broken element cannot abort\n * the rest of the scan. A no-op in non-DOM environments (e.g. SSR).\n *\n * @returns {void}\n */\nfunction autoInit() {\n if (!isBrowser()) return;\n\n const elements = document.querySelectorAll('[data-waveform-player]');\n\n elements.forEach(element => {\n if (element.dataset.waveformInitialized === 'true') return;\n\n try {\n new WaveformPlayer(element);\n element.dataset.waveformInitialized = 'true';\n } catch (error) {\n console.error('[WaveformPlayer] Failed to initialize:', error, element);\n }\n });\n}\n\n// Initialize when DOM is ready: defer until DOMContentLoaded if the document is\n// still parsing, otherwise run the scan immediately on import.\nif (isBrowser()) {\n if (document.readyState === 'loading') {\n document.addEventListener('DOMContentLoaded', autoInit);\n } else {\n autoInit();\n }\n}\n\n/**\n * Static re-scan hook.\n *\n * Exposes {@link autoInit} as `WaveformPlayer.init` so callers can manually\n * (re-)scan the DOM after dynamically injecting `[data-waveform-player]` markup.\n * Already-initialized elements are skipped on subsequent calls.\n *\n * @type {typeof autoInit}\n */\nWaveformPlayer.init = autoInit;\n\n// For CDN/browser usage: expose the class as a global so it is reachable from a\n// plain <script> tag without an ES module loader.\nif (isBrowser()) {\n window.WaveformPlayer = WaveformPlayer;\n}\n\n/**\n * The {@link WaveformPlayer} class.\n * @type {typeof WaveformPlayer}\n */\nexport default WaveformPlayer;\n\n// Named exports\nexport {WaveformPlayer};"],
|
|
5
|
+
"mappings": "AAqBO,SAASA,EAAMC,EAAQ,CAC1B,IAAIC,EAAM,KACV,QAAS,EAAI,EAAG,EAAID,EAAO,OAAQ,IAC3BA,EAAO,CAAC,EAAIC,IAAKA,EAAMD,EAAO,CAAC,GAEvC,OAAOC,CACX,CAQO,SA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BD,EAAU,IACV,OAAO,eAAiBP,GAO5B,IAAOW,GAAQX",
|
|
6
|
+
"names": ["maxOf", "values", "max", "escapeHtml", "str", "formatCssLength", "value", "isSafeHref", "url", "u", "clamp", "min", "parseBoolAttr", "parseColorValue", "parseDataAttributes", "element", "options", "setBool", "optKey", "dataKey", "v", "setNum", "float", "raw", "setLength", "setJson", "e", "formatSeekValueText", "template", "args", "sequentialIndex", "match", "position", "index", "formatTime", "seconds", "total", "hrs", "mins", "secs", "idCounter", "generateId", "hash", "i", "extractTitleFromUrl", "parts", "l", "parseColor", "color", "m", "r", "g", "b", "perceivedBrightness", "c", "mergeOptions", "sources", "result", "source", "key", "debounce", "func", "wait", "timeout", "later", "resampleData", "data", "targetLength", "ratio", "lower", "upper", "fraction", "bucketSize", "start", "end", "count", "j", "nearestIndex", "makeFill", "ctx", "value", "canvas", "options", "w", "h", "dir", "x0", "y0", "x1", "y1", "grad", "c", "i", "fillBar", "x", "y", "radii", "r", "max", "clampR", "clamp", "barRadiusPx", "dpr", "barRadii", "capsulePath", "startX", "endX", "centerY", "barHeight", "drawBars", "peaks", "progress", "barWidth", "barSpacing", "barCount", "resampledPeaks", "resampleData", "height", "progressWidth", "baseFill", "progFill", "peakHeight", "drawMirror", "topRadii", "botRadii", "drawLine", "width", "amplitude", "drawCurve", "color", "lineWidth", "endProgress", "addGlow", "stroke", "solid", "points", "samples", "peakValue", "waveOffset", "cp1x", "cp1y", "cp2x", "cp2y", "drawBlocks", "blockSize", "blockGap", "blockCount", "j", "blockOffset", "drawDots", "dotRadius", "drawSeekbar", "borderRadius", "active", "DRAWING_STYLES", "draw", "detectBPM", "buffer", "channelData", "sampleRate", "onsets", "detectOnsets", "intervals", "i", "tempoGroups", "interval", "tempo", "bucket", "maxCount", "detectedBPM", "count", "e", "previousEnergy", "energy", "j", "energyDiff", "threshold", "lastOnset", "minDistance", "extractPeaks", "buffer", "samples", "sampleSize", "channels", "peaks", "c", "chan", "i", "start", "end", "min", "max", "j", "value", "peak", "maxPeak", "maxOf", "generateWaveform", "url", "shouldDetectBPM", "audioContext", "AudioCtx", "arrayBuffer", "audioBuffer", "normalizePeaks", "bpm", "detectBPM", "generatePlaceholderWaveform", "data", "base", "variation", "clamp", "targetMax", "scaleFactor", "LIGHT_THRESHOLD", "hasThemeHint", "scheme", "root", "body", "collectBackdrop", "el", "sum", "alpha", "node", "c", "parseColor", "weight", "detectCanvasScheme", "text", "perceivedBrightness", "detectColorScheme", "start", "canvas", "brightness", "COLOR_PRESETS", "getColorPreset", "presetName", "detected", "DEFAULT_OPTIONS", "STYLE_DEFAULTS", "ARTWORK_FALLBACK", "SEEK_STEP_SECONDS", "SEEK_PAGE_SECONDS", "INTERACTIVE_ELEMENTS", "WaveformPlayer", "_WaveformPlayer", "container", "options", "dataOptions", "parseDataAttributes", "userOptions", "mergeOptions", "DEFAULT_OPTIONS", "preset", "getColorPreset", "COLOR_PRESETS", "detectColorScheme", "key", "value", "styleDefaults", "STYLE_DEFAULTS", "generateId", "type", "detail", "cancelable", "event", "percent", "error", "buttonAlign", "buttonVars", "formatCssLength", "buttonStyleAttr", "artworkOnButton", "buttonArtworkHTML", "escapeHtml", "buttonHTML", "infoHTML", "rate", "img", "span", "artist", "titleEl", "artwork", "artworkAlt", "textEl", "speedBtn", "speedMenu", "isOpen", "setSpeedMenu", "open", "opts", "o", "focusOption", "i", "selectOption", "opt", "e", "player", "hasAudio", "currentTime", "actions", "clamp", "duration", "current", "target", "seconds", "clamped", "title", "label", "formatSeekValueText", "formatTime", "details", "onNext", "onPrev", "state", "d", "rect", "endDrag", "debounce", "url", "resolve", "reject", "metadataHandler", "errorHandler", "extractTitleFromUrl", "result", "generateWaveform", "generatePlaceholderWaveform", "hasArtworkOption", "hasArtworkAltOption", "data", "r", "json", "parsed", "draw", "dpr", "marker", "index", "position", "markerEl", "tooltip", "el", "els", "dur", "t", "active", "best", "mt", "tip", "clientX", "pct", "h", "targetPercent", "loading", "isPlaying", "playIcon", "pauseIcon", "update", "newProgress", "bpm", "refresh", "p", "obs", "mq", "speedValue", "btn", "on", "playing", "wasPlaying", "volume", "v", "clampedRate", "idOrElement", "instance", "element", "samples", "audioUrl", "swapped", "WaveformPlayer", "formatTime", "extractTitleFromUrl", "escapeHtml", "isSafeHref", "parseDataAttributes", "detectColorScheme", "isBrowser", "autoInit", "element", "error", "index_default"]
|
|
7
7
|
}
|