All files / player-terminal/src/tui timeline.ts

99.07% Statements 107/108
95.23% Branches 60/63
100% Functions 10/10
99.06% Lines 106/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                  3x 3x 3x           59x 56x     3x 3x 3x 3x 6x 6x 6x 3x 3x 3x   3x     3x       58x 56x   2x 2x       61x 61x 61x 58x   3x       60x 60x 60x 60x 59x   1x               60x       60x 60x 60x 60x 1920x 1920x 1x   1919x 1919x 62x 62x 62x       60x 1x     59x 59x             59x 59x 59x 60x 60x 60x   59x       1x 1x 1x       60x 57x     3x 3x 3x 3x 5x 5x 5x 3x 3x 3x   2x     3x             59x 51x     8x 8x 8x 8x 14x 14x 14x 5x 5x 5x   9x     8x             61x 57x     4x 4x 4x 4x 7x 7x 7x 4x 4x 4x   3x     4x 1x   3x 3x 1x   2x    
import type { ScrollTimelinePoint } from '@be-music/chart-model';
 
export interface MeasureTimelinePoint {
  measure: number;
  seconds: number;
}
 
export interface BPMTimelinePoint {
  bpm: number;
  seconds: number;
}
 
export interface StopWindowPoint {
  startSeconds: number;
  endSeconds: number;
}
 
export const _____MEASURE_BEATS = 4;
const MEASURE_SIGNATURE_MAX_DENOMINATOR = 32;
const MEASURE_SIGNATURE_TOLERANCE = 1e-8;
 
export function findCurrentMeasure(
  timeline: ReadonlyArray<MeasureTimelinePoint> | undefined,
  currentSeconds: number,
): number {
  if (!timeline || timeline.length === 0) {
    return 0;
  }
 
  let low = 0;
  let high = timeline.length - 1;
  let best = 0;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    const point = timeline[mid];
    if (point.seconds <= currentSeconds) {
      best = point.measure;
      low = mid + 1;
      continue;
    }
    high = mid - 1;
  }
 
  return Math.max(0, best);
}
 
export function findTotalMeasures(timeline: ReadonlyArray<MeasureTimelinePoint> | undefined): number {
  if (!timeline || timeline.length === 0) {
    return 1;
  }
  const last = timeline[timeline.length - 1];
  return Math.max(1, Math.floor(last.measure));
}
 
export function resolveMeasureLength(measureLengths: ReadonlyMap<number, number> | undefined, measure: number): number {
  const safeMeasure = Math.max(0, Math.floor(measure));
  const length = measureLengths?.get(safeMeasure);
  if (typeof length !== 'number' || !Number.isFinite(length) || length <= 0) {
    return 1;
  }
  return length;
}
 
export function formatMeasureSignature(length: number | undefined): string {
  const safeLength = typeof length === 'number' && Number.isFinite(length) && length > 0 ? length : 1;
  const quarterBeats = safeLength * _____MEASURE_BEATS;
  const fraction = approximateFraction(quarterBeats, MEASURE_SIGNATURE_MAX_DENOMINATOR, MEASURE_SIGNATURE_TOLERANCE);
  if (fraction) {
    return `${fraction.numerator}/${fraction.denominator * _____MEASURE_BEATS}`;
  }
  return `${formatMeterDecimal(quarterBeats)}/${_____MEASURE_BEATS}`;
}
 
function approximateFraction(
  value: number,
  maxDenominator: number,
  tolerance: number,
): { numerator: number; denominator: number } | undefined {
  Iif (!Number.isFinite(value) || value <= 0 || !Number.isFinite(maxDenominator) || maxDenominator < 1) {
    return undefined;
  }
 
  let bestNumerator = 0;
  let bestDenominator = 1;
  let bestError = Number.POSITIVE_INFINITY;
  for (let denominator = 1; denominator <= Math.floor(maxDenominator); denominator += 1) {
    const numerator = Math.round(value * denominator);
    if (numerator <= 0) {
      continue;
    }
    const error = Math.abs(value - numerator / denominator);
    if (error < bestError) {
      bestError = error;
      bestNumerator = numerator;
      bestDenominator = denominator;
    }
  }
 
  if (bestNumerator <= 0 || bestError > tolerance) {
    return undefined;
  }
 
  const divisor = greatestCommonDivisor(bestNumerator, bestDenominator);
  return {
    numerator: Math.floor(bestNumerator / divisor),
    denominator: Math.floor(bestDenominator / divisor),
  };
}
 
function greatestCommonDivisor(left: number, right: number): number {
  let a = Math.abs(Math.floor(left));
  let b = Math.abs(Math.floor(right));
  while (b !== 0) {
    const rest = a % b;
    a = b;
    b = rest;
  }
  return Math.max(1, a);
}
 
function formatMeterDecimal(value: number): string {
  const safe = Number.isFinite(value) ? Math.max(0, value) : 0;
  const fixed = safe.toFixed(6);
  return fixed.replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
}
 
export function findCurrentBPM(timeline: ReadonlyArray<BPMTimelinePoint> | undefined, currentSeconds: number): number {
  if (!timeline || timeline.length === 0) {
    return 0;
  }
 
  let low = 0;
  let high = timeline.length - 1;
  let best = timeline[0].bpm;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    const point = timeline[mid];
    if (point.seconds <= currentSeconds) {
      best = point.bpm;
      low = mid + 1;
      continue;
    }
    high = mid - 1;
  }
 
  return Math.max(0, best);
}
 
export function findCurrentScroll(
  timeline: ReadonlyArray<ScrollTimelinePoint> | undefined,
  currentBeat: number,
): number {
  if (!timeline || timeline.length === 0) {
    return 1;
  }
 
  let low = 0;
  let high = timeline.length - 1;
  let best = 1;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    const point = timeline[mid];
    if (point.beat <= currentBeat) {
      best = point.speed;
      low = mid + 1;
      continue;
    }
    high = mid - 1;
  }
 
  return Number.isFinite(best) ? best : 1;
}
 
export function findRemainingStopSeconds(
  stopWindows: ReadonlyArray<StopWindowPoint> | undefined,
  currentSeconds: number,
): number {
  if (!stopWindows || stopWindows.length === 0 || !Number.isFinite(currentSeconds)) {
    return 0;
  }
 
  let low = 0;
  let high = stopWindows.length - 1;
  let index = -1;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    const point = stopWindows[mid];
    if (point.startSeconds <= currentSeconds) {
      index = mid;
      low = mid + 1;
      continue;
    }
    high = mid - 1;
  }
 
  if (index < 0) {
    return 0;
  }
  const window = stopWindows[index];
  if (currentSeconds >= window.endSeconds) {
    return 0;
  }
  return Math.max(0, window.endSeconds - currentSeconds);
}