All files / player-browser/src/session/play tempo-range.ts

100% Statements 15/15
100% Branches 17/17
100% Functions 1/1
100% Lines 11/11

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                                                    217x 217x   217x 238x 235x 235x 235x   217x 4x 2x   213x    
import { resolveChartMainBPM, type MainBPMChart, type TimingResolver } from '@be-music/chart-model';
 
/**
 * The lowest, highest, and main BPM of a play: the built-in HUD shows them, and HS-FIX `AVERAGE` and the replay's
 * visible time read the main one, on every family's play screen. Not the select screen's `ChartBPMRange`
 * (`chart/bpm-range.ts`), which counts the header `#BPM` too, as beatoraja's `getMinBPM` / `getMaxBPM` do.
 */
export interface PlayTempoRange {
  minBPM: number;
  maxBPM: number;
  mainBPM: number;
}
 
/** What {@link resolvePlayTempoRange} reads of the play's resolved chart (`ResolvedChart`). */
export interface PlayTempoChart extends MainBPMChart {
  readonly timing: Pick<TimingResolver, 'bpmAtBeat' | 'tempoPoints'>;
}
 
/**
 * Works out a play's BPM range from its resolved chart. `minBPM` and `maxBPM` are the lowest and highest BPM of the
 * tempo map; `mainBPM` is the chart's main BPM, the BPM the most judged notes sit at (`resolveChartMainBPM` in
 * `@be-music/chart-model`, beatoraja's), else the start BPM for a chart without notes. Tempo points with a non-positive or
 * non-finite BPM are ignored. Returns `fallbackBPM` for all three when no tempo point is usable, and `undefined` when
 * there is no usable fallback either.
 */
export function resolvePlayTempoRange(chart: PlayTempoChart, fallbackBPM?: number): PlayTempoRange | undefined {
  let minBPM = Number.POSITIVE_INFINITY;
  let maxBPM = Number.NEGATIVE_INFINITY;
  let startBPM: number | undefined;
  for (const { bpm } of chart.timing.tempoPoints) {
    if (!Number.isFinite(bpm) || bpm <= 0) continue;
    startBPM ??= bpm;
    if (bpm < minBPM) minBPM = bpm;
    if (bpm > maxBPM) maxBPM = bpm;
  }
  if (startBPM === undefined) {
    if (fallbackBPM === undefined || !Number.isFinite(fallbackBPM) || fallbackBPM <= 0) return undefined;
    return { minBPM: fallbackBPM, maxBPM: fallbackBPM, mainBPM: fallbackBPM };
  }
  return { minBPM, maxBPM, mainBPM: resolveChartMainBPM(chart) ?? startBPM };
}