/** * @module core * @description Main WaveformPlayer class */ import type {WaveformPeaks, WaveformPlayerInstanceEventMap, WaveformPlayerOptions, WaveformPlayerUtils, WaveformTrackDetail} from './types.js'; import {draw} from './drawing.js'; import {generateWaveform, generatePlaceholderWaveform} from './audio.js'; import { formatTime, formatSeekValueText, extractTitleFromUrl, generateId, parseDataAttributes, mergeOptions, debounce, clamp, escapeHtml, isSafeHref, fileNameFromUrl, formatCssLength, toFiniteNumber, toPeaks, DEFAULT_SAMPLES, inTimeRanges, coversDuration, } from './utils.js'; import { DEFAULT_OPTIONS, STYLE_DEFAULTS, getColorPreset, COLOR_PRESETS, detectColorScheme, normalizeOptions, normalizeMarkers, BUTTON_ALIGNMENTS, PLAYBACK_RATE_MIN, PLAYBACK_RATE_MAX } from './themes.js'; /** * The classes the player puts on its mount container, and the only ones it * may take off. createDOM() swaps these and leaves every other class alone, * so a page or framework that styles the mount node (`class="my-theme"`, a * Svelte/Vue scoped class, a CSS module) keeps its styling (#32). Listed * explicitly rather than as a `waveform-` prefix so a page's own * `waveform-something` class is never touched either. Add to this list when * the player starts toggling a new class on the container. */ const CONTAINER_CLASSES = [ 'waveform-player', 'waveform-layout-preview', 'waveform-theme-light', 'waveform-is-placeholder', ]; /** * Placeholder shown when an artwork URL fails to load (404 / broken) — a muted * music-note tile, so a missing cover degrades gracefully instead of showing the * browser's broken-image icon. Inline SVG data-URI (no font / network needed). */ const ARTWORK_FALLBACK = 'data:image/svg+xml,' + encodeURIComponent( '' ); // Icons for the optional meta-row controls (decorative: each control is named // by its aria-label). Material Symbols paths, 24px grid, drawn in currentColor. const svgIcon = (d: string) => ``; const LOOP_ICON = svgIcon('M7 7h10v3l4-4-4-4v3H5v6h2V7zm10 10H7v-3l-4 4 4 4v-3h12v-6h-2v4z'); const DOWNLOAD_ICON = svgIcon('M5 20h14v-2H5v2zM19 9h-4V3H9v6H5l7 7 7-7z'); const SPEAKER = 'M3 9v6h4l5 5V4L7 9H3z'; const VOLUME_ICON = svgIcon(SPEAKER + 'm13.5 3A4.5 4.5 0 0 0 14 7.97v8.05c1.48-.73 2.5-2.25 2.5-4.02z'); const MUTED_ICON = svgIcon(SPEAKER + 'm18 .4L19.6 8 17 10.6 14.4 8 13 9.4l2.6 2.6-2.6 2.6 1.4 1.4 2.6-2.6 2.6 2.6 1.4-1.4-2.6-2.6z'); /** * Whether this platform honours a volume set from script. iOS doesn't — the * hardware buttons own volume and `audio.volume` reads back unchanged — so a * slider there would move without changing anything. * @returns */ function volumeIsScriptable(): boolean { const probe = new Audio(); probe.volume = 0.5; return probe.volume === 0.5; } // Keyboard seek steps (seconds) for the accessible slider. const SEEK_STEP_SECONDS = 5; const SEEK_PAGE_SECONDS = 10; // Controls that own their own focus. A click landing on one of these (or a // descendant) must leave focus on the control, not pull it onto the player // container — otherwise activating the play button/slider steals focus onto the // wrapper, moving it off the control the user just operated. const INTERACTIVE_ELEMENTS = 'button, a[href], input, [role="slider"]'; /** * Accessible name for the speed button: the visible rate first, then the * `speedLabel` text — "1.5x Playback speed". A fixed label would hide the rate * the button shows (WCAG 2.5.3 Label in Name, axe `label-content-name-mismatch`). * @param rate - Current playback rate. * @param label - The `speedLabel` option. * @returns */ function speedButtonLabel(rate: number, label?: string): string { return label ? `${rate}x ${label}` : `${rate}x`; } /** * Typed listeners for the events a player dispatches on itself (see * {@link WaveformPlayerInstanceEventMap}). Types only: merged into the class * below, it emits nothing. */ export interface WaveformPlayer { addEventListener(type: K, listener: (this: WaveformPlayer, ev: WaveformPlayerInstanceEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void; removeEventListener(type: K, listener: (this: WaveformPlayer, ev: WaveformPlayerInstanceEventMap[K]) => any, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void; } /** * WaveformPlayer - Modern audio player with waveform visualization. * * An `EventTarget`: lifecycle events (`play`, `pause`, `ended`, `load`, …) * are dispatched on the instance as plain events, and on the container as * bubbling, composed `waveformplayer:*` CustomEvents carrying the details. * @class */ // biome-ignore lint/suspicious/noUnsafeDeclarationMerging: see the interface above export class WaveformPlayer extends EventTarget { static readonly instances: Map = new Map(); static currentlyPlaying: WaveformPlayer | null = null; /** Audio URLs already warned about lacking byte-range support. @internal */ static _noRangeWarned: Set = new Set(); /** Shared helpers, attached by entry.ts. */ declare static readonly utils: WaveformPlayerUtils; /** Initialize every `[data-waveform-player]` in `root` (or the document). Attached by entry.ts. */ declare static init: (root?: Document | Element) => void; /** * Register the `` element (default tag name `waveform-player`). * The default entry does this on import; this reaches it from a `