All files / player-browser/src/chart bpm-range.ts

100% Statements 15/15
75% Branches 9/12
100% Functions 1/1
100% Lines 12/12

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                59x                   23x 23x 17x 17x 23x 23x 27x 27x   17x 23x 23x    
import { resolveChart } from '@be-music/chart-model';
import type { BeMusicJSON } from '@be-music/chart-source';
 
export interface ChartBPMRange {
  readonly min: number;
  readonly max: number;
}
 
const bpmRangeByChart = new WeakMap<BeMusicJSON, ChartBPMRange>();
 
/**
 * The lowest and highest BPM of a chart, as beatoraja's `BMSModel.getMinBPM` / `getMaxBPM` report them: the header
 * BPM and every tempo the resolved chart's tempo map (`ResolvedChart.timing`) plays at — channel `03` and `#BPMxx`
 * (`08`) changes and bmson `bpm_events`, with the format rules applied once (non-positive values ignored, `08` winning
 * over `03` at one position, object keys normalized). Cached per chart object, since song select asks again on every
 * cursor move.
 */
export function resolveChartBPMRange(chart: BeMusicJSON): ChartBPMRange {
  const cached = bpmRangeByChart.get(chart);
  if (cached !== undefined) return cached;
  const header = chart.metadata.bpm;
  let min = header > 0 ? header : Number.POSITIVE_INFINITY;
  let max = header > 0 ? header : 0;
  for (const point of resolveChart(chart).timing.tempoPoints) {
    if (point.bpm < min) min = point.bpm;
    if (point.bpm > max) max = point.bpm;
  }
  const range: ChartBPMRange = Number.isFinite(min) ? { min, max } : { min: 0, max: 0 };
  bpmRangeByChart.set(chart, range);
  return range;
}