All files / player-terminal/src/tui high-speed.ts

100% Statements 19/19
90.47% Branches 19/21
100% Functions 3/3
100% Lines 19/19

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      4x 4x 4x 4x     235x 1x   234x       69x 69x 69x 69x 69x                     6x 6x 6x 1x   5x 6x 6x    
import { DEFAULT_HIGH_SPEED, MAX_HIGH_SPEED, MIN_HIGH_SPEED } from '@be-music/player-engine/signals';
import { clamp } from '@be-music/shared-utils/core';
 
const _____MEASURE_BEATS = 4;
const _____MEASURE_ROWS_AT_HS1 = 16;
const MIN_SCROLL_WINDOW_BEATS = 0.25;
const MIN_GRID_ROWS = 4;
 
export function normalizeHighSpeed(value: number): number {
  if (!Number.isFinite(value) || value <= 0) {
    return DEFAULT_HIGH_SPEED;
  }
  return Math.min(MAX_HIGH_SPEED, Math.max(MIN_HIGH_SPEED, value));
}
 
export function resolveVisibleBeatsForTUIGrid(rowCount: number, highSpeed: number, authored: boolean = false): number {
  const safeRows = Number.isFinite(rowCount) ? Math.max(MIN_GRID_ROWS, Math.floor(rowCount)) : MIN_GRID_ROWS;
  const exactAuthored = authored && Number.isFinite(highSpeed) && highSpeed >= 0.25 && highSpeed <= 100;
  const safeHighSpeed = exactAuthored ? highSpeed : normalizeHighSpeed(highSpeed);
  const visibleMeasures = safeRows / (_____MEASURE_ROWS_AT_HS1 * safeHighSpeed);
  return exactAuthored
    ? visibleMeasures * _____MEASURE_BEATS
    : Math.max(MIN_SCROLL_WINDOW_BEATS, visibleMeasures * _____MEASURE_BEATS);
}
 
export function resolveAnimatedHighSpeedValue(
  fromHighSpeed: number,
  toHighSpeed: number,
  elapsedMs: number,
  durationMs: number,
): number {
  const from = normalizeHighSpeed(fromHighSpeed);
  const to = normalizeHighSpeed(toHighSpeed);
  if (!Number.isFinite(durationMs) || durationMs <= 0) {
    return to;
  }
  const safeElapsedMs = Number.isFinite(elapsedMs) ? elapsedMs : 0;
  const ratio = clamp(safeElapsedMs / durationMs, 0, 1);
  return from + (to - from) * ratio;
}