@arraypress/waveform-sounds 0.2.0 → 0.3.1

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.
@@ -98,7 +98,7 @@ function formatDuration(seconds) {
98
98
  const s = Number(seconds);
99
99
  if (!Number.isFinite(s) || s < 0) return "";
100
100
  if (s > 0 && s < 0.95) return `${Math.max(0.1, Math.round(s * 10) / 10).toFixed(1)}s`;
101
- const total = Math.round(s);
101
+ const total = s < 1 ? Math.ceil(s) : Math.floor(s);
102
102
  return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}`;
103
103
  }
104
104
  function parseDuration(value) {
@@ -338,7 +338,7 @@ function partsOf(menu) {
338
338
  value: menu.querySelector("[data-ws-menu-value]")
339
339
  };
340
340
  }
341
- var focusOwner = (p) => p.search || p.list;
341
+ var focusOwner = (p) => p.search || p.list || p.pop.querySelector("input") || p.pop.querySelector("button");
342
342
  var optionsOf = (p, visibleOnly = false) => [...p.list.querySelectorAll('[role="option"]')].filter((o) => !visibleOnly || !o.hidden);
343
343
  var Menus = class {
344
344
  /**
@@ -370,6 +370,15 @@ var Menus = class {
370
370
  this.open(menu);
371
371
  }
372
372
  }, sig);
373
+ if (!p.list) {
374
+ p.pop.addEventListener("keydown", (e) => {
375
+ if (e.key !== "Escape") return;
376
+ e.preventDefault();
377
+ e.stopPropagation();
378
+ this.close(true);
379
+ }, sig);
380
+ continue;
381
+ }
373
382
  p.list.addEventListener("click", (e) => {
374
383
  const opt = e.target.closest('[role="option"]');
375
384
  if (opt) this._pick(name, opt.dataset.value);
@@ -404,8 +413,8 @@ var Menus = class {
404
413
  }
405
414
  menu.classList.remove("ws-menu--end");
406
415
  if (p.pop.getBoundingClientRect().right > this.container.getBoundingClientRect().right + 1) menu.classList.add("ws-menu--end");
407
- this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
408
- focusOwner(p).focus();
416
+ if (p.list) this._activate(menu, p.list.querySelector('[role="option"][aria-selected="true"]'));
417
+ focusOwner(p)?.focus();
409
418
  }
410
419
  /**
411
420
  * Close the open menu, if any.
@@ -433,6 +442,7 @@ var Menus = class {
433
442
  const menu = this.menus[name];
434
443
  if (!menu) return;
435
444
  const p = partsOf(menu);
445
+ if (!p.list) return;
436
446
  let label = null;
437
447
  for (const opt of optionsOf(p)) {
438
448
  const on = opt.dataset.value === String(value ?? "");
@@ -570,6 +580,31 @@ function limitToReveal(limit, pos) {
570
580
  return pos >= limit ? pos + 1 : limit;
571
581
  }
572
582
 
583
+ // src/js/data/bpm.js
584
+ function bpmFilterFromRange(lo, hi, extent) {
585
+ const a = Math.min(lo, hi), b = Math.max(lo, hi);
586
+ return {
587
+ bpmMin: a > extent.min ? String(a) : "",
588
+ bpmMax: b < extent.max ? String(b) : ""
589
+ };
590
+ }
591
+ function bpmRangeFromFilter(filter, extent) {
592
+ const read = (v, fallback) => {
593
+ const n = Number(v);
594
+ return v === "" || v == null || !Number.isFinite(n) ? fallback : Math.min(Math.max(n, extent.min), extent.max);
595
+ };
596
+ const lo = read(filter.bpmMin, extent.min);
597
+ const hi = read(filter.bpmMax, extent.max);
598
+ return lo <= hi ? { lo, hi } : { lo: hi, hi: lo };
599
+ }
600
+ function bpmFraction(value, extent) {
601
+ const span = extent.max - extent.min;
602
+ return span > 0 ? Math.min(Math.max((value - extent.min) / span, 0), 1) : 0;
603
+ }
604
+ function hasBpmFilter(filter) {
605
+ return filter.bpmMin != null && filter.bpmMin !== "" || filter.bpmMax != null && filter.bpmMax !== "";
606
+ }
607
+
573
608
  // src/js/render/strings.js
574
609
  var DEFAULT_STRINGS = {
575
610
  search: "Search sounds",
@@ -585,6 +620,8 @@ var DEFAULT_STRINGS = {
585
620
  bpm: "BPM",
586
621
  bpmMin: "Min BPM",
587
622
  bpmMax: "Max BPM",
623
+ anyBpm: "Any BPM",
624
+ bpmRange: "{min}\u2013{max} BPM",
588
625
  sort: "Sort",
589
626
  sortBy: "Sort by",
590
627
  sortDefault: "Default",
@@ -619,6 +656,11 @@ var SORT_LABEL_KEYS = {
619
656
  function fill(template, vars = {}) {
620
657
  return String(template).replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
621
658
  }
659
+ function bpmLabel(filter, extent, strings = DEFAULT_STRINGS) {
660
+ if (!hasBpmFilter(filter)) return strings.anyBpm;
661
+ const { lo, hi } = bpmRangeFromFilter(filter, extent);
662
+ return fill(strings.bpmRange, { min: lo, max: hi });
663
+ }
622
664
  function countText(shown, total, strings = DEFAULT_STRINGS) {
623
665
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
624
666
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
@@ -676,7 +718,7 @@ var DEFAULT_OPTIONS = {
676
718
  progressColor: null,
677
719
  barWidth: 2,
678
720
  barGap: 1,
679
- loop: false,
721
+ loop: null,
680
722
  autoAdvance: false,
681
723
  arrowAudition: true,
682
724
  idPrefix: null,
@@ -809,6 +851,9 @@ function toolbarPlan(f, o) {
809
851
  count: !!o.showCount
810
852
  };
811
853
  }
854
+ function loopsByDefault(loop, f) {
855
+ return loop == null ? f.loops > 0 : !!loop;
856
+ }
812
857
  function renderSearch(s) {
813
858
  return h(
814
859
  "label",
@@ -862,15 +907,45 @@ function renderMenu(name, m) {
862
907
  )
863
908
  );
864
909
  }
865
- function renderBpmRange(s, range) {
866
- const field = (attr, label, placeholder) => h("input", { type: "number", inputmode: "numeric", [attr]: true, "aria-label": label, placeholder, min: "0", step: "1" });
910
+ function renderBpmMenu({ id, s, range }) {
911
+ const popId = `${id}-bpm-pop`;
912
+ const handle = (attr, label, value) => h("input", {
913
+ type: "range",
914
+ class: "ws-range-input",
915
+ [attr]: true,
916
+ "aria-label": label,
917
+ min: range.min,
918
+ max: range.max,
919
+ step: "1",
920
+ value
921
+ });
867
922
  return h(
868
- "span",
869
- { class: "ws-bpm-range", role: "group", "aria-label": s.bpm },
870
- field("data-ws-bpm-min", s.bpmMin, range.min),
871
- h("span", { "aria-hidden": "true" }, "\u2013"),
872
- field("data-ws-bpm-max", s.bpmMax, range.max),
873
- h("span", { class: "ws-bpm-unit", "aria-hidden": "true" }, text(s.bpm))
923
+ "div",
924
+ { class: "ws-menu ws-menu--bpm", "data-ws-menu": "bpm" },
925
+ h(
926
+ "button",
927
+ { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
928
+ h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
929
+ h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
930
+ ICONS.chevron
931
+ ),
932
+ h(
933
+ "div",
934
+ { class: "ws-menu-pop ws-bpm-pop", id: popId, role: "dialog", "aria-label": s.bpm, "data-ws-menu-pop": true, hidden: true },
935
+ h(
936
+ "div",
937
+ { class: "ws-bpm-head" },
938
+ h("span", { class: "ws-bpm-readout", "data-ws-bpm-readout": true, "aria-live": "polite" }, text(fill(s.bpmRange, range))),
939
+ h("button", { type: "button", class: "ws-bpm-clear", "data-ws-bpm-clear": true }, text(s.anyBpm))
940
+ ),
941
+ h(
942
+ "div",
943
+ { class: "ws-range", "data-ws-bpm-range": true },
944
+ handle("data-ws-bpm-min", s.bpmMin, range.min),
945
+ handle("data-ws-bpm-max", s.bpmMax, range.max)
946
+ ),
947
+ h("div", { class: "ws-range-ends", "aria-hidden": "true" }, h("span", {}, text(range.min)), h("span", {}, text(range.max)))
948
+ )
874
949
  );
875
950
  }
876
951
  function renderLoopFilter(s) {
@@ -887,8 +962,8 @@ function renderLoopFilter(s) {
887
962
  button("one-shot", s.oneShots)
888
963
  );
889
964
  }
890
- function renderLoopToggle(s) {
891
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": "false" }, ICONS.loop, h("span", {}, text(s.loop)));
965
+ function renderLoopToggle(s, pressed) {
966
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
892
967
  }
893
968
  function renderChip(value, label, count, pressed) {
894
969
  return h(
@@ -914,7 +989,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
914
989
  placeholder: s.findKey,
915
990
  options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
916
991
  }),
917
- plan.bpm && renderBpmRange(s, f.bpm),
992
+ plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
918
993
  plan.loops && renderLoopFilter(s),
919
994
  plan.sorts.length > 0 && menu("sort", {
920
995
  label: s.sort,
@@ -922,7 +997,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
922
997
  value: plan.sorts[0],
923
998
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
924
999
  }),
925
- plan.loop && renderLoopToggle(s)
1000
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
926
1001
  ].filter(Boolean);
927
1002
  const chips = plan.types === "chips" && h(
928
1003
  "div",
@@ -1209,13 +1284,16 @@ var WaveformSounds = class _WaveformSounds {
1209
1284
  this._bind();
1210
1285
  this._observe();
1211
1286
  this._resolveColors();
1287
+ if (!this._loopSet) this.loop = loopsByDefault(this.options.loop, facets(this.sounds));
1212
1288
  this._setLoop(this.loop);
1289
+ this._bpmExtent = facets(this.sounds).bpm;
1213
1290
  this._sorts = availableSorts(this.render.sorts, facets(this.sounds));
1214
1291
  if (!this._sortSet) this.sortBy = this._sorts[0] || "default";
1215
1292
  this._readUrl();
1216
1293
  this._syncControls();
1217
1294
  this._apply({ resort: this.sortBy !== "default" });
1218
1295
  if (this._playerClass()) this._ensureEngine();
1296
+ if (this.render.player === "strip" && this.engine && this.currentIndex == null) this._cueFirst();
1219
1297
  this._emit("ready", { sounds: this.sounds.length });
1220
1298
  this.options.onReady?.(this);
1221
1299
  }
@@ -1241,6 +1319,10 @@ var WaveformSounds = class _WaveformSounds {
1241
1319
  search: q("[data-ws-search]"),
1242
1320
  bpmMin: q("[data-ws-bpm-min]"),
1243
1321
  bpmMax: q("[data-ws-bpm-max]"),
1322
+ bpmRange: q("[data-ws-bpm-range]"),
1323
+ bpmReadout: q("[data-ws-bpm-readout]"),
1324
+ bpmClear: q("[data-ws-bpm-clear]"),
1325
+ bpmLabel: q('[data-ws-menu="bpm"] [data-ws-menu-value]'),
1244
1326
  loop: q("[data-ws-loop]"),
1245
1327
  count: q("[data-ws-count]"),
1246
1328
  empty: q("[data-ws-empty]"),
@@ -1280,12 +1362,20 @@ var WaveformSounds = class _WaveformSounds {
1280
1362
  this.setFilter({ query: "" });
1281
1363
  }
1282
1364
  }, sig);
1283
- const onBpm = () => {
1365
+ const onBpm = (moved) => {
1366
+ const lo = Number($.bpmMin.value), hi = Number($.bpmMax.value);
1367
+ if (lo > hi) moved.value = String(moved === $.bpmMin ? hi : lo);
1368
+ const f = bpmFilterFromRange(Number($.bpmMin.value), Number($.bpmMax.value), this._bpmExtent);
1369
+ this._showBpm(f);
1284
1370
  clearTimeout(bpmTimer);
1285
- bpmTimer = setTimeout(() => this.setFilter({ bpmMin: $.bpmMin?.value ?? "", bpmMax: $.bpmMax?.value ?? "" }), BPM_DELAY);
1371
+ bpmTimer = setTimeout(() => this.setFilter(f), BPM_DELAY);
1286
1372
  };
1287
- $.bpmMin?.addEventListener("input", onBpm, sig);
1288
- $.bpmMax?.addEventListener("input", onBpm, sig);
1373
+ $.bpmMin?.addEventListener("input", () => onBpm($.bpmMin), sig);
1374
+ $.bpmMax?.addEventListener("input", () => onBpm($.bpmMax), sig);
1375
+ $.bpmClear?.addEventListener("click", () => {
1376
+ clearTimeout(bpmTimer);
1377
+ this.setFilter({ bpmMin: "", bpmMax: "" });
1378
+ }, sig);
1289
1379
  this.menus = new Menus(root, {
1290
1380
  signal: this._ctl.signal,
1291
1381
  onPick: (name, value) => name === "sort" ? this.setSort(value) : this.setFilter({ [name]: value })
@@ -1577,11 +1667,32 @@ var WaveformSounds = class _WaveformSounds {
1577
1667
  this.menus?.setValue("type", f.type || "");
1578
1668
  this.menus?.setValue("key", f.key || "");
1579
1669
  this.menus?.setValue("sort", this.sortBy);
1580
- if ($.bpmMin && $.bpmMin.value !== String(f.bpmMin)) $.bpmMin.value = f.bpmMin;
1581
- if ($.bpmMax && $.bpmMax.value !== String(f.bpmMax)) $.bpmMax.value = f.bpmMax;
1670
+ if ($.bpmMin && $.bpmMax && this._bpmExtent) {
1671
+ const { lo, hi } = bpmRangeFromFilter(f, this._bpmExtent);
1672
+ $.bpmMin.value = String(lo);
1673
+ $.bpmMax.value = String(hi);
1674
+ this._showBpm(f);
1675
+ }
1582
1676
  $.chips.forEach((c) => c.setAttribute("aria-pressed", String(c.dataset.wsType === (f.type || ""))));
1583
1677
  $.loopFilter.forEach((b) => b.setAttribute("aria-pressed", String(b.dataset.wsLoopFilter === (f.loop || ""))));
1584
1678
  }
1679
+ /**
1680
+ * Show a BPM filter in its menu: the button label, the panel's readout,
1681
+ * the filled part of the track, and which handle sits on top (the one
1682
+ * that can still move, when they meet).
1683
+ * @private
1684
+ */
1685
+ _showBpm(f) {
1686
+ const $ = this.$, extent = this._bpmExtent;
1687
+ if (!$?.bpmRange || !extent) return;
1688
+ const { lo, hi } = bpmRangeFromFilter(f, extent);
1689
+ if ($.bpmLabel) $.bpmLabel.textContent = bpmLabel(f, extent, this.strings);
1690
+ if ($.bpmReadout) $.bpmReadout.textContent = fill(this.strings.bpmRange, { min: lo, max: hi });
1691
+ $.bpmRange.style.setProperty("--ws-lo", String(bpmFraction(lo, extent)));
1692
+ $.bpmRange.style.setProperty("--ws-hi", String(bpmFraction(hi, extent)));
1693
+ $.bpmRange.classList.toggle("is-min-top", lo >= hi && lo > extent.min);
1694
+ $.bpmRange.classList.toggle("is-active", lo > extent.min || hi < extent.max);
1695
+ }
1585
1696
  /**
1586
1697
  * Lay the rows out for the current filter, sort and page: order, hide,
1587
1698
  * "Show more", the empty state and the count. Then report it.
@@ -1608,6 +1719,7 @@ var WaveformSounds = class _WaveformSounds {
1608
1719
  }
1609
1720
  if ($.empty) $.empty.hidden = shown.length > 0;
1610
1721
  if ($.count) $.count.textContent = countText(shown.length, this.sounds.length, this.strings);
1722
+ if (this._cued && !this.playing) this._cueFirst();
1611
1723
  this._queueUrl();
1612
1724
  this._emit("filter", { visible: shown.length, total: this.sounds.length, filter: { ...this.filter }, sort: this.sortBy });
1613
1725
  this.options.onFilter?.(shown, this);
@@ -1692,6 +1804,7 @@ var WaveformSounds = class _WaveformSounds {
1692
1804
  }
1693
1805
  const index = this._indexOf(target);
1694
1806
  if (index == null) return;
1807
+ this._cued = false;
1695
1808
  const engine = this._ensureEngine();
1696
1809
  if (!engine) return;
1697
1810
  if (index === this.currentIndex && engine.audio?.src) {
@@ -1708,6 +1821,25 @@ var WaveformSounds = class _WaveformSounds {
1708
1821
  const sound = this.sounds[index];
1709
1822
  engine.loadTrack(sound.url, sound.title, sound.type || null, { waveform: enginePeaks(sound, this.render.player), autoplay: true });
1710
1823
  }
1824
+ /**
1825
+ * Strip layout: load the first sound the list shows into the docked
1826
+ * player WITHOUT playing it, so the player shows a title and waveform
1827
+ * (and its play button plays that sound) instead of an empty frame.
1828
+ * Only metadata is fetched — the browser default for any `<audio>`.
1829
+ * The row isn't marked current until it plays.
1830
+ * @private
1831
+ */
1832
+ _cueFirst() {
1833
+ const engine = this.engine;
1834
+ const first = this.visible[0];
1835
+ if (!engine || !first) return;
1836
+ const index = this.sounds.indexOf(first);
1837
+ if (index === this.currentIndex) return;
1838
+ this.currentIndex = index;
1839
+ this.progress = 0;
1840
+ this._cued = true;
1841
+ engine.loadTrack(first.url, first.title, first.type || null, { waveform: enginePeaks(first, "strip"), autoplay: false });
1842
+ }
1711
1843
  /** Pause the current sound. */
1712
1844
  pause() {
1713
1845
  if (this.engine && this.playing) this.engine.pause();
@@ -1740,7 +1872,7 @@ var WaveformSounds = class _WaveformSounds {
1740
1872
  */
1741
1873
  _step(dir) {
1742
1874
  const shown = this.visible;
1743
- const at = this.currentIndex == null ? -1 : shown.indexOf(this.sounds[this.currentIndex]);
1875
+ const at = this.currentIndex == null || this._cued ? -1 : shown.indexOf(this.sounds[this.currentIndex]);
1744
1876
  const target = shown[at + dir];
1745
1877
  if (!target) return false;
1746
1878
  const limit = limitToReveal(this.limit, at + dir);
@@ -1758,6 +1890,7 @@ var WaveformSounds = class _WaveformSounds {
1758
1890
  * @param {boolean} on
1759
1891
  */
1760
1892
  setLoop(on) {
1893
+ this._loopSet = true;
1761
1894
  this.loop = !!on;
1762
1895
  this._setLoop(this.loop);
1763
1896
  }
@@ -1868,6 +2001,7 @@ var WaveformSounds = class _WaveformSounds {
1868
2001
  _setPlaying(on) {
1869
2002
  const changed = this.playing !== on;
1870
2003
  this.playing = on;
2004
+ if (on) this._cued = false;
1871
2005
  const index = this.currentIndex;
1872
2006
  if (index == null) return;
1873
2007
  this._paintRow(index);
@@ -1911,7 +2045,7 @@ var WaveformSounds = class _WaveformSounds {
1911
2045
  }
1912
2046
  /** The sound playing (or paused) now, or null. */
1913
2047
  get current() {
1914
- return this.currentIndex == null ? null : this.sounds[this.currentIndex];
2048
+ return this.currentIndex == null || this._cued ? null : this.sounds[this.currentIndex];
1915
2049
  }
1916
2050
  /**
1917
2051
  * Tear down: listeners, observers, timers and the engine. Markup this