All files / player-engine/src/core extra-note-modifier.ts

100% Statements 75/75
100% Branches 41/41
100% Functions 14/14
100% Lines 60/60

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 113 114 115 116 117 118 119 120                        6627x                               3513x 3513x 19x 19x   132x 19x   3513x 3513x 56x 39x 56x 56x   19x 19x 141x 19x 19x 19x 16x 16x 16x   19x 19x 19x 19x 31x 15x 15x   31x 223x         31x 31x 136x 136x 136x 863x 136x 136x 850x 850x 36x 36x 36x 36x 36x                     36x       19x 18x 30x 23x 18x 30x   18x 48x 48x   30x 23x   18x       30x          
import {
  PLAY_MODE_LAYOUTS,
  type ResolvedChart,
  type ResolvedNote,
  type TimedSampleTrigger,
} from '@be-music/chart-model';
import { toTimedPlayableNote, type TimedLandmineNote, type TimedPlayableNote } from '../playable-notes.ts';
import { resolveModifierLanes } from './chart-modifier-timelines.ts';
import { withChartOverrides } from './resolved-chart-overrides.ts';
 
/** beatoraja 0.8.9 PlayerConfig: default 0 (disabled), integer range 0..100. */
export function clampExtraNoteDepth(value: number): number {
  return Number.isFinite(value) ? Math.max(0, Math.min(100, Math.trunc(value))) : 0;
}
 
/**
 * ExtraNoteModifier at 83574048, after LN/mine modification and before lane shuffling.
 * The source computes blank cells once per row: later depth iterations can overwrite a
 * converted note while removing its background sound. Preserve that behavior, including
 * the keyCount-1 WAV search and retaining the search offset rather than the chosen lane.
 * Source noteType is unused; every conversion remains a normal note.
 */
export function addExtraNotes(
  source: ResolvedChart,
  notes: TimedPlayableNote[],
  mines: readonly TimedLandmineNote[],
  options: { depth?: number; scratch?: boolean; laneModeExtension?: string } = {},
): { chart: ResolvedChart; changes: number } {
  const depth = clampExtraNoteDepth(options.depth ?? 0);
  if (depth === 0) return { chart: source, changes: 0 };
  const lanes = resolveModifierLanes(source, options.laneModeExtension);
  const scratch = new Set(
    (source.playVariant === '9' ? [] : PLAY_MODE_LAYOUTS[source.playVariant].lanes)
      .filter((lane) => lane.kind !== 'key')
      .map((lane) => lane.channel),
  );
  const rows = new Map<number, TimedSampleTrigger[]>();
  for (const trigger of source.sampleTriggers) {
    if (trigger.channel !== '01') continue;
    const row = rows.get(trigger.beat) ?? [];
    row.push(trigger);
    rows.set(trigger.beat, row);
  }
  const removed = new Set<TimedSampleTrigger>();
  const effective = new Map<string, { trigger: TimedSampleTrigger; note: ResolvedNote }>();
  const lastNote: Array<string | undefined> = lanes.map(() => undefined);
  const orderedNotes = [...notes].sort((a, b) => a.beat - b.beat);
  const occupied = new Map<number, Set<string>>();
  for (const note of [...notes, ...mines]) {
    const row = occupied.get(note.beat) ?? new Set<string>();
    row.add(note.channel);
    occupied.set(note.beat, row);
  }
  const heldUntil = new Map<string, number>();
  let noteIndex = 0;
  let lastOffset = 0;
  for (const [beat, background] of [...rows].sort((a, b) => a[0] - b[0])) {
    while (noteIndex < orderedNotes.length && orderedNotes[noteIndex]!.beat <= beat) {
      const note = orderedNotes[noteIndex++]!;
      if (note.endBeat !== undefined) heldUntil.set(note.channel, note.endBeat);
    }
    const blank = lanes.map(
      (lane) =>
        (options.scratch === true || !scratch.has(lane)) &&
        !occupied.get(beat)?.has(lane) &&
        (heldUntil.get(lane) ?? -Infinity) < beat,
    );
    let backgroundIndex = 0;
    for (let d = 0; d < depth && backgroundIndex < background.length; d++) {
      const trigger = background[backgroundIndex]!;
      let offset = lastOffset;
      for (let j = 1; j < lanes.length; j++, offset = (offset + 1) % lanes.length)
        if (lastNote[offset] === trigger.sampleKey) break;
      lastOffset = offset;
      for (let j = 0; j < lanes.length; j++) {
        const key = (offset + j) % lanes.length;
        if (!blank[key]) continue;
        const channel = lanes[key]!;
        lastNote[key] = trigger.sampleKey;
        removed.add(trigger);
        backgroundIndex++;
        effective.set(`${beat}:${channel}`, {
          trigger,
          note: {
            kind: 'tap',
            channel,
            beat,
            seconds: trigger.seconds,
            sampleKey: trigger.sampleKey,
            event: { ...trigger.event, channel },
          },
        });
        break;
      }
    }
  }
  if (removed.size === 0) return { chart: source, changes: 0 };
  const added = [...effective.values()];
  for (const { note } of added) notes.push(toTimedPlayableNote(note));
  notes.sort((a, b) => a.seconds - b.seconds || a.channel.localeCompare(b.channel));
  const converted = new Map(
    added.map(({ trigger, note }) => [trigger, { ...trigger, event: note.event, channel: note.channel }]),
  );
  const sampleTriggers = source.sampleTriggers.flatMap((trigger) => {
    const replacement = converted.get(trigger);
    return replacement ? [replacement] : removed.has(trigger) ? [] : [trigger];
  });
  const resolvedNotes = [...source.notes, ...added.map(({ note }) => note)].sort(
    (a, b) => a.beat - b.beat || a.channel.localeCompare(b.channel),
  );
  return {
    chart: withChartOverrides(source, {
      notes: resolvedNotes,
      sampleTriggers,
      lastNoteSeconds: added.reduce((last, { note }) => Math.max(last, note.seconds), source.lastNoteSeconds),
    }),
    changes: removed.size,
  };
}