All files / chart-model/src wav-directives.ts

90.83% Statements 109/120
88.11% Branches 89/101
100% Functions 13/13
98.94% Lines 94/95

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                                                                              598x 598x     164x 164x 164x 163x 163x 159x 159x 155x 155x 164x 164x   293x   12x   143x 143x 142x 142x 277x 277x 277x 277x 8x 8x 8x 8x   269x 269x 269x 178x 76x   142x                         26x 25x 25x 25x 25x                       314x 314x 28x 28x 28x   22x 22x   314x               33988x 33988x 147x                   4621x 4621x 66x 66x   4621x                                                                   29025x 29002x   23x         37x                             95x 95x 95x 92x 92x     92x 92x 92x   91x   22x 22x   52x 52x   16x 16x   1x   90x 90x 90x                             316x 316x 40x 40x 29x     316x         310x 310x 14x 310x 13x 13x 13x 13x   310x                               313x 313x    
import { normalizeObjectKey, type BeMusicJSON } from '@be-music/chart-source';
 
/**
 * Parsed `#EXWAVxx [flags] params filename` directive.
 *
 * `#EXWAVxx` declares an "extended" WAV slot whose playback should be modified by the listed parameters. The `flags`
 * token names which channels are present (in order), and whitespace-separated parameters follow those flags
 * positionally. Be-Music also accepts its earlier comma-separated parameter spelling.
 *
 * Supported flags (hitkey BMS Memo, nanasigroove 1.552): - `p` — stereo pan (`-10000..10000`, where 0 is center).
 * - `v` — playback attenuation in millibels (hundredths of a dB). Negative values attenuate, `0` is unity. - `f` —
 * playback frequency in Hz. The sample is resampled (or replayed at a different rate) so its native sample rate maps
 * onto this output rate. - `r` — a rate multiplier nanasigroove 1.552 reads but its manual does not list (#1197).
 */
export interface BMSExWAV {
  /** Raw filename token, joined back together if it contained spaces. */
  filename: string;
  /** Pan, when the `p` flag was present. */
  pan?: number;
  /** Volume in millibels (1/100 dB), when the `v` flag was present. */
  volumeMillibels?: number;
  /** Playback frequency in Hz, when the `f` flag was present. */
  frequencyHz?: number;
  /** Playback rate multiplier, when the `r` flag was present; applied after `f`. */
  rate?: number;
}
 
/**
 * Parses one `#EXWAVxx` directive value (the post-`#EXWAVxx ` text).
 *
 * A flag word of up to five letters (`pvf` / `vp` / `v` / `fr`, in any case) is followed by one whitespace-separated
 * number per letter, then the filename, as nanasigroove 1.552 reads it (`nanasi.exe` 0x4d05b4-0x4d07a4). A repeated
 * letter reads another number that replaces the earlier one. `p`, `v` and `f` take integers, `r` a decimal. Earlier
 * Be-Music comma-separated parameters remain accepted. Trailing filename tokens are joined back together so filenames
 * can contain spaces.
 *
 * Returns `undefined` for malformed input — better to skip a broken `#EXWAV` than to apply nonsense to the audible
 * signal.
 */
const INTEGER_PARAMETER = /^[+-]?\d+$/u;
const DECIMAL_PARAMETER = /^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?$/u;
 
export function parseBMSExWAV(raw: string): BMSExWAV | undefined {
  Iif (typeof raw !== 'string') return undefined;
  const trimmed = raw.trim();
  if (trimmed.length === 0) return undefined;
  const tokens = trimmed.split(/\s+/u);
  if (tokens.length < 3) return undefined;
  const flags = tokens[0]!.toLowerCase();
  if (!/^[pvfr]{1,5}$/u.test(flags)) return undefined;
  const commaSeparated = tokens[1]!.includes(',');
  const params = commaSeparated ? tokens[1]!.split(',') : tokens.slice(1, 1 + flags.length);
  const filenameStart = commaSeparated ? 2 : 1 + flags.length;
  if (
    params.length !== flags.length ||
    params.some((value, index) => !(flags[index] === 'r' ? DECIMAL_PARAMETER : INTEGER_PARAMETER).test(value))
  )
    return undefined;
  // Parameter count determines the boundary; the remaining filename may contain spaces.
  const filename = tokens.slice(filenameStart).join(' ');
  if (filename.length === 0) return undefined;
  const out: BMSExWAV = { filename };
  for (let index = 0; index < flags.length; index += 1) {
    const flag = flags[index];
    const valueRaw = params[index];
    Iif (valueRaw === undefined) break;
    if (flag === 'r') {
      const rate = Number.parseFloat(valueRaw.trim());
      Iif (!Number.isFinite(rate)) return undefined;
      out.rate = rate;
      continue;
    }
    const value = Number.parseInt(valueRaw.trim(), 10);
    Iif (!Number.isSafeInteger(value)) return undefined;
    if (flag === 'p') out.pan = value;
    else if (flag === 'v') out.volumeMillibels = value;
    else Eif (flag === 'f') out.frequencyHz = value;
  }
  return out;
}
 
/**
 * Maps a millibel volume value (`#EXWAVxx v` parameter, `1/100 dB`) to a 0..N linear gain multiplier. `0 mB`
 * round-trips to `1.0` (unity); negative values attenuate; positive values boost. We clamp the output below `8.0` (≈
 * +18 dB) so a malformed chart can't blow the bus, and at `0` so very-negative inputs collapse to silence rather than
 * producing a denormal.
 *
 * The unit is the millibel (a hundredth of a decibel), as in DirectSound.
 * `v -200` is −2 dB and `v -600` is −6 dB. Playback collectors enforce the dialect's attenuation-only bounds.
 */
export function exWAVVolumeMillibelsToLinearGain(millibels: number): number {
  if (!Number.isFinite(millibels)) return 1;
  const dB = millibels / 100;
  const gain = 10 ** (dB / 20);
  Iif (!Number.isFinite(gain) || gain < 0) return 0;
  return Math.min(8, gain);
}
 
/**
 * Builds a `slot → linear-gain-multiplier` map for every `#EXWAVxx` entry that supplies a `v` (volume) flag. Slots
 * without a parsed `v` (or whose `#EXWAVxx` doesn't even have three tokens) are absent from the result so the consumer
 * falls through to unity gain.
 *
 * `entries` is the parser-produced `chart.bms.exWAV` map (raw post-`#EXWAVxx ` strings keyed by slot id). The returned
 * keys are passed through verbatim — they're already normalized to the chart's id base when the parser ingested them.
 */
export function collectBMSExWAVVolumeMultipliers(entries: Readonly<Record<string, string>>): Map<string, number> {
  const out = new Map<string, number>();
  for (const [slot, raw] of Object.entries(entries)) {
    const parsed = parseBMSExWAV(raw);
    Iif (parsed === undefined) continue;
    if (parsed.volumeMillibels === undefined) continue;
    // nanasigroove defines attenuation only; its minimum is explicitly silent.
    const volume = Math.max(-10000, Math.min(0, parsed.volumeMillibels));
    out.set(slot, volume === -10000 ? 0 : exWAVVolumeMillibelsToLinearGain(volume));
  }
  return out;
}
 
/**
 * The `#EXWAVxx` declarations that take effect: those no later `#WAVxx` of the same slot redefines (`exWAVOverridden`).
 * nanasigroove 1.552 stores `#WAV` and `#EXWAV` in one table and the later line wins (`nanasi.exe` 0x4d07c9, 0x4d0abb).
 */
export function resolveBmsExWAVDeclarations(json: Pick<BeMusicJSON, 'bms'>): Record<string, string> {
  const overridden = json.bms.exWAVOverridden;
  if (overridden === undefined || overridden.length === 0) return json.bms.exWAV;
  return Object.fromEntries(Object.entries(json.bms.exWAV).filter(([key]) => !overridden.includes(key)));
}
 
/**
 * Effective sample filenames. A valid EXWAV definition owns its shared WAV slot unless a later `#WAV` of the slot
 * redefines it ({@link resolveBmsExWAVDeclarations}); invalid EXWAV leaves the ordinary WAV definition intact (Be-Music's
 * reading: nanasigroove misreads such a line). Neither the authored resource map nor the raw EXWAV declarations are
 * mutated.
 */
export function resolveBmsWAVResources(json: Pick<BeMusicJSON, 'resources' | 'bms'>): Record<string, string> {
  const resources = { ...json.resources.wav };
  for (const [key, raw] of Object.entries(resolveBmsExWAVDeclarations(json))) {
    const parsed = parseBMSExWAV(raw);
    if (parsed) resources[key] = parsed.filename;
  }
  return resources;
}
 
/**
 * Parameter byte for a `#WAVCMD pp xx vv` entry.
 *
 * - `'pitch'` (`pp = 00`) — relative chromatic pitch, `0..127`, with 60 as unity.
 * - `'volume'` (`pp = 01`) — percentage volume, with 100 as unity; values above 100 amplify.
 * - `'duration'` (`pp = 02`) — playback time in 0.5 ms units; zero plays to the original end.
 *
 * Source: http://harinezumi.s14.xrea.com/download/mbm.txt (MacBeat MOD specification, 2000-04-19).
 */
export type BmsWAVCmdParam = 'pitch' | 'volume' | 'duration';
 
export interface BmsWAVCmdEntry {
  param: BmsWAVCmdParam;
  /** Slot id (`#WAVxx`), normalized via `normalizeObjectKey`. */
  slot: string;
  /**
   * Raw integer value as written in the chart. The interpretation depends on `param` — see {@link BmsWAVCmdParam}.
   * Volume is a percentage; convert to a gain multiplier via {@link wavCmdVolumePercentToLinearGain}.
   */
  value: number;
}
 
/**
 * Maps a BMS `#VOLWAV n` value (`bms.volWAV`) to the chart-wide linear gain: `n / 100` for `0 < n < 200`, unity for
 * anything else, including a missing value.
 *
 * This is beatoraja's range (`AbstractAudioDriver` applies `#VOLWAV` only when `0 < n < 200`). LR2 ignores `#VOLWAV`,
 * so a chart tuned for it plays at full volume there; following beatoraja keeps `#VOLWAV 0` from muting a chart and
 * keeps values of 200 and above from amplifying it into clipping.
 */
export function volWAVToLinearGain(volWAV: number | undefined): number {
  if (typeof volWAV !== 'number' || !Number.isFinite(volWAV) || volWAV <= 0 || volWAV >= 200) {
    return 1;
  }
  return volWAV / 100;
}
 
/** Convert MacBeat percentage volume to linear gain. Values above 100 amplify; negative inputs mute. */
export function wavCmdVolumePercentToLinearGain(percent: number): number {
  return Number.isFinite(percent) ? Math.max(0, percent) / 100 : 1;
}
 
/**
 * Parses one `#WAVCMD pp xx vv` line. Whitespace-separated tokens: - `pp` (parameter byte): `00`/`01`/`02`
 * (pitch/volume/duration). - `xx` (slot id): two-character base-36 (or base-62) id matching a `#WAVxx` declaration.
 * Returned as the normalized key. - `vv`: integer value.
 *
 * Returns `undefined` for malformed lines so callers can skip them rather than crashing on an unrecognized parameter
 * byte.
 *
 * @param idBase  Pass `62` for charts declared with `#BASE 62` so
 *                lowercase slot ids are preserved as-is.
 */
export function parseBmsWAVCmd(line: string, idBase: 36 | 62 = 36): BmsWAVCmdEntry | undefined {
  Iif (typeof line !== 'string') return undefined;
  const tokens = line.trim().split(/\s+/u);
  if (tokens.length < 3) return undefined;
  const [paramRaw, slotRaw, valueRaw] = tokens;
  Iif (paramRaw === undefined || slotRaw === undefined || valueRaw === undefined) {
    return undefined;
  }
  const paramByte = Number.parseInt(paramRaw, 10);
  const value = Number.parseInt(valueRaw, 10);
  if (!Number.isFinite(paramByte) || !Number.isFinite(value)) return undefined;
  let param: BmsWAVCmdParam;
  switch (paramByte) {
    case 0:
      param = 'pitch';
      break;
    case 1:
      param = 'volume';
      break;
    case 2:
      param = 'duration';
      break;
    default:
      return undefined;
  }
  const slot = normalizeObjectKey(slotRaw, idBase);
  Iif (slot.length === 0) return undefined;
  return { param, slot, value };
}
 
/**
 * Builds a `slot → linear-gain-multiplier` map by collecting every `#WAVCMD 01 xx vv` (volume) line. Slots with no
 * `#WAVCMD 01` line stay absent (the consumer falls back to unity gain). When a chart writes multiple volume lines for
 * the same slot the LAST one wins. This is Be-Music's policy; MacBeat leaves duplication undefined.
 *
 * Pitch / duration lines are intentionally skipped — they require sample-graph wiring that
 * lives in the audio engine, not in this pure pre-process.
 */
export function collectBmsWAVCmdVolumeMultipliers(
  lines: ReadonlyArray<string>,
  idBase: 36 | 62 = 36,
): Map<string, number> {
  const out = new Map<string, number>();
  for (const line of lines) {
    const entry = parseBmsWAVCmd(line, idBase);
    if (entry?.param === 'volume') {
      out.set(entry.slot, wavCmdVolumePercentToLinearGain(entry.value));
    }
  }
  return out;
}
 
/** Per-slot authored volume, including exact intrinsic gain retained by composed Be-Music JSON charts. */
export function collectBmsWAVVolumeMultipliers(json: Pick<BeMusicJSON, 'bms' | 'resources'>): Map<string, number> {
  const result = collectBmsWAVCmdVolumeMultipliers(json.bms.wavCmds, json.bms.base ?? 36);
  for (const [key, gain] of collectBMSExWAVVolumeMultipliers(resolveBmsExWAVDeclarations(json)))
    result.set(key, (result.get(key) ?? 1) * gain);
  for (const [key, gain] of Object.entries(json.resources.wavGain ?? {})) {
    Iif (!Number.isFinite(gain) || gain < 0) throw new RangeError(`Invalid intrinsic WAV gain for slot ${key}`);
    const combined = (result.get(key) ?? 1) * gain;
    Iif (!Number.isFinite(combined)) throw new RangeError(`Combined WAV gain overflow for slot ${key}`);
    result.set(key, combined);
  }
  return result;
}
 
/**
 * Whether a chart's keysounds restart from the head when the same `#WAVxx` slot is retriggered (one voice per slot).
 *
 * BMS uses the LR2-compatible monophonic policy: a retrigger stops the still-playing instance of that slot and plays
 * it again from the start. bmson layers sample slices / `c: true` continuation instead, so its slots are polyphonic.
 * Every audio backend (Node mixer, WebAudio session, offline renderer) must consult this single predicate rather than
 * encoding the per-format rule locally. With `sampleKey`, a slot listed in `resources.wavSourceFormat` follows its own
 * format's rule (a BMS stage's slot in a NONSTOP chart stitched as bmson stays monophonic).
 */
export function usesMonophonicWAVPlayback(
  json: Pick<BeMusicJSON, 'sourceFormat'> & { resources?: Pick<BeMusicJSON['resources'], 'wavSourceFormat'> },
  sampleKey?: string,
): boolean {
  const slotFormat = sampleKey === undefined ? undefined : json.resources?.wavSourceFormat?.[sampleKey];
  return (slotFormat ?? json.sourceFormat) === 'bms';
}