@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
@@ -92,7 +92,7 @@ function formatDuration(seconds) {
92
92
  const s = Number(seconds);
93
93
  if (!Number.isFinite(s) || s < 0) return "";
94
94
  if (s > 0 && s < 0.95) return `${Math.max(0.1, Math.round(s * 10) / 10).toFixed(1)}s`;
95
- const total = Math.round(s);
95
+ const total = s < 1 ? Math.ceil(s) : Math.floor(s);
96
96
  return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}`;
97
97
  }
98
98
  function parseDuration(value) {
@@ -223,6 +223,20 @@ var ICONS = {
223
223
  loop: icon("M17 4l3 3-3 3V8H8a3 3 0 0 0-3 3v1H3v-1a5 5 0 0 1 5-5h9V4zM7 20l-3-3 3-3v2h9a3 3 0 0 0 3-3v-1h2v1a5 5 0 0 1-5 5H7v2z")
224
224
  };
225
225
 
226
+ // src/js/data/bpm.js
227
+ function bpmRangeFromFilter(filter, extent) {
228
+ const read = (v, fallback) => {
229
+ const n = Number(v);
230
+ return v === "" || v == null || !Number.isFinite(n) ? fallback : Math.min(Math.max(n, extent.min), extent.max);
231
+ };
232
+ const lo = read(filter.bpmMin, extent.min);
233
+ const hi = read(filter.bpmMax, extent.max);
234
+ return lo <= hi ? { lo, hi } : { lo: hi, hi: lo };
235
+ }
236
+ function hasBpmFilter(filter) {
237
+ return filter.bpmMin != null && filter.bpmMin !== "" || filter.bpmMax != null && filter.bpmMax !== "";
238
+ }
239
+
226
240
  // src/js/render/strings.js
227
241
  var DEFAULT_STRINGS = {
228
242
  search: "Search sounds",
@@ -238,6 +252,8 @@ var DEFAULT_STRINGS = {
238
252
  bpm: "BPM",
239
253
  bpmMin: "Min BPM",
240
254
  bpmMax: "Max BPM",
255
+ anyBpm: "Any BPM",
256
+ bpmRange: "{min}\u2013{max} BPM",
241
257
  sort: "Sort",
242
258
  sortBy: "Sort by",
243
259
  sortDefault: "Default",
@@ -272,6 +288,11 @@ var SORT_LABEL_KEYS = {
272
288
  function fill(template, vars = {}) {
273
289
  return String(template).replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
274
290
  }
291
+ function bpmLabel(filter, extent, strings = DEFAULT_STRINGS) {
292
+ if (!hasBpmFilter(filter)) return strings.anyBpm;
293
+ const { lo, hi } = bpmRangeFromFilter(filter, extent);
294
+ return fill(strings.bpmRange, { min: lo, max: hi });
295
+ }
275
296
  function countText(shown, total, strings = DEFAULT_STRINGS) {
276
297
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
277
298
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
@@ -345,6 +366,9 @@ function toolbarPlan(f, o) {
345
366
  count: !!o.showCount
346
367
  };
347
368
  }
369
+ function loopsByDefault(loop, f) {
370
+ return loop == null ? f.loops > 0 : !!loop;
371
+ }
348
372
  function renderSearch(s) {
349
373
  return h(
350
374
  "label",
@@ -398,15 +422,45 @@ function renderMenu(name, m) {
398
422
  )
399
423
  );
400
424
  }
401
- function renderBpmRange(s, range) {
402
- const field = (attr, label, placeholder) => h("input", { type: "number", inputmode: "numeric", [attr]: true, "aria-label": label, placeholder, min: "0", step: "1" });
425
+ function renderBpmMenu({ id, s, range }) {
426
+ const popId = `${id}-bpm-pop`;
427
+ const handle = (attr, label, value) => h("input", {
428
+ type: "range",
429
+ class: "ws-range-input",
430
+ [attr]: true,
431
+ "aria-label": label,
432
+ min: range.min,
433
+ max: range.max,
434
+ step: "1",
435
+ value
436
+ });
403
437
  return h(
404
- "span",
405
- { class: "ws-bpm-range", role: "group", "aria-label": s.bpm },
406
- field("data-ws-bpm-min", s.bpmMin, range.min),
407
- h("span", { "aria-hidden": "true" }, "\u2013"),
408
- field("data-ws-bpm-max", s.bpmMax, range.max),
409
- h("span", { class: "ws-bpm-unit", "aria-hidden": "true" }, text(s.bpm))
438
+ "div",
439
+ { class: "ws-menu ws-menu--bpm", "data-ws-menu": "bpm" },
440
+ h(
441
+ "button",
442
+ { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
443
+ h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
444
+ h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
445
+ ICONS.chevron
446
+ ),
447
+ h(
448
+ "div",
449
+ { class: "ws-menu-pop ws-bpm-pop", id: popId, role: "dialog", "aria-label": s.bpm, "data-ws-menu-pop": true, hidden: true },
450
+ h(
451
+ "div",
452
+ { class: "ws-bpm-head" },
453
+ h("span", { class: "ws-bpm-readout", "data-ws-bpm-readout": true, "aria-live": "polite" }, text(fill(s.bpmRange, range))),
454
+ h("button", { type: "button", class: "ws-bpm-clear", "data-ws-bpm-clear": true }, text(s.anyBpm))
455
+ ),
456
+ h(
457
+ "div",
458
+ { class: "ws-range", "data-ws-bpm-range": true },
459
+ handle("data-ws-bpm-min", s.bpmMin, range.min),
460
+ handle("data-ws-bpm-max", s.bpmMax, range.max)
461
+ ),
462
+ h("div", { class: "ws-range-ends", "aria-hidden": "true" }, h("span", {}, text(range.min)), h("span", {}, text(range.max)))
463
+ )
410
464
  );
411
465
  }
412
466
  function renderLoopFilter(s) {
@@ -423,8 +477,8 @@ function renderLoopFilter(s) {
423
477
  button("one-shot", s.oneShots)
424
478
  );
425
479
  }
426
- function renderLoopToggle(s) {
427
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": "false" }, ICONS.loop, h("span", {}, text(s.loop)));
480
+ function renderLoopToggle(s, pressed) {
481
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
428
482
  }
429
483
  function renderChip(value, label, count, pressed) {
430
484
  return h(
@@ -450,7 +504,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
450
504
  placeholder: s.findKey,
451
505
  options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
452
506
  }),
453
- plan.bpm && renderBpmRange(s, f.bpm),
507
+ plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
454
508
  plan.loops && renderLoopFilter(s),
455
509
  plan.sorts.length > 0 && menu("sort", {
456
510
  label: s.sort,
@@ -458,7 +512,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
458
512
  value: plan.sorts[0],
459
513
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
460
514
  }),
461
- plan.loop && renderLoopToggle(s)
515
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
462
516
  ].filter(Boolean);
463
517
  const chips = plan.types === "chips" && h(
464
518
  "div",
@@ -57,7 +57,7 @@ function formatDuration(seconds) {
57
57
  const s = Number(seconds);
58
58
  if (!Number.isFinite(s) || s < 0) return "";
59
59
  if (s > 0 && s < 0.95) return `${Math.max(0.1, Math.round(s * 10) / 10).toFixed(1)}s`;
60
- const total = Math.round(s);
60
+ const total = s < 1 ? Math.ceil(s) : Math.floor(s);
61
61
  return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, "0")}`;
62
62
  }
63
63
  function parseDuration(value) {
@@ -188,6 +188,20 @@ var ICONS = {
188
188
  loop: icon("M17 4l3 3-3 3V8H8a3 3 0 0 0-3 3v1H3v-1a5 5 0 0 1 5-5h9V4zM7 20l-3-3 3-3v2h9a3 3 0 0 0 3-3v-1h2v1a5 5 0 0 1-5 5H7v2z")
189
189
  };
190
190
 
191
+ // src/js/data/bpm.js
192
+ function bpmRangeFromFilter(filter, extent) {
193
+ const read = (v, fallback) => {
194
+ const n = Number(v);
195
+ return v === "" || v == null || !Number.isFinite(n) ? fallback : Math.min(Math.max(n, extent.min), extent.max);
196
+ };
197
+ const lo = read(filter.bpmMin, extent.min);
198
+ const hi = read(filter.bpmMax, extent.max);
199
+ return lo <= hi ? { lo, hi } : { lo: hi, hi: lo };
200
+ }
201
+ function hasBpmFilter(filter) {
202
+ return filter.bpmMin != null && filter.bpmMin !== "" || filter.bpmMax != null && filter.bpmMax !== "";
203
+ }
204
+
191
205
  // src/js/render/strings.js
192
206
  var DEFAULT_STRINGS = {
193
207
  search: "Search sounds",
@@ -203,6 +217,8 @@ var DEFAULT_STRINGS = {
203
217
  bpm: "BPM",
204
218
  bpmMin: "Min BPM",
205
219
  bpmMax: "Max BPM",
220
+ anyBpm: "Any BPM",
221
+ bpmRange: "{min}\u2013{max} BPM",
206
222
  sort: "Sort",
207
223
  sortBy: "Sort by",
208
224
  sortDefault: "Default",
@@ -237,6 +253,11 @@ var SORT_LABEL_KEYS = {
237
253
  function fill(template, vars = {}) {
238
254
  return String(template).replace(/\{(\w+)\}/g, (match, name) => name in vars ? String(vars[name]) : match);
239
255
  }
256
+ function bpmLabel(filter, extent, strings = DEFAULT_STRINGS) {
257
+ if (!hasBpmFilter(filter)) return strings.anyBpm;
258
+ const { lo, hi } = bpmRangeFromFilter(filter, extent);
259
+ return fill(strings.bpmRange, { min: lo, max: hi });
260
+ }
240
261
  function countText(shown, total, strings = DEFAULT_STRINGS) {
241
262
  if (shown !== total) return fill(strings.countFiltered, { count: shown, total });
242
263
  return total === 1 ? strings.countOne : fill(strings.count, { count: total });
@@ -310,6 +331,9 @@ function toolbarPlan(f, o) {
310
331
  count: !!o.showCount
311
332
  };
312
333
  }
334
+ function loopsByDefault(loop, f) {
335
+ return loop == null ? f.loops > 0 : !!loop;
336
+ }
313
337
  function renderSearch(s) {
314
338
  return h(
315
339
  "label",
@@ -363,15 +387,45 @@ function renderMenu(name, m) {
363
387
  )
364
388
  );
365
389
  }
366
- function renderBpmRange(s, range) {
367
- const field = (attr, label, placeholder) => h("input", { type: "number", inputmode: "numeric", [attr]: true, "aria-label": label, placeholder, min: "0", step: "1" });
390
+ function renderBpmMenu({ id, s, range }) {
391
+ const popId = `${id}-bpm-pop`;
392
+ const handle = (attr, label, value) => h("input", {
393
+ type: "range",
394
+ class: "ws-range-input",
395
+ [attr]: true,
396
+ "aria-label": label,
397
+ min: range.min,
398
+ max: range.max,
399
+ step: "1",
400
+ value
401
+ });
368
402
  return h(
369
- "span",
370
- { class: "ws-bpm-range", role: "group", "aria-label": s.bpm },
371
- field("data-ws-bpm-min", s.bpmMin, range.min),
372
- h("span", { "aria-hidden": "true" }, "\u2013"),
373
- field("data-ws-bpm-max", s.bpmMax, range.max),
374
- h("span", { class: "ws-bpm-unit", "aria-hidden": "true" }, text(s.bpm))
403
+ "div",
404
+ { class: "ws-menu ws-menu--bpm", "data-ws-menu": "bpm" },
405
+ h(
406
+ "button",
407
+ { type: "button", class: "ws-menu-btn", "data-ws-menu-btn": true, "aria-haspopup": "dialog", "aria-expanded": "false", "aria-controls": popId },
408
+ h("span", { class: "ws-sr" }, text(`${s.bpm}: `)),
409
+ h("span", { class: "ws-menu-value", "data-ws-menu-value": true }, text(bpmLabel({}, range, s))),
410
+ ICONS.chevron
411
+ ),
412
+ h(
413
+ "div",
414
+ { class: "ws-menu-pop ws-bpm-pop", id: popId, role: "dialog", "aria-label": s.bpm, "data-ws-menu-pop": true, hidden: true },
415
+ h(
416
+ "div",
417
+ { class: "ws-bpm-head" },
418
+ h("span", { class: "ws-bpm-readout", "data-ws-bpm-readout": true, "aria-live": "polite" }, text(fill(s.bpmRange, range))),
419
+ h("button", { type: "button", class: "ws-bpm-clear", "data-ws-bpm-clear": true }, text(s.anyBpm))
420
+ ),
421
+ h(
422
+ "div",
423
+ { class: "ws-range", "data-ws-bpm-range": true },
424
+ handle("data-ws-bpm-min", s.bpmMin, range.min),
425
+ handle("data-ws-bpm-max", s.bpmMax, range.max)
426
+ ),
427
+ h("div", { class: "ws-range-ends", "aria-hidden": "true" }, h("span", {}, text(range.min)), h("span", {}, text(range.max)))
428
+ )
375
429
  );
376
430
  }
377
431
  function renderLoopFilter(s) {
@@ -388,8 +442,8 @@ function renderLoopFilter(s) {
388
442
  button("one-shot", s.oneShots)
389
443
  );
390
444
  }
391
- function renderLoopToggle(s) {
392
- return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": "false" }, ICONS.loop, h("span", {}, text(s.loop)));
445
+ function renderLoopToggle(s, pressed) {
446
+ return h("button", { type: "button", class: "ws-loop", "data-ws-loop": true, "aria-pressed": String(pressed) }, ICONS.loop, h("span", {}, text(s.loop)));
393
447
  }
394
448
  function renderChip(value, label, count, pressed) {
395
449
  return h(
@@ -415,7 +469,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
415
469
  placeholder: s.findKey,
416
470
  options: [{ value: "", label: s.anyKey }, ...f.keys.map((k) => ({ value: k, label: k }))]
417
471
  }),
418
- plan.bpm && renderBpmRange(s, f.bpm),
472
+ plan.bpm && renderBpmMenu({ id, s, range: f.bpm }),
419
473
  plan.loops && renderLoopFilter(s),
420
474
  plan.sorts.length > 0 && menu("sort", {
421
475
  label: s.sort,
@@ -423,7 +477,7 @@ function renderToolbar(plan, { o, s, f, total, id }) {
423
477
  value: plan.sorts[0],
424
478
  options: plan.sorts.map((k) => ({ value: k, label: s[SORT_LABEL_KEYS[k]] }))
425
479
  }),
426
- plan.loop && renderLoopToggle(s)
480
+ plan.loop && renderLoopToggle(s, loopsByDefault(o.loop, f))
427
481
  ].filter(Boolean);
428
482
  const chips = plan.types === "chips" && h(
429
483
  "div",