All files / chart-writer/src wav-gain.ts

95.45% Statements 21/22
88.88% Branches 16/18
100% Functions 3/3
95% Lines 19/20

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        328x 10x 9x           127x 127x 5x 1x 4x 4x 4x 4x   4x 4x 4x 2x       2x   2x   2x 2x    
import { collectBmsWAVCmdVolumeMultipliers } from '@be-music/chart-model';
import { cloneJSON, createEmptyJSON, resolveBMSBase, type BeMusicJSON } from '@be-music/chart-source';
 
export function intrinsicWAVGains(json: BeMusicJSON): Array<[string, number]> {
  return Object.entries(json.resources.wavGain ?? {}).filter(([key, gain]) => {
    if (!Number.isFinite(gain) || gain < 0) throw new RangeError(`Invalid intrinsic WAV gain: ${key}`);
    return gain !== 1;
  });
}
 
/** Convert exactly representable slot gain into MacBeat percentage volume; never round audible gain. */
export function applyIntrinsicWAVGainForBMS(json: BeMusicJSON): BeMusicJSON {
  const gains = intrinsicWAVGains(json);
  if (!gains.length) return json;
  if (json.bms.controlFlow.length)
    throw new RangeError('Resolve BMS control flow before exporting intrinsic WAV gain.');
  const volumes = collectBmsWAVCmdVolumeMultipliers(json.bms.wavCmds, resolveBMSBase(json));
  const output = cloneJSON(json);
  for (const [key, gain] of gains) {
    Iif (!/^[0-9A-Za-z]{2}$/u.test(key))
      throw new RangeError('Intrinsic WAV gain slot cannot be represented by BMS; export Be-Music JSON.');
    const combined = (volumes.get(key) ?? 1) * gain;
    const percent = Math.round(combined * 100);
    if (!Number.isSafeInteger(percent) || percent / 100 !== combined) {
      throw new RangeError(
        'Intrinsic WAV gain cannot be represented exactly as BMS percentage volume; export Be-Music JSON.',
      );
    }
    output.bms.wavCmds.push(`01 ${key} ${percent}`);
  }
  delete output.resources.wavGain;
  // A new volume command must not be hidden by the preserved source, even when callers skip validation.
  output.preservation = createEmptyJSON(output.sourceFormat).preservation;
  return output;
}