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 | 49x 49x 218x 218x 28x 28x 28x 3x 6x 28x 28x 28x 28x 28x 28x 28x 28x 14x 10x 10x 28x 11x 28x 18x 3x 18x 18x 18x 18x 18x 17x 17x 3x 3x 4x 3x 6x 3x 3x 3x 3x 3x 21x 153x 21x 21x 21x 21x 84x 21x 21x 3x 21x 21x 21x 1x 21x 23x 22x 22x 22x 9x 9x 9x 9x 9x 5x 9x 9x 25x 25x 2x 25x 25x 24x 24x 24x 24x 13x 13x 13x 13x 13x 1x 1x 1x 23x 23x 23x 23x 23x 25x 12x 23x 8x 13x 13x 13x 13x 7x 5x 7x 13x 9x 9x 9x 1x 21x 21x 23x 23x 50x 39x 39x 39x 23x 23x 21x 20x 20x 26x 26x 52x 52x 10040x 10040x 10040x 10040x 10040x 10040x 10040x 10040x 10040x 10040x 5023x 10040x 10040x 10040x 10040x 10040x 2x 2x 2x 2x 1x 1x 2x 2x 2x 5021x 5021x 5021x 5021x 5021x 5021x 10034x 10034x 10034x 10034x 9x 9x 9x 9x 2x 2x 2x 2x 2x 2x 1x 1x | import { collectBmsonBGAFrameKeys } from './bmson-bga.ts';
// BGA resource loading for the play session (`session/play/play-media.ts`), which every gameplay family plays
// through. One place decides which `#BMPxx` / bmson BGA resources a play needs, decodes
// them (stills through the shared texture loader, videos through the video loader with its transcode fallback), keys the
// BMS layer track's black out and cuts the `#BGAxx` sub-region views.
import { Rectangle, Texture } from 'pixi.js';
import { runWithConcurrency } from '@be-music/shared-utils/core';
import {
parseBMSExBMP,
buildKeyBoundBGATimeline,
createTimingResolver,
keysOutBGALayerBlack,
resolveBMSExBMPColorKey,
resolvePoorBGAMode,
type BmsARGB,
type KeyBoundBGACue,
type BmsonBGAEventCue,
} from '@be-music/chart-model';
import type { BeMusicJSON } from '@be-music/chart-source';
import { loadAssetBytes, loadAssetFile, resolveChartBGAAsset } from '../collection/collection.ts';
import type { BrowserAssetSource } from '../collection/types.ts';
import { loadTextureFromBytes, loadVideoTexture, type VideoTranscodeOptions } from '../media/textures.ts';
import { collectBGATextureLoadKeys, isVideoExtension, type BGACue } from './bga-timeline.ts';
import { logger } from '../logger.ts';
import { collectBGASubRegions } from './bga-subregions.ts';
import { createColorKeyVideoTexture } from '../media/color-key-video.ts';
import { keyBoundBGAResourceKey } from './key-bound-bga.ts';
const log = logger('play-bga');
/** BGA resources decoded at once: image / video decoders allocate native memory outside the JS heap. */
const BGA_DECODE_CONCURRENCY = 4;
export interface PlayBGATimeline {
base: readonly BGACue[];
layer: readonly BGACue[];
layer2?: readonly BGACue[];
poor: readonly BGACue[];
}
/** A video BGA: its `<video>` element and the object URL it plays from (revoke it when the play ends). */
export interface PlayBGAVideo {
video: HTMLVideoElement;
objectURL: string;
dispose?: () => void;
}
/** Where {@link loadPlayBGA} puts what it decoded, keyed by the timeline's BMP key. */
export interface PlayBGAResources {
/** Base + POOR track textures (black kept). */
textures: Map<string, Texture>;
/** Layer track textures: black keyed out for a BMS chart, kept for bmson (its spec says so). */
layerTextures: Map<string, Texture>;
videos: Map<string, PlayBGAVideo>;
/** Each visible sub-region's position on the 256×256 canvas after composing and clipping aliases. */
subRegionOrigins: Map<string, { x: number; y: number }>;
}
export { resolvePoorBGAMode, type PoorBGAMode } from '@be-music/chart-model';
/**
* BMS: a `#BMP00` is the implicit POOR image until an explicit `#xxx06` cue fires, as LR2 starts its miss layer from
* BMP00. Only `#POORBGA 2` (no POOR image at all) turns it off. Returns that key (`'00'`), or `undefined` when the
* convention does not apply.
*/
export function resolvePoorBGAFallbackKey(chart: BeMusicJSON): string | undefined {
const poorBMP00 = chart.resources.bmp['00'] || parseBMSExBMP(chart.bms.exBMP['00'] ?? '')?.filename;
return resolvePoorBGAMode(chart.bms.poorBGA) !== 2 && typeof poorBMP00 === 'string' && poorBMP00.length > 0
? '00'
: undefined;
}
/**
* The BGA resources a play loads: every BMP key its timeline (and the implicit POOR image) references, with the source
* BMPs of referenced `#BGAxx` sub-regions, mapped to the file to load — `#BMPxx` paths for BMS, the `bga.header[]`
* names for bmson. `base` / `layer` tell which track(s) each key is drawn on.
*/
export function collectPlayBGAResources(
chart: BeMusicJSON,
timeline: PlayBGATimeline,
poorFallbackKey: string | undefined,
includeBmsonEvents = true,
bmsonBGACues?: readonly BmsonBGAEventCue[],
): { paths: Map<string, string>; base: Set<string>; layer: Set<string> } {
const { base, layer } = collectBGATextureLoadKeys(chart, timeline, poorFallbackKey);
const sequences = includeBmsonEvents
? collectBmsonBGAFrameKeys(chart, bmsonBGACues)
: { layer: new Set<string>(), poor: new Set<string>() };
const extras = collectBGATextureLoadKeys(
chart,
{
base: [...sequences.poor].map((bmpKey) => ({ seconds: 0, bmpKey })),
layer: [...sequences.layer].map((bmpKey) => ({ seconds: 0, bmpKey })),
poor: [],
},
undefined,
);
for (const key of extras.base) base.add(key);
for (const key of extras.layer) layer.add(key);
// An LRX chart drawn by the compositor can show any of its images: a keyframe chain counts its image id up.
Iif ((chart.lrx?.bga?.length ?? 0) > 0) {
for (const key of Object.keys(chart.resources.bmp)) {
base.add(key);
layer.add(key);
}
}
const referenced = new Set<string>([...base, ...layer]);
const paths = new Map<string, string>();
for (const [key, path] of Object.entries(chart.resources.bmp)) {
if (typeof path === 'string' && referenced.has(key)) paths.set(key, path);
}
for (const [key, raw] of Object.entries(chart.bms.exBMP)) {
if (!referenced.has(key) || paths.get(key)) continue;
const definition = parseBMSExBMP(raw);
if (definition) paths.set(key, definition.filename);
}
for (const entry of chart.bmson.bga.header) {
if (referenced.has(entry.name)) paths.set(entry.name, entry.name);
}
return { paths, base, layer };
}
export interface LoadPlayBGAOptions {
/** Remapped selections from NONSTOP; omitted uses the chart A5 controls. */
keyBoundBGACues?: readonly KeyBoundBGACue[];
bmsonBGACues?: readonly BmsonBGAEventCue[];
/** The resolved chart (after `#RANDOM` / `#IF`). */
chart: BeMusicJSON;
source: BrowserAssetSource;
chartPath: string;
timeline: PlayBGATimeline;
poorFallbackKey?: string;
/** Where the decoded resources land (filled in place). */
into: PlayBGAResources;
/** How a video the browser can't play is transcoded. */
transcode?: VideoTranscodeOptions;
/** The play tempo: videos run at it, so their time stays the chart time since their cue. */
tempo: number;
/** True once the play was abandoned: what is still loading is dropped and freed. */
isCancelled?: () => boolean;
/** Fraction of BGA resource loading completed, excluding GPU uploads. */
onProgress?: (fraction: number) => void;
}
/**
* Loads every BGA resource the play's timeline references into `options.into`. Each resource is read on demand (lazy
* `File` entries), found through the image / video resolver (`foo.bmp` shipped as `foo.png`, `_bga.mpg` re-encoded to
* `_bga.mp4`), and decoded with bounded parallelism. Videos start paused, muted and at the play tempo; a file that
* fails to decode is skipped so the rest of the chart still renders.
*/
export async function loadPlayBGA(options: LoadPlayBGAOptions): Promise<void> {
const definitions = new Map(
(options.keyBoundBGACues ?? buildKeyBoundBGATimeline(options.chart, createTimingResolver(options.chart))).map(
(cue) => [cue.definitionId, cue.definition],
),
);
const setCount = 1 + definitions.size;
let completedSets = 0;
const reportSetProgress =
options.onProgress && ((fraction: number) => options.onProgress?.((completedSets + fraction) / setCount));
await loadBGAResourceSet({ ...options, onProgress: reportSetProgress });
if (options.isCancelled?.()) return;
completedSets++;
for (const [id, definition] of definitions) {
const into: PlayBGAResources = {
textures: new Map(),
layerTextures: new Map(),
videos: new Map(),
subRegionOrigins: new Map(),
};
await loadBGAResourceSet(
{
...options,
poorFallbackKey: undefined,
onProgress: reportSetProgress,
into,
timeline: { base: [], poor: [], layer: definition.frames.map((bmpKey) => ({ seconds: 0, bmpKey })) },
},
definition.argb,
);
for (const [key, texture] of into.layerTextures)
options.into.layerTextures.set(keyBoundBGAResourceKey(id, key), texture);
for (const [key, origin] of into.subRegionOrigins)
options.into.subRegionOrigins.set(keyBoundBGAResourceKey(id, key), origin);
for (const [key, video] of into.videos) options.into.videos.set(keyBoundBGAResourceKey(id, key), video);
Iif (options.isCancelled?.()) return;
completedSets++;
}
}
async function loadBGAResourceSet(options: LoadPlayBGAOptions, swBGAColorKey?: BmsARGB): Promise<void> {
const { chart, source, chartPath, into } = options;
const isCancelled = options.isCancelled ?? (() => false);
const { paths, base, layer } = collectPlayBGAResources(
chart,
options.timeline,
options.poorFallbackKey,
swBGAColorKey === undefined,
options.bmsonBGACues,
);
// The bmson spec draws layer blacks as black ("Unlike BMS Layer Channel #xxx07"); BMS keys them out.
// A BMS stage's image in a NONSTOP chart stitched as bmson keeps the BMS rule (`resources.bmpSourceFormat`).
const keyOutLayerBlack = (key: string): boolean => swBGAColorKey === undefined && keysOutBGALayerBlack(chart, key);
const aliasSources = new Set([...collectBGASubRegions(chart).values()].map((region) => region.sourceBMP));
const trackKeys = (cues: readonly BGACue[]) =>
collectBGATextureLoadKeys(chart, { base: cues, layer: [], poor: [] }, undefined).base;
const sequenceKeys =
swBGAColorKey === undefined
? collectBmsonBGAFrameKeys(chart, options.bmsonBGACues)
: { layer: new Set<string>(), poor: new Set<string>() };
const firstLayer = trackKeys([
...options.timeline.layer,
...[...sequenceKeys.layer].map((bmpKey) => ({ seconds: 0, bmpKey })),
]);
const secondLayer = trackKeys(options.timeline.layer2 ?? []);
const baseOnly = trackKeys(options.timeline.base);
const poor = trackKeys([
...options.timeline.poor,
...[...sequenceKeys.poor].map((bmpKey) => ({ seconds: 0, bmpKey })),
...(options.poorFallbackKey ? [{ seconds: 0, bmpKey: options.poorFallbackKey }] : []),
]);
const loadResource = async ([key, path]: [string, string], report: (fraction: number) => void): Promise<void> => {
if (isCancelled()) return;
const resolved = resolveChartBGAAsset(source, chartPath, path);
try {
if (isVideoExtension(resolved?.path ?? path)) {
// A video plays (and transcodes) from its file, which is not read into memory.
const file = await loadAssetFile(resolved?.entry);
Iif (!file || isCancelled()) return;
// Reused video BMPs need an independent playhead on each track.
// NUL-prefixed keys are internal and cannot collide with an external asset path.
const videoKeys = [key];
if (base.has(key) && firstLayer.has(key)) videoKeys.push(String.fromCharCode(0) + 'layer:' + key);
if ((base.has(key) || firstLayer.has(key)) && secondLayer.has(key))
videoKeys.push(String.fromCharCode(0) + 'layer2:' + key);
if (poor.has(key) && baseOnly.has(key)) videoKeys.push(String.fromCharCode(0) + 'poor:' + key);
for (const [index, videoKey] of videoKeys.entries()) {
const transcode = aliasSources.has(key) ? { ...options.transcode, maxLongEdgePx: 0 } : options.transcode;
const handle = await loadVideoTexture(
resolved?.path ?? path,
file,
options.onProgress
? { ...transcode, onProgress: (fraction) => report((index + fraction) / videoKeys.length) }
: transcode,
);
report((index + 1) / videoKeys.length);
if (!handle) continue;
Iif (isCancelled()) {
releaseVideo({ video: handle.video, objectURL: handle.objectURL });
destroyTexture(handle.texture);
return;
}
let texture = handle.texture;
let dispose: (() => void) | undefined;
const bmsColorKey = resolveBMSExBMPColorKey(chart, key);
if (swBGAColorKey || bmsColorKey) {
try {
const keyed = createColorKeyVideoTexture(handle.video, { swBGAColorKey, bmsColorKey });
texture = keyed.texture;
dispose = keyed.dispose;
(base.has(key) ? into.textures : into.layerTextures).set(
`${String.fromCharCode(0)}native:${videoKey}`,
handle.texture,
);
} catch (error) {
releaseVideo(handle);
destroyTexture(handle.texture);
throw error;
}
}
texture.label = `bga[${key}]:video`;
handle.video.pause();
handle.video.defaultPlaybackRate = options.tempo;
handle.video.playbackRate = options.tempo;
into.videos.set(videoKey, {
video: handle.video,
objectURL: handle.objectURL,
...(dispose ? { dispose } : {}),
});
if ((videoKey === key || videoKey.startsWith(String.fromCharCode(0) + 'poor:')) && base.has(key))
into.textures.set(videoKey, texture);
if (layer.has(key)) into.layerTextures.set(videoKey, texture);
}
return;
}
const bytes = await loadAssetBytes(resolved?.entry);
Iif (!bytes || isCancelled()) return;
const bmsColorKey = resolveBMSExBMPColorKey(chart, key);
if (base.has(key)) {
const texture = bmsColorKey
? await loadTextureFromBytes(resolved?.path ?? path, bytes, { bmsColorKey })
: await loadTextureFromBytes(resolved?.path ?? path, bytes);
Iif (isCancelled()) return destroyTexture(texture);
Eif (texture) into.textures.set(key, texture);
}
if (layer.has(key)) {
const texture = await loadTextureFromBytes(resolved?.path ?? path, bytes, {
keyOutBlack: keyOutLayerBlack(key) && bmsColorKey === undefined,
...(bmsColorKey ? { bmsColorKey } : {}),
...(swBGAColorKey ? { swBGAColorKey } : {}),
});
Iif (isCancelled()) return destroyTexture(texture);
Eif (texture) into.layerTextures.set(key, texture);
}
} catch (error) {
log.debug('bga decode skipped', { key, path, error });
}
};
let processed = 0;
await runWithConcurrency([...paths], BGA_DECODE_CONCURRENCY, async (resource) => {
let resourceProgress = 0;
const report = (fraction: number): void => {
if (isCancelled() || fraction <= resourceProgress) return;
processed += fraction - resourceProgress;
resourceProgress = fraction;
options.onProgress?.(Math.min(1, processed / paths.size));
};
await loadResource(resource, report);
report(1);
});
if (!isCancelled()) {
registerSubRegionBGATextures(chart, into);
if (paths.size === 0) options.onProgress?.(1);
}
}
/**
* Cuts texture views for `#BGAxx YY sx sy ex ey dx dy` sub-regions, composing chained aliases from their loaded root
* image on each track. Records the visible rectangle's placement after clipping; missing/cyclic aliases stay absent.
* The views share their source's `TextureSource`: no extra upload.
*/
export function registerSubRegionBGATextures(chart: BeMusicJSON, into: PlayBGAResources): void {
const regions = collectBGASubRegions(chart);
for (const textures of [into.textures, into.layerTextures]) {
const visited = new Set<string>();
for (const root of regions.keys()) {
const path: string[] = [];
let current = root;
while (!visited.has(current) && !textures.has(current) && regions.has(current)) {
visited.add(current);
path.push(current);
current = regions.get(current)!.sourceBMP;
}
// Dependencies are registered before their consumers, independent of declaration order. Cycles have no
// loaded source, so none of their texture views are created.
for (const slot of path.reverse()) {
const region = regions.get(slot)!;
const parent = textures.get(region.sourceBMP);
if (!parent) continue;
const origin = into.subRegionOrigins.get(region.sourceBMP) ?? { x: 0, y: 0 };
// A parent's texture contains only its visible rectangle, located at origin on a 256×256 alias canvas.
// Root images retain their native coordinates; each alias clips both the source and destination edges.
const left = Math.max(0, region.dx, region.dx + origin.x - region.sx);
const top = Math.max(0, region.dy, region.dy + origin.y - region.sy);
const right = Math.min(
256,
region.dx + region.ex - region.sx,
region.dx + origin.x + parent.frame.width - region.sx,
);
const bottom = Math.min(
256,
region.dy + region.ey - region.sy,
region.dy + origin.y + parent.frame.height - region.sy,
);
if (right <= left || bottom <= top) {
// Keep a real canvas resource so Canvas skins can display a blank base/POOR too, and avoid sharing
// Texture.EMPTY's global source with the session's owned textures.
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 256;
// WebGPU requires a canvas with a rendering context, including the transparent layer canvas.
const context = canvas.getContext('2d')!;
if (textures === into.textures) {
context.fillStyle = '#000000';
context.fillRect(0, 0, 256, 256);
}
textures.set(slot, Texture.from(canvas));
into.subRegionOrigins.set(slot, { x: 0, y: 0 });
continue;
}
const frame = new Rectangle(
parent.frame.x + region.sx + left - region.dx - origin.x,
parent.frame.y + region.sy + top - region.dy - origin.y,
right - left,
bottom - top,
);
textures.set(slot, new Texture({ source: parent.source, frame }));
into.subRegionOrigins.set(slot, { x: left, y: top });
const video = into.videos.get(region.sourceBMP);
if (video) into.videos.set(slot, video);
// Each plane keeps its own root video/playhead, while aliases share that plane's root. A transition
// between aliases still uses a different cue key and restarts the root at the new cue's offset.
for (const track of textures === into.textures ? ['poor'] : ['layer', 'layer2']) {
const parentKey = String.fromCharCode(0) + track + ':' + region.sourceBMP;
const parentVideo = into.videos.get(parentKey);
const parentTexture = textures.get(parentKey);
if (!parentVideo || !parentTexture) continue;
const key = String.fromCharCode(0) + track + ':' + slot;
textures.set(key, new Texture({ source: parentTexture.source, frame: frame.clone() }));
into.subRegionOrigins.set(key, { x: left, y: top });
into.videos.set(key, parentVideo);
}
}
}
}
}
/** Stops a video BGA and frees its object URL. Pause before destroying its texture: in-flight frame callbacks throw. */
export function releaseVideo({ video, objectURL, dispose }: PlayBGAVideo): void {
dispose?.();
try {
video.pause();
video.removeAttribute('src');
video.load();
} catch {
// `load()` can throw on a detached video.
}
URL.revokeObjectURL(objectURL);
}
function destroyTexture(texture: Texture | undefined): void {
try {
texture?.destroy(true);
} catch {
// Already destroyed.
}
}
|