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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | 22x 86x 86x 86x 86x 86x 86x 86x 86x 31x 13x 13x 18x 18x 86x 52x 52x 52x 52x 10x 10x 52x 110x 110x 3x 2x 110x 110x 110x 3x 3x 3x 2x 2x 3x 3x 2x 122x 122x 34x 122x 41x 41x 35x 35x 35x 35x 76x 17x 17x 4x 4x 4x 4x 4x 4x 13x 13x 2x 2x 3x 3x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 15x 10x 7x 26x 26x 51x 26x 52x 52x | /**
* What the player sees of a play on the built-in and LR2 play screens (`PlaySession`), and how it changes: the
* hi-speed (the hotkeys, HS-FIX), the play tempo the engine follows, the lane covers, the visible time, and the play
* replay's view, recorded as it changes or shown back from a replay.
*
* The functions read and write the scene's state through {@link PlayViewSettingsContext}; the play session passes itself.
*/
import type { PlayerTempoChange } from '@be-music/player-engine';
import type { PlayerInputSignalBus, PlayerUIFramePayload } from '@be-music/player-engine/signals';
import { resolveChartPlayfieldVisual } from '@be-music/chart-model';
import {
createReplayViewCoalescer,
DEFAULT_REPLAY_PLAYBACK_OPTIONS,
resolveReplayHiSpeed,
resolveReplayView,
resolveReplayOptionsAt,
type ReplayViewCoalescer,
type ReplayViewState,
} from '@be-music/player-engine/replay';
import {
AC32_HI_SPEED_BASE,
applyChartLaneCovers,
BE_MUSIC_HI_SPEED_SCALE,
resolveHiSpeedForWhiteNumber,
resolveLaneCover,
normalizeFloatingLaneCover,
captureReplayView,
resolveWhiteNumber,
type LaneCover,
type HiSpeedCommand,
type LaneCoverCommand,
type LaneCoverState,
type ReplayViewCursor,
} from '@be-music/player-scene-model';
import { setVideoPlaybackTempo } from '../../runtime/play-tempo.ts';
import type { PlaySessionOptions } from './play-options.ts';
import type { PlayTempoRange } from './tempo-range.ts';
import { PIXELS_PER_BEAT } from '../../stage/design.ts';
import type { BeMusicLaneCover } from '../../skin/be-music/types.ts';
import type { PlayReplayViewScale, PlaySessionTheme } from './play-session-theme.ts';
import { applyPlayEvent, type PlayStateContext } from './play-state.ts';
import type { PlayfieldLaneRect } from '../../view/types.ts';
import { DEFAULT_CONTROL_ACTIONS, type ControlActionMap } from '../controls/control-actions.ts';
/** The scene's members the view settings read and write: the play session's members of these names. */
export interface PlayViewSettingsContext extends PlayStateContext {
readonly options: Readonly<Pick<PlaySessionOptions, 'onHiSpeedChange' | 'onTempoChange' | 'replayView' | 'replay'>>;
readonly timingResolver?: Pick<import('@be-music/chart-model').TimingResolver, 'bpmAtBeat'>;
currentBeat?(seconds: number): number;
readonly disposed: boolean;
tempo: number;
readonly playbackTempo?: number;
readonly bgaVideos: Map<string, { video: HTMLVideoElement; objectURL: string }>;
readonly laneX: Map<string, PlayfieldLaneRect>;
readonly chartBPMRange: PlayTempoRange | undefined;
drawnLaneCovers:
| { covers: readonly LaneCoverState[]; manualSudden: boolean; lineLift: number; drawn: LaneCover[] }
| undefined;
/** The engine's latest frame: a chart's M-SUDDEN and LINEMOVE (#931) change the covers as drawn. */
readonly engineFrame?: Pick<PlayerUIFramePayload, 'chartOptions' | 'currentSeconds'>;
replayViewCoalescer: ReplayViewCoalescer | undefined;
readonly replayViewCursor: ReplayViewCursor | undefined;
readonly sharedEngineInputSignals: PlayerInputSignalBus | undefined;
readonly theme: PlaySessionTheme;
currentSeconds(): number;
}
/**
* The player's hi-speed keys: step the player's hi-speed by `delta` (the play state keeps it on the 1/1000 grid and in
* range, and ignores it while a course locks the hi-speed); HS-FIX scales the drawn one, and the note scroll glides to
* it. The host hears of the player's new hi-speed, to remember it for the next play. Returns whether it changed.
*/
export function adjustHiSpeed(ctx: PlayViewSettingsContext, delta: number, fine = false): boolean {
return changePlayView(
ctx,
fine && ctx.playState.view.hiSpeed.floating
? { type: 'hi-speed-fine', direction: delta }
: { type: 'hi-speed-step', delta },
);
}
/** Apply an interactive setting at the current BPM; replays apply their recorded views separately. */
export function changePlayView(ctx: PlayViewSettingsContext, command: HiSpeedCommand | LaneCoverCommand): boolean {
const bpm = ctx.timingResolver?.bpmAtBeat(ctx.currentBeat?.(ctx.currentSeconds()) ?? 0);
const rawCover = ctx.playState.view.covers[0];
const cover =
rawCover && command.type === 'floating-speed-mode' && command.enabled && !ctx.playState.view.fixedDisplayArea
? normalizeFloatingLaneCover(rawCover)
: rawCover;
const visibleFraction = Math.max(
0,
1 - (cover?.suddenOn ? cover.sudden : 0) - ((cover?.liftOn ?? false) ? (cover?.lift ?? 0) : 0),
);
const event =
command.type === 'floating-speed-mode' ||
command.type === 'floating-speed-recalculate' ||
command.type === 'floating-speed-restore'
? { ...command, bpm: bpm ?? command.bpm, visibleFraction }
: command.type === 'hi-speed-step' ||
command.type === 'hi-speed-set' ||
command.type === 'hi-speed-fine' ||
command.type === 'cover-adjust' ||
command.type === 'cover-toggle'
? { ...command, bpm, ...(command.type === 'hi-speed-fine' ? { visibleFraction } : {}) }
: command;
const previous = applyPlayEvent(ctx, event);
const next = ctx.playState.view.hiSpeed;
if (
next.player !== previous.view.hiSpeed.player ||
next.normalLevel !== previous.view.hiSpeed.normalLevel ||
(next.floating !== undefined) !== (previous.view.hiSpeed.floating !== undefined)
) {
if (next.floating || previous.view.hiSpeed.floating) {
// Preserve accepted mode switches even when several happen before the next rendered frame.
ctx.sharedEngineInputSignals?.pushCommand({
kind: 'options',
values: {
floatingHiSpeed: next.floating !== undefined,
hiSpeed: next.player,
normalHiSpeedLevel: next.normalLevel ?? 0,
floatingHiSpeedProfile: 'AC32-measured',
},
});
ctx.options.onHiSpeedChange?.(next.player, next.normalLevel, next.floating !== undefined);
} else Iif (next.normalLevel !== undefined) ctx.options.onHiSpeedChange?.(next.player, next.normalLevel);
else ctx.options.onHiSpeedChange?.(next.player);
}
return ctx.playState !== previous;
}
/**
* Follows a tempo change the engine made: the BGA videos and the host. The chart's clock follows from the engine's next
* frame, and the engine's audio session moves a NONSTOP chart's fades.
*/
export function followTempoChange(ctx: PlayViewSettingsContext, change: PlayerTempoChange): void {
if (ctx.disposed) return;
ctx.tempo = change.tempo;
setVideoPlaybackTempo(
[...ctx.bgaVideos.values()].map((handle) => handle.video),
ctx.playbackTempo ?? change.tempo,
);
ctx.options.onTempoChange?.(change.tempo);
}
/**
* The lane covers of each play side with lanes as drawn ({@link renderShutter} draws them; a skin that sets
* `drawsLaneCover` reads them to draw its own): the play state's covers through `resolveLaneCover`, a replay's
* included, under a chart's M-SUDDEN and LINEMOVE (`resolveChartCovers`). The array is kept until they change.
*/
export function resolveLaneCovers(ctx: PlayViewSettingsContext): readonly BeMusicLaneCover[] {
const covers = ctx.playState.view.covers;
const effect = resolveChartPlayfieldVisual(ctx.engineFrame?.chartOptions, ctx.engineFrame?.currentSeconds ?? 0);
let cache = ctx.drawnLaneCovers;
if (
cache === undefined ||
cache.covers !== covers ||
cache.manualSudden !== effect.manualSudden ||
cache.lineLift !== effect.lineLift
) {
cache = {
covers,
manualSudden: effect.manualSudden,
lineLift: effect.lineLift,
drawn: applyChartLaneCovers(covers, effect).map(resolveLaneCover),
};
ctx.drawnLaneCovers = cache;
}
return cache.drawn;
}
/**
* HIDDEN_M-SUDDEN's cover keys (#931): F3 lowers the player's SUDDEN+ cover (more of the lane covered) and F4 raises
* it, by 1 % of the lane (5 % with Shift), as nanasigroove's F3 / F4. F5 stays the restart key, so there is no reset
* key. The step moves the cover's height only, keeping whether the player has it switched on, and is taken while a
* course locks the covers: the chart asked for the cover. The keys are the player's (`actions`: `play.m-sudden-down` and
* `play.m-sudden-up`), F3 / F4 by default. Returns whether `event` was such a key.
*/
export function handleChartManualSuddenKey(
ctx: PlayViewSettingsContext,
event: Pick<KeyboardEvent, 'code' | 'shiftKey'>,
actions: ControlActionMap = DEFAULT_CONTROL_ACTIONS.lr2,
): boolean {
const down = actions.is('play.m-sudden-down', event);
if (!down && !actions.is('play.m-sudden-up', event)) return false;
if (!resolveChartPlayfieldVisual(ctx.engineFrame?.chartOptions, 0).manualSudden) return false;
const delta = (down ? 1 : -1) * (event.shiftKey ? 0.05 : 0.01);
const covers = ctx.playState.view.covers.map((cover) => ({
side: cover.side,
sudden: Math.min(1, Math.max(0, Math.round((cover.sudden + delta) * 10_000) / 10_000)),
}));
changePlayView(ctx, { type: 'cover-set', covers });
return true;
}
/**
* Lengthens (or shortens) how long a note stays visible by `deltaMs` (beatoraja's duration, the white number), the way
* beatoraja's SELECT + a key does: the hi-speed is scaled so that the white number at the current BPM, below the
* covers, moves by `deltaMs` (to 1 ms at least). Returns whether it changed; nothing changes while a course locks the
* hi-speed, before the lanes are laid out, or while the BPM is unknown.
*/
export function adjustWhiteNumber(ctx: PlayViewSettingsContext, deltaMs: number): boolean {
Iif (ctx.playState.view.hiSpeed.locked) return false;
const bpm = ctx.timingResolver?.bpmAtBeat(ctx.currentBeat?.(ctx.currentSeconds()) ?? 0);
if (bpm === undefined || !(bpm > 0)) return false;
const cover = ctx.playState.view.covers[0];
const hidden = (cover?.suddenOn ? cover.sudden : 0) + ((cover?.liftOn ?? false) ? (cover?.lift ?? 0) : 0);
const white = resolveWhiteNumber({
laneBeats: resolveLaneBeats(ctx),
hiSpeed: ctx.playState.view.hiSpeed.drawn,
sudden: Math.min(1, hidden),
bpm,
});
Iif (!(white > 0)) return false;
return changePlayView(ctx, { type: 'hi-speed-scale', multiplier: white / Math.max(1, white + deltaMs) });
}
/** The scale the replay reads the view's hi-speed on, and the main BPM it takes the visible time at. */
interface ReplayScale extends PlayReplayViewScale {
readonly mainBPM?: number;
}
/**
* The scale the replay reads the view's hi-speed on: the view's own (`PlaySessionTheme.replayView`, the beatoraja
* view's lane of four beats), else the built-in and LR2 scale over the lanes laid out last frame. Every family takes
* the visible time at the play's main BPM (`chartBPMRange`, the one HS-FIX reads). `undefined` until the lanes are
* laid out.
*/
function resolveReplayScale(ctx: PlayViewSettingsContext): ReplayScale | undefined {
const scale =
ctx.theme.replayView?.() ??
(ctx.laneX.size === 0 ? undefined : { scale: BE_MUSIC_HI_SPEED_SCALE, laneBeats: resolveLaneBeats(ctx) });
if (scale === undefined) return undefined;
const mainBPM = ctx.playState.view.hsFix === 'CONSTANT' ? 150 : ctx.chartBPMRange?.mainBPM;
return mainBPM !== undefined ? { ...scale, mainBPM } : scale;
}
/**
* What the player sees now in the replay's units (`resolveReplayView`): the drawn hi-speed on this scene's scale,
* the HS-FIX, each side's covers, and the visible time at the main BPM without `#SCROLL` — the white number over the
* lanes laid out last frame, the LIFT left out as beatoraja leaves it out — so another scale's replay can match it.
* Rounded to the replay's precision, so cover fractions record without float noise.
*/
function currentReplayView(ctx: PlayViewSettingsContext): ReplayViewState {
const scale = resolveReplayScale(ctx);
return captureReplayView(ctx.playState.view, {
scale: scale?.scale ?? BE_MUSIC_HI_SPEED_SCALE,
laneBeats: scale?.laneBeats ?? 0,
...(scale?.mainBPM !== undefined ? { mainBPM: scale.mainBPM } : {}),
});
}
/** The view the recorded replay starts from (`play.view`); later changes go through {@link updateReplayView}. */
export function startReplayView(ctx: PlayViewSettingsContext): ReplayViewState {
syncMeasuredSourceScale(ctx);
const view = currentReplayView(ctx);
ctx.replayViewCoalescer = createReplayViewCoalescer(view, { minIntervalMs: 0 });
return view;
}
/**
* Per frame, once the lanes are laid out: while recording, sends the engine what changed in the view on every rendered frame where it changes; while replaying, shows the recorded view at the playhead as far as
* {@link PlaySessionOptions.replayView} asks.
*/
export function updateReplayView(ctx: PlayViewSettingsContext, seconds: number, seek = false): void {
if (resolveReplayScale(ctx) === undefined) return;
syncMeasuredSourceScale(ctx);
if (ctx.replayViewCursor !== undefined) {
const timeUs = Math.round(seconds * 1_000_000);
const replay = ctx.options.replay;
Iif ((ctx.options.replayView?.hiSpeed ?? true) && replay) {
const selected = resolveReplayOptionsAt(replay.play.options, replay.play.optionChanges, timeUs);
if (
typeof selected.floatingHiSpeed === 'boolean' &&
selected.floatingHiSpeed !== (ctx.playState.view.hiSpeed.floating !== undefined)
) {
const cover = ctx.playState.view.covers[0];
applyPlayEvent(ctx, {
type: 'floating-speed-mode',
enabled: selected.floatingHiSpeed,
bpm: ctx.timingResolver?.bpmAtBeat(ctx.currentBeat?.(seconds) ?? 0) ?? 120,
visibleFraction: Math.max(0, 1 - (cover?.suddenOn ? cover.sudden : 0) - (cover?.liftOn ? cover.lift : 0)),
});
}
}
const recorded = seek ? ctx.replayViewCursor.seek(timeUs) : ctx.replayViewCursor.next(timeUs);
Eif (recorded !== undefined) applyReplayView(ctx, recorded);
return;
}
const signals = ctx.sharedEngineInputSignals;
if (ctx.replayViewCoalescer === undefined || signals === undefined) return;
const change = ctx.replayViewCoalescer.observe(currentReplayView(ctx), performance.now());
Eif (change !== undefined) signals.pushCommand({ kind: 'view', change });
}
/** Sends the engine the view change still held back by the coalescer, as the play ends ({@link updateReplayView}). */
export function flushReplayView(ctx: PlayViewSettingsContext): void {
const signals = ctx.sharedEngineInputSignals;
if (ctx.replayViewCursor !== undefined || ctx.replayViewCoalescer === undefined || signals === undefined) return;
Eif (resolveReplayScale(ctx) === undefined) return;
const change = ctx.replayViewCoalescer.flush(currentReplayView(ctx));
if (change !== undefined) signals.pushCommand({ kind: 'view', change });
}
/**
* Shows a replay's recorded view over the viewer's own: the covers as recorded (SUDDEN+ and HIDDEN+, and the LIFT on a
* view that draws one, `PlaySessionTheme.drawsLift`), and the hi-speed as recorded on this scale, or matching the
* recorded visible time when it was played on another (`resolveReplayHiSpeed`).
*/
function applyReplayView(ctx: PlayViewSettingsContext, recorded: ReplayViewState): void {
const replay = ctx.options.replayView ?? DEFAULT_REPLAY_PLAYBACK_OPTIONS;
const view = resolveReplayView(recorded, currentReplayView(ctx), replay);
const drawsLift = ctx.theme.drawsLift?.() === true;
Eif ((replay.covers || (drawsLift && replay.lift)) && view.covers !== undefined) {
applyPlayEvent(ctx, {
type: 'cover-set',
recorded: true,
covers: view.covers.map(({ side, sudden, hidden, lift }) => ({
side,
sudden: drawsLift && !ctx.playState.view.covers[0]?.absolute ? resolveCoverAboveLift(sudden, lift) : sudden,
suddenOn: sudden > 0,
// The log's HIDDEN+ is a fraction of the whole lane; the play state's is of the part above the LIFT.
hidden: drawsLift ? resolveCoverAboveLift(hidden, lift) : hidden,
hiddenOn: hidden > 0,
...(drawsLift ? { lift, liftOn: lift > 0 } : {}),
})),
});
}
Iif (!replay.hiSpeed) return;
const cover = view.covers?.[0];
const sudden = ctx.playState.view.covers[0]?.absolute
? (cover?.sudden ?? 0) + (drawsLift ? (cover?.lift ?? 0) : 0)
: cover
? resolveCoverAboveLift(cover.sudden, drawsLift ? cover.lift : 0)
: 0;
const scale = resolveReplayScale(ctx);
const mainBPM = scale?.mainBPM;
const hiSpeed = resolveReplayHiSpeed(
view,
ctx.playState.view.hiSpeed.floating ||
ctx.playState.view.hiSpeed.classic ||
ctx.playState.view.hiSpeed.normalLevel !== undefined
? 'ac32-measured'
: (scale?.scale ?? BE_MUSIC_HI_SPEED_SCALE),
(visibleMs) =>
mainBPM === undefined
? undefined
: resolveHiSpeedForWhiteNumber({
laneBeats: scale?.laneBeats ?? 0,
sudden,
bpm: mainBPM,
whiteNumberMs: visibleMs,
}),
);
Eif (hiSpeed !== undefined) applyPlayEvent(ctx, { type: 'hi-speed-drawn', value: hiSpeed });
}
/** A HIDDEN+ cover of `hidden` of the whole lane as a fraction of the part above `lift` (none over a full LIFT). */
function resolveCoverAboveLift(hidden: number, lift: number): number {
return lift < 1 ? Math.min(1, hidden / (1 - lift)) : 0;
}
/**
* How long a note stays visible below `cover` (the first play side's) at the current scroll and BPM, over the lanes
* laid out last frame: the white number (`resolveWhiteNumber`, the LIFT left out), divided by the `#SCROLL` × `#SPEED`
* rate at the playhead (0 while the chart scrolls backwards or stands still, as beatoraja shows it). `undefined` until
* the lanes are laid out or while the BPM is unknown.
*/
export function resolveVisibleMs(
ctx: PlayViewSettingsContext,
cover: BeMusicLaneCover | undefined,
bpm: number | undefined,
scrollRate: number,
): number | undefined {
if (ctx.laneX.size === 0 || bpm === undefined || !(bpm > 0)) return undefined;
return resolveWhiteNumber({
laneBeats: resolveLaneBeats(ctx),
hiSpeed: ctx.playState.view.hiSpeed.drawn,
sudden: ctx.playState.view.covers[0]?.absolute
? (cover?.sudden ?? 0) + (cover?.lift ?? 0)
: cover
? cover.lift < 1
? resolveCoverAboveLift(cover.sudden, cover.lift)
: (ctx.playState.view.covers.find((state) => state.side === cover.side && state.suddenOn)?.sudden ?? 0)
: 0,
bpm: ctx.playState.view.hsFix === 'CONSTANT' ? 150 : bpm,
scrollRate: ctx.playState.view.hsFix === 'CONSTANT' ? 1 : scrollRate,
});
}
/**
* The lanes laid out last frame in beats at hi-speed 1: the tallest lane's height in design pixels over the scroll of
* one beat (the view's scale, or `PIXELS_PER_BEAT`). 0 before the lanes are laid out.
*/
function resolveLaneBeats(ctx: PlayViewSettingsContext): number {
let laneHeight = 0;
for (const lane of ctx.laneX.values()) {
laneHeight = Math.max(laneHeight, (lane.baseBottom ?? lane.bottom) - lane.top);
}
return laneHeight / (ctx.theme.pixelsPerBeat?.() ?? PIXELS_PER_BEAT);
}
/** Map measured source multipliers onto the active skin's lane scale without changing the selected source value. */
function syncMeasuredSourceScale(ctx: PlayViewSettingsContext): void {
const state = ctx.playState.view.hiSpeed;
Eif (!state.floating && !state.classic && state.normalLevel === undefined) return;
const scale = resolveReplayScale(ctx);
if (scale === undefined || !(scale.laneBeats > 0)) return;
applyPlayEvent(ctx, { type: 'hi-speed-scale', multiplier: (scale.laneBeats * 36000) / AC32_HI_SPEED_BASE });
}
|