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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 130x 3022x 3022x 2068x 2068x 954x 954x 954x 954x 3022x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 4x 4x 1x 3x 2x 2x 2x 1x 4x 4x 4x 3x 3x 1x 2x 2x 4x 4x 4x 2x 4x 6684x 6684x 6684x 351710x 259912x 91798x 164x 91634x 6684x 2888x 2255x 633x 633x 1613x 1613x 633x 633x 87590x 87590x 87590x 85977x 1613x 1613x 1613x 633x 1176x 89203x | import {
firesBeforeTempoReversal,
isPlayLaneSoundChannel,
usesMonophonicWAVPlayback,
type ResolvedChart,
type TimedSampleTrigger,
} from '@be-music/chart-model';
import { throwIfAborted } from '@be-music/shared-utils/core';
import { normalizeChannel, type BeMusicEvent } from '@be-music/chart-source';
import type { TimedPlayableNote } from '../playable-notes.ts';
import type { AudioSession, AudioSessionLoadProgress, PlayerOptions } from './engine-types.ts';
import { resolveOutputWriter } from './engine-trace.ts';
const DEBUG_ACTIVE_AUDIO_FALLBACK_SECONDS = 0.18;
interface RealtimeAudioTrigger {
event: BeMusicEvent;
seconds: number;
channel: string;
}
export async function createAudioSessionIfEnabled(
resolvedChart: ResolvedChart,
options: PlayerOptions,
mode: 'auto' | 'manual',
onLoadProgress?: (progress: AudioSessionLoadProgress) => void,
): Promise<AudioSession | undefined> {
throwIfAborted(options.signal);
if (options.audio === false || !options.createAudioSession) {
onLoadProgress?.({ ratio: 1, message: 'Audio disabled; skipping audio setup.' });
return undefined;
}
const session = await options.createAudioSession({
json: resolvedChart.source,
resolvedChart,
options,
mode,
onLoadProgress,
});
throwIfAborted(options.signal);
if (session) resolveOutputWriter(options)(`Audio backend: ${session.backendLabel}\n`);
onLoadProgress?.({ ratio: 1, message: session ? 'Audio ready.' : 'Audio unavailable.' });
return session;
}
interface DebugSampleWindow {
sampleKey: string;
startSeconds: number;
endSeconds: number;
label: string;
}
interface DebugActiveAudioState {
activeAudioFiles: string[];
activeAudioVoiceCount: number;
}
interface DebugActiveAudioEstimator {
resolve: (nowSeconds: number) => DebugActiveAudioState;
}
export async function createDebugActiveAudioEstimator(
resolved: ResolvedChart,
options: {
baseDir?: string;
signal?: AbortSignal;
loadSampleDurations?: PlayerOptions['loadAudioSampleDurations'];
} = {},
): Promise<DebugActiveAudioEstimator> {
throwIfAborted(options.signal);
const json = resolved.source;
const triggers = resolved.sampleTriggers;
const sampleDurationSecondsByKey =
(await options.loadSampleDurations?.(resolved, options)) ?? new Map<string, number>();
throwIfAborted(options.signal);
const windows: DebugSampleWindow[] = triggers
.map((trigger) => {
const startSeconds = Math.max(0, trigger.seconds);
const sampleSeconds =
typeof trigger.sampleDurationSeconds === 'number' && Number.isFinite(trigger.sampleDurationSeconds)
? Math.max(0, trigger.sampleDurationSeconds) + (trigger.sampleReleaseSeconds ?? 0)
: Math.max(
0,
(sampleDurationSecondsByKey.get(trigger.sampleKey) ?? DEBUG_ACTIVE_AUDIO_FALLBACK_SECONDS) -
Math.max(0, trigger.sampleOffsetSeconds),
);
// Seconds of the sample sound for 1 / rate seconds at a trigger's own rate (an LRX note's `frq`).
const rate =
trigger.samplePlaybackRate !== undefined && trigger.samplePlaybackRate > 0 ? trigger.samplePlaybackRate : 1;
const durationSeconds = sampleSeconds / rate;
return {
sampleKey: trigger.sampleKey,
startSeconds,
endSeconds: startSeconds + durationSeconds,
label: trigger.samplePath ?? trigger.sampleKey,
} satisfies DebugSampleWindow;
})
.filter((window) => window.endSeconds > window.startSeconds)
.sort((left, right) => left.startSeconds - right.startSeconds);
const latestBySampleKey = new Map<string, number>();
for (let index = 0; index < windows.length; index += 1) {
const window = windows[index]!;
Iif (!usesMonophonicWAVPlayback(json, window.sampleKey)) continue;
const previousIndex = latestBySampleKey.get(window.sampleKey);
Iif (previousIndex !== undefined) {
const previousWindow = windows[previousIndex]!;
if (window.startSeconds < previousWindow.endSeconds) {
previousWindow.endSeconds = window.startSeconds;
}
}
latestBySampleKey.set(window.sampleKey, index);
}
let active: DebugSampleWindow[] = [];
let nextIndex = 0;
let lastResolvedSeconds = Number.NEGATIVE_INFINITY;
const rebuildAt = (nowSeconds: number): void => {
active = [];
nextIndex = 0;
while (nextIndex < windows.length && windows[nextIndex]!.startSeconds <= nowSeconds) {
const window = windows[nextIndex]!;
nextIndex += 1;
Eif (window.endSeconds > nowSeconds) {
active.push(window);
}
}
};
return {
resolve: (nowSeconds: number): DebugActiveAudioState => {
const safeNowSeconds = Number.isFinite(nowSeconds) ? Math.max(0, nowSeconds) : 0;
if (safeNowSeconds < lastResolvedSeconds) {
rebuildAt(safeNowSeconds);
} else {
while (nextIndex < windows.length && windows[nextIndex]!.startSeconds <= safeNowSeconds) {
const window = windows[nextIndex]!;
nextIndex += 1;
if (window.endSeconds > safeNowSeconds) {
active.push(window);
}
}
}
lastResolvedSeconds = safeNowSeconds;
let writeIndex = 0;
for (let readIndex = 0; readIndex < active.length; readIndex += 1) {
const window = active[readIndex]!;
if (window.endSeconds <= safeNowSeconds) {
continue;
}
Iif (writeIndex !== readIndex) {
active[writeIndex] = window;
}
writeIndex += 1;
}
active.length = writeIndex;
const uniqueLabels = new Set<string>();
for (const window of active) {
uniqueLabels.add(window.label);
}
return {
activeAudioFiles: [...uniqueLabels],
activeAudioVoiceCount: active.length,
};
},
};
}
/**
* The chart's sample triggers on the channels `includeChannel` accepts, as realtime audio triggers. Under an LR2
* negative-BPM reversal (#134) only what fires before LR2's event pump freezes is kept: everything strictly before the
* reversal, and on its tick only what LR2 orders before the negative BPM (BGM, BGA) — see `firesBeforeTempoReversal`.
*/
export function collectRealtimeAudioTriggers(
resolved: ResolvedChart,
includeChannel: (channel: string) => boolean = () => true,
): Array<TimedSampleTrigger & RealtimeAudioTrigger> {
const filtered: Array<TimedSampleTrigger & RealtimeAudioTrigger> = [];
const { reversal } = resolved;
for (const trigger of resolved.sampleTriggers) {
if (!includeChannel(trigger.channel)) {
continue;
}
if (!firesBeforeTempoReversal(reversal, trigger.seconds, trigger.channel)) {
continue;
}
filtered.push({
...trigger,
seconds: Math.max(0, trigger.seconds),
channel: normalizeChannel(trigger.channel),
});
}
return filtered;
}
/**
* Adds the play-lane triggers of `shadowedNotes` to `target` (kept sorted by time). Notes are matched to triggers by
* channel, position and value rather than by identity, because a host-prepared chart may come from another resolution
* of the same chart than the one the engine collects triggers from.
*/
export function appendShadowedNoteTriggers(
target: Array<TimedSampleTrigger & RealtimeAudioTrigger>,
resolved: ResolvedChart,
shadowedNotes: readonly TimedPlayableNote[] | undefined,
): void {
if (!shadowedNotes || shadowedNotes.length === 0) {
return;
}
const pendingByKey = new Map<string, number>();
for (const note of shadowedNotes) {
const key = createEventMatchKey(note.event);
pendingByKey.set(key, (pendingByKey.get(key) ?? 0) + 1);
}
let appended = false;
for (const trigger of collectRealtimeAudioTriggers(resolved, isPlayLaneSoundChannel)) {
const key = createEventMatchKey(trigger.event);
const pending = pendingByKey.get(key) ?? 0;
if (pending === 0) {
continue;
}
pendingByKey.set(key, pending - 1);
target.push(trigger);
appended = true;
}
Eif (appended) {
target.sort((left, right) => left.seconds - right.seconds);
}
}
function createEventMatchKey(event: BeMusicEvent): string {
return `${normalizeChannel(event.channel)}:${event.measure}:${event.position[0]}/${event.position[1]}:${event.value}`;
}
|