@arraypress/waveform-player 1.14.0 → 1.16.0

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.
@@ -568,7 +568,6 @@ function detectOnsets(channelData, sampleRate) {
568
568
  // src/js/audio.js
569
569
  function extractPeaks(buffer, samples = 200) {
570
570
  const sampleSize = buffer.length / samples;
571
- const sampleStep = ~~(sampleSize / 10) || 1;
572
571
  const channels = buffer.numberOfChannels;
573
572
  const peaks = [];
574
573
  for (let c = 0; c < channels; c++) {
@@ -578,7 +577,7 @@ function extractPeaks(buffer, samples = 200) {
578
577
  const end = ~~(start + sampleSize);
579
578
  let min = 0;
580
579
  let max = 0;
581
- for (let j = start; j < end; j += sampleStep) {
580
+ for (let j = start; j < end; j++) {
582
581
  const value = chan[j];
583
582
  if (value > max) max = value;
584
583
  if (value < min) min = value;
@@ -689,10 +688,12 @@ var DEFAULT_OPTIONS = {
689
688
  // Core settings
690
689
  url: "",
691
690
  height: 64,
692
- // Source peak resolution. The drawer resamples these to fit
693
- // canvasWidth / (barWidth + barSpacing) bars, so this is fidelity headroom,
694
- // not the visible bar count.
695
- samples: 256,
691
+ // Source peak resolution for LIVE decode (ignored when peaks are supplied).
692
+ // The drawer resamples these to fit canvasWidth / (barWidth + barSpacing)
693
+ // bars, so this is fidelity headroom, not the visible bar count. 1800 (the
694
+ // SoundCloud/WaveformGen figure) keeps wide / high-DPI waveforms crisp; the
695
+ // every-frame scan means a higher value costs no extra extraction time.
696
+ samples: 1800,
696
697
  preload: "metadata",
697
698
  // Audio mode — 'self' = player owns the <audio> element (default, current
698
699
  // behavior). 'external' = player is a visualization-only surface; no audio
@@ -787,6 +788,9 @@ var STYLE_DEFAULTS = {
787
788
  };
788
789
 
789
790
  // src/js/core.js
791
+ var ARTWORK_FALLBACK = "data:image/svg+xml," + encodeURIComponent(
792
+ '<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>'
793
+ );
790
794
  var SEEK_STEP_SECONDS = 5;
791
795
  var SEEK_PAGE_SECONDS = 10;
792
796
  var WaveformPlayer = class _WaveformPlayer {
@@ -844,6 +848,7 @@ var WaveformPlayer = class _WaveformPlayer {
844
848
  this.ctx = null;
845
849
  this.waveformData = [];
846
850
  this.progress = 0;
851
+ this._activeMarkerIndex = -1;
847
852
  this.isPlaying = false;
848
853
  this.isLoading = false;
849
854
  this.hasError = false;
@@ -1026,6 +1031,11 @@ var WaveformPlayer = class _WaveformPlayer {
1026
1031
  this.titleEl = this.container.querySelector(".waveform-title");
1027
1032
  this.subtitleEl = this.container.querySelector(".waveform-subtitle");
1028
1033
  this.artworkEl = this.container.querySelector(".waveform-artwork");
1034
+ if (this.artworkEl) {
1035
+ this.artworkEl.addEventListener("error", () => {
1036
+ if (!this.artworkEl.src.startsWith("data:")) this.artworkEl.src = ARTWORK_FALLBACK;
1037
+ });
1038
+ }
1029
1039
  this.currentTimeEl = this.container.querySelector(".time-current");
1030
1040
  this.totalTimeEl = this.container.querySelector(".time-total");
1031
1041
  this.bpmEl = this.container.querySelector(".waveform-bpm");
@@ -1328,6 +1338,7 @@ var WaveformPlayer = class _WaveformPlayer {
1328
1338
  this.audio.addEventListener("error", (e) => this.onError(e));
1329
1339
  }
1330
1340
  this.canvas.addEventListener("click", (e) => this.handleCanvasClick(e));
1341
+ this.setupHoverTime();
1331
1342
  this.resizeHandler = debounce(() => this.resizeCanvas(), 100);
1332
1343
  window.addEventListener("resize", this.resizeHandler);
1333
1344
  }
@@ -1571,6 +1582,7 @@ var WaveformPlayer = class _WaveformPlayer {
1571
1582
  renderMarkers() {
1572
1583
  if (!this.markersContainer) return;
1573
1584
  this.markersContainer.innerHTML = "";
1585
+ this._activeMarkerIndex = -1;
1574
1586
  if (!this.options.showMarkers || !this.options.markers?.length) return;
1575
1587
  const duration = this.getSeekDuration();
1576
1588
  if (!duration) {
@@ -1614,6 +1626,64 @@ var WaveformPlayer = class _WaveformPlayer {
1614
1626
  const markers = this.markersContainer.querySelectorAll(".waveform-marker");
1615
1627
  markers.forEach((el, i) => el.classList.toggle("active", i === index));
1616
1628
  }
1629
+ /**
1630
+ * Highlight the marker the playhead has most recently passed — the rendered
1631
+ * marker with the greatest `time` ≤ the current time — and reveal its label.
1632
+ * Driven from the progress loop; a no-op when there are no markers. Reuses
1633
+ * {@link WaveformPlayer#setActiveMarker}, and only re-applies when the active
1634
+ * marker actually changes.
1635
+ * @private
1636
+ */
1637
+ updateActiveMarker() {
1638
+ if (!this.markersContainer) return;
1639
+ const els = this.markersContainer.querySelectorAll(".waveform-marker");
1640
+ if (!els.length) return;
1641
+ const dur = this.getSeekDuration();
1642
+ const t = dur ? this.progress * dur : 0;
1643
+ let active = -1;
1644
+ let best = -Infinity;
1645
+ els.forEach((el, i) => {
1646
+ const mt = parseFloat(el.getAttribute("data-time"));
1647
+ if (Number.isFinite(mt) && mt <= t + 0.05 && mt > best) {
1648
+ best = mt;
1649
+ active = i;
1650
+ }
1651
+ });
1652
+ if (active !== this._activeMarkerIndex) {
1653
+ this._activeMarkerIndex = active;
1654
+ this.setActiveMarker(active);
1655
+ }
1656
+ }
1657
+ /**
1658
+ * Build a tooltip that follows the pointer over the waveform and shows the
1659
+ * time at that position. Enabled by the `showHoverTime` option; works in both
1660
+ * self and external modes (it only needs {@link WaveformPlayer#getSeekDuration}).
1661
+ * Uses the same pointer-X → percentage math as seeking.
1662
+ * @private
1663
+ */
1664
+ setupHoverTime() {
1665
+ if (!this.options.showHoverTime || !this.seekEl) return;
1666
+ const tip = document.createElement("div");
1667
+ tip.className = "waveform-hover-time";
1668
+ tip.setAttribute("aria-hidden", "true");
1669
+ this.seekEl.appendChild(tip);
1670
+ this.hoverTimeEl = tip;
1671
+ this.seekEl.addEventListener("pointermove", (e) => {
1672
+ const dur = this.getSeekDuration();
1673
+ if (!dur) {
1674
+ tip.style.opacity = "0";
1675
+ return;
1676
+ }
1677
+ const rect = this.canvas.getBoundingClientRect();
1678
+ const pct = clamp((e.clientX - rect.left) / rect.width);
1679
+ tip.textContent = formatTime(pct * dur);
1680
+ tip.style.left = pct * 100 + "%";
1681
+ tip.style.opacity = "1";
1682
+ });
1683
+ this.seekEl.addEventListener("pointerleave", () => {
1684
+ tip.style.opacity = "0";
1685
+ });
1686
+ }
1617
1687
  // ============================================
1618
1688
  // Event Handlers
1619
1689
  // ============================================
@@ -1832,6 +1902,7 @@ var WaveformPlayer = class _WaveformPlayer {
1832
1902
  if (this.options.onTimeUpdate) {
1833
1903
  this.options.onTimeUpdate(this.audio.currentTime, this.audio.duration, this);
1834
1904
  }
1905
+ this.updateActiveMarker();
1835
1906
  this.updateSeekAccessibility();
1836
1907
  }
1837
1908
  // ============================================
@@ -2068,6 +2139,7 @@ var WaveformPlayer = class _WaveformPlayer {
2068
2139
  this.totalTimeEl.dataset._extDur = String(duration);
2069
2140
  }
2070
2141
  this.drawWaveform?.();
2142
+ this.updateActiveMarker();
2071
2143
  this._emit("waveformplayer:timeupdate", { player: this, currentTime, duration, progress: this.progress, url: this.options.url });
2072
2144
  if (this.options.onTimeUpdate) this.options.onTimeUpdate(currentTime, duration, this);
2073
2145
  if (this.progress >= 1) {