All files / player-browser/src/runtime wav-playback.ts

88.88% Statements 16/18
92.85% Branches 13/14
66.66% Functions 2/3
94.11% Lines 16/17

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                    8x 8x           4x 4x               8x 8x 8x 8x         2x 2x 2x 2x 1x 1x   1x   2x      
import { resolveBmsWAVPlayback, type BmsWAVPlaybackModifiers } from '@be-music/chart-model';
import { transformStereoPCM } from '@be-music/shared-utils/core';
 
/** Apply authored sample effects before any keep-pitch tempo stretch; each slot owns its resulting buffer. */
export function prepareWAVPlaybackBuffer(
  context: Pick<BaseAudioContext, 'createBuffer'>,
  buffer: AudioBuffer,
  modifiers: BmsWAVPlaybackModifiers,
  nativeSampleRate = buffer.sampleRate,
): AudioBuffer {
  const options = resolveBmsWAVPlayback(modifiers, nativeSampleRate);
  if (
    options.playbackRate === 1 &&
    options.leftGain === 1 &&
    options.rightGain === 1 &&
    options.maxDurationSeconds === undefined
  )
    return buffer;
  const pcm = transformStereoPCM(
    buffer.getChannelData(0),
    buffer.getChannelData(buffer.numberOfChannels > 1 ? 1 : 0),
    buffer.sampleRate,
    buffer.sampleRate,
    options,
  );
  // Web Audio cannot allocate zero frames. A zero-frame decoded input is represented by one silent frame.
  const output = context.createBuffer(2, Math.max(1, pcm.left.length), buffer.sampleRate);
  output.getChannelData(0).set(pcm.left);
  output.getChannelData(1).set(pcm.right);
  return output;
}
 
/** DecodeAudioData resamples to the host rate, so EXWAV frequency must use the encoded track's rate instead. */
export async function readNativeAudioSampleRate(bytes: Uint8Array): Promise<number> {
  const { Input, BufferSource, ALL_FORMATS } = await import('mediabunny');
  const input = new Input({ source: new BufferSource(bytes), formats: ALL_FORMATS });
  try {
    const track = await input.getPrimaryAudioTrack();
    const rate = await track?.getSampleRate();
    Iif (rate === undefined || !Number.isFinite(rate) || rate <= 0)
      throw new Error('Cannot determine native audio sample rate for EXWAV frequency');
    return rate;
  } finally {
    input.dispose();
  }
}