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 | 2x 3x 3x 1x 1x 4x 4x 4x 4x 15x 15x 15x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 1x 2x 1x 1x 2x 2x 1x 1x 1x 1x 7x 7x 7x 7x 7x 2x 2x 2x 2x 10x 2x 8x 2x 6x 2x 4x 1x 3x 2x 1x 1x 1x 1x 25x 25x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 1x 2x 25x 25x 1x 25x 25x 49x 24x 24x 26x 26x 16x 16x 1x 25x 26x 26x 42x 26x 9x 9x 1x 8x 8x 9x 7x 7x 6x 6x 6x 2x 4x 7x 2x 2x 5x 5x 5x 11x 11x 5x 6x 5x | import { compareEvents, sortEvents } from '@be-music/chart-model';
import { readFile, writeFile } from 'node:fs/promises';
import { extname, resolve } from 'node:path';
import {
BMS_JSON_FORMAT,
DEFAULT_BPM,
cloneJSON,
createEmptyJSON,
ensureMeasure,
normalizeChannel,
normalizeObjectKey,
resolveBMSBase,
type BeMusicEvent,
type BeMusicJSON,
} from '@be-music/chart-source';
import { parseChart, parseChartFile } from '@be-music/chart-parser';
import { stringifyBmson, stringifyBMS } from '@be-music/chart-writer';
export async function importChart(inputPath: string): Promise<BeMusicJSON> {
return parseChartFile(resolve(inputPath));
}
export async function loadJSONFile(filePath: string): Promise<BeMusicJSON> {
const content = await readFile(resolve(filePath), 'utf8');
return parseChart(content, 'json');
}
export async function saveJSONFile(filePath: string, json: BeMusicJSON): Promise<void> {
const normalized = canSerializeJSONAsIs(json) ? json : normalizeJSON(json);
await writeFile(resolve(filePath), `${JSON.stringify(normalized, null, 2)}\n`, 'utf8');
}
export async function exportChart(filePath: string, json: BeMusicJSON): Promise<void> {
const outputPath = resolve(filePath);
const extension = extname(outputPath).toLowerCase();
const content = extension === '.bmson' ? stringifyBmson(json) : stringifyBMS(json);
await writeFile(outputPath, content, 'utf8');
}
export function setMetadata(json: BeMusicJSON, key: string, value: string): BeMusicJSON {
const normalized = normalizeJSON(json);
const property = key.toLowerCase();
switch (property) {
case 'title':
normalized.metadata.title = value;
return normalized;
case 'subtitle':
normalized.metadata.subtitle = value;
return normalized;
case 'artist':
normalized.metadata.artist = value;
return normalized;
case 'genre':
normalized.metadata.genre = value;
return normalized;
case 'comment':
normalized.metadata.comment = value;
return normalized;
case 'stagefile':
normalized.metadata.stageFile = value;
return normalized;
case 'playlevel':
normalized.metadata.playLevel = parsePlayLevelValue(value);
return normalized;
case 'rank':
normalized.metadata.rank = Number.parseFloat(value);
return normalized;
case 'total':
normalized.metadata.total = Number.parseFloat(value);
return normalized;
case 'difficulty':
normalized.metadata.difficulty = Number.parseFloat(value);
return normalized;
case 'bpm': {
const bpm = Number.parseFloat(value);
if (Number.isFinite(bpm) && bpm > 0) {
normalized.metadata.bpm = bpm;
}
return normalized;
}
default:
normalized.metadata.extras[key.toUpperCase()] = value;
return normalized;
}
}
function parsePlayLevelValue(value: string): number | string {
const trimmed = value.trim();
if (/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/.test(trimmed)) {
const parsed = Number.parseFloat(trimmed);
Eif (Number.isFinite(parsed)) {
return parsed;
}
}
return trimmed;
}
export function addNote(
json: BeMusicJSON,
params: {
measure: number;
channel: string;
positionNumerator: number;
positionDenominator: number;
value: string;
},
): BeMusicJSON {
const normalized = normalizeJSON(json);
const event = createNormalizedNoteEvent(params, resolveBMSBase(normalized));
ensureMeasure(normalized, event.measure);
insertSortedEvent(normalized.events, event);
return normalized;
}
export function deleteNote(
json: BeMusicJSON,
params: {
measure: number;
channel: string;
positionNumerator: number;
positionDenominator: number;
value?: string;
},
): BeMusicJSON {
const normalized = normalizeJSON(json);
// Honor `#BASE 62` so a delete keyed on `value: '0a'` doesn't accidentally match a `'0A'` event (or vice versa) by
// way of an implicit case-fold during target / event comparison.
const idBase = resolveBMSBase(normalized);
const target = createNormalizedNoteTarget(params, idBase);
normalized.events = normalized.events.filter((event) => {
if (event.measure !== target.measure) {
return true;
}
if (normalizeChannel(event.channel) !== target.channel) {
return true;
}
if (!isSamePosition(event, target.position)) {
return true;
}
if (target.value !== undefined && normalizeObjectKey(event.value, idBase) !== target.value) {
return true;
}
return false;
});
return normalized;
}
export function listNotes(json: BeMusicJSON, measure?: number): BeMusicEvent[] {
const target = normalizeJSON(json);
Eif (measure === undefined) {
return [...target.events];
}
return target.events.filter((event) => event.measure === measure);
}
export function createBlankJSON(): BeMusicJSON {
return createEmptyJSON('json');
}
function normalizeJSON(json: BeMusicJSON): BeMusicJSON {
const cloned = canCloneJSONFast(json) ? cloneJSON(json) : structuredClone(json);
if (!cloned.metadata) {
cloned.metadata = {
bpm: DEFAULT_BPM,
extras: {},
};
}
cloned.metadata.extras = cloned.metadata.extras ?? {};
cloned.metadata.bpm =
Number.isFinite(cloned.metadata.bpm) && cloned.metadata.bpm > 0 ? cloned.metadata.bpm : DEFAULT_BPM;
cloned.resources = cloned.resources ?? {
wav: {},
bmp: {},
bpm: {},
stop: {},
text: {},
};
cloned.resources.wav = cloned.resources.wav ?? {};
cloned.resources.bmp = cloned.resources.bmp ?? {};
cloned.resources.bpm = cloned.resources.bpm ?? {};
cloned.resources.stop = cloned.resources.stop ?? {};
cloned.resources.text = cloned.resources.text ?? {};
cloned.preservation = cloned.preservation ?? createEmptyJSON('json').preservation;
cloned.preservation.bms = cloned.preservation.bms ?? { sourceLines: [], objectLines: [] };
cloned.preservation.bms.sourceLines = cloned.preservation.bms.sourceLines ?? [];
cloned.preservation.bms.objectLines = cloned.preservation.bms.objectLines ?? [];
cloned.preservation.bmson = cloned.preservation.bmson ?? {
lines: [],
bpmEvents: [],
stopEvents: [],
soundChannels: [],
};
cloned.preservation.bmson.lines = cloned.preservation.bmson.lines ?? [];
cloned.preservation.bmson.bpmEvents = cloned.preservation.bmson.bpmEvents ?? [];
cloned.preservation.bmson.stopEvents = cloned.preservation.bmson.stopEvents ?? [];
cloned.preservation.bmson.soundChannels = cloned.preservation.bmson.soundChannels ?? [];
if (!hasOnlyFiniteMeasures(cloned.measures)) {
cloned.measures = (cloned.measures ?? []).filter(
(measure) => Number.isFinite(measure.index) && Number.isFinite(measure.length),
);
}
Iif (!areEventsSorted(cloned.events)) {
cloned.events = sortEvents(cloned.events ?? []);
}
return cloned;
}
function canSerializeJSONAsIs(json: BeMusicJSON): boolean {
return (
json.format === BMS_JSON_FORMAT &&
json.metadata !== undefined &&
Number.isFinite(json.metadata.bpm) &&
json.metadata.bpm > 0 &&
json.metadata.extras !== undefined &&
hasCompleteResources(json) &&
hasCompletePreservation(json) &&
Array.isArray(json.measures) &&
hasOnlyFiniteMeasures(json.measures) &&
Array.isArray(json.events) &&
areEventsSorted(json.events)
);
}
function canCloneJSONFast(json: BeMusicJSON): boolean {
return (
json.metadata !== undefined &&
json.metadata.extras !== undefined &&
hasCompleteResources(json) &&
Array.isArray(json.measures) &&
Array.isArray(json.events) &&
hasFastCloneBMSState(json) &&
hasFastCloneBmsonState(json)
);
}
function hasCompleteResources(json: BeMusicJSON): boolean {
return (
json.resources !== undefined &&
json.resources.wav !== undefined &&
json.resources.bmp !== undefined &&
json.resources.bpm !== undefined &&
json.resources.stop !== undefined &&
json.resources.text !== undefined
);
}
function hasCompletePreservation(json: BeMusicJSON): boolean {
return (
json.preservation !== undefined &&
json.preservation.bms !== undefined &&
json.preservation.bmson !== undefined &&
Array.isArray(json.preservation.bms.sourceLines) &&
Array.isArray(json.preservation.bms.objectLines) &&
Array.isArray(json.preservation.bmson.lines) &&
Array.isArray(json.preservation.bmson.bpmEvents) &&
Array.isArray(json.preservation.bmson.stopEvents) &&
Array.isArray(json.preservation.bmson.soundChannels)
);
}
function hasFastCloneBMSState(json: BeMusicJSON): boolean {
return (
json.bms !== undefined &&
hasCompletePreservation(json) &&
Array.isArray(json.bms.controlFlow) &&
(json.bms.lnObjs === undefined || Array.isArray(json.bms.lnObjs)) &&
json.bms.exRank !== undefined &&
json.bms.argb !== undefined &&
Array.isArray(json.bms.stp) &&
json.bms.changeOption !== undefined &&
json.bms.songText !== undefined &&
json.bms.exWAV !== undefined &&
json.bms.exBMP !== undefined &&
json.bms.bga !== undefined &&
json.bms.scroll !== undefined &&
json.bms.speed !== undefined &&
json.bms.swBGA !== undefined
);
}
function hasFastCloneBmsonState(json: BeMusicJSON): boolean {
return (
json.bmson !== undefined &&
hasCompletePreservation(json) &&
json.bmson.info !== undefined &&
json.bmson.bga !== undefined &&
Array.isArray(json.bmson.bga.header) &&
Array.isArray(json.bmson.bga.events) &&
Array.isArray(json.bmson.bga.layerEvents) &&
Array.isArray(json.bmson.bga.poorEvents)
);
}
function hasOnlyFiniteMeasures(measures: BeMusicJSON['measures'] | undefined): boolean {
Iif (!Array.isArray(measures)) {
return false;
}
for (let index = 0; index < measures.length; index += 1) {
const measure = measures[index]!;
if (!Number.isFinite(measure.index) || !Number.isFinite(measure.length)) {
return false;
}
}
return true;
}
function areEventsSorted(events: BeMusicJSON['events'] | undefined): boolean {
Iif (!Array.isArray(events)) {
return false;
}
for (let index = 1; index < events.length; index += 1) {
Iif (compareEvents(events[index - 1]!, events[index]!) > 0) {
return false;
}
}
return true;
}
function normalizePositionFraction(numerator: number, denominator: number): { numerator: number; denominator: number } {
const safeDenominator = Number.isFinite(denominator) && denominator > 0 ? Math.max(1, Math.floor(denominator)) : 1;
if (!Number.isFinite(numerator)) {
return { numerator: 0, denominator: safeDenominator };
}
const safeNumerator = Math.floor(numerator);
return {
numerator: Math.max(0, Math.min(safeDenominator - 1, safeNumerator)),
denominator: safeDenominator,
};
}
function createNormalizedNoteTarget(
params: {
measure: number;
channel: string;
positionNumerator: number;
positionDenominator: number;
value?: string;
},
base: 36 | 62 = 36,
): {
measure: number;
channel: string;
position: { numerator: number; denominator: number };
value?: string;
} {
return {
measure: Math.max(0, Math.floor(params.measure)),
channel: normalizeChannel(params.channel),
position: normalizePositionFraction(params.positionNumerator, params.positionDenominator),
value: params.value === undefined ? undefined : normalizeObjectKey(params.value, base),
};
}
function createNormalizedNoteEvent(
params: {
measure: number;
channel: string;
positionNumerator: number;
positionDenominator: number;
value: string;
},
base: 36 | 62 = 36,
): BeMusicEvent {
const target = createNormalizedNoteTarget(params, base);
return {
measure: target.measure,
channel: target.channel,
position: [target.position.numerator, target.position.denominator],
value: target.value!,
};
}
function isSamePosition(event: BeMusicEvent, target: { numerator: number; denominator: number }): boolean {
const left = BigInt(event.position[0]) * BigInt(target.denominator);
const right = BigInt(target.numerator) * BigInt(event.position[1]);
if (left !== right) {
return false;
}
return true;
}
function insertSortedEvent(events: BeMusicEvent[], event: BeMusicEvent): void {
if (events.length === 0) {
events.push(event);
return;
}
let low = 0;
let high = events.length;
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (compareEvents(events[mid]!, event) <= 0) {
low = mid + 1;
} else {
high = mid;
}
}
events.splice(low, 0, event);
}
|