@waveform-playlist/core 12.6.1 → 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.d.mts CHANGED
@@ -727,7 +727,8 @@ type SnapTo = 'bar' | 'beat' | '1/2' | '1/4' | '1/8' | '1/16' | '1/32' | '1/2T'
727
727
  * Straight subdivisions (1/2, 1/4, 1/8, 1/16, 1/32) are always expressed as
728
728
  * fractions of a quarter note (ppqn), independent of the time signature
729
729
  * denominator. Triplet subdivisions use × 2/3 of the corresponding straight
730
- * value. 'bar' and 'beat' depend on the first meter entry's time signature.
730
+ * value. 'bar' and 'beat' depend on the `timeSignature` argument (the
731
+ * caller passes the meter entry active at the position).
731
732
  * 'off' returns 0.
732
733
  */
733
734
  declare function snapToTicks(snapTo: SnapTo, timeSignature: [number, number], ppqn?: number): number;
@@ -797,8 +798,18 @@ declare function ticksToBarBeat(tick: number, meterEntries: MeterEntry[], ppqn:
797
798
  /**
798
799
  * Snaps a tick position to the nearest grid boundary defined by `snapTo`.
799
800
  *
800
- * Finds the meter entry active at the tick position and snaps relative to
801
- * that meter's segment start.
801
+ * Finds the meter entry active at the tick position, then the BAR containing
802
+ * the tick, and snaps relative to that bar's start: grid lines restart at
803
+ * each bar line (the same rule as `@dawcore/transport`'s `quantizeTick` and
804
+ * the drawn ruler grid), so odd meters stay aligned — in 7/8 at 960 PPQN a
805
+ * quarter-note grid is 0, 960, 1920, 2880 and then the bar line at 3360,
806
+ * never 3840. A subdivision longer than the bar leaves only the bar lines.
807
+ * An exact tie snaps up; a tick already on a line stays put. Unlike
808
+ * `quantizeTick`, the result is not rounded (a fractional grid size at an
809
+ * unusual PPQN gives a fractional tick), triplet steps use the rounded
810
+ * `snapToTicks` value, negative ticks are accepted (the lattice extends
811
+ * backwards) and meter entries need not sit on bar boundaries of the
812
+ * preceding meter (the next line is capped at the following entry).
802
813
  *
803
814
  * Returns the original tick unchanged when `snapTo` is 'off'.
804
815
  */
package/dist/index.d.ts CHANGED
@@ -727,7 +727,8 @@ type SnapTo = 'bar' | 'beat' | '1/2' | '1/4' | '1/8' | '1/16' | '1/32' | '1/2T'
727
727
  * Straight subdivisions (1/2, 1/4, 1/8, 1/16, 1/32) are always expressed as
728
728
  * fractions of a quarter note (ppqn), independent of the time signature
729
729
  * denominator. Triplet subdivisions use × 2/3 of the corresponding straight
730
- * value. 'bar' and 'beat' depend on the first meter entry's time signature.
730
+ * value. 'bar' and 'beat' depend on the `timeSignature` argument (the
731
+ * caller passes the meter entry active at the position).
731
732
  * 'off' returns 0.
732
733
  */
733
734
  declare function snapToTicks(snapTo: SnapTo, timeSignature: [number, number], ppqn?: number): number;
@@ -797,8 +798,18 @@ declare function ticksToBarBeat(tick: number, meterEntries: MeterEntry[], ppqn:
797
798
  /**
798
799
  * Snaps a tick position to the nearest grid boundary defined by `snapTo`.
799
800
  *
800
- * Finds the meter entry active at the tick position and snaps relative to
801
- * that meter's segment start.
801
+ * Finds the meter entry active at the tick position, then the BAR containing
802
+ * the tick, and snaps relative to that bar's start: grid lines restart at
803
+ * each bar line (the same rule as `@dawcore/transport`'s `quantizeTick` and
804
+ * the drawn ruler grid), so odd meters stay aligned — in 7/8 at 960 PPQN a
805
+ * quarter-note grid is 0, 960, 1920, 2880 and then the bar line at 3360,
806
+ * never 3840. A subdivision longer than the bar leaves only the bar lines.
807
+ * An exact tie snaps up; a tick already on a line stays put. Unlike
808
+ * `quantizeTick`, the result is not rounded (a fractional grid size at an
809
+ * unusual PPQN gives a fractional tick), triplet steps use the rounded
810
+ * `snapToTicks` value, negative ticks are accepted (the lattice extends
811
+ * backwards) and meter entries need not sit on bar boundaries of the
812
+ * preceding meter (the next line is capped at the following entry).
802
813
  *
803
814
  * Returns the original tick unchanged when `snapTo` is 'off'.
804
815
  */
package/dist/index.js CHANGED
@@ -516,10 +516,17 @@ function computeMusicalTicks(params) {
516
516
  if (segmentTickStart > segmentTickEnd) {
517
517
  continue;
518
518
  }
519
+ const restartAtBar = zoomLevel !== "coarse";
519
520
  const offsetIntoSegment = segmentTickStart - segmentStartTick;
520
- const firstStepOffset = Math.floor(offsetIntoSegment / stepTicks) * stepTicks;
521
- const firstStepTick = segmentStartTick + firstStepOffset;
522
- for (let tick = firstStepTick; tick <= segmentTickEnd && tick < segmentEndTick; tick += stepTicks) {
521
+ const anchorTick = restartAtBar ? segmentStartTick + Math.floor(offsetIntoSegment / tpBar) * tpBar : segmentStartTick;
522
+ const firstStepTick = anchorTick + Math.floor((segmentTickStart - anchorTick) / stepTicks) * stepTicks;
523
+ const nextGridTick = (tick) => {
524
+ const next = tick + stepTicks;
525
+ if (!restartAtBar) return next;
526
+ const nextBarTick = segmentStartTick + (Math.floor((tick - segmentStartTick) / tpBar) + 1) * tpBar;
527
+ return Math.min(next, nextBarTick);
528
+ };
529
+ for (let tick = firstStepTick; tick <= segmentTickEnd && tick < segmentEndTick; tick = nextGridTick(tick)) {
523
530
  const pixel = tick / ticksPerPixel;
524
531
  if (pixel < startPixel || pixel > endPixel) {
525
532
  continue;
@@ -588,18 +595,26 @@ function ticksToBarBeat(tick, meterEntries, ppqn) {
588
595
  function snapTickToGrid(tick, snapTo, meterEntries, ppqn = 960) {
589
596
  if (snapTo === "off") return tick;
590
597
  let meter = meterEntries[0] ?? { tick: 0, numerator: 4, denominator: 4 };
598
+ let nextEntryTick = Infinity;
591
599
  for (const entry of meterEntries) {
592
600
  if (entry.tick <= tick) {
593
601
  meter = entry;
594
602
  } else {
603
+ nextEntryTick = entry.tick;
595
604
  break;
596
605
  }
597
606
  }
598
607
  const ts = [meter.numerator, meter.denominator];
599
608
  const gridSize = snapToTicks(snapTo, ts, ppqn);
600
609
  if (gridSize <= 0) return tick;
601
- const offset = tick - meter.tick;
602
- return meter.tick + Math.round(offset / gridSize) * gridSize;
610
+ const barLength = ticksPerBar(ts, ppqn);
611
+ if (!(barLength > 0)) {
612
+ return meter.tick + Math.round((tick - meter.tick) / gridSize) * gridSize;
613
+ }
614
+ const barStart = meter.tick + Math.floor((tick - meter.tick) / barLength) * barLength;
615
+ const previous = barStart + Math.floor((tick - barStart) / gridSize) * gridSize;
616
+ const next = Math.min(previous + gridSize, barStart + barLength, nextEntryTick);
617
+ return tick - previous < next - tick ? previous : next;
603
618
  }
604
619
 
605
620
  // src/utils/meterDetection.ts