All files / player-engine/src/core chain-audio.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 5/5
100% Lines 15/15

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                                                    3021x 3021x 265520x 265520x 265520x 7x   3021x   6x 8x   8x 8x 8x   6x       211x     2x        
import type { ResolvedChainSound } from '@be-music/chart-model';
import type { BeMusicEvent } from '@be-music/chart-source';
import { REALTIME_AUDIO_TRIGGER_EPSILON_SECONDS } from './engine-clock.ts';
 
/**
 * LRX chain notes as a play sounds them: each press that sounds a note queues the chain notes tied to it, each its
 * `delaySeconds` after the press, and the play fires them as its clock reaches them. Pressing the note again queues
 * them again, as plugout does (`plugout4.exe` 0x43bcd0).
 */
export interface ChainSoundQueue {
  /** Queues `sounds` after a press at chart second `seconds`, firing at once what is due then. */
  schedule(seconds: number, sounds: readonly ResolvedChainSound[]): void;
  /** Fires every queued sound due by chart second `seconds`, in order. */
  triggerUntil(seconds: number): void;
  /** Drops every queued sound (a seek or a restarted practice loop). */
  clear(): void;
  /** Nothing is queued. */
  readonly done: boolean;
}
 
interface QueuedChainSound {
  seconds: number;
  event: BeMusicEvent;
}
 
export function createChainSoundQueue(fire: (event: BeMusicEvent, seconds: number) => void): ChainSoundQueue {
  const queue: QueuedChainSound[] = [];
  const triggerUntil = (seconds: number): void => {
    let due = 0;
    while (due < queue.length && queue[due]!.seconds <= seconds + REALTIME_AUDIO_TRIGGER_EPSILON_SECONDS) due += 1;
    if (due === 0) return;
    for (const sound of queue.splice(0, due)) fire(sound.event, sound.seconds);
  };
  return {
    schedule(seconds, sounds) {
      for (const sound of sounds) {
        const entry = { seconds: seconds + sound.delaySeconds, event: sound.event };
        // Stable by time: a sound goes after every sound queued for the same moment.
        let index = queue.length;
        while (index > 0 && queue[index - 1]!.seconds > entry.seconds) index -= 1;
        queue.splice(index, 0, entry);
      }
      triggerUntil(seconds);
    },
    triggerUntil,
    clear() {
      queue.length = 0;
    },
    get done() {
      return queue.length === 0;
    },
  };
}