All files / player-browser/src/session/play result-delay.ts

89.13% Statements 41/46
71.42% Branches 25/35
100% Functions 6/6
100% Lines 40/40

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                    49x 49x 49x 49x             9x   8x 8x 8x 12x 12x 12x 12x 12x       8x 8x 8x 8x                     3x 3x       3x 3x 3x                     6x 6x 4x 4x 4x   6x                     4x         4x 4x   4x   4x 4x 4x 4x   4x       4x       4x       24x    
import { resolveBmsWAVResources, type TimingResolver, type TimedSampleTrigger } from '@be-music/chart-model';
import { type BeMusicEvent, type BeMusicJSON, normalizeObjectKey, resolveBMSBase } from '@be-music/chart-source';
import type { TimedPlayableNote } from '@be-music/player-engine/display';
import {
  clampSampleDuration,
  clampSampleOffset,
  decodedSampleKey,
  type WebAudioSessionSlicePlayback,
} from '../../runtime/web-audio-session.ts';
 
const RESULT_DELAY_BEATS = 8;
const RESULT_DELAY_MIN_MS = 50;
const AUDIO_TAIL_PAD_MS = 500;
const AUDIO_TAIL_MAX_MS = 30_000;
 
export function resolvePostChartResultDelayMs(
  notes: ReadonlyArray<Pick<TimedPlayableNote, 'beat' | 'endBeat' | 'seconds' | 'endSeconds'>>,
  resolver: Pick<TimingResolver, 'beatToSeconds'> | undefined,
  currentSeconds: number,
): number {
  if (notes.length === 0 || resolver === undefined) return RESULT_DELAY_MIN_MS;
 
  let lastBeat = 0;
  let lastSeconds = 0;
  for (const note of notes) {
    const noteBeat = finiteOr(note.endBeat, note.beat);
    const noteSeconds = finiteOr(note.endSeconds, note.seconds);
    Eif (noteSeconds > lastSeconds || (Math.abs(noteSeconds - lastSeconds) < 1e-6 && noteBeat > lastBeat)) {
      lastBeat = noteBeat;
      lastSeconds = noteSeconds;
    }
  }
 
  const targetSeconds = resolver.beatToSeconds(lastBeat + RESULT_DELAY_BEATS);
  Iif (!Number.isFinite(targetSeconds)) return RESULT_DELAY_MIN_MS;
  const remainingMs = Math.round((targetSeconds - Math.max(currentSeconds, lastSeconds)) * 1000);
  return Math.max(RESULT_DELAY_MIN_MS, remainingMs);
}
 
export function resolveGameplayAudioTailCleanupDelayMs(options: {
  chart: BeMusicJSON;
  notes: ReadonlyArray<Pick<TimedPlayableNote, 'event' | 'seconds'>>;
  autoSampleTriggers: ReadonlyArray<Pick<TimedSampleTrigger, 'event' | 'seconds'>>;
  decodedSamples: ReadonlyMap<string, AudioBuffer>;
  bmsonSlicePlayback?: ReadonlyMap<BeMusicEvent, WebAudioSessionSlicePlayback>;
  currentSeconds: number;
}): number {
  const wavResources = resolveBmsWAVResources(options.chart);
  const latestEndSeconds = Math.max(
    resolveLatestSampleEndSeconds(options.notes, options, wavResources),
    resolveLatestSampleEndSeconds(options.autoSampleTriggers, options, wavResources),
  );
  Iif (!Number.isFinite(latestEndSeconds) || latestEndSeconds <= options.currentSeconds) return 0;
  const remainingMs = Math.ceil((latestEndSeconds - options.currentSeconds) * 1000) + AUDIO_TAIL_PAD_MS;
  return Math.max(0, Math.min(AUDIO_TAIL_MAX_MS, remainingMs));
}
 
function resolveLatestSampleEndSeconds(
  timedEvents: ReadonlyArray<{ event: BeMusicEvent; seconds: number }>,
  context: Pick<
    Parameters<typeof resolveGameplayAudioTailCleanupDelayMs>[0],
    'chart' | 'decodedSamples' | 'bmsonSlicePlayback'
  >,
  wavResources: Readonly<Record<string, string>>,
): number {
  let latest = 0;
  for (const timed of timedEvents) {
    const duration = resolveSamplePlaybackDurationSeconds(timed.event, context, wavResources);
    Iif (duration === undefined) continue;
    latest = Math.max(latest, timed.seconds + duration);
  }
  return latest;
}
 
function resolveSamplePlaybackDurationSeconds(
  event: BeMusicEvent,
  context: Pick<
    Parameters<typeof resolveGameplayAudioTailCleanupDelayMs>[0],
    'chart' | 'decodedSamples' | 'bmsonSlicePlayback'
  >,
  wavResources: Readonly<Record<string, string>>,
): number | undefined {
  const sampleKey = normalizeObjectKey(
    event.value,
    resolveBMSBase(context.chart),
    context.chart.sourceFormat === 'bmson',
  );
  const path = wavResources[sampleKey];
  Iif (typeof path !== 'string') return undefined;
  const buffer =
    context.decodedSamples.get(decodedSampleKey(path, sampleKey)) ??
    context.decodedSamples.get(normalizeSamplePath(path));
  Iif (buffer === undefined) return undefined;
  const slice = context.bmsonSlicePlayback?.get(event);
  const offsetSeconds = clampSampleOffset(slice?.offsetSeconds, buffer.duration);
  const durationSeconds = clampSampleDuration(slice?.durationSeconds, buffer.duration, offsetSeconds);
  const sampleSeconds =
    durationSeconds !== undefined
      ? Math.min(durationSeconds + (slice?.releaseSeconds ?? 0), buffer.duration - offsetSeconds)
      : Math.max(0, buffer.duration - offsetSeconds);
  // An LRX note's own rate plays its window faster or slower.
  return sampleSeconds / (slice?.rate !== undefined && slice.rate > 0 ? slice.rate : 1);
}
 
function normalizeSamplePath(path: string): string {
  return path.replace(/\\/g, '/').toLowerCase();
}
 
function finiteOr(primary: number | undefined, fallback: number): number {
  return typeof primary === 'number' && Number.isFinite(primary) ? primary : fallback;
}