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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 3019x 3019x 3019x 3100x 3019x 3019x 3019x 23x 1x 1x 22x 3019x 108x 20x 20x 20x 3x 20x 3019x 3019x 3019x 3019x 74x 43x 3019x 269108x 6137x 69x 6x 1x 5x 4x 150x 5881x 5877x 2x 2x 5875x 69x 265898x 265898x 265898x 5x 5x 5x 265650x 475x 475x 11x 11x 204x 92x 92x 204x 6x 6x 4x 4x 92x 92x 3013x 1x 1x 3019x 3123x 278165x | import { resolveChartOptionsAt } from '@be-music/chart-model';
import { formatSeconds } from '../utils.ts';
import {
applyPlaybackHighSpeedAction,
applyPlaybackTempoChange,
consumePlaybackInputCommands,
recordPendingViewChanges,
togglePlaybackPause,
} from './playback-support.ts';
import { writeRuntimeEventLog } from './engine-trace.ts';
import type { PlayerInterruptReason } from './engine-types.ts';
import type { PlaySession } from './play-session.ts';
import type { PlayJudge } from './play-strategy.ts';
/**
* Playback controls: the input commands every play takes the same way — pause, HIGH-SPEED, tempo changes (live, or a
* replay following its log's), view changes for the replay, seeks and interrupts — and the lane commands it hands its
* judge.
*/
export interface PlayControls {
/** Drains the input bus and applies every command on it. */
consume(): void;
/** The host opened the chart-start gate, or no command gate was configured. */
readonly startRequested: boolean;
/**
* The seek to apply on this tick, if one is waiting: the last `seek` command drained so far (seeks coalesce, the last
* one wins), or the play's start (`PlayerOptions.startAtSeconds`) until the chart starts. Taking it clears it.
*/
takeSeek(): number | undefined;
/** Applies the replay's recorded tempo changes due by `nowSec`. */
followReplayTempoChanges(nowSec: number): void;
/** Restores the replay tempo and cursor at a seek, including while paused. */
seekReplayTempoChanges(seconds: number, nowMs: number): void;
/** Interrupts the play at chart second `seconds` (a replay reaching the instant its log was aborted at). */
interrupt(reason: PlayerInterruptReason, seconds: number): void;
/** The chart has started: applies a pause sent during the lead-in. */
chartStarted(): void;
/** Records the view changes still on the input bus once the loop is over (sent as the play ended). */
recordPendingViewChanges(): void;
/** The interrupt the play received, if any. */
readonly interruptedReason: PlayerInterruptReason | undefined;
}
export function createPlayControls(session: PlaySession, judge: PlayJudge): PlayControls {
const {
options,
tempoClock,
uiEnabled,
writeOutput,
audioSession,
activeStateSignals,
replayRecorder,
inputSignals,
} = session;
let highSpeed = session.highSpeed;
let startRequested = options.waitForStart !== true;
let interruptedReason: PlayerInterruptReason | undefined;
// The chart second a command takes effect at: the play clock's, or the chart's start before it runs.
const commandSeconds = (): number => (session.clock ? tempoClock.chartSecondsAt(session.clock.nowMs()) : 0);
// A pause sent during the lead-in holds the chart at its start.
let pauseAtStart = false;
// The seek waiting for the next tick: the play's start first, when its judge can start partway.
let pendingSeek: number | undefined =
judge.seek !== undefined && session.startAtSeconds > 0 ? session.startAtSeconds : undefined;
const togglePause = (): void => {
if (session.clock === undefined) {
pauseAtStart = !pauseAtStart;
return;
}
togglePlaybackPause({
playbackClock: session.clock,
audioSession,
activeStateSignals,
onStateChange: !uiEnabled
? (state, nowMs) => {
writeRuntimeEventLog(writeOutput, 'playback-state', [
['time', formatSeconds(tempoClock.chartSecondsAt(nowMs))],
['state', state],
]);
}
: undefined,
});
};
const changeTempo = (tempo: number): void => {
applyPlaybackTempoChange({
tempoClock,
// Before the chart starts (the lead-in) a change sets the tempo it starts at.
nowMs: session.clock?.nowMs() ?? 0,
tempo: tempo * session.chartBaseSpeed,
setAudioTempo: (next) => audioSession?.setTempo?.(next),
// A replay's tempo only sets how fast it is re-driven: its re-recorded log carries none.
recordTempoChange: (seconds) => {
if (!session.replay) replayRecorder?.recordTempoChange(seconds, tempo);
},
onTempoChange: (change) => {
if (!uiEnabled) {
writeRuntimeEventLog(writeOutput, 'tempo-change', [
['time', formatSeconds(change.seconds)],
['value', `x${change.tempo}`],
]);
}
options.onTempoChange?.({ ...change, tempo });
},
});
};
// A replay follows its log's tempo changes as its chart time reaches each; a live play takes its tempo from commands.
const replayTempoChanges = session.replay ? (options.replayTempoChanges ?? []) : [];
let replayTempoChangeIndex = 0;
const handleLaneCommand = judge.handleLaneCommand?.bind(judge);
const traceInterrupt = (reason: PlayerInterruptReason, seconds: number): void => {
if (uiEnabled || session.clock === undefined) return;
writeRuntimeEventLog(writeOutput, 'interrupt', [
['time', formatSeconds(seconds)],
['reason', reason],
]);
};
return {
consume: () => {
consumePlaybackInputCommands({
inputSignals,
isInterrupted: () => interruptedReason !== undefined,
setInterruptedReason: (reason) => {
interruptedReason = reason;
},
onTogglePause: togglePause,
onHighSpeedAction: (action) => {
if (
resolveChartOptionsAt(
session.chartOptions,
Math.round(Math.min(commandSeconds(), session.resolvedChart.reversal?.seconds ?? Infinity) * 1_000_000),
).hiSpeed !== undefined
)
return;
highSpeed = applyPlaybackHighSpeedAction({
action,
currentHighSpeed: highSpeed,
activeStateSignals,
onHighSpeedChange: options.onHighSpeedChange,
logHighSpeedChange: !uiEnabled
? (nextHighSpeed) => {
writeRuntimeEventLog(writeOutput, 'high-speed-change', [
['time', formatSeconds(commandSeconds())],
['value', `x${nextHighSpeed.toFixed(1)}`],
]);
}
: undefined,
});
},
onOptionChange: (values) => {
if (typeof values.judgeTimingMs === 'number' && Number.isSafeInteger(values.judgeTimingMs))
options.judgeTimingMs = values.judgeTimingMs;
replayRecorder?.recordOptionChange(commandSeconds(), values);
},
onViewChange: (change) => replayRecorder?.recordViewChange(commandSeconds(), change),
onTempoChange: changeTempo,
onSeek: (seconds) => {
Eif (judge.seek !== undefined) pendingSeek = seconds;
},
// Lane and key-state commands go to the judge; a strategy without live key input drops them.
onUnhandledCommand: (command) => {
if (command.kind === 'start') startRequested = true;
else if (command.kind === 'control-input') {
Eif (!session.replay) {
replayRecorder?.recordControlInput(command.action, commandSeconds(), command.button, command.code);
}
} else Eif (command.kind === 'lane-input' || command.kind === 'kitty-state') handleLaneCommand?.(command);
},
logInterrupt: (reason) => traceInterrupt(reason, commandSeconds()),
});
},
takeSeek: () => {
const seconds = pendingSeek;
pendingSeek = undefined;
return seconds;
},
interrupt: (reason, seconds) => {
Iif (interruptedReason !== undefined) return;
traceInterrupt(reason, seconds);
interruptedReason = reason;
},
followReplayTempoChanges: (nowSec) => {
while (replayTempoChangeIndex < replayTempoChanges.length) {
const change = replayTempoChanges[replayTempoChangeIndex]!;
if (change.timeUs / 1_000_000 > nowSec) return;
replayTempoChangeIndex += 1;
changeTempo(change.tempo);
}
},
seekReplayTempoChanges: (seconds, nowMs) => {
if (!session.replay) return;
replayTempoChangeIndex = 0;
let tempo = options.speed ?? 1;
while (replayTempoChangeIndex < replayTempoChanges.length) {
const change = replayTempoChanges[replayTempoChangeIndex]!;
if (change.timeUs / 1_000_000 > seconds) break;
Eif (Number.isFinite(change.tempo) && change.tempo > 0) tempo = change.tempo;
replayTempoChangeIndex += 1;
}
tempoClock.rebase(nowMs, seconds);
changeTempo(tempo);
},
chartStarted: () => {
if (pauseAtStart) {
pauseAtStart = false;
togglePause();
}
},
recordPendingViewChanges: () => {
recordPendingViewChanges(inputSignals, replayRecorder, Math.max(0, commandSeconds()));
},
get startRequested() {
return startRequested;
},
get interruptedReason() {
return interruptedReason;
},
};
}
|