All files / chart-writer/src bga-export.ts

96.42% Statements 108/112
92.3% Branches 72/78
94.11% Functions 16/17
97.87% Lines 92/94

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                                                                                                                24x 24x 24x 24x   24x                           209x 209x 209x 209x 209x 16136x 16136x 16128x 16125x 16122x 16121x   209x 209x 209x 209x   8x 8x 8x 25x 25x 12x 12x   25x   11x 8x 8x 23x       8x 209x   8x 1x       8x 8x 1x         8x 3x       8x         8x                                     41x 41x 38x   3x 3x 3x 5x 5x   5x 5x 5x   3x 3x 3x 4x 4x 4x 4x     7x     3x 41x 41x 41x 41x 41x 9x   9x       9x 2x 2x   7x     41x 41x 41x 41x 1x       3x 2x       3x       3x 3x                   3x 29x 3x 7x 7x 8x 7x 7x      
import { createBeatResolver, resolvePoorBGAMode } from '@be-music/chart-model';
import {
  intToBase36,
  normalizeChannel,
  normalizeObjectKey,
  resolveBMSBase,
  type BeMusicEvent,
  type BeMusicJSON,
} from '@be-music/chart-source';
 
/**
 * Something a format conversion could not carry over exactly; the output still loads, without that part or with it
 * changed (#1198). A caller that must not convert with such a loss can throw from the handler.
 *
 * - Judging changes: `mine-damage` (the gauge a mine drains), `total` (the gauge's TOTAL), `long-note-mode` (how a long
 *   note is judged), `stp` (BMS `#STP` stops, which move every later note), `exrank` (BMS `#EXRANKxx` changes,
 *   which the LR2 ruleset judges by), `bpm` (BMS negative or undefined `#BPMxx`, LR2's reverse scroll) and `stop` (BMS
 *   STOPs without a positive `#STOPxx`, which move every later note 1/192 of a measure earlier).
 * - Presentation changes: `speed` (BMS `SPEED` scroll changes), `sound-continuation` (bmson `c`, a note that continues
 *   its sound slice), `ln-end-sound` (beatoraja's `up`, a long note's end keysound) and the `bga-*` codes.
 */
export interface ChartExportDiagnostic {
  code:
    | 'bga-layer2'
    | 'bga-extended'
    | 'bga-layer-black-key'
    | 'bga-poor-mode'
    | 'bga-sequence'
    | 'mine-damage'
    | 'total'
    | 'long-note-mode'
    | 'stp'
    | 'exrank'
    | 'bpm'
    | 'stop'
    | 'speed'
    | 'sound-continuation'
    | 'ln-end-sound';
  message: string;
}
 
export type ChartExportDiagnosticHandler = (diagnostic: ChartExportDiagnostic) => void;
 
interface BmsonBGAEventOutput {
  y: number;
  id: number;
}
 
/** Ordinary BMS BGA rewritten as native bmson header and event lists. */
export interface BmsBGAForBmson {
  header: Array<{ id: number; name: string }>;
  events: BmsonBGAEventOutput[];
  layerEvents: BmsonBGAEventOutput[];
  poorEvents: BmsonBGAEventOutput[];
}
 
const BMS_BASE_CHANNEL = '04';
const BMS_POOR_CHANNEL = '06';
const BMS_LAYER_CHANNEL = '07';
const BMS_LAYER2_CHANNEL = '0A';
/** Opacity (`0B`–`0E`), ARGB (`A1`–`A4`) and key-bound SWBGA (`A5`) controls. */
const BMS_EXTENDED_BGA_CHANNELS: ReadonlySet<string> = new Set(['0B', '0C', '0D', '0E', 'A1', 'A2', 'A3', 'A4', 'A5']);
 
/**
 * BMS image resources and `04` / `07` / `06` cues as bmson BGA, at `resolution` pulses per beat. Ids start after
 * `firstId - 1`, so they never collide with native bmson ids, and never use 0, which bmson reads as a clear. A cue to an
 * undefined image keeps an id without a header entry, which shows nothing as in BMS. `#BMP00` becomes the initial POOR
 * image, as Be-Music shows it until the first authored POOR cue. Parts native bmson cannot hold are reported.
 */
export function convertBmsBGAToBmson(
  json: BeMusicJSON,
  resolution: number,
  firstId: number,
  report: ChartExportDiagnosticHandler | undefined,
): BmsBGAForBmson | undefined {
  const base = resolveBMSBase(json);
  const cues = { base: [] as BeMusicEvent[], layer: [] as BeMusicEvent[], poor: [] as BeMusicEvent[] };
  let layer2Cues = 0;
  let extendedControls = 0;
  for (const event of json.events) {
    const channel = normalizeChannel(event.channel);
    if (channel === BMS_BASE_CHANNEL) cues.base.push(event);
    else if (channel === BMS_LAYER_CHANNEL) cues.layer.push(event);
    else if (channel === BMS_POOR_CHANNEL) cues.poor.push(event);
    else if (channel === BMS_LAYER2_CHANNEL) layer2Cues += 1;
    else if (BMS_EXTENDED_BGA_CHANNELS.has(channel)) extendedControls += 1;
  }
  const images = Object.entries(json.resources.bmp).filter(([, name]) => typeof name === 'string' && name.length > 0);
  const poorMode = resolvePoorBGAMode(json.bms.poorBGA);
  const fallbackPoor = poorMode !== 2 && images.some(([key]) => key === '00');
  if (cues.base.length + cues.layer.length + cues.poor.length === 0 && images.length === 0) return undefined;
 
  const ids = new Map<string, number>();
  let nextId = Math.max(1, firstId);
  const idOf = (key: string): number => {
    let id = ids.get(key);
    if (id === undefined) {
      id = nextId++;
      ids.set(key, id);
    }
    return id;
  };
  const header = images.map(([key, name]) => ({ id: idOf(key), name }));
  const beatResolver = createBeatResolver(json);
  const toEvents = (events: BeMusicEvent[]): BmsonBGAEventOutput[] =>
    events.map((event) => ({
      y: Math.max(0, Math.round(beatResolver.eventToBeat(event) * resolution)),
      id: idOf(normalizeObjectKey(event.value, base)),
    }));
  const poorEvents = poorMode === 2 ? [] : toEvents(cues.poor);
  if (fallbackPoor && !poorEvents.some((event) => event.y === 0)) poorEvents.unshift({ y: 0, id: idOf('00') });
 
  if (layer2Cues > 0)
    report?.({
      code: 'bga-layer2',
      message: `${layer2Cues} BMS layer2 (0A) cue(s) dropped: native bmson has a single layer plane.`,
    });
  const transformed = Object.keys(json.bms.bga).length + Object.keys(json.bms.exBMP).length;
  if (extendedControls > 0 || transformed > 0 || Object.keys(json.bms.swBGA).length > 0)
    report?.({
      code: 'bga-extended',
      message:
        'BMS BGA extensions (#BGAxx crops, #EXBMPxx keys, #ARGBxx / #SWBGAxx, channels 0B–0E / A1–A5) have no native bmson form and were not converted.',
    });
  if (cues.layer.length > 0)
    report?.({
      code: 'bga-layer-black-key',
      message: 'BMS layer black is transparent; a bmson layer draws black, so layer images may need an alpha channel.',
    });
  Iif (poorMode === 1 && poorEvents.length > 0)
    report?.({
      code: 'bga-poor-mode',
      message: '#POORBGA 1 overlays the POOR image; bmson replaces the BGA with it on a miss.',
    });
  return { header, events: toEvents(cues.base), layerEvents: toEvents(cues.layer), poorEvents };
}
 
/** Native bmson BGA rewritten as BMS image resources and `04` / `07` / `06` objects. */
export interface BmsonBGAForBMS {
  bmp: Record<string, string>;
  events: BeMusicEvent[];
}
 
/**
 * The ordinary bmson BGA of `json` as BMS: each header image gets the next unused two-character key, and each base,
 * layer and POOR event an object at its pulse. A clear (id 0) or an id without a header points at one undefined key,
 * which shows nothing, as in bmson. Sequences, `id_set`, conditions and intervals have no BMS form and are reported and
 * skipped.
 */
export function convertBmsonBGAToBMS(
  json: BeMusicJSON,
  report: ChartExportDiagnosticHandler | undefined,
): BmsonBGAForBMS | undefined {
  const bga = json.bmson.bga;
  if (bga.header.length === 0 && bga.events.length + bga.layerEvents.length + bga.poorEvents.length === 0) {
    return undefined;
  }
  const used = new Set([...Object.keys(json.resources.bmp), '00']);
  let next = 1;
  const allocate = (): string => {
    while (used.has(intToBase36(next, 2))) next += 1;
    Iif (next > 36 * 36 - 1)
      throw new RangeError('Cannot export bmson BGA: it needs more than 1,295 two-character BMS image ids.');
    const key = intToBase36(next, 2);
    used.add(key);
    return key;
  };
  const bmp: Record<string, string> = {};
  const keyById = new Map<number, string>();
  for (const entry of bga.header) {
    Iif (keyById.has(entry.id)) continue;
    const key = allocate();
    keyById.set(entry.id, key);
    bmp[key] = entry.name;
  }
  let blankKey: string | undefined;
  const keyFor = (id: number | undefined): string =>
    (id !== undefined && id !== 0 ? keyById.get(id) : undefined) ?? (blankKey ??= allocate());
 
  const sequenceIds = new Set((bga.sequences ?? []).map((entry) => entry.id));
  const resolution = bmsonResolution(json);
  const position = createPulsePositionResolver(json, resolution);
  const events: BeMusicEvent[] = [];
  let skipped = 0;
  const add = (entries: typeof bga.events, channel: string, allowExtended: boolean): void => {
    for (const entry of entries) {
      const extended =
        entry.idSet !== undefined ||
        entry.condition !== undefined ||
        entry.interval !== undefined ||
        sequenceIds.has(entry.id ?? 0);
      if (extended && !allowExtended) {
        skipped += 1;
        continue;
      }
      events.push({ ...position(entry.y), channel, value: keyFor(entry.id) });
    }
  };
  add(bga.events, BMS_BASE_CHANNEL, true);
  add(bga.layerEvents, BMS_LAYER_CHANNEL, false);
  add(bga.poorEvents, BMS_POOR_CHANNEL, false);
  if (skipped > 0 || (bga.sequences?.length ?? 0) > 0)
    report?.({
      code: 'bga-sequence',
      message: `bmson BGA sequences and conditional layer events have no BMS form; ${skipped} event(s) were skipped.`,
    });
  if (bga.layerEvents.length > 0)
    report?.({
      code: 'bga-layer-black-key',
      message: 'A bmson layer draws black; BMS treats layer black as transparent.',
    });
  return { bmp, events };
}
 
function bmsonResolution(json: BeMusicJSON): number {
  const resolution = json.bmson.info.resolution;
  return typeof resolution === 'number' && Number.isFinite(resolution) && resolution >= 1
    ? Math.floor(resolution)
    : 240;
}
 
/** A pulse at the chart's bmson resolution as a measure and an exact pulse fraction of that measure. */
function createPulsePositionResolver(
  json: BeMusicJSON,
  resolution: number,
): (pulse: number) => Pick<BeMusicEvent, 'measure' | 'position'> {
  const beatResolver = createBeatResolver(json);
  const measureStartPulse = (measure: number): number => Math.round(beatResolver.measureToBeat(measure) * resolution);
  return (pulse) => {
    const target = Math.max(0, Math.round(pulse));
    let measure = 0;
    while (measureStartPulse(measure + 1) <= target) measure += 1;
    const start = measureStartPulse(measure);
    return { measure, position: [target - start, Math.max(1, measureStartPulse(measure + 1) - start)] };
  };
}