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

94.73% Statements 18/19
94.44% Branches 17/18
100% Functions 2/2
100% Lines 18/18

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                                          282x 282x 1939x 1939x 1939x 1939x 1637x 1637x   302x     282x 1932x 1932x 7x         282x 302x 285x 285x     282x    
import type { ResolvedChart } from './resolved-chart.ts';
 
/** What {@link resolveChartMainBPM} reads of a resolved chart. */
export interface MainBPMChart {
  readonly notes: ReadonlyArray<Pick<ResolvedChart['notes'][number], 'kind' | 'beat' | 'endBeat' | 'lnMode'>>;
  readonly timing: Pick<ResolvedChart['timing'], 'bpmAtBeat'>;
}
 
/**
 * The chart's main BPM: the BPM the most judged notes sit at. It is beatoraja's main BPM, which its play screen pegs
 * HS-FIX `MAINBPM` to and takes the white and green numbers at the main BPM at (`LaneRenderer.init`: a count of
 * `TimeLine.getTotalNotes()` per timeline BPM; its song database and BPM graph count the same way). A tap and a
 * long-note head count once; a long-note tail counts too when its mode judges it (CN / HCN); mines and invisible notes
 * do not count. A note on a BPM change counts at the new BPM. Unlike a time average or the BPM held longest, it is not
 * moved by the split-second BPM spikes of BPM × N + STOP gimmicks or by stretches without notes. Ties go to the BPM
 * reached first (beatoraja's pick among ties follows its `HashMap` order). `undefined` for a chart without notes.
 *
 * The browser player reads the main BPM only from here: HS-FIX `AVERAGE`, the replay's `visibleMs`, the HUD and the
 * beatoraja skins' main-BPM numbers and BPM graph.
 */
export function resolveChartMainBPM(chart: MainBPMChart): number | undefined {
  const counts = new Map<number, { count: number; firstBeat: number }>();
  const add = (beat: number): void => {
    const bpm = chart.timing.bpmAtBeat(beat);
    Iif (!Number.isFinite(bpm) || bpm <= 0) return;
    const entry = counts.get(bpm);
    if (entry) {
      entry.count += 1;
      entry.firstBeat = Math.min(entry.firstBeat, beat);
    } else {
      counts.set(bpm, { count: 1, firstBeat: beat });
    }
  };
  for (const note of chart.notes) {
    add(note.beat);
    if (note.kind === 'ln' && note.endBeat !== undefined && (note.lnMode === 2 || note.lnMode === 3)) {
      add(note.endBeat);
    }
  }
  let main: number | undefined;
  let best: { count: number; firstBeat: number } | undefined;
  for (const [bpm, entry] of counts) {
    if (!best || entry.count > best.count || (entry.count === best.count && entry.firstBeat < best.firstBeat)) {
      main = bpm;
      best = entry;
    }
  }
  return main;
}