All files / player-engine/src/core engine-types.ts

100% Statements 3/3
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3

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 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            5x 5x 5x                                                                                                                                                                                                                            
import type {
  ChartOptionProgram,
  ChartOptionDiagnostic,
  ChartPlayOptionProgram,
  PlayerChartPlayOptions,
} from '@be-music/chart-model';
import type { ChartTextStage } from './chart-text.ts';
export type { ChartTextStage } from './chart-text.ts';
import type { BattleMode } from './battle-mode.ts';
import type { LR2ExtraLevel } from './lr2-extra-lanes.ts';
import type { LongNoteModifier, LongNotePlacement } from './long-note-modifier.ts';
import type { MineModifier, MinePlacement } from './mine-modifier.ts';
import {
  type KeyBoundBGACue,
  type BmsonBGAEventCue,
  type ChartPlayVariant,
  type RandomDraws,
  type ResolvedChart,
} from '@be-music/chart-model';
import type { LogEntry } from '@be-music/shared-utils/log';
import { type BeMusicEvent, type BeMusicJSON } from '@be-music/chart-source';
import { type PlayerStateSignals } from '../state-signals.ts';
import { type LaneBinding } from './lane-layout.ts';
import { type ImageResizeAlgorithm } from '../image-resize-algorithm.ts';
import { type TUINoteHeight } from './ui-options.ts';
import { type AudioSinkClockState } from '../audio-clock.ts';
import { type PlayerGaugeSummary, type PlayerUISignalBus } from './ui-signal-bus.ts';
import { type PlayerInputSignalBus } from './input-signal-bus.ts';
import type { GaugePick, GaugeAutoShift } from '../ruleset/gauge-type.ts';
import { type LongNoteStyle, type RulesetId, type RulesetWindowTables } from '../ruleset/index.ts';
import { type ReplayRecordingOptions } from '../replay/recorder.ts';
import type { BeMusicReplay, ReplayCourseStage, ReplayInputEvent, ReplayInputSource } from '../replay/format.ts';
import type { ReplayTempoChange } from '../replay/view.ts';
import { type PreparedPlaybackChartData } from './bootstrap.ts';
import type { GuideSound } from './guide-sound.ts';
 
/**
 * Public types of the playback engine: options, runtimes, the audio-session contract and the summary.
 */
export interface PlayerUIRuntime {
  readonly tuiEnabled: boolean;
  readonly playbackEndSeconds?: number;
  start: () => void;
  stop: () => void | Promise<void>;
  dispose: () => void | Promise<void>;
  triggerPoor: (seconds: number) => void;
  clearPoor: () => void;
}
 
export interface PlayerInputRuntime {
  start: () => void;
  stop: () => void;
  /**
   * Whether the input reports its key releases (`kitty-state` releases), declared before the play starts. `true`: a
   * manual play judges it as key-state input from its first key, so even its first press is judged as it comes, with
   * no inferred hold. Absent (or `false`): the engine detects it, inferring when each key came up
   * (`play-key-inference.ts`) until the first `kitty-state` command arrives. A browser always reports releases; a
   * terminal does only under the kitty keyboard protocol (or Windows' win32-input-mode).
   */
  reportsKeyReleases?: boolean;
}
 
export interface CreatePlayerUIRuntimeContext {
  keyBoundBGACues?: readonly KeyBoundBGACue[];
  /** Remapped bmson event animations supplied by a NONSTOP host. */
  bmsonBGACues?: readonly BmsonBGAEventCue[];
  json: BeMusicJSON;
  mode: 'AUTO' | 'MANUAL' | 'AUTO SCRATCH';
  laneDisplayMode: string;
  laneBindings: LaneBinding[];
  speed: number;
  uiFPS?: number;
  tuiVisibleNotesLimit?: number;
  tuiNoteHeight: TUINoteHeight;
  judgeWindowMs: number;
  highSpeed: number;
  imageResizeAlgorithm: ImageResizeAlgorithm;
  videoBGAStreaming?: boolean;
  showLaneChannels: boolean;
  randomPatternSummary?: string;
  stateSignals: PlayerStateSignals;
  uiSignals: PlayerUISignalBus;
  baseDir: string;
  loadSignal?: AbortSignal;
  onBGALoadProgress: (progress: { ratio: number; detail?: string }) => void;
}
 
export interface CreatePlayerInputRuntimeContext {
  mode: 'auto' | 'manual';
  inputSignals: PlayerInputSignalBus;
  inputTokenToChannels: ReadonlyMap<string, readonly string[]>;
}
 
/**
 * Play settings: what decides the judgments and the score, or which chart / branch is played. Plain serializable
 * values, and the set a replay has to carry to reproduce a play (see "Engine options by category" in
 * docs/architecture.md for the ones it does not record yet). The engine does not read `auto`: the host picks
 * `autoPlay` or `manualPlay`.
 */
export interface PlayerPlaySettings {
  /** Absent retains beatoraja-compatible bmson zero-total recovery. */
  bmsonTotalMode?: import('../ruleset/bmson-total.ts').BmsonTotalMode;
  /** beatoraja scroll modifier; REMOVE changes coordinates and ADD replaces the scroll-rate timeline. Event times stay fixed. */
  scrollModifier?: import('./scroll-modifier.ts').ScrollModifier;
  scrollSection?: number;
  scrollRate?: number;
  /** Effective source ADD curve; supplying it restores the original draws. */
  scrollModifierTimeline?: readonly import('@be-music/chart-model').ScrollTimelinePoint[];
  auto?: boolean;
  autoScratch?: boolean;
  /** beatoraja's note-selection algorithm; other source rulesets retain their own fixed selection. */
  judgeAlgorithm?: 'combo' | 'duration' | 'lowest';
  /** LR2's mania option GAMBOL (LR2 rules only): 1 or 2 fixes the judge windows; 0 or absent is off. Recorded into the replay. */
  lr2Gambol?: 0 | 1 | 2;
  /** beatoraja CUSTOM JUDGE rates; absent while the option is off. Recorded into the replay. */
  customJudge?: import('../ruleset/custom-judge.ts').BeatorajaCustomJudge;
  /**
   * PRACTICE (beatoraja's mode, under every ruleset): plays only the range, with its gauge, TOTAL and judgerank read under
   * the judge ruleset, from the head of the bar before the range's (`resolvePracticePlayWindow`). The source applies no chart modifier in PRACTICE; a practice play
   * records no replay.
   */
  practice?: import('./practice.ts').PlayPractice;
  /**
   * The automatic judge-timing adjustment of a manual play (default `'OFF'`): beatoraja's calibration or LR2's ON /
   * SILENT. Display-only: it moves {@link judgeTimingMs}, never the input or audio clocks. AUTO and replays never adjust.
   */
  judgeTimingAutoAdjust?: import('./judge-timing.ts').JudgeTimingAutoAdjust;
  judgeTimingMs?: number;
  longNoteModifier?: LongNoteModifier;
  /** Probability for source long-note additions, default 1. */
  longNoteRate?: number;
  /** beatoraja EXTRA: source depth 0..100 (0 disables it), with optional scratch participation. */
  extraNoteDepth?: number;
  extraNoteScratch?: boolean;
  /** LR2 beta3 EXTRA, independently selected from beatoraja EXTRA depth. Courses ignore it. */
  sevenToNinePattern?: import('./seven-to-nine.ts').SevenToNinePattern;
  sevenToNineType?: import('./seven-to-nine.ts').SevenToNineType;
  hRandomThresholdBPM?: number;
  battleMode?: BattleMode;
  /** The 2P player's gauge in a two-player play (`battleMode: 'TWO-PLAYER'`); absent plays the 1P gauge. */
  gauge2P?: GaugePick;
  /** LR2 D-BATTLE: scratch objects stay playable, as LR2's hidden `randSC` command keeps them (buttons 12 + 13 at decide). */
  dBattleScratch?: boolean;
  lr2ExtraMode?: boolean;
  lr2ExtraLevel?: LR2ExtraLevel;
  /**
   * LR2's mania chart generators (ADD NOTES, LOUDNESS, ADD LONGNOTES, ADD MINES): each rate is 0 (off) or 10–100. `seed`
   * is the seed a replay recorded; without one the play draws a fresh seed. Not applied in a course or in PRACTICE.
   */
  lr2Mania?: import('./lr2-mania-chart.ts').LR2ManiaChartRates & { seed?: string };
  mineModifier?: MineModifier;
  /** Independently assist the source play side; explicit values override autoScratch for that side. */
  autoScratch1P?: boolean;
  autoScratch2P?: boolean;
  /**
   * ANY KEY (That-Which-Must-Not-Be-Named PC, #751): a press takes the nearest unjudged note in reach on any lane and
   * holds that lane until its key comes up. Manual play only; the replay records it as `play.anyKey`.
   */
  anyKey?: boolean;
  /**
   * Gauge the player selected, as an LR2-family id (`'GROOVE'` / `'EASY'` / `'HARD'` / `'DEATH'`). Mapped onto the
   * active ruleset's own line-up — `'GROOVE'` is beatoraja's `NORMAL`, `'DEATH'` is its `HAZARD`, and so on.
   * Defaults to `recordReplay.gauge`, then `'GROOVE'`. The replay records the gauge the play ran on.
   *
   * A value qualified with a source (`'lr2:HARD'`, `'beatoraja:EX-HARD'`, `'____:EASY'`) runs that source's gauge
   * under whichever ruleset judges the play, with the source's auto-shift ladder, fallback gauge and course gauges
   * (ADR 0019, #1171).
   */
  gauge?: GaugePick;
  /**
   * The compat ruleset the play is judged under: `'lr2'` (default), `'beatoraja'` or `'____'`. It owns every judge
   * rule — the windows (beatoraja's SEVENKEYS windows scaled by its judgerank, That-Which-Must-Not-Be-Named's fixed ±16.67 / ±33.33 /
   * ±116.67 / ±250 ms), which note a press resolves against, the long-note style, the empty-POOR rule, mines, the gauge
   * line-up and its curve, and the score — and the replay simulator plays by the same tables. Dynamic `#EXRANKxx`
   * changes are an LR2 concept and only apply under `'lr2'`. Recorded into the replay (`play.judgeRuleset`) so
   * replays and re-simulations judge under the same ruleset.
   */
  judgeRuleset?: RulesetId;
  /**
   * The `#RANDOM` / `#SWITCH` outcomes of this play (`resolvePlayChart`, or `drawRandomOutcomes` from
   * `@be-music/chart-model`). With a chart the host already resolved, these are the draws it was resolved with and only feed
   * the RANDOM summary. With a chart that still carries `bms.controlFlow` (e.g. replaying a replay against the source
   * chart), the engine resolves it exactly once with these draws. The engine never draws itself: a chart with control
   * flow and no draws is rejected with a `TypeError`.
   */
  randomDraws?: RandomDraws;
  /** Player tempo multiplier relative to the chart's standard playback rate. */
  speed?: number;
  inferBmsLNTypeWhenMissing?: boolean;
  /**
   * The chart file's extension (`.bms` / `.bme` / `.pms` / ...). Feeds the extension rules of the shared lane-mode rule
   * (`resolveChartPlayVariant` from `@be-music/chart-model`): `.pms` is 9 KEY. Every other extension leaves the decision to the
   * lanes in use (a `.bme` chart without a 6 / 7 key column is 5 / 10 KEY, as in beatoraja).
   */
  laneModeExtension?: string;
  /**
   * The chart's play variant, when the host declares it. Routes straight to the corresponding lane mode:
   *
   *   - `'5'`  → `5-key-sp`     - `'10'` → `5-key-dp`
   *   - `'7'`  → `7-key-sp`     - `'14'` → `14-key-dp`
   *   - `'9'`  → `9-key`        - `'24'` → `24-key-sp`    - `'48'` → `48-key-dp`
   *
   * When omitted, the engine resolves it with `resolveChartPlayVariant` over the resolved chart's events (after
   * `#RANDOM`), `laneModeExtension`, and `#PLAYER` — the same rule the web hosts use to pick play skins — so the lane
   * mode (and the beatoraja per-mode judge windows that follow from it) never depends on mines, invisible objects, or
   * which long-note notation the chart uses.
   */
  playVariant?: ChartPlayVariant;
  /**
   * End the play the moment a survival gauge empties, as LR2 and beatoraja do: nothing after the judgment that emptied
   * it counts, and the play stops there. Default `true`; `false` keeps judging to the end of the chart with the gauge
   * dead (`failedMidPlay`). Recorded into the replay (`play.endOnFailure`) so re-simulations stop at the same point.
   */
  endOnFailure?: boolean;
  /**
   * Gauge auto-shift (beatoraja's policies, along the judge ruleset's own gauge ladder); when present, takes precedence
   * over endOnFailure. Applies to both players of a two-player play.
   */
  gaugeAutoShift?: GaugeAutoShift;
  /** The lowest gauge BEST CLEAR / SELECT TO UNDER shift to. */
  bottomShiftableGauge?: import('../ruleset/gauge-type.ts').GaugeShiftBottom;
  /**
   * This play is one stage of a course: the stage runs on the ruleset's course gauge under the course's judge
   * constraint, starting from the state the previous stage left (`carryIn`; absent for the first stage). Recorded into
   * the replay (`play.course`), so the stage replays on its own.
   */
  course?: ReplayCourseStage;
  /**
   * The judge rank changes of a stitched LR2 NONSTOP chart's later stages (`NonstopStitchMap.stageRanks` from
   * `@be-music/player-engine/course`), in chart seconds: from each change on, the windows of that stage's `#RANK`. Applied under
   * LR2 (and beatoraja for mixed bmson courses) and recorded into the replay (`chart.judgeRank.stageRanks`), so a replay
   * and a re-simulation judge with them.
   */
  stageRanks?: ReadonlyArray<PlayerStageRank>;
  /**
   * The chart second the play starts at instead of the chart's beginning: the play's first seek (a `{ kind: 'seek' }`
   * input command, applied as the chart starts). AUTO play and replays rebuild their judgments; manual play starts practice and skips earlier note heads and tails without scoring.
   * The judge state there is the one a play from the start reaches (the notes before it judged, without their keysounds,
   * flashes or bombs), the volume changes before it apply, and sounds that started earlier and still ring start
   * partway, through `AudioSession.triggerEventAt`. A play that seeks records no replay.
   */
  startAtSeconds?: number;
}
 
/** From `seconds` on, mines explode with the `sampleKey` WAV slot (silent without one). */
export interface LandmineSampleKeyCue {
  seconds: number;
  sampleKey?: string;
}
 
/** One judge rank change of {@link PlayerPlaySettings.stageRanks}. */
export interface PlayerStageRank {
  seconds: number;
  rank: number;
  table: 'chart' | 'lr2-nonstop' | 'bmson' | 'bms-exrank' | 'bms-exrank-change';
}
 
/**
 * Play runtime: the host services the engine runs on — adapter factories, the abort signal, callbacks and
 * output sinks, and the chart data a host prepared itself. Functions and live objects, never serialized.
 */
export interface PlayerPlayRuntime {
  /** Versioned effective chart-authored display operations, e.g. a recorded replay or NONSTOP program. */
  chartOptionProgram?: ChartOptionProgram;
  /**
   * Chart-authored gameplay options (RANDOM, judgment CRAZY, #844) the play runs with, e.g. a recorded replay's. Absent:
   * compiled from the chart's initial `#OPTION`s. A host applies the arrangement and ADDMINE before preparing the chart;
   * the engine applies the judge modifiers and records the program.
   */
  chartPlayOptionProgram?: ChartPlayOptionProgram;
  /**
   * The player's own nanasigroove judgment options (GOOD2BAD / GREAT2BAD, HELLJUDGE), added to the chart's where the
   * chart sets none (`withPlayerChartPlayOptions`). Ignored when {@link chartPlayOptionProgram} is given: that program is
   * the effective one.
   */
  playerChartPlayOptions?: PlayerChartPlayOptions;
  /** Diagnostics retained by a host compiling stage-owned programs before playback. */
  chartOptionDiagnostics?: readonly ChartOptionDiagnostic[];
  /** Stage-owned chart text supplied by a NONSTOP host; overrides flattened TEXT definitions. */
  chartTextStages?: readonly ChartTextStage[];
  /** Host-remapped input-bound BGA selections, e.g. NonstopStitchMap.keyBoundBGA. Overrides chart A5 controls. */
  keyBoundBGACues?: readonly KeyBoundBGACue[];
  /** Remapped bmson event animations supplied by a NONSTOP host. */
  bmsonBGACues?: readonly BmsonBGAEventCue[];
  /**
   * The WAV slot a mine explodes with, by chart time, in place of `#WAV00` (`NonstopStitchMap.landmineSampleKeys`): a
   * mine uses the last cue at or before its own time, and a cue without `sampleKey` keeps it silent. Absent: `#WAV00`.
   */
  landmineSampleKeys?: readonly LandmineSampleKeyCue[];
  /**
   * Pre-built playback chart data the host wants the engine to use verbatim instead of running its own
   * `preparePlaybackChartData` pass. When provided, the engine treats the supplied {@link PreparedPlaybackChartData}
   * as the single source of truth for the playable / landmine / invisible / scorable note arrays, the lane
   * bindings, the input-token map, the active free-zone channel set, and the playback's `totalSeconds`.
   *
   * Why this exists: the web renderer used to call `extractTimedNotes` independently of the engine to build its
   * own `notes` / `mineNotes` / `invisibleNotes` arrays, and `applyEngineFrame` then synced the engine's
   * per-note `judged` flag onto the renderer's parallel arrays by **shared array index**. Any divergence
   * between the two extract calls (a different `inferBmsLNTypeWhenMissing` flag, a different
   * `laneModeExtension`, a stale `bms.controlFlow` array re-resolved on the engine side, a `random1P: 'OFF'`
   * truthy-check evaluating to `false` because `'OFF'` is a non-empty string, …) shifted the index alignment
   * and the renderer ended up applying judge flags to the wrong notes — symptoms ranged from notes vanishing
   * partway down the lane in HIDE-on-judge mode, to mid-chart full-combo cues, to AUTO PLAY exScore landing
   * below the EX-MAX 200_000 ceiling. Each of those was patched by aligning one more argument between the
   * two extract sites, but the underlying design was inherently fragile.
   *
   * Letting the host hand the engine a ready-made `preparedChart` removes the entire class of bugs at the
   * structural level: the renderer and the engine literally hold the same `TimedPlayableNote[]` / `TimedLandmineNote[]`
   * instances, so any judge mutation the engine performs on a note is visible to the renderer with no sync
   * step, and there is no second extract that could disagree with the first.
   *
   * Hosts that omit this option (TUI, every existing test) keep the original behavior — the engine
   * runs its own `preparePlaybackChartData` internally, just like before.
   */
  preparedChart?: PreparedPlaybackChartData;
  /** Recorded mine population for deterministic playback of a generated mine option. */
  mineModifierPlacements?: readonly MinePlacement[];
  longNoteModifierPlacements?: readonly LongNotePlacement[];
  createUIRuntime?: (context: CreatePlayerUIRuntimeContext) => Promise<PlayerUIRuntime | undefined>;
  createInputRuntime?: (context: CreatePlayerInputRuntimeContext) => PlayerInputRuntime | undefined;
  /** The host's audio backend. Omitted or returning undefined means playback without audio. */
  createAudioSession?: (context: CreateAudioSessionContext) => Promise<AudioSession | undefined>;
  /** Sample durations in seconds for autoplay end timing and active-audio diagnostics. */
  loadAudioSampleDurations?: (
    resolved: ResolvedChart,
    options: { baseDir?: string; signal?: AbortSignal },
  ) => Promise<ReadonlyMap<string, number>>;
  signal?: AbortSignal;
  /**
   * Keeps a play open after its chart is over, so the player's keys still light the lanes (and the chart's remaining
   * background sounds still play) while the host shows its outro. When the chart is over — every note judged and the
   * judge windows passed — `onSettled` runs once; the play then carries on until `signal` aborts, and only then ends
   * normally (its summary and replay as usual; aborting this signal is not an interrupt). Aborting it before the chart
   * is over ends the play once nothing is left to judge, ahead of the judge windows and the chart's remaining
   * background sounds (the host finishing a play whose notes are all judged). Without it a play ends as soon as its
   * chart is over. Manual, replay and AUTO play alike.
   */
  holdAfterChart?: { signal: AbortSignal; onSettled?: () => void };
  audioBaseDir?: string;
  onLoadProgress?: (progress: PlayerLoadProgress) => void;
  onLoadComplete?: () => void;
  onHighSpeedChange?: (highSpeed: number) => void;
  /**
   * Called when the play tempo changes during the play (a `{ kind: 'tempo' }` input command, or a replay following
   * {@link PlayerPlayRecording.replayTempoChanges}), with the new player multiplier and the chart second it took
   * effect at. Chart time stays continuous across the change. The clock frames carry the effective playback rate.
   */
  onTempoChange?: (change: PlayerTempoChange) => void;
  onResolvedChart?: (json: BeMusicJSON) => void;
  /**
   * Called once, before playback starts, with the ruleset the play is judged under — after `judgeRuleset`,
   * `judgeWindowMs`, and (for a replay) the recorded settings were applied. Lets a host show the windows the engine
   * actually uses instead of re-deriving them. Under LR2 a mid-chart `#EXRANKxx` can narrow or widen the windows
   * later; `windows` is the table in force at the start of the chart.
   */
  onRulesetResolved?: (info: PlayerRulesetInfo) => void;
  onLog?: (entry: LogEntry) => void;
  writeOutput?: (text: string) => void;
}
 
/**
 * Play tuning: audio, latency and rendering knobs. They change how the play sounds and looks, never what is
 * judged (`audioOffsetMs` moves the judge clock against live input, but recorded inputs are already on chart
 * time). The engine does not read `tui` or `audioTailSeconds`.
 */
export interface PlayerPlayTuning {
  audio?: boolean;
  volume?: number;
  bgmVolume?: number;
  playVolume?: number;
  compressor?: boolean;
  compressorThresholdDb?: number;
  compressorRatio?: number;
  compressorAttackMs?: number;
  compressorReleaseMs?: number;
  compressorMakeupDb?: number;
  limiter?: boolean;
  limiterCeilingDb?: number;
  limiterReleaseMs?: number;
  leadInMs?: number;
  /** Wait for a `start` input command as well as the lead-in before starting chart time and audio. */
  waitForStart?: boolean;
  audioOffsetMs?: number;
  audioHeadPaddingMs?: number;
  audioTailSeconds?: number;
  audioLeadMs?: number;
  audioLeadMaxMs?: number;
  audioLeadStepUpMs?: number;
  audioLeadStepDownMs?: number;
  highSpeed?: number;
  uiFPS?: number;
  tui?: boolean;
  tuiVisibleNotesLimit?: number;
  tuiNoteHeight?: TUINoteHeight;
  imageResizeAlgorithm?: ImageResizeAlgorithm;
  videoBGAStreaming?: boolean;
}
 
/**
 * Play debug: diagnostic overrides. `judgeWindowMs` changes the judgments (the replay records it as
 * `play.judgeWindowOverrideMs`); the others only change what is shown or heard. The engine does not read
 * `showInvisibleNotes`.
 */
export interface PlayerPlayDebug {
  judgeWindowMs?: number;
  debugActiveAudio?: boolean;
  showInvisibleNotes?: boolean;
  /**
   * Debug aid — synthesizes a short sine tone for `#WAVxx` references whose file is missing or fails to decode.
   * The spec-compliant default is silence (LR2 / beatoraja play nothing for a broken keysound reference); enable
   * this only in test rigs / chart debugging where hearing that a trigger fired matters.
   */
  missingSampleToneSeconds?: number;
}
 
/**
 * Play recording: replay capture and replay. `recordReplay` declares the host-side settings the log records
 * (`gauge`, `randomLane`, `dpFlip`, `native`) next to the chart identity, and `replayInputs` re-drives a
 * recorded input stream.
 */
export interface PlayerPlayRecording {
  /**
   * Host-declared play settings merged into the recorded replay (`randomLane`, `dpFlip`, `native`, ...). The engine
   * itself knows mode / auto-scratch / judge-window override; everything host-side (which lane shuffle produced
   * `preparedChart`, ...) arrives through this bag. `gauge` here is a fallback for {@link PlayerPlaySettings.gauge}: the play runs on, and
   * the log records, one gauge. Only meaningful together with {@link onReplayRecorded}.
   */
  recordReplay?: ReplayRecordingOptions;
  /**
   * Enables replay recording: when set, the engine snapshots the resolved chart it actually played
   * (post-`#RANDOM`, post lane-shuffle via `preparedChart`), records every judged key press / release with
   * chart-relative timestamps, and hands the assembled {@link BeMusicReplay} here right before `autoPlay` /
   * `manualPlay` resolves — including the ESC (aborted) exit. A play that sought (`startAtSeconds`, a `seek` command)
   * records none: it is discarded at the first seek. The replay's `results.native` caches this run's
   * engine summary; see `@be-music/player-engine/replay` for the format and the LR2 / beatoraja / That-Which-Must-Not-Be-Named re-simulation
   * tools.
   */
  onReplayRecorded?: (replay: BeMusicReplay) => void;
  /**
   * Replay playback: a recorded replay input stream (`replay.inputs`) `manualPlay` re-drives DETERMINISTICALLY.
   * Each event fires at its exact chart-relative microsecond timestamp (no wall-clock jitter — the judge timestamp
   * is the recorded one), so replaying a log against the same resolved chart reproduces the original judgments.
   * While a replay is active, live lane / kitty input commands are ignored; pause, high-speed, and interrupt
   * commands keep working. The caller is responsible for mounting the SAME resolved chart the log was recorded
   * against (`preparedChart`, or a chart remapped to the log's note arrangement). `autoPlay` has no inputs to
   * re-drive, but a replay of an AUTO-played log passes its (empty) inputs too: either way the play is a replay, so
   * its `speed` and tempo changes only set how fast it is re-driven and a re-recorded log carries neither.
   */
  replayInputs?: readonly ReplayInputEvent[];
  /**
   * The input-source table the {@link replayInputs} name (`play.inputSources` of their log). An input whose source it
   * does not hold is judged as from an unknown source (the lenient single-source rule for a scratch hold).
   */
  replayInputSources?: readonly ReplayInputSource[];
  /**
   * The tempo changes a replay follows (`play.tempoChanges` of its log, in chart time), on top of `speed` as the tempo
   * at the start (`play.tempo`). Each takes effect once the replay's chart time reaches it, as a live `{ kind: 'tempo' }`
   * command would. Read only in a replay ({@link replayInputs} set), by `manualPlay` and `autoPlay` alike; it changes no
   * judgment.
   */
  replayTempoChanges?: readonly ReplayTempoChange[];
  /**
   * Where the replayed play was aborted (`play.abortedAtUs` of a log with `play.aborted`), in chart microseconds: the
   * replay judges up to it and stops there, as an ESC, as the replay simulators stop there, instead of missing the
   * rest of the chart. Read only in a replay ({@link replayInputs} set).
   */
  replayAbortedAtUs?: number;
  /** A recorded ESC applied the LR2 end-of-stage gauge rule after the event pump froze. */
  replayExitedReversal?: boolean;
  replayCompletedAtReversal?: boolean;
  replayReversalActivationUs?: number;
}
 
/**
 * Options of `autoPlay` / `manualPlay`: the union of the five categories above. Every field belongs to exactly one
 * of them (`engine-types.test.ts` enforces it), so a new option has to be classified when it is added.
 */
export interface PlayerOptions
  extends PlayerPlaySettings, PlayerPlayRuntime, PlayerPlayTuning, PlayerPlayDebug, PlayerPlayRecording {}
 
/** A tempo change during a play; see {@link PlayerOptions.onTempoChange}. */
export interface PlayerTempoChange {
  /** The new tempo (chart seconds per clock second). */
  tempo: number;
  /** The chart second the change took effect at. */
  seconds: number;
}
 
/** The judging setup a play resolved to; see {@link PlayerOptions.onRulesetResolved}. */
export interface PlayerRulesetInfo {
  /** Versioned ruleset id (`'lr2/0'`, `'beatoraja/0'`, `'____/0'`). */
  id: string;
  rulesetId: RulesetId;
  /** How the ruleset plays the chart's long notes (LN / per-note / charge). */
  longNoteStyle: LongNoteStyle;
  /**
   * Judgment windows (µs) at the start of the chart, in the ruleset convention `delta = noteTime - inputTime`: each pair
   * is `[lateBound <= 0, earlyBound >= 0]`, so a positive delta is an EARLY press.
   */
  windows: RulesetWindowTables;
}
 
export interface PlayerSummary {
  /**
   * The active ruleset's judgment count — its EX-SCORE denominator. Charge-note styles count a long note's head
   * and tail separately, so this is NOT always the number of notes on screen.
   */
  total: number;
  perfect: number;
  fast: number;
  slow: number;
  great: number;
  good: number;
  bad: number;
  /** Missed notes, plus LR2 mine hits (`minePoor`). Empty POORs are counted separately. */
  poor: number;
  /**
   * Empty POOR (空POOR) — a press with no note in reach but one inside the ruleset's miss window. It costs gauge
   * and fires the POOR cue without consuming a note, so it never reaches EX-SCORE and is tracked apart from
   * `poor`. Whether a player's POOR counter displays the two summed is a per-ruleset presentation choice: LR2 does
   * (OpenLR2 `ApplyJudgeNote` increments `playerstat.poor` for it), which is why the split is exposed here.
   */
  emptyPoor: number;
  /** LR2 mine hits already included in `poor`; not judgment notes and never a combo break. */
  minePoor?: number;
  exScore: number;
  score: number;
  /**
   * The combo running now: the one the judge cues count, rebuilt with the rest of the summary when the play seeks. The
   * engine's summaries always carry it; it is optional so a summary a host builds itself need not.
   */
  combo?: number;
  /** Final combo eligibility when empty POOR can break combo; absent in legacy/count-only results. */
  fullCombo?: boolean;
 
  /** The highest combo of the play so far; see {@link combo}. */
  maxCombo?: number;
  gauge?: PlayerGaugeSummary;
  /** For a course stage: the course-wide combo running now and the course-wide max combo (carried across stages). */
  course?: { combo: number; maxCombo: number };
  /** The 2P player's summary of a two-player play (LR2 BATTLE); this summary is the 1P player's. */
  secondPlayer?: PlayerSummary;
}
 
export interface PlayerLoadProgress {
  ratio: number;
  message: string;
  detail?: string;
  audioStatus?: PlayerLoadComponentStatus;
  graphicsStatus?: PlayerLoadComponentStatus;
}
 
export interface PlayerLoadComponentStatus {
  state: 'pending' | 'ready' | 'disabled';
  message: string;
  detail?: string;
}
 
export type PlayerInterruptReason = 'escape' | 'ctrl-c' | 'restart';
 
export class PlayerInterruptedError extends Error {
  readonly reason: PlayerInterruptReason;
 
  readonly exitCode: number;
 
  constructor(reason: PlayerInterruptReason) {
    super(`Player interrupted: ${reason}`);
    this.reason = reason;
    this.exitCode = reason === 'ctrl-c' ? 130 : 0;
  }
}
 
/**
 * The host-provided audio playback contract. Node and browser adapters implement this shape and plug in via
 * {@link PlayerOptions.createAudioSession}.
 *
 * **Lifetime**: caller invokes `start()` once chart playback begins, `pause()` / `resume()` during input-driven
 * pauses, and exactly one of `finish()` (graceful drain) or `dispose()` (abort, no drain) at the end of playback.
 *
 * **Optional methods** are absent on backends that don't model the corresponding concept — e.g. test mocks may
 * skip clock state, and pure-render backends may not handle `triggerEvent` / `stopChannel`.
 */
export interface AudioSession {
  start: () => void;
  finish: () => Promise<void>;
  dispose: () => Promise<void>;
  /**
   * Milliseconds to wait between {@link start} and the chart's note-zero moment. Backends that need a known lead-in
   * (e.g. to fully fill an audio output buffer before the chart's first note) report it here so the engine schedules
   * note timings against this offset.
   */
  chartStartDelayMs: number;
  backendLabel: string;
  pause: () => void;
  resume: () => void;
  getClockState?: () => AudioSinkClockState;
  getActiveAudioFiles?: () => string[];
  getActiveAudioVoiceCount?: () => number;
  triggerEvent?: (event: BeMusicEvent) => void;
  /**
   * Plays `event`'s sound at `outputSeconds` on the clock {@link getClockState} reports (`outputSeconds`): the engine
   * queues the chart's realtime audio (volume changes, background sounds, AUTO play's keysounds) as its write head
   * (`scheduledSeconds`) reaches it, and a backend whose write head runs ahead of its output places each one at its due
   * time with this, instead of when the engine's tick reached it. A time already past plays at once. Without it, those
   * go through {@link triggerEvent} and sound as they are queued.
   */
  scheduleEventAt?: (event: BeMusicEvent, outputSeconds: number) => void;
  /**
   * Plays the guide sound of a judgment (beatoraja's GUIDE SE), at once. Present only on a session that has the sound
   * set's guide sounds and a play that turned them on; a sound the set lacks plays nothing.
   */
  playGuideSound?: (sound: GuideSound) => void;
  /**
   * Plays `event`'s sound as if it had started `offsetSeconds` ago, for a play that seeks (`PlayerOptions.startAtSeconds`,
   * a `seek` command). A sound shorter than the offset plays nothing. Without it, sounds that started before the second
   * sought stay silent.
   */
  triggerEventAt?: (event: BeMusicEvent, offsetSeconds: number) => void;
  /**
   * The play seeks to chart second `seconds`: the sounds playing now stop and the gains go back to the chart's start
   * (the engine then replays the volume changes before `seconds` and starts what still rings there through
   * {@link triggerEventAt}); a backend that schedules on the chart's time (fades, stage rates) moves it to `seconds`.
   * The play clock carries on: chart time re-bases in the engine. Without it, sounds playing at the seek ring on.
   */
  seek?: (seconds: number) => void;
  stopChannel?: (channel: string) => void;
  /**
   * The play tempo changed during the play (see {@link PlayerOptions.onTempoChange}): samples started from now on, and
   * the ones still playing, follow the new tempo as far as the backend can. The engine's clock re-anchors itself, so a
   * backend without it keeps playing at the tempo it started with.
   */
  setTempo?: (tempo: number) => void;
}
 
/**
 * Context handed to a {@link PlayerOptions.createAudioSession} factory. The resolved chart is shared with the engine
 * so adapters use the same timing, sample slices and chart facts.
 */
export interface CreateAudioSessionContext {
  json: BeMusicJSON;
  resolvedChart: ResolvedChart;
  options: PlayerOptions;
  mode: 'auto' | 'manual';
  onLoadProgress?: (progress: AudioSessionLoadProgress) => void;
}
 
export interface RandomPatternSelection {
  index: number;
  current: number;
  total: number;
}
 
/**
 * Progress signal emitted while an {@link AudioSession} is loading. Values are normalized:
 * - `ratio` is in `[0, 1]` (0 = just started, 1 = ready)
 * - `message` is a short human-readable status (`"Loading key sounds..."`, `"Audio ready."`)
 * - `detail` is optional context (the WAV currently decoding, the failed asset, etc.)
 */
export interface AudioSessionLoadProgress {
  ratio: number;
  message: string;
  detail?: string;
}
 
/** A chart resolved for one play, with the `#RANDOM` / `#SWITCH` draws it was resolved with. */
export interface ResolvedPlayChart {
  /** The chart with the drawn branches applied; it carries no control flow. */
  chart: BeMusicJSON;
  /** The draws `chart` was resolved with: hand them to the engine (`PlayerOptions.randomDraws`) and the replay. */
  randomDraws: RandomDraws;
}
 
export interface ResolvePlayChartOptions {
  /** Random source for the draws, in `[0, 1)`. Defaults to `Math.random`. */
  random?: () => number;
  /** Draws to resolve with instead of drawing, e.g. the ones a replay recorded. */
  randomDraws?: RandomDraws;
}