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

100% Statements 24/24
90.9% Branches 20/22
100% Functions 6/6
100% Lines 22/22

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                          47x               8x 8x             4x                         223x   223x 246x 22x             246x   223x 14x 14x             223x 223x 223x                   446x 446x 39x 26x 26x                     26x 26x    
import type { ScrollTimelinePoint, SpeedTimelinePoint, TimingResolver } from '@be-music/chart-model';
 
/** What a guide label marks: a BPM change, a STOP, a `#SCROLL` change or a `#SPEED` change. */
export type BPMGuideKind = 'bpm' | 'stop' | 'scroll' | 'speed';
 
export interface BPMGuideEvent {
  readonly beat: number;
  readonly seconds: number;
  readonly kind: BPMGuideKind;
  readonly text: string;
}
 
/** Which kinds of guide label to show; each defaults to on for BPM and STOP (beatoraja's guide) and off for the rest. */
export interface BPMGuideKinds {
  readonly bpm: boolean;
  readonly stop: boolean;
  readonly scroll: boolean;
  readonly speed: boolean;
}
 
/** beatoraja's BPM GUIDE: BPM changes and STOPs, without the `#SCROLL` / `#SPEED` labels Be-Music adds. */
export const DEFAULT_BPM_GUIDE_KINDS: BPMGuideKinds = { bpm: true, stop: true, scroll: false, speed: false };
 
/**
 * The guide labels a play shows. The BPM labels are beatoraja's BPM GUIDE, the play option `bpmGuide` (a light assist
 * on a chart whose BPM changes), so they follow it alone. STOP, `#SCROLL` and `#SPEED` labels are viewing preferences
 * (`kinds`); without them, STOPs follow `bpmGuide` as beatoraja's guide draws them, and `#SCROLL` / `#SPEED` stay off.
 */
export function resolveShownGuideKinds(bpmGuide: boolean | undefined, kinds: BPMGuideKinds | undefined): BPMGuideKinds {
  const bpm = bpmGuide === true;
  return kinds === undefined
    ? { bpm, stop: bpm, scroll: false, speed: false }
    : { bpm, stop: kinds.stop, scroll: kinds.scroll, speed: kinds.speed };
}
 
/** Whether any guide label shows. */
export function showsAnyGuide(kinds: BPMGuideKinds): boolean {
  return kinds.bpm || kinds.stop || kinds.scroll || kinds.speed;
}
 
/**
 * The guide labels of a chart, in beat order. BPM changes and positive STOPs as beatoraja 0.8.9's `LaneRenderer` labels
 * them (`BPM <n>`, `STOP <n>ms`, integers; beatoraja writes `BPM<n>`, Be-Music spaces every label's number alike), and Be-Music's `#SCROLL` / `#SPEED` changes (`SCROLL <x>`, `SPEED <x>`, up to
 * two decimals), each where the value actually changes from the one before (starting from 1).
 */
export function collectBPMGuideEvents(
  timing: TimingResolver,
  scroll: readonly ScrollTimelinePoint[] = [],
  speed: readonly SpeedTimelinePoint[] = [],
): BPMGuideEvent[] {
  const result: BPMGuideEvent[] = [];
  let previousBPM: number | undefined;
  for (const point of timing.tempoPoints) {
    if (previousBPM !== undefined && point.bpm !== previousBPM) {
      result.push({
        beat: point.beat,
        seconds: timing.beatToSeconds(point.beat),
        kind: 'bpm',
        text: `BPM ${Math.trunc(point.bpm)}`,
      });
    }
    previousBPM = point.bpm;
  }
  for (const point of timing.stopPoints) {
    Eif (point.seconds > 0)
      result.push({
        beat: point.beat,
        seconds: timing.beatToSeconds(point.beat),
        kind: 'stop',
        text: `STOP ${Math.trunc(point.seconds * 1000)}ms`,
      });
  }
  pushKeyframes(result, timing, scroll, 'scroll', 'SCROLL');
  pushKeyframes(result, timing, speed, 'speed', 'SPEED');
  return result.sort((a, b) => a.beat - b.beat || a.seconds - b.seconds);
}
 
function pushKeyframes(
  out: BPMGuideEvent[],
  timing: TimingResolver,
  points: readonly { readonly beat: number; readonly speed: number }[],
  kind: 'scroll' | 'speed',
  label: string,
): void {
  let previous = 1;
  for (const point of points) {
    if (!Number.isFinite(point.speed) || point.speed === previous) continue;
    previous = point.speed;
    out.push({
      beat: point.beat,
      seconds: timing.beatToSeconds(point.beat),
      kind,
      text: `${label} ${formatRate(point.speed)}`,
    });
  }
}
 
/** A rate with up to two decimals and no trailing zeros: `0.5`, `2`, `-1.25`. */
function formatRate(value: number): string {
  const rounded = Math.round(value * 100) / 100;
  return String(Object.is(rounded, -0) ? 0 : rounded);
}