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 | 243x 243x 135x 135x 135x 135x 135x 339x 12x 327x 102x 102x 327x 168x 168x 168x 168x 168x 168x 190x 190x 190x 190x 190x 190x 190x 174x 16x 16x 190x 16x 174x 152x 22x 152x 155x 155x 155x 155x 155x 155x 155x 155x 161x 161x 161x 161x 161x 161x 161x 1x 1x 1x 160x 155x 5x 5x 5x 94x 94x 94x 94x 94x 108x 108x 108x 99x 99x 91x 91x 8x 9x 7x 17x 3x 7x 7x 7x 429x 429x 429x 73x 135x 135x 135x 281x 329x 242x 135x 135x 135x 135x 329x 329x 329x 329x 329x 329x 329x 329x 329x 194x 135x 135x 135x 270x 270x 265x 265x 270x 270x 270x 270x 535x 535x 270x 270x 265x 69x 69x 196x 196x 270x 608x 329x 582x 523x 523x 523x 523x 859x 161x 1020x 1020x 93x 927x 246x 246x 2x 244x 244x 244x 244x 241x 3x 3x 3x 1238x 1238x 1238x 927x 1128x 853x 853x 135x 71x 64x 16x 14x 14x 24x 24x 3x 21x 21x 21x 14x 11x 11x 11x 43x 43x 4x 2x 4x 4x 2x 1x 2x 2x 9x 9x 9x | import { clamp, findLastIndexAtOrBefore } from '@be-music/shared-utils/core';
import type { ScrollTimelinePoint, SpeedTimelinePoint, TempoPoint } from '@be-music/chart-model';
export interface ScrollDistanceMapperOptions {
lookaheadBeats?: number;
invalidDistance?: number;
}
export interface ScrollDistanceMapperLike {
/** Guaranteed positive distance for every later beat, permitting safe forward-only culling. */
readonly strictlyIncreasing?: true;
/** Mirrors a beat in converted scroll-distance space (LR2 CONSTANT negative-BPM reversal). */
mirrorBeatAt?: (anchorBeat: number, forwardBeat: number) => number;
distanceBetween: (fromBeat: number, toBeat: number) => number;
hasBidirectionalScrollWithinLookahead: (fromBeat: number) => boolean;
maxBeatWithinDistance: (fromBeat: number, distance: number) => number;
/**
* Where the chart first runs past a lane `distance` beats tall ({@link ScrollDistanceMapper.firstBeatBeyondDistance}).
* Optional so implementations written before it keep type-checking; callers then walk without an early exit.
*/
firstBeatBeyondDistance?: (fromBeat: number, distance: number) => number;
/**
* The scroll distance per beat at a beat ({@link ScrollDistanceMapper.rateAt}). Optional so implementations written
* before it keep type-checking; callers then assume a rate of 1.
*/
rateAt?: (beat: number) => number;
}
interface ScrollSegment {
startBeat: number;
scrollSpeed: number;
speedStart: number;
speedSlope: number;
startDistance: number;
}
interface SegmentDistanceTerms {
linear: number;
quadratic: number;
}
interface TimelinePointNormalizerOptions {
allowNegativeSpeed: boolean;
dropConsecutiveSameSpeed: boolean;
}
const DEFAULT_SCROLL_LOOKAHEAD_BEATS = 4 * 64;
const BEAT_EPSILON = 1e-9;
export class ScrollDistanceMapper implements ScrollDistanceMapperLike {
private readonly segments: ScrollSegment[];
private readonly lookaheadBeats: number;
private readonly invalidDistance: number;
/**
* The last `fromBeat` {@link distanceBetween} measured from, and its distance: a frame measures every note and bar
* line from the same playhead, so its distance is worked out once per frame instead of once per object.
*/
private lastFromBeat = Number.NaN;
private lastFromDistance = 0;
constructor(
scrollTimeline?: ReadonlyArray<ScrollTimelinePoint>,
speedTimeline?: ReadonlyArray<SpeedTimelinePoint>,
options: ScrollDistanceMapperOptions = {},
) {
this.segments = buildScrollSegments(scrollTimeline, speedTimeline);
this.lookaheadBeats = normalizeLookaheadBeats(options.lookaheadBeats);
this.invalidDistance = options.invalidDistance ?? Number.NaN;
}
distanceBetween(fromBeat: number, toBeat: number): number {
if (!Number.isFinite(fromBeat) || !Number.isFinite(toBeat)) {
return this.invalidDistance;
}
if (fromBeat !== this.lastFromBeat) {
this.lastFromDistance = this.distanceAt(fromBeat);
this.lastFromBeat = fromBeat;
}
return this.distanceAt(toBeat) - this.lastFromDistance;
}
hasBidirectionalScrollWithinLookahead(fromBeat: number): boolean {
const safeFromBeat = Number.isFinite(fromBeat) ? Math.max(0, fromBeat) : 0;
const capBeat = safeFromBeat + this.lookaheadBeats;
let index = findLastSegmentIndexByBeat(this.segments, safeFromBeat);
let sawPositive = false;
let sawNegative = false;
while (index < this.segments.length) {
const segment = this.segments[index]!;
const segmentStartBeat = Math.max(safeFromBeat, segment.startBeat);
const nextStartBeat = this.segments[index + 1]?.startBeat ?? Number.POSITIVE_INFINITY;
const segmentEndBeat = Math.min(capBeat, nextStartBeat);
Iif (segmentEndBeat - segmentStartBeat <= BEAT_EPSILON) {
if (segmentEndBeat >= capBeat) {
break;
}
index += 1;
continue;
}
const signedDistance = integratedSignedSegmentDistance(segment, segmentStartBeat, segmentEndBeat);
if (signedDistance > BEAT_EPSILON) {
sawPositive = true;
} else Eif (signedDistance < -BEAT_EPSILON) {
sawNegative = true;
}
if (sawPositive && sawNegative) {
return true;
}
if (segmentEndBeat >= capBeat) {
break;
}
index += 1;
}
return false;
}
maxBeatWithinDistance(fromBeat: number, distance: number): number {
const safeFromBeat = Number.isFinite(fromBeat) ? Math.max(0, fromBeat) : 0;
const safeDistance = Number.isFinite(distance) ? Math.max(0, distance) : 0;
Iif (safeDistance <= 0) {
return safeFromBeat;
}
const capBeat = safeFromBeat + this.lookaheadBeats;
let beat = safeFromBeat;
let remainingDistance = safeDistance;
let index = findLastSegmentIndexByBeat(this.segments, beat);
while (index < this.segments.length && beat < capBeat && remainingDistance > BEAT_EPSILON) {
const segment = this.segments[index]!;
const nextStartBeat = this.segments[index + 1]?.startBeat ?? Number.POSITIVE_INFINITY;
const segmentEndBeat = Math.min(capBeat, nextStartBeat);
const span = Math.max(0, segmentEndBeat - beat);
Iif (span <= 0) {
index += 1;
continue;
}
const traversableDistance = integratedAbsoluteSegmentDistance(segment, beat, segmentEndBeat);
if (traversableDistance <= BEAT_EPSILON) {
beat = segmentEndBeat;
index += 1;
continue;
}
if (traversableDistance >= remainingDistance) {
return Math.min(capBeat, beat + solveBeatDeltaWithinSegment(segment, beat, remainingDistance));
}
remainingDistance -= traversableDistance;
beat = segmentEndBeat;
index += 1;
}
return capBeat;
}
/**
* The first beat at or after `fromBeat` whose signed scroll distance from `fromBeat` exceeds `distance` — where the
* chart first runs past a lane `distance` beats tall — or `+Infinity` when it never does. Negative and zero
* `#SCROLL` segments only bring the distance down or hold it, so the walk carries on through them; unlike
* {@link maxBeatWithinDistance} it neither takes absolute distances nor stops at the lookahead cap.
*
* beatoraja's `LaneRenderer` walks the chart's timelines from the playhead and stops after the first one past the lane
* top, so nothing beyond this beat is drawn, even what a later negative `#SCROLL` brings back into the lane.
*/
firstBeatBeyondDistance(fromBeat: number, distance: number): number {
const safeFromBeat = Number.isFinite(fromBeat) ? Math.max(0, fromBeat) : 0;
const limit = Number.isFinite(distance) ? Math.max(0, distance) : 0;
let beat = safeFromBeat;
let travelled = 0;
for (let index = findLastSegmentIndexByBeat(this.segments, beat); index < this.segments.length; index += 1) {
const segment = this.segments[index]!;
const nextStartBeat = this.segments[index + 1]?.startBeat ?? Number.POSITIVE_INFINITY;
if (segment.scrollSpeed > 0) {
// The last segment runs forever: it overruns any lane unless `#SPEED` holds it at 0.
const segmentDistance = Number.isFinite(nextStartBeat)
? integratedSignedSegmentDistance(segment, beat, nextStartBeat)
: segmentDistanceTerms(segment, beat, segment.scrollSpeed).linear > BEAT_EPSILON
? Number.POSITIVE_INFINITY
: 0;
if (travelled + segmentDistance > limit) {
const delta = solveBeatDeltaWithinSegment(segment, beat, limit - travelled);
return Number.isFinite(nextStartBeat) ? Math.min(nextStartBeat, beat + delta) : beat + delta;
}
travelled += segmentDistance;
} else if (Number.isFinite(nextStartBeat)) {
travelled += integratedSignedSegmentDistance(segment, beat, nextStartBeat);
}
beat = Math.max(beat, nextStartBeat);
}
return Number.POSITIVE_INFINITY;
}
/**
* The scroll distance per beat at `beat`: the `#SCROLL` multiplier times the interpolated `#SPEED`. Negative while the
* chart scrolls backwards, 0 while it stands still.
*/
rateAt(beat: number): number {
const safeBeat = Number.isFinite(beat) ? Math.max(0, beat) : 0;
const segment = this.segments[findLastSegmentIndexByBeat(this.segments, safeBeat)]!;
return segmentDistanceTerms(segment, safeBeat, segment.scrollSpeed).linear;
}
private distanceAt(beat: number): number {
const safeBeat = Number.isFinite(beat) ? Math.max(0, beat) : 0;
const segment = this.segments[findLastSegmentIndexByBeat(this.segments, safeBeat)]!;
return segment.startDistance + integratedSignedSegmentDistance(segment, segment.startBeat, safeBeat);
}
}
export function createScrollDistanceMapper(
scrollTimeline?: ReadonlyArray<ScrollTimelinePoint>,
speedTimeline?: ReadonlyArray<SpeedTimelinePoint>,
options?: ScrollDistanceMapperOptions,
): ScrollDistanceMapper {
return new ScrollDistanceMapper(scrollTimeline, speedTimeline, options);
}
function buildScrollSegments(
scrollTimeline?: ReadonlyArray<ScrollTimelinePoint>,
speedTimeline?: ReadonlyArray<SpeedTimelinePoint>,
): ScrollSegment[] {
const scrollPoints = normalizeScrollPoints(scrollTimeline);
const speedPoints = normalizeSpeedPoints(speedTimeline);
const breakpoints = [
...new Set([...scrollPoints.map((point) => point.beat), ...speedPoints.map((point) => point.beat)]),
]
.filter((beat) => Number.isFinite(beat) && beat >= 0)
.sort((left, right) => left - right);
Iif (breakpoints.length === 0 || breakpoints[0] !== 0) {
breakpoints.unshift(0);
}
const segments: ScrollSegment[] = [];
let distance = 0;
for (let index = 0; index < breakpoints.length; index += 1) {
const startBeat = breakpoints[index]!;
const endBeat = breakpoints[index + 1];
const scrollSpeed = resolveScrollSpeedAtBeat(scrollPoints, startBeat);
const speedStart = resolveInterpolatedSpeedAtBeat(speedPoints, startBeat);
const speedEnd = typeof endBeat === 'number' ? resolveInterpolatedSpeedAtBeat(speedPoints, endBeat) : speedStart;
const speedSlope =
typeof endBeat === 'number' && endBeat > startBeat ? (speedEnd - speedStart) / (endBeat - startBeat) : 0;
const segment = {
startBeat,
scrollSpeed,
speedStart,
speedSlope,
startDistance: distance,
};
segments.push(segment);
if (typeof endBeat === 'number') {
distance += integratedSignedSegmentDistance(segment, startBeat, endBeat);
}
}
return segments.length > 0
? segments
: [{ startBeat: 0, scrollSpeed: 1, speedStart: 1, speedSlope: 0, startDistance: 0 }];
}
function normalizeScrollPoints(timeline?: ReadonlyArray<ScrollTimelinePoint>): ScrollTimelinePoint[] {
return normalizeTimelinePoints(timeline, {
allowNegativeSpeed: true,
dropConsecutiveSameSpeed: true,
});
}
/**
* `#SPEEDxx` keyframes behind a beat-0 head of `1`, so the speed ramps linearly from 1 at beat 0 to the first keyframe,
* as beatoraja's `LaneRenderer.getCurrentSpeed` ramps from 1.0 at time 0 (a keyframe on beat 0 applies at once).
* Be-Music interpolates in beats where beatoraja interpolates in time; see docs/format-compat.md §2.10.
*/
function normalizeSpeedPoints(timeline?: ReadonlyArray<SpeedTimelinePoint>): SpeedTimelinePoint[] {
return normalizeTimelinePoints(timeline, {
allowNegativeSpeed: false,
dropConsecutiveSameSpeed: false,
});
}
function normalizeTimelinePoints<T extends ScrollTimelinePoint | SpeedTimelinePoint>(
timeline: ReadonlyArray<T> | undefined,
options: TimelinePointNormalizerOptions,
): T[] {
const collected: Array<{ beat: number; speed: number }> = [];
for (const point of timeline ?? []) {
Iif (
!Number.isFinite(point.beat) ||
!Number.isFinite(point.speed) ||
point.beat < 0 ||
(!options.allowNegativeSpeed && point.speed < 0)
) {
continue;
}
collected.push({
beat: point.beat,
speed: point.speed,
});
}
collected.sort((left, right) => left.beat - right.beat);
const points: Array<{ beat: number; speed: number }> = [{ beat: 0, speed: 1 }, ...collected];
const merged: Array<{ beat: number; speed: number }> = [];
for (const point of points) {
const previous = merged.at(-1);
if (!previous) {
merged.push({ ...point });
continue;
}
if (Math.abs(point.beat - previous.beat) < BEAT_EPSILON) {
previous.speed = point.speed;
continue;
}
Iif (options.dropConsecutiveSameSpeed && Math.abs(point.speed - previous.speed) < BEAT_EPSILON) {
continue;
}
merged.push({ ...point });
}
return merged as T[];
}
function resolveScrollSpeedAtBeat(points: ReadonlyArray<ScrollTimelinePoint>, beat: number): number {
const index = findLastIndexAtOrBefore(points, beat, (point) => point.beat);
return points[Math.max(0, index)]?.speed ?? 1;
}
function resolveInterpolatedSpeedAtBeat(points: ReadonlyArray<SpeedTimelinePoint>, beat: number): number {
const index = findLastIndexAtOrBefore(points, beat, (point) => point.beat);
const current = points[Math.max(0, index)] ?? { beat: 0, speed: 1 };
const next = points[index + 1];
Eif (!next || beat <= current.beat || Math.abs(next.beat - current.beat) < BEAT_EPSILON) {
return current.speed;
}
const ratio = clamp((beat - current.beat) / (next.beat - current.beat), 0, 1);
return current.speed + (next.speed - current.speed) * ratio;
}
function integratedSignedSegmentDistance(segment: ScrollSegment, fromBeat: number, toBeat: number): number {
return integratedSegmentDistance(segment, fromBeat, toBeat, segment.scrollSpeed);
}
function integratedAbsoluteSegmentDistance(segment: ScrollSegment, fromBeat: number, toBeat: number): number {
return integratedSegmentDistance(segment, fromBeat, toBeat, Math.abs(segment.scrollSpeed));
}
function integratedSegmentDistance(
segment: ScrollSegment,
fromBeat: number,
toBeat: number,
scrollScale: number,
): number {
const delta = Math.max(0, toBeat - fromBeat);
if (delta <= 0) {
return 0;
}
return integrateSegmentDistanceTerms(segmentDistanceTerms(segment, fromBeat, scrollScale), delta);
}
function solveBeatDeltaWithinSegment(segment: ScrollSegment, fromBeat: number, distance: number): number {
const safeDistance = Number.isFinite(distance) ? Math.max(0, distance) : 0;
if (safeDistance <= 0) {
return 0;
}
const absScroll = Math.abs(segment.scrollSpeed);
Iif (absScroll <= BEAT_EPSILON) {
return 0;
}
const { linear, quadratic } = segmentDistanceTerms(segment, fromBeat, absScroll);
if (Math.abs(quadratic) <= BEAT_EPSILON) {
return linear <= BEAT_EPSILON ? 0 : safeDistance / linear;
}
const discriminant = linear * linear + 4 * quadratic * safeDistance;
Iif (discriminant <= 0) {
return 0;
}
return Math.max(0, (-linear + Math.sqrt(discriminant)) / (2 * quadratic));
}
function segmentDistanceTerms(segment: ScrollSegment, fromBeat: number, scrollScale: number): SegmentDistanceTerms {
const offset = Math.max(0, fromBeat - segment.startBeat);
const baseSpeed = segment.speedStart + segment.speedSlope * offset;
return {
linear: scrollScale * baseSpeed,
quadratic: 0.5 * scrollScale * segment.speedSlope,
};
}
function integrateSegmentDistanceTerms({ linear, quadratic }: SegmentDistanceTerms, deltaBeats: number): number {
return linear * deltaBeats + quadratic * deltaBeats * deltaBeats;
}
const segmentStartBeat = (segment: ScrollSegment): number => segment.startBeat;
function findLastSegmentIndexByBeat(segments: ScrollSegment[], beat: number): number {
const index = findLastIndexAtOrBefore(segments, beat, segmentStartBeat);
return Math.max(0, index);
}
function normalizeLookaheadBeats(value: number | undefined): number {
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
return DEFAULT_SCROLL_LOOKAHEAD_BEATS;
}
return value;
}
/**
* LR2 beta3 HS-FIX CONSTANT: 150-BPM distance, excluding STOP duration. The playhead's
* STOP-aware beat clock freezes the mapper during stops. LR2 has no #SCROLL/#SPEED directives.
* OpenLR2 583a3850 LR2f.cpp L26529-L26540: converted distance += elapsedMilliseconds * 1.2.
* Requires the resolved chart's finite tempo map, including its beat-zero tempo.
*/
export function createLR2ConstantScrollMapper(
tempoPoints: readonly Pick<TempoPoint, 'beat' | 'bpm'>[],
): ScrollDistanceMapper & {
readonly strictlyIncreasing: true;
mirrorBeatAt: (anchorBeat: number, forwardBeat: number) => number;
} {
if (!tempoPoints.some((point) => point.beat === 0)) throw new RangeError('LR2 CONSTANT requires a beat-zero tempo.');
let maxTempoRatio = 1;
const timeline = tempoPoints.map(({ beat, bpm }) => {
const speed = 150 / Math.abs(bpm);
if (!Number.isFinite(beat) || beat < 0 || !Number.isFinite(bpm) || !Number.isFinite(speed) || speed <= 0)
throw new RangeError('LR2 CONSTANT requires finite beats and nonzero BPM values.');
maxTempoRatio = Math.max(maxTempoRatio, Math.abs(bpm) / 150);
return { beat, speed };
});
const initialRate = timeline.filter((point) => point.beat === 0).at(-1)!.speed;
return new LR2ConstantScrollMapper(timeline, initialRate, DEFAULT_SCROLL_LOOKAHEAD_BEATS * maxTempoRatio);
}
/** Source LR2f.cpp L2186-L2188 mirrors converted positions, including positions before chart zero. */
class LR2ConstantScrollMapper extends ScrollDistanceMapper {
readonly strictlyIncreasing = true as const;
private readonly initialRate: number;
constructor(timeline: ScrollTimelinePoint[], initialRate: number, lookaheadBeats: number) {
super(timeline, undefined, { invalidDistance: 0, lookaheadBeats });
this.initialRate = initialRate;
}
override distanceBetween(fromBeat: number, toBeat: number): number {
Iif (!Number.isFinite(fromBeat) || !Number.isFinite(toBeat)) return 0;
return (
super.distanceBetween(Math.max(0, fromBeat), Math.max(0, toBeat)) +
(Math.min(0, toBeat) - Math.min(0, fromBeat)) * this.initialRate
);
}
override firstBeatBeyondDistance(fromBeat: number, distance: number): number {
if (!Number.isFinite(fromBeat) || fromBeat >= 0) return super.firstBeatBeyondDistance(fromBeat, distance);
const limit = Number.isFinite(distance) ? Math.max(0, distance) : 0;
const beforeZero = -fromBeat * this.initialRate;
return limit <= beforeZero
? fromBeat + limit / this.initialRate
: super.firstBeatBeyondDistance(0, limit - beforeZero);
}
override maxBeatWithinDistance(fromBeat: number, distance: number): number {
if (!Number.isFinite(fromBeat) || fromBeat >= 0) return super.maxBeatWithinDistance(fromBeat, distance);
const limit = Number.isFinite(distance) ? Math.max(0, distance) : 0;
const beforeZero = -fromBeat * this.initialRate;
return limit <= beforeZero
? fromBeat + limit / this.initialRate
: super.maxBeatWithinDistance(0, limit - beforeZero);
}
mirrorBeatAt(anchorBeat: number, forwardBeat: number): number {
Iif (!Number.isFinite(anchorBeat) || !Number.isFinite(forwardBeat)) return 0;
const target = 2 * this.distanceBetween(0, anchorBeat) - this.distanceBetween(0, forwardBeat);
return target < 0 ? target / this.initialRate : super.firstBeatBeyondDistance(0, target);
}
}
|