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 | 6036x 1286x 156x 156x 156x 156x 156x 156x 1132x 1121x 156x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 6036x 13x 13x 13x 13x 24096x 24096x 58x 12910x 12602x 4128x 8493x 8493x 8310x 8310x 7749x 7749x 3475x 3475x 853x 853x 3254x 3254x 3098x 3098x 277x 296x 296x 26292x 26292x 9020x 3507x 3507x 129899x 129899x 42484x 3507x 16737x 16737x 13688x 4959x 3468x 3468x 194485x 194485x 193889x 596x 596x 1x 595x 3468x 3932x 3932x 42512x 42512x 42512x 9719x 42512x 42512x 444x 42512x 40785x 40785x 608x 40177x 189043x 188046x 997x 997x 189043x 2x 995x 40177x 47600x 47600x 47600x | import {
normalizeChannel,
normalizeObjectKey,
resolveBMSBase,
type BeMusicEvent,
type ChartDiagnostic,
type BeMusicJSON,
type BeMusicSourceFormat,
} from '@be-music/chart-source';
import { buildBGATimelines, type BGATimelines } from './bga.ts';
import { collectChartDiagnostics } from './chart-diagnostics.ts';
import { hasControlFlow, type RandomDraws } from './control-flow.ts';
import {
isBmsBGMVolumeChangeChannel,
isBMSDynamicVolumeChangeChannel,
isBMSKeyVolumeChangeChannel,
parseBMSDynamicVolumeGain,
resolveChartPlayVariant,
type BeatResolver,
type ChartPlayVariant,
} from './index.ts';
import { resolveChartLongNoteMode, type LongNoteMode } from './note-rules.ts';
import { getPlayModeLayouts, type PlayModeLayout } from './play-modes.ts';
import {
resolveInvisibleNotes,
resolveJudgedNoteEntries,
resolveJudgedNotes,
resolveMineNotes,
type ResolvedNote,
type ResolvedNoteContext,
type ResolvedNoteLists,
} from './resolved-notes.ts';
import { collectSampleTriggersWithContext, type TimedSampleTrigger } from './sample-triggers.ts';
import {
createMeasureBoundariesBeats,
createScrollTimeline,
createSpeedTimeline,
type ScrollTimelinePoint,
type SpeedTimelinePoint,
} from './timelines.ts';
import {
createTimingBuildContext,
createTimingResolverWithContext,
type TempoReversalPoint,
type TimingResolver,
} from './timing.ts';
export interface ResolveChartOptions {
/**
* The `#RANDOM` / `#SWITCH` outcomes the chart was resolved with (`drawRandomOutcomes`), recorded on the result for
* the replay and the RANDOM summary. `resolveChart` does not apply them: resolve the control flow first
* (`resolveBMSControlFlowWithDraws` in `@be-music/chart-parser`, or `resolvePlayChart` in `@be-music/player-engine`).
*/
randomDraws?: RandomDraws;
/** The chart's path or extension (`.pms` names 9 KEY). Only consulted when `playVariant` is omitted. */
chartPath?: string;
/** The play variant, when the host already decided it. Defaults to `resolveChartPlayVariant` over the chart. */
playVariant?: ChartPlayVariant;
/**
* Infer `#LNTYPE 1` for `5x` / `6x` long notes when the chart declares no `#LNTYPE` (the browser hosts' choice);
* otherwise such a chart reads `5x` / `6x` the strict way. Defaults to `false`, the engine's default.
*/
inferBmsLNTypeWhenMissing?: boolean;
}
/** The judge-rank inputs a ruleset turns into judge windows. Raw chart values: each ruleset applies its own defaults. */
export interface ResolvedJudgeRank {
/** `#RANK` (BMS) or the parsed rank, when the chart has one. */
readonly rank?: number;
/** `#DEFEXRANK` (BMS) or `info.judge_rank` (bmson), when the chart has one. */
readonly exRank?: number;
/** `#EXRANKxx` changes placed on channel `A0` (BMS), in time order, with the raw `#EXRANK` value (100 = NORMAL). */
readonly changes: ReadonlyArray<{ readonly seconds: number; readonly exRankValue: number }>;
}
/** A BMS dynamic volume change (`#xxx97` BGM / `#xxx98` key sounds). */
export interface ResolvedVolumeChange {
readonly seconds: number;
/** Which bus the change scales: `bgm` (channel `97`) or `play` (channel `98`, the key sounds). */
readonly bus: 'bgm' | 'play';
/** Linear gain (`parseBMSDynamicVolumeGain`). */
readonly gain: number;
readonly event: BeMusicEvent;
}
/** The chart's TOTAL as authored. */
export interface ResolvedTotal {
/** `#TOTAL` (BMS) or `info.total` (bmson), when the chart has a finite one. */
readonly value?: number;
/** `absolute` for BMS `#TOTAL`; `percent` for bmson, whose `total` is a percentage of the ruleset's default TOTAL. */
readonly unit: 'absolute' | 'percent';
}
/**
* A chart ready to play: the Resolved layer every player-side consumer reads, built once from the Source layer
* (`BeMusicJSON`) by {@link resolveChart}. Control flow is decided (the draws are recorded), the lane mode is fixed,
* long notes, mines, invisible objects, FREE ZONE and duplicate notes are resolved, and every time is in seconds after
* BPM / STOP / `#STP`. Consumers never re-read the format rules from the source chart.
*
* The heavy parts (notes, triggers, timelines) are computed on first access and then kept, so a consumer only pays for
* what it reads. The value is deterministic: the same source and options always give the same result.
*/
export interface ResolvedChart {
/** The (control-flow-resolved) source chart this was resolved from: metadata, resources and raw sections. */
readonly source: BeMusicJSON;
readonly sourceFormat: BeMusicSourceFormat;
/** Nonfatal warnings from parsing and resolving this chart. */
readonly diagnostics: readonly ChartDiagnostic[];
/** The `#RANDOM` / `#SWITCH` outcomes the source was resolved with (`[]` when the chart had none or none were given). */
readonly randomDraws: RandomDraws;
/**
* `true` when the source still carries `#RANDOM` / `#SWITCH` control flow. Only its unconditional content is then
* resolved — what a song list shows before any draw; a play must resolve the control flow first.
*/
readonly hasUnresolvedControlFlow: boolean;
/** The object-ID base (`#BASE 62` or 36) keysound and resource keys are normalized under. */
readonly idBase: 36 | 62;
readonly playVariant: ChartPlayVariant;
/** The play-mode table layouts of `playVariant` (two for 9 KEY: PMS-STD and BME-type). */
readonly layouts: ReadonlyArray<PlayModeLayout>;
/** The chart-level long-note mode (`#LNMODE` / bmson `info.ln_type`, otherwise LN). */
readonly lnMode: LongNoteMode;
readonly inferBmsLNTypeWhenMissing: boolean;
readonly beatResolver: BeatResolver;
/** The tempo map: BPM points, STOP / `#STP` points, beat → seconds. */
readonly timing: TimingResolver;
/**
* The LR2 negative-BPM reversal (`timing.reversal`): where the first negative `#BPMxx` arms the reverse scroll and
* the event pump freezes. Absent when the chart has none. See docs/bms-spec.md "Negative BPM".
*/
readonly reversal: TempoReversalPoint | undefined;
/** The source events in chart order (`sortEvents`). */
readonly sortedEvents: ReadonlyArray<BeMusicEvent>;
/** Judged notes (`tap` / `ln`), one per lane and position. */
readonly notes: ReadonlyArray<ResolvedNote>;
/** Duplicate notes that lost their lane and position to a judged note but carry another keysound. */
readonly shadowedNotes: ReadonlyArray<ResolvedNote>;
readonly mines: ReadonlyArray<ResolvedNote>;
readonly invisibleNotes: ReadonlyArray<ResolvedNote>;
/**
* Every sound the chart triggers by itself or from a lane: BGM (`01`), lane keysounds and invisible objects, with
* `#LNOBJ` ends and suppressed `#LNTYPE` halves left out and bmson slices resolved. Mines are not included.
*/
readonly sampleTriggers: ReadonlyArray<TimedSampleTrigger>;
/** BMS dynamic volume changes (97 / 98), in chart order; a bmson chart has none unless stitched from BMS stages. */
readonly volumeChanges: ReadonlyArray<ResolvedVolumeChange>;
/** BGA cues (base / layer / layer 2 / POOR), BMS channels and bmson `bga` alike. */
readonly bga: BGATimelines;
readonly scroll: ReadonlyArray<ScrollTimelinePoint>;
readonly speed: ReadonlyArray<SpeedTimelinePoint>;
/** Bar-line beats; empty when the chart suppresses them (bmson `lines: []`). */
readonly barLines: ReadonlyArray<number>;
readonly barLinesSuppressed: boolean;
readonly judgeRank: ResolvedJudgeRank;
readonly total: ResolvedTotal;
/** When the last judged note, mine or invisible object ends, in seconds. */
readonly lastNoteSeconds: number;
}
/**
* The single Source → Resolved step: resolves `chart` for play and returns its {@link ResolvedChart}.
*
* `chart` should already carry no control flow (resolve it with `resolveBMSControlFlowWithDraws` or `resolvePlayChart`
* and pass the draws as `options.randomDraws`); a chart that still does is resolved from its unconditional content
* only, as a song list sees it.
*/
export function resolveChart(chart: BeMusicJSON, options: ResolveChartOptions = {}): ResolvedChart {
return new LazyResolvedChart(chart, options);
}
/** What {@link countScoredNotes} reports: the scored notes and the play variant they were resolved under. */
export interface ScoredNoteCount {
/** The notes the engine scores: the judged notes without FREE ZONE ones (a long note counts once). */
readonly notes: number;
readonly playVariant: ChartPlayVariant;
}
/** Notes carry their time in seconds; counting them needs none, so this stands in for a BPM / STOP timing resolver. */
const NO_SECONDS = { beatToSeconds: () => 0 } as unknown as TimingResolver;
/**
* How many notes a chart scores, as `resolveChart(chart, options).notes` counts them without its FREE ZONE notes, and
* the play variant that decided FREE ZONE. A song list needs only the count, so this resolves the same judged notes
* without timing every one of them.
*/
export function countScoredNotes(
chart: BeMusicJSON,
options: Omit<ResolveChartOptions, 'randomDraws'> = {},
): ScoredNoteCount {
const playVariant =
options.playVariant ??
resolveChartPlayVariant({
chartPath: options.chartPath,
events: chart.events,
bms: chart.bms,
bmson: chart.bmson,
lrx: chart.lrx,
});
const context = createTimingBuildContext(chart);
const { notes } = resolveJudgedNoteEntries({
json: chart,
timing: NO_SECONDS,
beatResolver: context.beatResolver,
sortedEvents: context.sortedEvents,
idBase: resolveBMSBase(chart),
resolvePlayVariant: () => playVariant,
inferBmsLNTypeWhenMissing: options.inferBmsLNTypeWhenMissing === true,
});
let count = 0;
for (const note of notes) {
if (note.freeZone !== true) {
count += 1;
}
}
return { notes: count, playVariant };
}
class LazyResolvedChart implements ResolvedChart {
readonly source: BeMusicJSON;
readonly sourceFormat: BeMusicSourceFormat;
readonly randomDraws: RandomDraws;
readonly hasUnresolvedControlFlow: boolean;
readonly idBase: 36 | 62;
readonly inferBmsLNTypeWhenMissing: boolean;
readonly beatResolver: BeatResolver;
readonly sortedEvents: ReadonlyArray<BeMusicEvent>;
readonly timing: TimingResolver;
readonly reversal: TempoReversalPoint | undefined;
private readonly playVariantInput: ChartPlayVariant | undefined;
private readonly chartPath: string | undefined;
private playVariantCache: ChartPlayVariant | undefined;
private judgedCache: ResolvedNoteLists | undefined;
private diagnosticsCache: ChartDiagnostic[] | undefined;
private readonly droppedStackedNotes: BeMusicEvent[] = [];
private minesCache: ResolvedNote[] | undefined;
private invisibleCache: ResolvedNote[] | undefined;
private triggersCache: TimedSampleTrigger[] | undefined;
private bgaCache: BGATimelines | undefined;
private volumeCache: ResolvedVolumeChange[] | undefined;
private scrollCache: ScrollTimelinePoint[] | undefined;
private speedCache: SpeedTimelinePoint[] | undefined;
private barLinesCache: number[] | undefined;
private judgeRankCache: ResolvedJudgeRank | undefined;
constructor(chart: BeMusicJSON, options: ResolveChartOptions) {
this.source = chart;
this.sourceFormat = chart.sourceFormat;
this.randomDraws = options.randomDraws ?? [];
this.hasUnresolvedControlFlow = hasControlFlow(chart);
this.idBase = resolveBMSBase(chart);
this.playVariantInput = options.playVariant;
this.chartPath = options.chartPath;
this.inferBmsLNTypeWhenMissing = options.inferBmsLNTypeWhenMissing === true;
const context = createTimingBuildContext(chart);
this.beatResolver = context.beatResolver;
this.sortedEvents = context.sortedEvents;
this.timing = createTimingResolverWithContext(chart, context);
this.reversal = this.timing.reversal;
}
get diagnostics(): readonly ChartDiagnostic[] {
Eif (this.diagnosticsCache === undefined) {
if (this.sourceFormat === 'bmson') this.judged();
this.diagnosticsCache = collectChartDiagnostics(this, this.droppedStackedNotes);
}
return this.diagnosticsCache;
}
get playVariant(): ChartPlayVariant {
this.playVariantCache ??=
this.playVariantInput ??
resolveChartPlayVariant({
chartPath: this.chartPath,
events: this.source.events,
bms: this.source.bms,
bmson: this.source.bmson,
lrx: this.source.lrx,
});
return this.playVariantCache;
}
get layouts(): ReadonlyArray<PlayModeLayout> {
return getPlayModeLayouts(this.playVariant);
}
get lnMode(): LongNoteMode {
return resolveChartLongNoteMode(this.source);
}
get notes(): ReadonlyArray<ResolvedNote> {
return this.judged().notes;
}
get shadowedNotes(): ReadonlyArray<ResolvedNote> {
return this.judged().shadowedNotes;
}
get mines(): ReadonlyArray<ResolvedNote> {
this.minesCache ??= resolveMineNotes(this.noteContext());
return this.minesCache;
}
get invisibleNotes(): ReadonlyArray<ResolvedNote> {
this.invisibleCache ??= resolveInvisibleNotes(this.noteContext());
return this.invisibleCache;
}
get sampleTriggers(): ReadonlyArray<TimedSampleTrigger> {
this.triggersCache ??= collectSampleTriggersWithContext(
this.source,
this.timing,
{ sortedEvents: this.sortedEvents as BeMusicEvent[], beatResolver: this.beatResolver },
{ inferBmsLNTypeWhenMissing: this.inferBmsLNTypeWhenMissing },
);
return this.triggersCache;
}
get volumeChanges(): ReadonlyArray<ResolvedVolumeChange> {
this.volumeCache ??= resolveVolumeChanges(this.source, this.sortedEvents, this.timing);
return this.volumeCache;
}
get bga(): BGATimelines {
this.bgaCache ??= buildBGATimelines(this.source, this.timing);
return this.bgaCache;
}
get scroll(): ReadonlyArray<ScrollTimelinePoint> {
this.scrollCache ??= createScrollTimeline(this.source, this.beatResolver);
return this.scrollCache;
}
get speed(): ReadonlyArray<SpeedTimelinePoint> {
this.speedCache ??= createSpeedTimeline(this.source, this.beatResolver);
return this.speedCache;
}
get barLinesSuppressed(): boolean {
return this.source.bmson.barlinesSuppressed === true;
}
get barLines(): ReadonlyArray<number> {
this.barLinesCache ??= this.barLinesSuppressed ? [] : createMeasureBoundariesBeats(this.source, this.beatResolver);
return this.barLinesCache;
}
get judgeRank(): ResolvedJudgeRank {
this.judgeRankCache ??= resolveJudgeRank(this.source, this.sortedEvents, this.timing, this.idBase);
return this.judgeRankCache;
}
get total(): ResolvedTotal {
return resolveTotalFacts(this.source);
}
get lastNoteSeconds(): number {
let last = 0;
for (const note of this.notes) {
const end =
typeof note.endSeconds === 'number' && Number.isFinite(note.endSeconds) && note.endSeconds > note.seconds
? note.endSeconds
: note.seconds;
if (end > last) {
last = end;
}
}
return Math.max(last, this.mines.at(-1)?.seconds ?? 0, this.invisibleNotes.at(-1)?.seconds ?? 0);
}
private judged(): ResolvedNoteLists {
this.judgedCache ??= resolveJudgedNotes(this.noteContext(), this.droppedStackedNotes);
return this.judgedCache;
}
private noteContext(): ResolvedNoteContext {
return {
json: this.source,
timing: this.timing,
beatResolver: this.beatResolver,
sortedEvents: this.sortedEvents,
idBase: this.idBase,
resolvePlayVariant: () => this.playVariant,
inferBmsLNTypeWhenMissing: this.inferBmsLNTypeWhenMissing,
};
}
}
function resolveVolumeChanges(
json: BeMusicJSON,
sortedEvents: ReadonlyArray<BeMusicEvent>,
timing: TimingResolver,
): ResolvedVolumeChange[] {
// BMS charts, and NONSTOP charts stitched as bmson from BMS stages, carry 97 / 98 objects; bmson itself has none.
const changes: ResolvedVolumeChange[] = [];
for (const event of sortedEvents) {
const channel = normalizeChannel(event.channel);
if (!isBMSDynamicVolumeChangeChannel(channel)) {
continue;
}
const gain = parseBMSDynamicVolumeGain(event.value);
if (gain === undefined) {
continue;
}
changes.push({
seconds: timing.eventToSeconds(event),
bus: isBmsBGMVolumeChangeChannel(channel) && !isBMSKeyVolumeChangeChannel(channel) ? 'bgm' : 'play',
gain,
event,
});
}
return changes;
}
function resolveJudgeRank(
json: BeMusicJSON,
sortedEvents: ReadonlyArray<BeMusicEvent>,
timing: TimingResolver,
idBase: 36 | 62,
): ResolvedJudgeRank {
const changes = collectExRankChanges(json, sortedEvents, idBase).map((change) => ({
seconds: timing.eventToSeconds(change.event),
exRankValue: change.exRankValue,
}));
return { ...resolveJudgeRankFacts(json), changes };
}
/**
* The chart's own judge-rank values: `#RANK` (or the parsed rank) and `#DEFEXRANK` / bmson `judge_rank`, each only when
* the chart has a finite one. Not part of the package API; {@link ResolvedChart.judgeRank} and the chart fingerprint
* read them.
*/
export function resolveJudgeRankFacts(json: BeMusicJSON): { rank?: number; exRank?: number } {
const facts: { rank?: number; exRank?: number } = {};
const rank = json.metadata.rank;
if (typeof rank === 'number' && Number.isFinite(rank)) {
facts.rank = rank;
}
const exRank = json.sourceFormat === 'bmson' ? json.bmson.info.judgeRank : json.bms.defExRank;
if (typeof exRank === 'number' && Number.isFinite(exRank)) {
facts.exRank = exRank;
}
return facts;
}
/** An `#EXRANKxx` change (BMS channel `A0`) with a positive value; `raw` is the definition as written. */
export interface ExRankChange {
readonly event: BeMusicEvent;
readonly raw: string;
readonly exRankValue: number;
}
/**
* The `#EXRANKxx` changes of a BMS chart in chart order (none for bmson): `A0` objects whose `#EXRANKxx` reads as a
* positive number. Not part of the package API.
*/
export function collectExRankChanges(
json: BeMusicJSON,
sortedEvents: ReadonlyArray<BeMusicEvent>,
idBase: 36 | 62,
): ExRankChange[] {
const changes: ExRankChange[] = [];
if (json.sourceFormat !== 'bms') {
return changes;
}
for (const event of sortedEvents) {
if (normalizeChannel(event.channel) !== 'A0') {
continue;
}
const raw = json.bms.exRank[normalizeObjectKey(event.value, idBase)];
const parsed = Number.parseFloat(raw ?? '');
if (raw === undefined || !Number.isFinite(parsed) || parsed <= 0) {
continue;
}
changes.push({ event, raw, exRankValue: parsed });
}
return changes;
}
/**
* The chart's TOTAL as authored: `#TOTAL` (absolute) or bmson `info.total` (a percentage of the ruleset's default),
* with `value` only when it is finite. Not part of the package API; {@link ResolvedChart.total} and the chart
* fingerprint read it.
*/
export function resolveTotalFacts(json: BeMusicJSON): ResolvedTotal {
const value = json.metadata.total;
const unit = json.bmson.info.totalUnit ?? (json.sourceFormat === 'bmson' ? 'percent' : 'absolute');
return typeof value === 'number' && Number.isFinite(value) ? { value, unit } : { unit };
}
|