All files / chart-parser/src/core bmson.ts

91.45% Statements 364/398
87.7% Branches 321/366
100% Functions 41/41
92.24% Lines 345/374

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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928                                                                                                                                                                                                                                                  442x               442x                             588x 588x     588x 588x 570x   18x 18x 21x 48x 48x 48x 48x           18x 43x 13x     9x 5x 5x 14x   5x                 150x 150x 150x 150x 1x                 211x 211x 211x 211x 37x           591x               716x 249x   467x 467x       653x 397x     256x 256x 1479x 6x 6x   1473x     1473x 1473x 1473x       256x 256x 68x   188x     188x       650x 397x     253x 253x 26572x     26572x 26572x               26572x     26572x           253x       650x 394x     256x 256x 24670x     24670x 24670x               24670x     24670x           256x               650x 593x     57x 57x 26144x 1x   26143x 26143x           3x   26140x           57x       650x 10x     640x 640x 39147x     39147x 39147x     39147x           640x       588x       588x       1176x 966x     210x 210x 210x     210x 210x     210x           210x             652x 453x     199x 199x 199x 1301x 1301x 1301x 1284x   17x         199x             652x 652x 94731x 94731x 78214x 78214x 78214x           16517x 16517x 16517x 16444x 16444x 16444x 16444x 16444x           73x 73x 73x 73x 73x                       32911x 204x   32707x       591x 591x 591x 591x 591x 591x 591x 591x 591x 591x 591x 591x   591x 591x 13x             591x 591x         591x         591x     591x 591x 72x     591x 591x     591x       591x 454x             137x                     62x                 62x 1x     61x 61x 25x     61x 61x 61x 61x 61x 2x 4x   2x   61x 61x 61x 61x       61x       14x 14x 14x       194x 1x   193x 193x 301x     301x 301x 301x     301x           193x       582x 507x 507x 474x 474x 474x 474x 474x 128x   474x 474x 474x 474x                 507x             94x 94x 94x 130x 129x 129x 129x 128x 128x 334x 333x 333x 333x 332x 127x           205x 205x     128x   94x       652x 638x     14x 14x 30x 30x 30x                   14x                       30x 30x 26x   4x 4x 4x 1x   3x       39147x       39147x 39147x 32259x     32259x 32259x       32259x       32259x 32216x   32259x 4822x   32259x 4770x   32259x 32259x 32259x 130x   32259x   39147x       210x       210x 210x 363x     363x 363x       363x       363x 363x   363x 131x   363x   210x       7172x 1193x         652x 615x                 652x 131x                   652x 505x   147x 147x 1x   146x                     652x 571x   81x       2579x 504x   2075x 2075x 5x   2070x       61x 61x       61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x   61x 61x 1x         61x 61x 61x 61x 61x 61x 2x     61x 61x 27x     61x       61x 4x             57x 57x                         16517x 16517x 16517x   16517x 155226x 155226x 79230x 79230x 79230x   75996x     16517x    
import { readBmsonExtraFields, normalizeBmsonExtraFieldMap } from './bmson-extra-fields.ts';
import type {
  BeMusicJSON,
  BeMusicPosition,
  BmsonBPMEventEntry,
  BmsonKeyChannelEntry,
  BmsonMineChannelEntry,
  BmsonMineNoteEntry,
  BmsonScrollEventEntry,
  BmsonSoundChannelEntry,
  BmsonSoundNoteEntry,
  BmsonStopEventEntry,
} from '@be-music/chart-source';
import { resolveBmsonModeHintLayout } from '@be-music/chart-model';
import { normalizeSortedUniqueNonNegativeIntegers } from '@be-music/shared-utils/core';
 
export interface BmsonInfo {
  title?: string;
  subtitle?: string;
  artist?: string;
  genre?: string;
  subartists?: unknown;
  chart_name?: string;
  level?: number;
  init_bpm?: number;
  resolution?: number;
  mode_hint?: string;
  judge_rank?: number;
  total?: number;
  ln_type?: number;
  back_image?: string;
  eyecatch_image?: string;
  title_image?: string;
  banner_image?: string;
  preview_music?: string;
}
 
interface BmsonLine {
  y: number;
}
 
export interface BmsonBPMEvent {
  y: number;
  bpm: number;
}
 
export interface BmsonStopEvent {
  y: number;
  duration: number;
}
 
/** beatoraja extension — from pulse `y` on, the lane scrolls at `rate` times its normal speed. */
export interface BmsonScrollEvent {
  y: number;
  rate: number;
}
 
export interface BmsonSoundNote {
  x?: number;
  y: number;
  l?: number;
  c?: boolean;
  up?: boolean;
}
 
export interface BmsonSoundChannel {
  name: string;
  notes?: BmsonSoundNote[];
}
 
/**
 * beatoraja's bmson extensions `key_channels` (hidden notes) and `mine_channels` (mines) share one note shape: a lane
 * `x`, a pulse `y` and, read on mines only, a gauge `damage` (0..100). The channel `name` is the sample the note plays
 * — the hidden note's keysound, or the mine's explosion.
 */
export interface BmsonExtensionNote {
  x?: number;
  y: number;
  damage?: number;
}
 
export interface BmsonExtensionChannel {
  name: string;
  notes?: BmsonExtensionNote[];
}
 
export interface BmsonDocument {
  version?: string;
  info?: BmsonInfo;
  lines?: Array<number | BmsonLine>;
  resolution?: number;
  bpm_events?: BmsonBPMEvent[];
  stop_events?: BmsonStopEvent[];
  /** beatoraja extension — scroll speed changes, read as BMS `#SCROLLxx` on channel `SC`. */
  scroll_events?: BmsonScrollEvent[];
  sound_channels?: BmsonSoundChannel[];
  /**
   * beatoraja extension — hidden (invisible) note channels. Each note maps onto a BMS invisible channel (`3x` for 1P,
   * `4x` for 2P) using the same `mode_hint` lane convention `sound_channels` uses for playable lanes.
   */
  key_channels?: BmsonExtensionChannel[];
  /**
   * beatoraja extension — mine channels. Each note maps onto a BMS landmine channel (`Dx` for 1P, `Ex` for 2P) the
   * same way, keeping its `damage`.
   */
  mine_channels?: BmsonExtensionChannel[];
  bga?: {
    bga_sequence?: Array<{ id: number; sequence: Array<{ time: number; id?: number }> }>;
    bga_header?: Array<{ id: number; name: string }>;
    bga_events?: Array<{ y: number; id?: number; id_set?: number[]; condition?: string; interval?: number }>;
    layer_events?: Array<{ y: number; id?: number; id_set?: number[]; condition?: string; interval?: number }>;
    poor_events?: Array<{ y: number; id?: number; id_set?: number[]; condition?: string; interval?: number }>;
  };
}
 
interface MeasurePositionWithFraction {
  measure: number;
  position: BeMusicPosition;
}
 
/** Lane codes of a `<side><lane>` BMS channel, left to right: the classic `1..9` columns, then the extended `A..Z`. */
const BMS_LANE_CODES = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 
/**
 * bmson 1.0.0 spec — when a chart omits / blanks `mode_hint`, the default is `beat-7k` ("Default value is 'beat-7k'").
 * Applied centrally in {@link buildBmsonLaneMap} so the default lane routing follows the spec instead of falling
 * through onto the unknown-hint positional fallback (which used to map x=6/7 onto `16` / `17` because those digits came
 * next in numeric order).
 */
const BMSON_DEFAULT_MODE_HINT = 'beat-7k';
 
/**
 * Resolves the `x` → BMS channel mapping for a chart's playable lanes. Order of resolution:
 *
 * 1. `mode_hint` blank / undefined → spec default `beat-7k` (per bmson 1.0.0).
 * 2. Canonical That-Which-Must-Not-Be-Named / Pop'n / keyboard hint → authoritative table. Unknown hints (BmsONE's single-player
 *    `keyboard-48k` among them, which is NOT the two-player `keyboard-24k-double`) take step 4.
 * 3. `generic-Nkeys` template → left-to-right positional map sized to N keys.
 * 4. Anything else is an unknown hint. When every `x` in use fits the `beat-7k` table, it is read as `beat-7k`, as
 *    beatoraja (with a warning), Qwilight and Bemuse read it. A chart with lanes past that table (BmsONE's
 *    `keyboard-36k` / `48k` / `60k`) would lose them to BGM there, so it keeps the positional map over its `x` values
 *    instead (Be-Music decision, docs/format-compat.md §3.2).
 */
export function buildBmsonLaneMap(soundChannels: BmsonSoundChannel[], modeHint?: string): Map<number, string> {
  const trimmedHint = typeof modeHint === 'string' ? modeHint.trim() : '';
  const effectiveHint = trimmedHint.length > 0 ? trimmedHint : BMSON_DEFAULT_MODE_HINT;
  // The fixed lane tables (the bmson spec's hints and beatoraja's keyboard hints) come from the play-mode table in
  // `@be-music/chart-model`, which the stringifier writes `x` from too.
  const hinted = resolveBmsonModeHintLayout(effectiveHint);
  if (hinted) {
    return new Map(hinted.channelByX);
  }
  const xValues = new Set<number>();
  for (const soundChannel of soundChannels) {
    for (const note of soundChannel.notes ?? []) {
      Eif (Number.isFinite(note.x)) {
        const lane = Math.floor(note.x!);
        Eif (lane > 0) {
          xValues.add(lane);
        }
      }
    }
  }
 
  const beat7k = resolveBmsonModeHintLayout(BMSON_DEFAULT_MODE_HINT)!;
  if ([...xValues].every((x) => beat7k.channelByX.has(x))) {
    return new Map(beat7k.channelByX);
  }
 
  const sorted = [...xValues].sort((left, right) => left - right);
  const map = new Map<number, string>();
  for (let index = 0; index < sorted.length; index += 1) {
    map.set(sorted[index], laneIndexToChannel(index));
  }
  return map;
}
 
/**
 * Converts a BMS playable lane channel into its matching BMS landmine channel — `Dx` for 1P (`1X` keys) and `Ex` for 2P
 * (`2X` keys). Used to route bmson `mine_channels` notes through the same lane resolver as playable notes while keeping
 * them tagged as mines downstream.
 */
export function bmsPlayableChannelToLandmineChannel(channel: string): string | undefined {
  Iif (channel.length !== 2) return undefined;
  const side = channel[0];
  const lane = channel[1];
  if (side === '1') return `D${lane}`;
  Eif (side === '2') return `E${lane}`;
  return undefined;
}
 
/**
 * Converts a BMS playable lane channel into its matching BMS invisible channel — `3x` for 1P (`1X` keys) and `4x` for
 * 2P (`2X` keys). Used to route bmson `key_channels` (hidden notes) through the same lane resolver as playable notes.
 */
export function bmsPlayableChannelToInvisibleChannel(channel: string): string | undefined {
  Iif (channel.length !== 2) return undefined;
  const side = channel[0];
  const lane = channel[1];
  if (side === '1') return `3${lane}`;
  Eif (side === '2') return `4${lane}`;
  return undefined;
}
 
export function resolveBmsonResolution(document: BmsonDocument): number {
  // `info.resolution`, else the root-level `resolution` older writers emit, else the spec default 240.
  return normalizeBmsonResolution(document.info?.resolution) ?? normalizeBmsonResolution(document.resolution) ?? 240;
}
 
/**
 * Per the bmson 1.0.0 spec, 0 / `null` / missing means the default (`undefined` here) and a negative resolution is read
 * as its absolute value (beatoraja uses 240 for any value <= 0 instead).
 */
function normalizeBmsonResolution(value: unknown): number | undefined {
  if (typeof value !== 'number' || !Number.isFinite(value)) {
    return undefined;
  }
  const resolution = Math.floor(Math.abs(value));
  return resolution > 0 ? resolution : undefined;
}
 
export function normalizeBmsonLines(lines: unknown): number[] {
  if (!Array.isArray(lines)) {
    return [];
  }
 
  const values: number[] = [];
  for (const line of lines) {
    if (typeof line === 'number' && Number.isFinite(line)) {
      values.push(Math.max(0, Math.floor(line)));
      continue;
    }
    Iif (!line || typeof line !== 'object') {
      continue;
    }
    const y = (line as Record<string, unknown>).y;
    Eif (typeof y === 'number' && Number.isFinite(y)) {
      values.push(Math.max(0, Math.floor(y)));
    }
  }
 
  const sorted = normalizeSortedUniqueNonNegativeIntegers(values);
  if (sorted.length === 0) {
    return [];
  }
  Iif (sorted[0] !== 0) {
    sorted.unshift(0);
  }
  return sorted;
}
 
export function normalizeBmsonBPMEvents(input: unknown, fromIR = false): BmsonBPMEventEntry[] {
  if (!Array.isArray(input)) {
    return [];
  }
 
  const events: BmsonBPMEventEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (
      typeof raw.y !== 'number' ||
      !Number.isFinite(raw.y) ||
      typeof raw.bpm !== 'number' ||
      !Number.isFinite(raw.bpm)
    ) {
      continue;
    }
    Iif (raw.bpm <= 0) {
      continue;
    }
    events.push({
      y: Math.max(0, Math.round(raw.y)),
      bpm: raw.bpm,
      ...readBmsonExtraFields(raw, 'bpm', fromIR),
    });
  }
  return events;
}
 
export function normalizeBmsonStopEvents(input: unknown, fromIR = false): BmsonStopEventEntry[] {
  if (!Array.isArray(input)) {
    return [];
  }
 
  const events: BmsonStopEventEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (
      typeof raw.y !== 'number' ||
      !Number.isFinite(raw.y) ||
      typeof raw.duration !== 'number' ||
      !Number.isFinite(raw.duration)
    ) {
      continue;
    }
    Iif (raw.duration <= 0) {
      continue;
    }
    events.push({
      y: Math.max(0, Math.round(raw.y)),
      duration: raw.duration,
      ...readBmsonExtraFields(raw, 'stop', fromIR),
    });
  }
  return events;
}
 
/**
 * beatoraja extension `scroll_events`. jbms-parser accepts any `rate` (a zero rate stops the lane, a negative one
 * reverses it), so only entries without a finite `y` or `rate` are dropped.
 */
export function normalizeBmsonScrollEvents(input: unknown, fromIR = false): BmsonScrollEventEntry[] {
  if (!Array.isArray(input)) {
    return [];
  }
 
  const events: BmsonScrollEventEntry[] = [];
  for (const item of input) {
    if (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    if (
      typeof raw.y !== 'number' ||
      !Number.isFinite(raw.y) ||
      typeof raw.rate !== 'number' ||
      !Number.isFinite(raw.rate)
    ) {
      continue;
    }
    events.push({
      y: Math.max(0, Math.round(raw.y)),
      rate: raw.rate,
      ...readBmsonExtraFields(raw, 'scroll', fromIR),
    });
  }
  return events;
}
 
export function normalizeBmsonSoundChannels(input: unknown, fromIR = false): BmsonSoundChannelEntry[] {
  if (!Array.isArray(input)) {
    return [];
  }
 
  const channels: BmsonSoundChannelEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (typeof raw.name !== 'string') {
      continue;
    }
    channels.push({
      name: raw.name,
      notes: normalizeBmsonSoundNotes(raw.notes, fromIR),
      ...readBmsonExtraFields(raw, 'channel', fromIR),
    });
  }
  return channels;
}
 
export function normalizeBmsonKeyChannels(input: unknown): BmsonKeyChannelEntry[] {
  return normalizeBmsonExtensionChannels(input, false);
}
 
export function normalizeBmsonMineChannels(input: unknown): BmsonMineChannelEntry[] {
  return normalizeBmsonExtensionChannels(input, true);
}
 
function normalizeBmsonExtensionChannels(input: unknown, keepDamage: boolean): BmsonMineChannelEntry[] {
  if (!Array.isArray(input)) {
    return [];
  }
 
  const channels: BmsonMineChannelEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (typeof raw.name !== 'string') {
      continue;
    }
    channels.push({
      name: raw.name,
      notes: normalizeBmsonExtensionNotes(raw.notes, keepDamage),
      ...readBmsonExtraFields(raw, 'channel'),
    });
  }
  return channels;
}
 
export function createMeasureLengthsFromBmsonLines(
  lines: number[],
  resolution: number,
): Array<{ index: number; length: number }> {
  if (lines.length < 2) {
    return [];
  }
 
  const ticksPerMeasure = Math.max(1, Math.floor(resolution * 4));
  const measures: Array<{ index: number; length: number }> = [];
  for (let index = 0; index + 1 < lines.length; index += 1) {
    const ticks = Math.max(1, lines[index + 1] - lines[index]);
    const length = ticks / ticksPerMeasure;
    if (Math.abs(length - 1) < 1e-9) {
      continue;
    }
    measures.push({
      index,
      length,
    });
  }
  return measures;
}
 
export function createBmsonPositionResolver(
  resolution: number,
  lines: number[],
): (y: number) => MeasurePositionWithFraction {
  const ticksPerMeasure = Math.max(1, Math.floor(resolution * 4));
  return (y: number) => {
    const normalizedY = Math.max(0, Math.round(y));
    if (lines.length < 2) {
      const measure = Math.max(0, Math.floor(normalizedY / ticksPerMeasure));
      const positionNumerator = normalizedY % ticksPerMeasure;
      return {
        measure,
        position: [positionNumerator, ticksPerMeasure],
      };
    }
 
    const lineIndex = findLastLineIndex(lines, normalizedY);
    const lastIndex = lines.length - 1;
    if (lineIndex < lastIndex) {
      const start = lines[lineIndex];
      const end = lines[lineIndex + 1];
      const denominator = Math.max(1, end - start);
      const numerator = Math.max(0, Math.min(denominator - 1, normalizedY - start));
      return {
        measure: lineIndex,
        position: [numerator, denominator],
      };
    }
 
    const start = lines[lastIndex];
    const offset = normalizedY - start;
    const measureOffset = Math.floor(offset / ticksPerMeasure);
    const numerator = offset % ticksPerMeasure;
    return {
      measure: lastIndex + measureOffset,
      position: [numerator, ticksPerMeasure],
    };
  };
}
 
/**
 * beatoraja bmson extension — long-note type values are `1` (LN), `2` (CN), `3` (HCN). Anything else (including the
 * spec-absent `0`) is treated as "unspecified" so the consumer falls back to the chart-level / player default.
 */
function normalizeBmsonLongNoteType(value: unknown): 1 | 2 | 3 | undefined {
  if (value === 1 || value === 2 || value === 3) {
    return value;
  }
  return undefined;
}
 
export function normalizeBmsonInfoForIR(info: BmsonInfo, resolution: number): BeMusicJSON['bmson']['info'] {
  const normalized: BeMusicJSON['bmson']['info'] = { ...readBmsonExtraFields(info, 'info') };
  copyIfString(normalized, 'title', info.title);
  copyIfString(normalized, 'subtitle', info.subtitle);
  copyIfString(normalized, 'artist', info.artist);
  copyIfString(normalized, 'genre', info.genre);
  copyIfString(normalized, 'chartName', info.chart_name);
  copyIfString(normalized, 'modeHint', info.mode_hint);
  copyIfString(normalized, 'backImage', info.back_image);
  copyIfString(normalized, 'eyecatchImage', info.eyecatch_image);
  copyIfString(normalized, 'titleImage', info.title_image);
  copyIfString(normalized, 'bannerImage', info.banner_image);
  copyIfString(normalized, 'previewMusic', info.preview_music);
 
  const subartists = normalizeBmsonSubartists(info.subartists);
  if (subartists !== undefined) {
    normalized.subartists = subartists;
  }
 
  // bmson 1.0.0 spec — `level` is an `unsigned long` and "must be ≥ 0. Negative values may be regarded as invalid by a
  // player." We treat the negative case as "invalid → drop the field" rather than silently coerce, so the missing-level
  // fallback (`undefined` → no displayed level) is what the chart's level shows in the UI. Non-integer inputs are
  // floored to honor the spec's `unsigned long` type.
  copyIfNonNegativeInteger(normalized, 'level', info.level);
  copyIfFiniteNumber(normalized, 'initBPM', info.init_bpm);
  // bmson 1.0.0 spec — `judge_rank` describes "the width of judgment window" with the spec text only making sense for
  // positive values ("smaller than 100" / "larger than 100" are framed relative to the player's default; 0 / negative
  // would mean a zero-or-inverted window). Drop the field for non-positive inputs so the consumer falls back to the
  // spec default of 100 rather than producing a nonsensical judge window.
  copyIfPositiveFiniteNumber(normalized, 'judgeRank', info.judge_rank);
  // bmson 1.0.0 spec — `total` "must be ≥ 0. If negative, take the absolute value." Normalizing at parse time keeps the
  // gauge formula (`+TOTAL/N`) on the positive branch and means every downstream consumer (gauge, stringifier,
  // round-trip re-parse) sees the canonical value. `total: 0` is kept as written; the spec says the lifebar then doesn't
  // increase, but the player's rulesets follow beatoraja and treat `total <= 0` as 100 % of their default TOTAL.
  copyIfFiniteNumberAbs(normalized, 'total', info.total);
  // beatoraja bmson extension — `info.ln_type` picks the chart-wide long-note type (1: LN, 2: CN, 3: HCN). Per-note
  // `t` entries override it downstream.
  const lnType = normalizeBmsonLongNoteType(info.ln_type);
  if (lnType !== undefined) {
    normalized.lnType = lnType;
  }
 
  Eif (resolution > 0) {
    normalized.resolution = resolution;
  }
 
  return normalized;
}
 
export function normalizeBmsonBGAForIR(input: BmsonDocument['bga'] | undefined): BeMusicJSON['bmson']['bga'] {
  if (!input || typeof input !== 'object') {
    return {
      header: [],
      events: [],
      layerEvents: [],
      poorEvents: [],
    };
  }
  return {
    ...readBmsonExtraFields(input, 'bga'),
    ...(input.bga_sequence !== undefined ? { sequences: normalizeBmsonBGASequences(input.bga_sequence) } : {}),
    header: normalizeBmsonBGAHeaderEntries(input.bga_header),
    events: normalizeBmsonBGAEventEntries(input.bga_events),
    layerEvents: normalizeBmsonBGAEventEntries(input.layer_events),
    poorEvents: normalizeBmsonBGAEventEntries(input.poor_events),
  };
}
 
export function normalizeBmsonExtensions(input: unknown): BeMusicJSON['bmson'] {
  const normalized: BeMusicJSON['bmson'] = {
    info: {},
    bga: {
      header: [],
      events: [],
      layerEvents: [],
      poorEvents: [],
    },
  };
  if (!input || typeof input !== 'object') {
    return normalized;
  }
 
  const raw = input as Record<string, unknown>;
  if (typeof raw.version === 'string' && raw.version.length > 0) {
    normalized.version = raw.version;
  }
 
  Object.assign(normalized, readBmsonExtraFields(raw, 'root', true));
  const channelExtraFields = normalizeBmsonExtraFieldMap(raw.channelExtraFields);
  const lineExtraFields = normalizeBmsonExtraFieldMap(raw.lineExtraFields);
  if (channelExtraFields) normalized.channelExtraFields = channelExtraFields;
  if (raw.channelKinds && typeof raw.channelKinds === 'object') {
    const entries = Object.entries(raw.channelKinds).filter((entry): entry is [string, 'sound' | 'key' | 'mine'] =>
      ['sound', 'key', 'mine'].includes(entry[1] as string),
    );
    Eif (entries.length) normalized.channelKinds = Object.fromEntries(entries);
  }
  if (lineExtraFields) normalized.lineExtraFields = lineExtraFields;
  normalized.info = normalizeBmsonInfoFromIR(raw.info);
  normalized.bga = normalizeBmsonBGAFromIR(raw.bga);
  Iif (raw.barlinesSuppressed === true) {
    normalized.barlinesSuppressed = true;
  }
 
  return normalized;
}
 
function laneIndexToChannel(index: number): string {
  const first = BMS_LANE_CODES[Math.floor(index / BMS_LANE_CODES.length)] ?? '1';
  const second = BMS_LANE_CODES[index % BMS_LANE_CODES.length];
  return `${first}${second}`;
}
 
function normalizeBmsonBGAHeaderEntries(input: unknown, fromIR = false): BeMusicJSON['bmson']['bga']['header'] {
  if (!Array.isArray(input)) {
    return [];
  }
  const entries: BeMusicJSON['bmson']['bga']['header'] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    const id = normalizePositiveInteger(raw.id);
    Iif (id === undefined || typeof raw.name !== 'string') {
      continue;
    }
    entries.push({
      id,
      name: raw.name,
      ...readBmsonExtraFields(raw, 'bgaHeader', fromIR),
    });
  }
  return entries;
}
 
function normalizeBmsonBGAEventEntries(input: unknown, fromIR = false): BeMusicJSON['bmson']['bga']['events'] {
  if (!Array.isArray(input)) return [];
  const entries: BeMusicJSON['bmson']['bga']['events'] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') continue;
    const raw = item as Record<string, unknown>;
    const id = normalizePositiveInteger(raw.id);
    const rawIdSet = raw.idSet ?? raw.id_set;
    const idSet = Array.isArray(rawIdSet)
      ? rawIdSet.map(normalizePositiveInteger).filter((value): value is number => value !== undefined)
      : undefined;
    const y = normalizePositiveInteger(raw.y);
    Iif (y === undefined || (id === undefined && idSet === undefined)) continue;
    const interval = normalizePositiveInteger(raw.interval);
    entries.push({
      y,
      ...(id !== undefined ? { id } : {}),
      ...readBmsonExtraFields(raw, 'bgaEvent', fromIR),
      ...(idSet !== undefined ? { idSet } : {}),
      ...(typeof raw.condition === 'string' ? { condition: raw.condition } : {}),
      ...(interval !== undefined ? { interval } : {}),
    });
  }
  return entries;
}
 
function normalizeBmsonBGASequences(
  input: unknown,
  fromIR = false,
): NonNullable<BeMusicJSON['bmson']['bga']['sequences']> {
  Iif (!Array.isArray(input)) return [];
  const entries: NonNullable<BeMusicJSON['bmson']['bga']['sequences']> = [];
  for (const item of input) {
    if (!item || typeof item !== 'object') continue;
    const raw = item as Record<string, unknown>;
    const id = normalizePositiveInteger(raw.id);
    if (id === undefined || !Array.isArray(raw.sequence)) continue;
    const sequence: (typeof entries)[number]['sequence'] = [];
    for (const item of raw.sequence) {
      if (!item || typeof item !== 'object') continue;
      const frame = item as Record<string, unknown>;
      const time = normalizePositiveInteger(frame.time);
      if (time === undefined) continue;
      if (frame.id === undefined || frame.id === -2147483648) {
        sequence.push({
          time,
          ...readBmsonExtraFields(frame, 'bgaFrame', fromIR),
          ...(frame.id === -2147483648 ? { id: -2147483648 } : {}),
        });
      } else {
        const id = normalizePositiveInteger(frame.id);
        if (id !== undefined) sequence.push({ time, id, ...readBmsonExtraFields(frame, 'bgaFrame', fromIR) });
      }
    }
    entries.push({ id, sequence, ...readBmsonExtraFields(raw, 'bgaSequence', fromIR) });
  }
  return entries;
}
 
function normalizeBmsonSubartists(input: unknown): string[] | undefined {
  if (!Array.isArray(input)) {
    return undefined;
  }
 
  const names: string[] = [];
  for (const item of input) {
    Eif (typeof item === 'string') {
      names.push(canonicalizeSubartistEntry(item));
      continue;
    }
    if (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    if (typeof raw.name === 'string') {
      names.push(canonicalizeSubartistEntry(raw.name));
    }
  }
  return names;
}
 
/**
 * bmson 1.0.0 spec SHOULD — "Implementers should trim the spaces before and after `key` and `value` in subartists."
 *
 * Trims each half of the `role:name` form (or just the name when the entry doesn't carry a role) and rejoins so the
 * stored array contains canonicalized strings. Role case is preserved — the consumer-facing helper {@link
 * parseBmsonSubartist} is the one that lower-cases for grouping; we don't want to lose the author's intended
 * capitalization in the IR.
 */
function canonicalizeSubartistEntry(entry: string): string {
  const colon = entry.indexOf(':');
  if (colon < 0) {
    return entry.trim();
  }
  const role = entry.slice(0, colon).trim();
  const name = entry.slice(colon + 1).trim();
  if (role.length === 0) {
    return name;
  }
  return `${role}:${name}`;
}
 
function normalizeBmsonSoundNotes(input: unknown, fromIR = false): BmsonSoundNoteEntry[] {
  Iif (!Array.isArray(input)) {
    return [];
  }
 
  const notes: BmsonSoundNoteEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (typeof raw.y !== 'number' || !Number.isFinite(raw.y)) {
      continue;
    }
 
    const note: BmsonSoundNoteEntry = {
      ...readBmsonExtraFields(raw, 'note', fromIR),
      y: Math.max(0, Math.round(raw.y)),
    };
    if (typeof raw.x === 'number' && Number.isFinite(raw.x)) {
      note.x = Math.floor(raw.x);
    }
    if (typeof raw.l === 'number' && Number.isFinite(raw.l) && raw.l >= 0) {
      note.l = Math.floor(raw.l);
    }
    if (typeof raw.c === 'boolean') {
      note.c = raw.c;
    }
    if (typeof raw.up === 'boolean') note.up = raw.up;
    const noteType = normalizeBmsonLongNoteType(raw.t);
    if (noteType !== undefined) {
      note.t = noteType;
    }
    notes.push(note);
  }
  return notes;
}
 
function normalizeBmsonExtensionNotes(input: unknown, keepDamage: boolean): BmsonMineNoteEntry[] {
  Iif (!Array.isArray(input)) {
    return [];
  }
 
  const notes: BmsonMineNoteEntry[] = [];
  for (const item of input) {
    Iif (!item || typeof item !== 'object') {
      continue;
    }
    const raw = item as Record<string, unknown>;
    Iif (typeof raw.y !== 'number' || !Number.isFinite(raw.y)) {
      continue;
    }
 
    const note: BmsonMineNoteEntry = {
      ...readBmsonExtraFields(raw, keepDamage ? 'mineNote' : 'keyNote'),
      y: Math.max(0, Math.round(raw.y)),
    };
    Eif (typeof raw.x === 'number' && Number.isFinite(raw.x)) {
      note.x = Math.floor(raw.x);
    }
    if (keepDamage && typeof raw.damage === 'number' && Number.isFinite(raw.damage) && raw.damage >= 0) {
      note.damage = raw.damage;
    }
    notes.push(note);
  }
  return notes;
}
 
function copyIfString<T extends object>(target: T, key: keyof T & string, value: unknown): void {
  if (typeof value === 'string') {
    (target as Record<string, unknown>)[key] = value;
  }
}
 
function copyIfFiniteNumber<T extends object>(target: T, key: keyof T & string, value: unknown): void {
  if (typeof value === 'number' && Number.isFinite(value)) {
    (target as Record<string, unknown>)[key] = value;
  }
}
 
/**
 * Same as {@link copyIfFiniteNumber} but applies `Math.abs` so spec fields that say "if negative, take the absolute
 * value" (notably `info.total`) get normalized at parse time. Keeps `0` intact so consumers see the value as written.
 */
function copyIfFiniteNumberAbs<T extends object>(target: T, key: keyof T & string, value: unknown): void {
  if (typeof value === 'number' && Number.isFinite(value)) {
    (target as Record<string, unknown>)[key] = Math.abs(value);
  }
}
 
/**
 * Copies a finite numeric value as a non-negative integer, dropping the field entirely when the input is negative or
 * non-finite. Matches the bmson 1.0.0 spec's `unsigned long` fields (notably `info.level`) which require `≥ 0` and the
 * spec's MAY clause that lets implementations regard negatives as invalid. Non-integer inputs are floored.
 */
function copyIfNonNegativeInteger<T extends object>(target: T, key: keyof T & string, value: unknown): void {
  if (typeof value !== 'number' || !Number.isFinite(value)) {
    return;
  }
  const floored = Math.floor(value);
  if (floored < 0) {
    return;
  }
  (target as Record<string, unknown>)[key] = floored;
}
 
/**
 * Copies a finite numeric value only when it is strictly positive (`> 0`). Matches bmson 1.0.0 spec fields whose
 * semantics only make sense for positive values — notably `info.judge_rank`, where the spec text ("smaller than 100" /
 * "larger than 100") frames the value as a width relative to the player's default. A `0` or negative input would imply
 * a zero-width or inverted judgement window, both nonsensical; dropping the field lets the consumer fall back to the
 * spec default of 100.
 */
function copyIfPositiveFiniteNumber<T extends object>(target: T, key: keyof T & string, value: unknown): void {
  if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
    return;
  }
  (target as Record<string, unknown>)[key] = value;
}
 
function normalizePositiveInteger(value: unknown): number | undefined {
  if (typeof value !== 'number' || !Number.isFinite(value)) {
    return undefined;
  }
  const normalized = Math.floor(value);
  if (normalized < 0) {
    return undefined;
  }
  return normalized;
}
 
function normalizeBmsonInfoFromIR(input: unknown): BeMusicJSON['bmson']['info'] {
  const info: BeMusicJSON['bmson']['info'] = {};
  Iif (!input || typeof input !== 'object') {
    return info;
  }
 
  const raw = input as Record<string, unknown>;
  Object.assign(info, readBmsonExtraFields(raw, 'info', true));
  if (raw.totalUnit === 'absolute' || raw.totalUnit === 'percent') info.totalUnit = raw.totalUnit;
  copyIfString(info, 'title', raw.title);
  copyIfString(info, 'subtitle', raw.subtitle);
  copyIfString(info, 'artist', raw.artist);
  copyIfString(info, 'genre', raw.genre);
  copyIfString(info, 'chartName', raw.chartName ?? raw.chart_name);
  copyIfString(info, 'modeHint', raw.modeHint ?? raw.mode_hint);
  copyIfString(info, 'backImage', raw.backImage ?? raw.back_image);
  copyIfString(info, 'eyecatchImage', raw.eyecatchImage ?? raw.eyecatch_image);
  copyIfString(info, 'titleImage', raw.titleImage ?? raw.title_image);
  copyIfString(info, 'bannerImage', raw.bannerImage ?? raw.banner_image);
  copyIfString(info, 'previewMusic', raw.previewMusic ?? raw.preview_music);
 
  const subartists = normalizeBmsonSubartists(raw.subartists);
  if (subartists !== undefined) {
    info.subartists = subartists;
  }
 
  // Match `normalizeBmsonInfoForIR`'s spec-compliant unsigned- integer + positive + abs handling for the re-parse path
  // so JSON round-trips don't reintroduce a negative / float level / judge_rank / total.
  copyIfNonNegativeInteger(info, 'level', raw.level);
  copyIfFiniteNumber(info, 'initBPM', raw.initBPM ?? raw.initBpm ?? raw.init_bpm);
  copyIfPositiveFiniteNumber(info, 'judgeRank', raw.judgeRank ?? raw.judge_rank);
  copyIfFiniteNumberAbs(info, 'total', raw.total);
  const lnType = normalizeBmsonLongNoteType(raw.lnType ?? raw.ln_type);
  if (lnType !== undefined) {
    info.lnType = lnType;
  }
 
  const resolution = normalizePositiveInteger(raw.resolution);
  if (resolution !== undefined && resolution > 0) {
    info.resolution = resolution;
  }
 
  return info;
}
 
function normalizeBmsonBGAFromIR(input: unknown): BeMusicJSON['bmson']['bga'] {
  if (!input || typeof input !== 'object') {
    return {
      header: [],
      events: [],
      layerEvents: [],
      poorEvents: [],
    };
  }
  const raw = input as Record<string, unknown>;
  return {
    ...readBmsonExtraFields(raw, 'bga', true),
    ...((raw.sequences ?? raw.bga_sequence) !== undefined
      ? { sequences: normalizeBmsonBGASequences(raw.sequences ?? raw.bga_sequence, true) }
      : {}),
    header: normalizeBmsonBGAHeaderEntries(raw.header ?? raw.bga_header, true),
    events: normalizeBmsonBGAEventEntries(raw.events ?? raw.bga_events, true),
    layerEvents: normalizeBmsonBGAEventEntries(raw.layerEvents ?? raw.layer_events, true),
    poorEvents: normalizeBmsonBGAEventEntries(raw.poorEvents ?? raw.poor_events, true),
  };
}
 
function findLastLineIndex(lines: number[], y: number): number {
  let low = 0;
  let high = lines.length - 1;
  let answer = 0;
 
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    if (lines[mid] <= y) {
      answer = mid;
      low = mid + 1;
      continue;
    }
    high = mid - 1;
  }
 
  return answer;
}