All files / chart-model/src reference-bpm.ts

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

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                              251x 251x 4x   247x 247x 245x   2x 1x   1x    
import type { BeMusicJSON } from '@be-music/chart-source';
 
/**
 * Resolves the chart's reference BPM for hi-speed / scroll-speed normalization, honoring the BMS spec for `#BASEBPM`.
 *
 * Spec — `#BASEBPM N` (hitkey BMS Memo): declares the chart's reference BPM for HS-fix style scroll calibration. When
 * set, scroll-speed-aware consumers should treat that value as the "main BPM" instead of the chart's initial `#BPM`.
 * When absent we fall back to the parser-extracted `metadata.bpm`, which is the chart's first declared `#BPM`.
 *
 * `fallbackBPM` is the optional last-resort value the host can supply (e.g. a song-list-cached BPM hint) for charts
 * that omit both `#BASEBPM` and `#BPM` — a rare but legal corner of the spec.
 *
 * Returns `undefined` when no positive BPM is available, so callers can early-out without applying a meaningless ratio.
 */
export function resolveChartReferenceBPM(json: BeMusicJSON, fallbackBPM?: number): number | undefined {
  const baseBPM = json.bms.baseBPM;
  if (typeof baseBPM === 'number' && Number.isFinite(baseBPM) && baseBPM > 0) {
    return baseBPM;
  }
  const metadataBPM = json.metadata.bpm;
  if (typeof metadataBPM === 'number' && Number.isFinite(metadataBPM) && metadataBPM > 0) {
    return metadataBPM;
  }
  if (typeof fallbackBPM === 'number' && Number.isFinite(fallbackBPM) && fallbackBPM > 0) {
    return fallbackBPM;
  }
  return undefined;
}