@waveform-playlist/core 12.6.0 → 12.6.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/index.mjs CHANGED
@@ -418,10 +418,17 @@ function computeMusicalTicks(params) {
418
418
  if (segmentTickStart > segmentTickEnd) {
419
419
  continue;
420
420
  }
421
+ const restartAtBar = zoomLevel !== "coarse";
421
422
  const offsetIntoSegment = segmentTickStart - segmentStartTick;
422
- const firstStepOffset = Math.floor(offsetIntoSegment / stepTicks) * stepTicks;
423
- const firstStepTick = segmentStartTick + firstStepOffset;
424
- for (let tick = firstStepTick; tick <= segmentTickEnd && tick < segmentEndTick; tick += stepTicks) {
423
+ const anchorTick = restartAtBar ? segmentStartTick + Math.floor(offsetIntoSegment / tpBar) * tpBar : segmentStartTick;
424
+ const firstStepTick = anchorTick + Math.floor((segmentTickStart - anchorTick) / stepTicks) * stepTicks;
425
+ const nextGridTick = (tick) => {
426
+ const next = tick + stepTicks;
427
+ if (!restartAtBar) return next;
428
+ const nextBarTick = segmentStartTick + (Math.floor((tick - segmentStartTick) / tpBar) + 1) * tpBar;
429
+ return Math.min(next, nextBarTick);
430
+ };
431
+ for (let tick = firstStepTick; tick <= segmentTickEnd && tick < segmentEndTick; tick = nextGridTick(tick)) {
425
432
  const pixel = tick / ticksPerPixel;
426
433
  if (pixel < startPixel || pixel > endPixel) {
427
434
  continue;
@@ -456,21 +463,60 @@ function computeMusicalTicks(params) {
456
463
  };
457
464
  return result;
458
465
  }
466
+ function ticksToBarBeat(tick, meterEntries, ppqn) {
467
+ if (ppqn <= 0) return { bar: 1, beat: 1 };
468
+ const entries = meterEntries.length > 0 ? meterEntries : [{ tick: 0, numerator: 4, denominator: 4 }];
469
+ let barOffset = 0;
470
+ for (let i = 0; i < entries.length; i++) {
471
+ const meter = entries[i];
472
+ const segmentStart = meter.tick;
473
+ const segmentEnd = i + 1 < entries.length ? entries[i + 1].tick : Number.MAX_SAFE_INTEGER;
474
+ const ts2 = [meter.numerator, meter.denominator];
475
+ const tpBar2 = ticksPerBar(ts2, ppqn);
476
+ const tpBeat2 = ticksPerBeat(ts2, ppqn);
477
+ if (tick >= segmentStart && tick < segmentEnd) {
478
+ const offset2 = tick - segmentStart;
479
+ const barIndexInSegment2 = Math.floor(offset2 / tpBar2);
480
+ const beatInBar2 = Math.floor(offset2 % tpBar2 / tpBeat2);
481
+ return { bar: barOffset + barIndexInSegment2 + 1, beat: beatInBar2 + 1 };
482
+ }
483
+ if (segmentEnd !== Number.MAX_SAFE_INTEGER) {
484
+ const segmentLen = segmentEnd - segmentStart;
485
+ barOffset += Math.floor(segmentLen / tpBar2);
486
+ }
487
+ }
488
+ const first = entries[0];
489
+ const ts = [first.numerator, first.denominator];
490
+ const tpBar = ticksPerBar(ts, ppqn);
491
+ const tpBeat = ticksPerBeat(ts, ppqn);
492
+ const offset = tick - first.tick;
493
+ const barIndexInSegment = Math.floor(offset / tpBar);
494
+ const beatInBar = Math.floor(offset % tpBar / tpBeat);
495
+ return { bar: barIndexInSegment + 1, beat: beatInBar + 1 };
496
+ }
459
497
  function snapTickToGrid(tick, snapTo, meterEntries, ppqn = 960) {
460
498
  if (snapTo === "off") return tick;
461
499
  let meter = meterEntries[0] ?? { tick: 0, numerator: 4, denominator: 4 };
500
+ let nextEntryTick = Infinity;
462
501
  for (const entry of meterEntries) {
463
502
  if (entry.tick <= tick) {
464
503
  meter = entry;
465
504
  } else {
505
+ nextEntryTick = entry.tick;
466
506
  break;
467
507
  }
468
508
  }
469
509
  const ts = [meter.numerator, meter.denominator];
470
510
  const gridSize = snapToTicks(snapTo, ts, ppqn);
471
511
  if (gridSize <= 0) return tick;
472
- const offset = tick - meter.tick;
473
- return meter.tick + Math.round(offset / gridSize) * gridSize;
512
+ const barLength = ticksPerBar(ts, ppqn);
513
+ if (!(barLength > 0)) {
514
+ return meter.tick + Math.round((tick - meter.tick) / gridSize) * gridSize;
515
+ }
516
+ const barStart = meter.tick + Math.floor((tick - meter.tick) / barLength) * barLength;
517
+ const previous = barStart + Math.floor((tick - barStart) / gridSize) * gridSize;
518
+ const next = Math.min(previous + gridSize, barStart + barLength, nextEntryTick);
519
+ return tick - previous < next - tick ? previous : next;
474
520
  }
475
521
 
476
522
  // src/utils/meterDetection.ts
@@ -800,6 +846,14 @@ function applyFadeOut(param, startTime, duration, type = "linear", startValue =
800
846
  }
801
847
 
802
848
  // src/keyboard.ts
849
+ function matchesKeyBinding(event, binding) {
850
+ const keyMatch = event.key.toLowerCase() === binding.key.toLowerCase() || event.key === binding.key;
851
+ const ctrlMatch = binding.ctrlKey === void 0 || event.ctrlKey === binding.ctrlKey;
852
+ const shiftMatch = binding.shiftKey === void 0 || event.shiftKey === binding.shiftKey;
853
+ const metaMatch = binding.metaKey === void 0 || event.metaKey === binding.metaKey;
854
+ const altMatch = binding.altKey === void 0 || event.altKey === binding.altKey;
855
+ return keyMatch && ctrlMatch && shiftMatch && metaMatch && altMatch;
856
+ }
803
857
  function handleKeyboardEvent(event, shortcuts, enabled) {
804
858
  if (!enabled) return;
805
859
  if (event.repeat) return;
@@ -807,14 +861,7 @@ function handleKeyboardEvent(event, shortcuts, enabled) {
807
861
  if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable) {
808
862
  return;
809
863
  }
810
- const matchingShortcut = shortcuts.find((shortcut) => {
811
- const keyMatch = event.key.toLowerCase() === shortcut.key.toLowerCase() || event.key === shortcut.key;
812
- const ctrlMatch = shortcut.ctrlKey === void 0 || event.ctrlKey === shortcut.ctrlKey;
813
- const shiftMatch = shortcut.shiftKey === void 0 || event.shiftKey === shortcut.shiftKey;
814
- const metaMatch = shortcut.metaKey === void 0 || event.metaKey === shortcut.metaKey;
815
- const altMatch = shortcut.altKey === void 0 || event.altKey === shortcut.altKey;
816
- return keyMatch && ctrlMatch && shiftMatch && metaMatch && altMatch;
817
- });
864
+ const matchingShortcut = shortcuts.find((shortcut) => matchesKeyBinding(event, shortcut));
818
865
  if (matchingShortcut) {
819
866
  if (matchingShortcut.preventDefault !== false) {
820
867
  event.preventDefault();
@@ -915,13 +962,137 @@ function watchMicrophoneDevices(listener, mediaDevices) {
915
962
  md.removeEventListener("devicechange", deliver);
916
963
  };
917
964
  }
965
+
966
+ // src/annotations/boundaries.ts
967
+ var LINK_THRESHOLD = 0.01;
968
+ var MIN_ANNOTATION_DURATION = 0.1;
969
+ var ANNOTATION_LINK_THRESHOLD_TICKS = 0.5;
970
+ function annotationMinDurationTicks(ppqn) {
971
+ return Math.max(1, Math.round(ppqn / 32));
972
+ }
973
+ function updateAnnotationBoundaries(params, options = {}) {
974
+ const { annotationIndex, newTime, isDraggingStart, annotations, duration, linkEndpoints } = params;
975
+ const linkThreshold = options.linkThreshold ?? LINK_THRESHOLD;
976
+ const minDuration = options.minDuration ?? MIN_ANNOTATION_DURATION;
977
+ const updatedAnnotations = [...annotations];
978
+ const annotation = annotations[annotationIndex];
979
+ if (isDraggingStart) {
980
+ const constrainedStart = Math.min(annotation.end - minDuration, Math.max(0, newTime));
981
+ const delta = constrainedStart - annotation.start;
982
+ updatedAnnotations[annotationIndex] = { ...annotation, start: constrainedStart };
983
+ if (linkEndpoints && annotationIndex > 0) {
984
+ const prevAnnotation = updatedAnnotations[annotationIndex - 1];
985
+ if (Math.abs(prevAnnotation.end - annotation.start) < linkThreshold) {
986
+ updatedAnnotations[annotationIndex - 1] = {
987
+ ...prevAnnotation,
988
+ end: Math.max(prevAnnotation.start + minDuration, prevAnnotation.end + delta)
989
+ };
990
+ } else if (constrainedStart <= prevAnnotation.end) {
991
+ updatedAnnotations[annotationIndex] = {
992
+ ...updatedAnnotations[annotationIndex],
993
+ start: prevAnnotation.end
994
+ };
995
+ }
996
+ } else if (!linkEndpoints && annotationIndex > 0 && constrainedStart < updatedAnnotations[annotationIndex - 1].end) {
997
+ updatedAnnotations[annotationIndex - 1] = {
998
+ ...updatedAnnotations[annotationIndex - 1],
999
+ end: constrainedStart
1000
+ };
1001
+ }
1002
+ } else {
1003
+ const constrainedEnd = Math.max(annotation.start + minDuration, Math.min(newTime, duration));
1004
+ const delta = constrainedEnd - annotation.end;
1005
+ updatedAnnotations[annotationIndex] = { ...annotation, end: constrainedEnd };
1006
+ if (linkEndpoints && annotationIndex < updatedAnnotations.length - 1) {
1007
+ const nextAnnotation = updatedAnnotations[annotationIndex + 1];
1008
+ if (Math.abs(nextAnnotation.start - annotation.end) < linkThreshold) {
1009
+ const newStart = nextAnnotation.start + delta;
1010
+ updatedAnnotations[annotationIndex + 1] = {
1011
+ ...nextAnnotation,
1012
+ start: Math.min(nextAnnotation.end - minDuration, newStart)
1013
+ };
1014
+ let currentIndex = annotationIndex + 1;
1015
+ while (currentIndex < updatedAnnotations.length - 1) {
1016
+ const current = updatedAnnotations[currentIndex];
1017
+ const next = updatedAnnotations[currentIndex + 1];
1018
+ if (Math.abs(next.start - current.end) < linkThreshold) {
1019
+ const nextDelta = current.end - annotations[currentIndex].end;
1020
+ updatedAnnotations[currentIndex + 1] = {
1021
+ ...next,
1022
+ start: Math.min(next.end - minDuration, next.start + nextDelta)
1023
+ };
1024
+ currentIndex++;
1025
+ } else {
1026
+ break;
1027
+ }
1028
+ }
1029
+ } else if (constrainedEnd >= nextAnnotation.start) {
1030
+ updatedAnnotations[annotationIndex] = {
1031
+ ...updatedAnnotations[annotationIndex],
1032
+ end: nextAnnotation.start
1033
+ };
1034
+ }
1035
+ } else if (!linkEndpoints && annotationIndex < updatedAnnotations.length - 1 && constrainedEnd > updatedAnnotations[annotationIndex + 1].start) {
1036
+ const nextAnnotation = updatedAnnotations[annotationIndex + 1];
1037
+ updatedAnnotations[annotationIndex + 1] = { ...nextAnnotation, start: constrainedEnd };
1038
+ let currentIndex = annotationIndex + 1;
1039
+ while (currentIndex < updatedAnnotations.length - 1) {
1040
+ const current = updatedAnnotations[currentIndex];
1041
+ const next = updatedAnnotations[currentIndex + 1];
1042
+ if (current.end > next.start) {
1043
+ updatedAnnotations[currentIndex + 1] = { ...next, start: current.end };
1044
+ currentIndex++;
1045
+ } else {
1046
+ break;
1047
+ }
1048
+ }
1049
+ }
1050
+ }
1051
+ return updatedAnnotations;
1052
+ }
1053
+
1054
+ // src/annotations/shortcuts.ts
1055
+ var noMods = { ctrlKey: false, metaKey: false };
1056
+ var DEFAULT_ANNOTATION_SHORTCUTS = {
1057
+ selectPrevious: [
1058
+ { key: "ArrowUp", ...noMods },
1059
+ { key: "ArrowLeft", ...noMods }
1060
+ ],
1061
+ selectNext: [
1062
+ { key: "ArrowDown", ...noMods },
1063
+ { key: "ArrowRight", ...noMods }
1064
+ ],
1065
+ selectFirst: [{ key: "Home", ...noMods }],
1066
+ selectLast: [{ key: "End", ...noMods }],
1067
+ clearSelection: [{ key: "Escape", ...noMods }],
1068
+ moveStartEarlier: [{ key: "[", ...noMods }],
1069
+ moveStartLater: [{ key: "]", ...noMods }],
1070
+ // '{' / '}' are what event.key reports for Shift+[ / Shift+] — no explicit
1071
+ // shiftKey needed; the key value itself encodes it.
1072
+ moveEndEarlier: [{ key: "{", ...noMods }],
1073
+ moveEndLater: [{ key: "}", ...noMods }],
1074
+ playActive: [{ key: "Enter", ...noMods }]
1075
+ };
1076
+ var ALL_ACTIONS = Object.keys(DEFAULT_ANNOTATION_SHORTCUTS);
1077
+ function resolveAnnotationShortcuts(remap) {
1078
+ return ALL_ACTIONS.flatMap((action) => {
1079
+ const override = remap?.[action];
1080
+ const bindings = override ? [override] : DEFAULT_ANNOTATION_SHORTCUTS[action];
1081
+ return bindings.map((binding) => ({ action, binding }));
1082
+ });
1083
+ }
918
1084
  export {
1085
+ ANNOTATION_LINK_THRESHOLD_TICKS,
1086
+ DEFAULT_ANNOTATION_SHORTCUTS,
919
1087
  DEFAULT_SPECTROGRAM_COLOR_MAP,
920
1088
  InteractionState,
1089
+ LINK_THRESHOLD,
921
1090
  MAX_CANVAS_WIDTH,
1091
+ MIN_ANNOTATION_DURATION,
922
1092
  MIN_PIXELS_PER_UNIT,
923
1093
  PPQN,
924
1094
  SPECTROGRAM_DEFAULTS,
1095
+ annotationMinDurationTicks,
925
1096
  appendPeaks,
926
1097
  appendToAudioBuffer,
927
1098
  applyFadeIn,
@@ -959,11 +1130,13 @@ export {
959
1130
  handleKeyboardEvent,
960
1131
  linearCurve,
961
1132
  logarithmicCurve,
1133
+ matchesKeyBinding,
962
1134
  normalizedToDb,
963
1135
  parseSpectrogramCanvasId,
964
1136
  pixelsToSamples,
965
1137
  pixelsToSeconds,
966
1138
  probeRangeSupport,
1139
+ resolveAnnotationShortcuts,
967
1140
  resolveRecordingOffsetSamples,
968
1141
  sCurveCurve,
969
1142
  samplesToPixels,
@@ -977,8 +1150,10 @@ export {
977
1150
  sortClipsByTime,
978
1151
  ticksPerBar,
979
1152
  ticksPerBeat,
1153
+ ticksToBarBeat,
980
1154
  ticksToSamples,
981
1155
  trackChannelCount,
1156
+ updateAnnotationBoundaries,
982
1157
  watchMicrophoneDevices
983
1158
  };
984
1159
  //# sourceMappingURL=index.mjs.map