All files / chart-model/src channels.ts

91.15% Statements 134/147
83.55% Branches 127/152
91.11% Functions 41/45
91.15% Lines 134/147

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                  598x 598x 598x 598x 598x 598x 598x 598x 598x 598x     4214x 4214x 4214x 4214x 4213x     1x 1x       9744x 9744x 9744x 9744x 3164x     6580x       337290x 25099x   312191x       45308x 45307x 45307x 45307x 21649x     23659x       231078x 231078x 231078x 231078x 6721x     224357x       272285x 272275x 272275x 272275x 45424x   226851x 829x   226022x 513x   225509x 16334x   209175x 159x   209016x   10x       544235x 544234x 544234x 465042x     79193x       7x 6x 6x 3x     4x       351897x               169x       708x               417x               194500x               617x 617x 617x 617x 2x   615x 615x 1x   614x       87607x 87607x 87606x 56843x   30763x 30763x   1x       9662x 8749x   913x 913x 907x   6x       16x             16x               1193516x 1193516x 1193501x   15x       2562213x       1281123x 16x   1281107x 1281107x 1281107x 1281103x   4x 4x 4x     4x               6580x       1x       312190x       1x       23658x               10x 1x   9x                     827404x       1892984x               1x       1x       1x       1892983x 1641558x   251425x 251425x                 843767x       743055x 743055x       224357x                                       79192x       87609x       351897x       195207x       194605x       194499x    
import { normalizeASCIIBase36Code } from '@be-music/shared-utils/core';
import { normalizeChannel, normalizeObjectKey, type BeMusicEvent } from '@be-music/chart-source';
 
/**
 * Bounds of the extended lane-code range. A `<side><lane>` channel's lane code is either a classic `1`-`9` column or one
 * of these `A`-`Z` columns, which together give 35 lanes per side — comfortably above the 24 the Keyboardmania modes
 * use, and the same span `laneIndexToChannel` (bmson `generic-Nkeys`) and `lane-layout.ts`'s `EXTENDED_LANE_DIGITS`
 * already emit.
 */
export const EXTENDED_LANE_CODE_MIN = 0x41; // 'A'
const EXTENDED_LANE_CODE_MAX = 0x5a; // 'Z'
const PACKED_CHANNEL_03 = 0x3033;
const PACKED_CHANNEL_08 = 0x3038;
const PACKED_CHANNEL_09 = 0x3039;
const PACKED_CHANNEL_97 = 0x3937;
const PACKED_CHANNEL_98 = 0x3938;
const PACKED_CHANNEL_99 = 0x3939;
const PACKED_CHANNEL_SC = 0x5343;
const PACKED_CHANNEL_SP = 0x5350;
 
export function parseBPMFrom03Token(value: string): number {
  Eif (value.length === 2) {
    const high = parseHexDigitFast(value.charCodeAt(0));
    const low = parseHexDigitFast(value.charCodeAt(1));
    if (high >= 0 && low >= 0) {
      return (high << 4) + low;
    }
  }
  const parsed = Number.parseInt(value, 16);
  return Number.isFinite(parsed) ? parsed : 0;
}
 
export function isTempoChannel(channel: string): boolean {
  Eif (channel.length === 2) {
    const high = channel.charCodeAt(0);
    const low = channel.charCodeAt(1);
    if (high === 0x30 && (low === 0x33 || low === 0x38)) {
      return true;
    }
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedTempoChannel, isTempoNormalizedChannel);
}
 
export function isStopChannel(channel: string): boolean {
  if (channel.length === 2 && channel.charCodeAt(0) === 0x30 && channel.charCodeAt(1) === 0x39) {
    return true;
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedStopChannel, isStopNormalizedChannel);
}
 
export function isScrollChannel(channel: string): boolean {
  if (channel.length === 2) {
    const high = channel.charCodeAt(0) & 0xdf;
    const low = channel.charCodeAt(1) & 0xdf;
    if (high === 0x53 && low === 0x43) {
      return true;
    }
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedScrollChannel, isScrollNormalizedChannel);
}
 
export function isLandmineChannel(channel: string): boolean {
  Eif (channel.length === 2) {
    const high = channel.charCodeAt(0) & 0xdf;
    const low = channel.charCodeAt(1);
    if ((high === 0x44 || high === 0x45) && low >= 0x31 && low <= 0x39) {
      return true;
    }
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedLandmineChannel, isLandmineNormalizedChannel);
}
 
export function isSampleTriggerChannel(channel: string): boolean {
  if (channel.length === 2) {
    const highCode = channel.charCodeAt(0);
    const lowCode = channel.charCodeAt(1);
    if (highCode === 0x30) {
      return lowCode === 0x31;
    }
    if (highCode === 0x39 && lowCode >= 0x37 && lowCode <= 0x39) {
      return false;
    }
    if ((highCode & 0xdf) === 0x41 && lowCode >= 0x30 && lowCode <= 0x36) {
      return false;
    }
    if ((highCode & 0xdf) === 0x53 && (lowCode & 0xdf) === 0x43) {
      return false;
    }
    if ((highCode & 0xdf) === 0x53 && (lowCode & 0xdf) === 0x50) {
      return false;
    }
    return true;
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedSampleTriggerChannel, isSampleTriggerNormalizedChannel);
}
 
export function isPlayableChannel(channel: string): boolean {
  if (channel.length === 2) {
    const high = channel.charCodeAt(0);
    if ((high === 0x31 || high === 0x32) && isLaneCode(channel.charCodeAt(1))) {
      return true;
    }
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedPlayableChannel, isPlayableNormalizedChannel);
}
 
export function isBMSLongNoteChannel(channel: string): boolean {
  if (channel.length === 2) {
    const high = channel.charCodeAt(0);
    if ((high === 0x35 || high === 0x36) && isLaneCode(channel.charCodeAt(1))) {
      return true;
    }
  }
  return matchesPackedOrNormalizedChannel(channel, isPackedBMSLongNoteChannel, isBMSLongNoteNormalizedChannel);
}
 
export function isPlayLaneSoundChannel(channel: string): boolean {
  return matchesPackedOrNormalizedChannel(channel, isPackedPlayLaneSoundChannel, isPlayLaneSoundNormalizedChannel);
}
 
/**
 * Whether a sound plays at the keysound volume (the play / key bus) rather than the BGM volume: a play lane's sound, or
 * an LRX background sound plugout plays in its key group (`BeMusicEvent.lrx.keyVolume`).
 */
export function playsAtKeysoundVolume(channel: string, event?: Pick<BeMusicEvent, 'lrx'>): boolean {
  return isPlayLaneSoundChannel(channel) || event?.lrx?.keyVolume === true;
}
 
export function isBmsBGMVolumeChangeChannel(channel: string): boolean {
  return matchesPackedOrNormalizedChannel(
    channel,
    isPackedBmsBGMVolumeChangeChannel,
    isBmsBGMVolumeChangeNormalizedChannel,
  );
}
 
export function isBMSKeyVolumeChangeChannel(channel: string): boolean {
  return matchesPackedOrNormalizedChannel(
    channel,
    isPackedBMSKeyVolumeChangeChannel,
    isBMSKeyVolumeChangeNormalizedChannel,
  );
}
 
export function isBMSDynamicVolumeChangeChannel(channel: string): boolean {
  return matchesPackedOrNormalizedChannel(
    channel,
    isPackedBMSDynamicVolumeChangeChannel,
    isBMSDynamicVolumeChangeNormalizedChannel,
  );
}
 
export function parseBMSDynamicVolumeGain(value: string): number | undefined {
  const normalized = normalizeObjectKey(value);
  const high = parseHexDigitFast(normalized.charCodeAt(0));
  const low = parseHexDigitFast(normalized.charCodeAt(1));
  if (high < 0 || low < 0) {
    return undefined;
  }
  const parsed = (high << 4) | low;
  if (parsed <= 0) {
    return undefined;
  }
  return parsed / 0xff;
}
 
export function mapBMSLongNoteChannelToPlayable(channel: string): string | undefined {
  const packed = tryPackChannel(channel);
  if (packed >= 0) {
    if (!isPackedBMSLongNoteChannel(packed)) {
      return undefined;
    }
    const lane = String.fromCharCode(packed & 0xff);
    return ((packed >> 8) & 0xff) === 0x35 ? `1${lane}` : `2${lane}`;
  }
  return mapBMSLongNoteNormalizedChannelToPlayable(resolveNormalizedChannelForPredicate(channel));
}
 
function parseHexDigitFast(code: number): number {
  if (code >= 0x30 && code <= 0x39) {
    return code - 0x30;
  }
  const uppercase = code & 0xdf;
  if (uppercase >= 0x41 && uppercase <= 0x46) {
    return uppercase - 0x41 + 10;
  }
  return -1;
}
 
function resolveNormalizedChannelForPredicate(channel: string): string {
  Iif (channel.length === 2) {
    const code0 = channel.charCodeAt(0);
    const code1 = channel.charCodeAt(1);
    if (isNormalizedBase36Code(code0) && isNormalizedBase36Code(code1)) {
      return channel;
    }
  }
  return normalizeChannel(channel);
}
 
function matchesPackedOrNormalizedChannel(
  channel: string,
  packedMatcher: (packed: number) => boolean,
  normalizedMatcher: (normalized: string) => boolean,
): boolean {
  const packed = tryPackChannel(channel);
  if (packed >= 0) {
    return packedMatcher(packed);
  }
  return normalizedMatcher(resolveNormalizedChannelForPredicate(channel));
}
 
function isNormalizedBase36Code(code: number): boolean {
  return (code >= 0x30 && code <= 0x39) || (code >= 0x41 && code <= 0x5a);
}
 
function tryPackChannel(channel: string): number {
  if (channel.length !== 2) {
    return -1;
  }
  const sourceHigh = channel.charCodeAt(0);
  const sourceLow = channel.charCodeAt(1);
  if (isNormalizedBase36Code(sourceHigh) && isNormalizedBase36Code(sourceLow)) {
    return (sourceHigh << 8) | sourceLow;
  }
  const high = normalizeASCIIBase36Code(sourceHigh);
  const low = normalizeASCIIBase36Code(sourceLow);
  Iif (high < 0 || low < 0) {
    return -1;
  }
  return (high << 8) | low;
}
 
function isTempoNormalizedChannel(normalized: string): boolean {
  return normalized === '03' || normalized === '08';
}
 
function isPackedTempoChannel(packed: number): boolean {
  return packed === PACKED_CHANNEL_03 || packed === PACKED_CHANNEL_08;
}
 
function isStopNormalizedChannel(normalized: string): boolean {
  return normalized === '09';
}
 
function isPackedStopChannel(packed: number): boolean {
  return packed === PACKED_CHANNEL_09;
}
 
function isScrollNormalizedChannel(normalized: string): boolean {
  return normalized === 'SC';
}
 
function isPackedScrollChannel(packed: number): boolean {
  return packed === PACKED_CHANNEL_SC;
}
 
function isLandmineNormalizedChannel(normalized: string): boolean {
  return /^[DE][1-9A-Z]$/.test(normalized);
}
 
function isSampleTriggerNormalizedChannel(normalized: string): boolean {
  if (/^0[0-9A-Z]$/.test(normalized)) {
    return normalized === '01';
  }
  return (
    normalized !== '97' &&
    normalized !== '98' &&
    normalized !== '99' &&
    !/^A[0-6]$/u.test(normalized) &&
    normalized !== 'SC' &&
    normalized !== 'SP'
  );
}
 
export function isPlayableNormalizedChannel(normalized: string): boolean {
  return /^[12][1-9A-Z]$/.test(normalized);
}
 
function isBMSLongNoteNormalizedChannel(normalized: string): boolean {
  return /^[56][1-9A-Z]$/.test(normalized);
}
 
function isPlayLaneSoundNormalizedChannel(normalized: string): boolean {
  return /^[1-6][1-9A-Z]$/.test(normalized);
}
 
function isBmsBGMVolumeChangeNormalizedChannel(normalized: string): boolean {
  return normalized === '97';
}
 
function isBMSKeyVolumeChangeNormalizedChannel(normalized: string): boolean {
  return normalized === '98';
}
 
function isBMSDynamicVolumeChangeNormalizedChannel(normalized: string): boolean {
  return isBmsBGMVolumeChangeNormalizedChannel(normalized) || isBMSKeyVolumeChangeNormalizedChannel(normalized);
}
 
export function mapBMSLongNoteNormalizedChannelToPlayable(normalized: string): string | undefined {
  if (!isBMSLongNoteNormalizedChannel(normalized)) {
    return undefined;
  }
  const lane = normalized[1]!;
  return normalized.charCodeAt(0) === 0x35 ? `1${lane}` : `2${lane}`;
}
 
/**
 * Lane channels span two code ranges: the classic `1`-`9` columns and the extended `A`-`Z` columns the 24-key
 * (Keyboardmania) modes author lanes 10..24 on. Both are valid for every `<side><lane>` channel family — playable
 * (`1X` / `2X`), invisible (`3X` / `4X`), legacy long note (`5X` / `6X`), and landmine (`DX` / `EX`).
 */
function isLaneCode(code: number): boolean {
  return (code >= 0x31 && code <= 0x39) || (code >= EXTENDED_LANE_CODE_MIN && code <= EXTENDED_LANE_CODE_MAX);
}
 
function isPackedLaneRange(packed: number, highStart: number, highEnd: number): boolean {
  const high = (packed >> 8) & 0xff;
  return high >= highStart && high <= highEnd && isLaneCode(packed & 0xff);
}
 
function isPackedLandmineChannel(packed: number): boolean {
  return isPackedLaneRange(packed, 0x44, 0x45);
}
 
function isPackedSampleTriggerChannel(packed: number): boolean {
  if (((packed >> 8) & 0xff) === 0x30) {
    return (packed & 0xff) === 0x31;
  }
  if (
    packed === PACKED_CHANNEL_97 ||
    packed === PACKED_CHANNEL_98 ||
    packed === PACKED_CHANNEL_99 ||
    packed === PACKED_CHANNEL_SC ||
    packed === PACKED_CHANNEL_SP
  ) {
    return false;
  }
  return packed < 0x4130 || packed > 0x4136;
}
 
function isPackedPlayableChannel(packed: number): boolean {
  return isPackedLaneRange(packed, 0x31, 0x32);
}
 
function isPackedBMSLongNoteChannel(packed: number): boolean {
  return isPackedLaneRange(packed, 0x35, 0x36);
}
 
function isPackedPlayLaneSoundChannel(packed: number): boolean {
  return isPackedLaneRange(packed, 0x31, 0x36);
}
 
function isPackedBmsBGMVolumeChangeChannel(packed: number): boolean {
  return packed === PACKED_CHANNEL_97;
}
 
function isPackedBMSKeyVolumeChangeChannel(packed: number): boolean {
  return packed === PACKED_CHANNEL_98;
}
 
function isPackedBMSDynamicVolumeChangeChannel(packed: number): boolean {
  return isPackedBmsBGMVolumeChangeChannel(packed) || isPackedBMSKeyVolumeChangeChannel(packed);
}