All files / player-engine/src/core tempo-clock.ts

98.18% Statements 54/55
84.61% Branches 33/39
100% Functions 12/12
100% Lines 46/46

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                                                                                      3065x       3031x 3031x   3031x   3031x 935895x 12062x   923836x   3031x 496966x 6556x   490413x   3031x 814884x   814884x   3031x 496966x 496966x   3031x 29x     29x 2x   2x 2x   27x     3031x     234916x           121011x 121011x 6020x     31x 31x 31x 31x 28x 28x     1x 1x 1x 1x 1x 1x     305x 305x 305x         305x        
/**
 * The play tempo over the play clock: chart time runs `tempo` times as fast as the play clock, and the tempo can change
 * during the play. Each change starts a segment at the chart time it took effect, so chart time stays continuous across
 * it. Pure: it reads no clock itself, the caller hands it play-clock milliseconds.
 */
 
/** One stretch of the play at one tempo, from its start on the play clock and in chart time. */
export interface TempoClockSegment {
  /** Play-clock milliseconds the segment starts at. */
  clockMs: number;
  /** Chart seconds the segment starts at. */
  chartSeconds: number;
  /** Chart seconds per play-clock second in the segment. */
  tempo: number;
}
 
export interface TempoClock {
  /** The tempo the play started at. */
  readonly initialTempo: number;
  /** The tempo in force now (the last change's, or the initial one). */
  readonly tempo: number;
  /** The segments so far, in time order; the first starts at 0 with {@link initialTempo}. */
  readonly segments: readonly TempoClockSegment[];
  /** Chart seconds at a play-clock time (0 before the start). */
  chartSecondsAt(clockMs: number): number;
  /** Play-clock milliseconds at a chart time; past the last change it runs at the current tempo. */
  clockMsAt(chartSeconds: number): number;
  /**
   * The chart time at a play-clock time as milliseconds at the initial tempo: the play clock itself until the first
   * change, and chart time scaled by `1000 / initialTempo` throughout, so it stays linear in chart time.
   */
  initialTempoMsAt(clockMs: number): number;
  /**
   * Changes the tempo at a play-clock time (clamped to the start of the last segment, so time never runs backwards).
   * Returns the chart seconds the change took effect at. A change to the tempo in force, or to a non-positive or
   * non-finite one, changes nothing.
   */
  setTempoAtClockMs(clockMs: number, tempo: number): number;
  /**
   * Changes the tempo at a chart time (clamped to the start of the last segment), e.g. to follow a change another
   * clock already made. Returns the play-clock milliseconds the change took effect at.
   */
  setTempoAtChartSeconds(chartSeconds: number, tempo: number): number;
  /**
   * Moves chart time to `chartSeconds` at play-clock time `clockMs` (a seek): the segments so far are dropped, and one
   * segment at `tempo` (the tempo in force, when absent or unusable) starts there. Chart time runs on from it as before;
   * {@link initialTempoMsAt} stays linear in chart time.
   */
  rebase(clockMs: number, chartSeconds: number, tempo?: number): void;
}
 
const isUsableTempo = (tempo: number): boolean => Number.isFinite(tempo) && tempo > 0;
 
/** A {@link TempoClock} starting at `initialTempo` (1 when unusable). */
export function createTempoClock(initialTempo = 1): TempoClock {
  const startTempo = isUsableTempo(initialTempo) ? initialTempo : 1;
  const segments: TempoClockSegment[] = [{ clockMs: 0, chartSeconds: 0, tempo: startTempo }];
  // Whether the first segment still is the play's own start (chart second 0 at play-clock 0, at the initial tempo).
  let startsAtZero = true;
 
  const segmentAtClockMs = (clockMs: number): TempoClockSegment => {
    for (let index = segments.length - 1; index > 0; index -= 1) {
      if (segments[index]!.clockMs <= clockMs) return segments[index]!;
    }
    return segments[0]!;
  };
  const segmentAtChartSeconds = (chartSeconds: number): TempoClockSegment => {
    for (let index = segments.length - 1; index > 0; index -= 1) {
      if (segments[index]!.chartSeconds <= chartSeconds) return segments[index]!;
    }
    return segments[0]!;
  };
  const chartSecondsAt = (clockMs: number): number => {
    const segment = segmentAtClockMs(clockMs);
    // `(ms / 1000) * tempo`, as the fixed-tempo engine computed it, so a play without changes reads the same values.
    return Math.max(0, segment.chartSeconds + ((clockMs - segment.clockMs) / 1000) * segment.tempo);
  };
  const clockMsAt = (chartSeconds: number): number => {
    const segment = segmentAtChartSeconds(chartSeconds);
    return segment.clockMs + ((chartSeconds - segment.chartSeconds) * 1000) / segment.tempo;
  };
  const append = (clockMs: number, chartSeconds: number, tempo: number): void => {
    const last = segments[segments.length - 1]!;
    // A second change at the same instant replaces the first (the initial segment is never rewritten, so
    // `initialTempo` stays the tempo of the first segment).
    if (segments.length > 1 && clockMs <= last.clockMs) {
      last.tempo = tempo;
      // Merge back into the previous segment when the change undoes the last one.
      if (segments[segments.length - 2]!.tempo === tempo) segments.pop();
      return;
    }
    segments.push({ clockMs, chartSeconds, tempo });
  };
 
  return {
    initialTempo: startTempo,
    get tempo() {
      return segments[segments.length - 1]!.tempo;
    },
    segments,
    chartSecondsAt,
    clockMsAt,
    initialTempoMsAt: (clockMs) => {
      const segment = segmentAtClockMs(clockMs);
      if (startsAtZero && segment === segments[0]) return clockMs;
      return (chartSecondsAt(clockMs) * 1000) / startTempo;
    },
    setTempoAtClockMs: (clockMs, tempo) => {
      const last = segments[segments.length - 1]!;
      const atMs = Math.max(last.clockMs, Number.isFinite(clockMs) ? clockMs : last.clockMs);
      const chartSeconds = chartSecondsAt(atMs);
      if (!isUsableTempo(tempo) || tempo === last.tempo) return chartSeconds;
      append(atMs, chartSeconds, tempo);
      return chartSeconds;
    },
    setTempoAtChartSeconds: (chartSeconds, tempo) => {
      const last = segments[segments.length - 1]!;
      const atSeconds = Math.max(last.chartSeconds, Number.isFinite(chartSeconds) ? chartSeconds : last.chartSeconds);
      const clockMs = clockMsAt(atSeconds);
      Iif (!isUsableTempo(tempo) || tempo === last.tempo) return clockMs;
      append(clockMs, atSeconds, tempo);
      return clockMs;
    },
    rebase: (clockMs, chartSeconds, tempo) => {
      const nextTempo = tempo !== undefined && isUsableTempo(tempo) ? tempo : segments[segments.length - 1]!.tempo;
      segments.length = 0;
      segments.push({
        clockMs: Number.isFinite(clockMs) ? clockMs : 0,
        chartSeconds: Number.isFinite(chartSeconds) ? Math.max(0, chartSeconds) : 0,
        tempo: nextTempo,
      });
      startsAtZero = false;
    },
  };
}