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

100% Statements 36/36
100% Branches 46/46
100% Functions 5/5
100% Lines 29/29

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                                        171x 171x 43x 43x 25x 25x   43x   171x 37x 37x 23x 23x 9x   171x 28x 28x 8x 20x 20x 20x   20x   171x                         31x 1x 66x 1x 29x 31x 31x                    
import { resolveBMSBase, type BeMusicJSON } from '@be-music/chart-source';
import { parseBMSExWAV, parseBmsWAVCmd, resolveBmsExWAVDeclarations } from './wav-directives.ts';
 
/** Authored sample effects, before the host's tempo and output sample-rate conversion. */
export interface BmsWAVPlaybackModifiers {
  /** Semitones relative to the sample's original pitch. */
  semitones?: number;
  frequencyHz?: number;
  /** EXWAV `r`: multiplies the rate after `frequencyHz`, as nanasigroove does (`nanasi.exe` 0x4d0984-0x4d09b8). */
  rate?: number;
  pan?: number;
  /** Maximum sounding time after pitch conversion; absent means natural sample end. */
  durationSeconds?: number;
}
 
/**
 * Be-Music composes MacBeat pitch with nanasigroove frequency. Last command wins per slot/parameter.
 * Out-of-range values clamp to the documented dialect bounds. Zero duration restores the natural end.
 */
export function collectBmsWAVPlaybackModifiers(json: Pick<BeMusicJSON, 'bms'>): Map<string, BmsWAVPlaybackModifiers> {
  const result = new Map<string, BmsWAVPlaybackModifiers>();
  const entryFor = (key: string): BmsWAVPlaybackModifiers => {
    let entry = result.get(key);
    if (!entry) {
      entry = {};
      result.set(key, entry);
    }
    return entry;
  };
  for (const raw of json.bms.wavCmds) {
    const command = parseBmsWAVCmd(raw, resolveBMSBase(json));
    if (!command || command.param === 'volume') continue;
    const entry = entryFor(command.slot);
    if (command.param === 'pitch') entry.semitones = Math.max(0, Math.min(127, command.value)) - 60;
    else entry.durationSeconds = command.value > 0 ? command.value / 2000 : undefined;
  }
  for (const [key, raw] of Object.entries(resolveBmsExWAVDeclarations(json))) {
    const parsed = parseBMSExWAV(raw);
    if (!parsed || (parsed.pan === undefined && parsed.frequencyHz === undefined && parsed.rate === undefined))
      continue;
    const entry = entryFor(key);
    if (parsed.pan !== undefined) entry.pan = Math.max(-10000, Math.min(10000, parsed.pan));
    if (parsed.frequencyHz !== undefined) entry.frequencyHz = Math.max(100, Math.min(100000, parsed.frequencyHz));
    // nanasigroove reads a rate at or below 0 as 1 and caps it at 2 (`nanasi.exe` 0x4d070f-0x4d074f).
    if (parsed.rate !== undefined) entry.rate = parsed.rate <= 0 ? 1 : Math.min(2, parsed.rate);
  }
  return result;
}
 
/** Resolve native-rate pitch and DirectSound-style channel attenuation without crossfeeding stereo channels. */
export function resolveBmsWAVPlayback(
  modifiers: BmsWAVPlaybackModifiers,
  nativeSampleRate: number,
): {
  playbackRate: number;
  leftGain: number;
  rightGain: number;
  maxDurationSeconds?: number;
} {
  if (!Number.isFinite(nativeSampleRate) || nativeSampleRate <= 0)
    throw new RangeError('A positive native sample rate is required');
  if (Object.values(modifiers).some((value) => value !== undefined && !Number.isFinite(value)))
    throw new RangeError('Sample modifiers must be finite');
  const pan = modifiers.pan ?? 0;
  const attenuation = (value: number): number => (value >= 10000 ? 0 : 10 ** (-value / 2000));
  return {
    playbackRate:
      2 ** ((modifiers.semitones ?? 0) / 12) *
      (modifiers.frequencyHz === undefined ? 1 : modifiers.frequencyHz / nativeSampleRate) *
      (modifiers.rate ?? 1),
    leftGain: pan > 0 ? attenuation(pan) : 1,
    rightGain: pan < 0 ? attenuation(-pan) : 1,
    maxDurationSeconds: modifiers.durationSeconds,
  };
}