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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | 597x 38x 38x 38x 38x 64x 64x 61x 38x 38x 68x 38x 133x 4003x 4003x 4003x 4003x 4003x 4003x 240092x 240092x 34743x 205349x 26563x 178786x 1993x 176793x 176793x 4003x 4003x 4003x 28306x 4003x 61x 4003x 48x 4003x 4003x 18x 4003x 4003x 176793x 176793x 176789x 176784x 176784x 176793x 176793x 176793x 4003x 241x 241x 28115x 28115x 24028x 24028x 24012x 225x 287x 287x 287x 28345x 28345x 28345x 27484x 27484x 28345x 28345x 287x 287x 27484x 27484x 27484x 27484x 27484x 28341x 28341x 28341x 28341x 28341x 4x 1x 4x 28341x 28187x 28187x 28341x 28341x 28341x 28341x 28341x 28341x 28341x 28345x 28341x 287x | import {
normalizeChannel,
normalizeObjectKey,
resolveBMSBase,
type BeMusicEvent,
type BeMusicJSON,
} from '@be-music/chart-source';
import {
collectLNOBJEndEvents,
isLandmineChannel,
isSampleTriggerChannel,
resolveBMSLongNotes,
type BeatResolver,
} from './index.ts';
import { resolveJudgedNotes } from './resolved-notes.ts';
import { resolveChartPlayVariant } from './play-variant.ts';
import { resolveBmsWAVResources } from './wav-directives.ts';
import { resolveLRXSamplePlayback } from './lrx-playback.ts';
import {
createTimingBuildContext,
createTimingResolver,
type TimingBuildContext,
type TimingResolver,
} from './timing.ts';
export interface TimedSampleTrigger {
event: BeMusicEvent;
beat: number;
seconds: number;
channel: string;
sampleKey: string;
samplePath?: string;
sampleOffsetSeconds: number;
sampleDurationSeconds?: number;
sampleSliceId?: string;
/** This trigger's own linear gain (an LRX `vol`), on top of the slot's; 1 when absent. */
sampleGain?: number;
/** Stereo balance from −1 (left) to 1 (right) (an LRX `pan`); centre when absent. */
samplePan?: number;
/** Rate the sample plays at, on top of any other (an LRX `frq`); 1 when absent. */
samplePlaybackRate?: number;
/** Fade-in from the start of the window, in seconds of the sample (an LRX `atk`). */
sampleAttackSeconds?: number;
/** Fade-out past the end of `sampleDurationSeconds`, in seconds of the sample (an LRX `rls`). */
sampleReleaseSeconds?: number;
}
export interface CollectSampleTriggersOptions {
inferBmsLNTypeWhenMissing?: boolean;
}
const EMPTY_EVENT_SET = new Set<BeMusicEvent>();
/**
* The chart's effective WAV resources ({@link resolveBmsWAVResources}) that some sample event names: a BGM, note,
* invisible note or long-note object, whatever becomes of it in play. Slots only declared, or named only by landmines
* (whose value is damage) or by `#WAVCMD` / `#EXWAV` tweaks, are left out, so a player need not decode them.
*/
export function resolveReferencedBmsWAVResources(json: BeMusicJSON): Record<string, string> {
const base = resolveBMSBase(json);
const longKeys = json.sourceFormat === 'bmson';
const referenced = new Set<string>();
for (const event of json.events) {
const channel = normalizeChannel(event.channel);
if (!isSampleTriggerChannel(channel) || isLandmineChannel(channel)) continue;
referenced.add(normalizeObjectKey(event.value, base, longKeys));
}
const resources: Record<string, string> = {};
for (const [key, path] of Object.entries(resolveBmsWAVResources(json))) {
if (referenced.has(normalizeObjectKey(key, base, longKeys))) resources[key] = path;
}
return resources;
}
export function collectSampleTriggers(
json: BeMusicJSON,
resolver: TimingResolver = createTimingResolver(json),
options: CollectSampleTriggersOptions = {},
): TimedSampleTrigger[] {
return collectSampleTriggersWithContext(json, resolver, createTimingBuildContext(json), options);
}
export function collectSampleTriggersWithContext(
json: BeMusicJSON,
resolver: TimingResolver,
context: TimingBuildContext,
options: CollectSampleTriggersOptions = {},
): TimedSampleTrigger[] {
const { sortedEvents, beatResolver } = context;
const isBMSChart = json.sourceFormat === 'bms';
const lnobjEndEvents = isBMSChart ? collectLNOBJEndEvents(json) : EMPTY_EVENT_SET;
const suppressedBMSLongNoteEvents = isBMSChart
? resolveBMSLongNotes(json, {
inferLNTypeWhenMissing: options.inferBmsLNTypeWhenMissing === true,
}).suppressedTriggerEvents
: EMPTY_EVENT_SET;
const selectedEvents: Array<{ event: BeMusicEvent; normalizedChannel: string }> = [];
for (const event of sortedEvents) {
const normalizedChannel = normalizeChannel(event.channel);
if (!isSampleTriggerChannel(normalizedChannel) || isLandmineChannel(normalizedChannel)) {
continue;
}
if (lnobjEndEvents.has(event)) {
continue;
}
if (suppressedBMSLongNoteEvents.has(event)) {
continue;
}
selectedEvents.push({ event, normalizedChannel });
}
const events = selectedEvents.map((item) => item.event);
const bmsonPlaybackMap =
json.sourceFormat === 'bmson' ? createBmsonSamplePlaybackMap(json, resolver, events, beatResolver) : undefined;
const idBase = resolveBMSBase(json);
const wavResources = resolveBmsWAVResources(json);
// Keep every up entry in the slicing pass, but only an attached LN-end sample is audible on a playable lane. A note
// of another length stacked on a bmson note is dropped with its sound, so it is not audible either.
const hasUp = json.sourceFormat === 'bmson' && events.some((event) => event.bmson?.up === true);
const judged =
hasUp || (json.sourceFormat === 'bmson' && hasStackedLaneEvents(selectedEvents, beatResolver))
? resolveJudgedNotes({
json,
timing: resolver,
beatResolver,
sortedEvents,
idBase,
resolvePlayVariant: () => resolveChartPlayVariant(json),
inferBmsLNTypeWhenMissing: options.inferBmsLNTypeWhenMissing === true,
})
: undefined;
const endSounds = hasUp
? new Set(judged!.notes.flatMap((note) => (note.endSoundEvent ? [note.endSoundEvent] : [])))
: undefined;
const layeredEvents = new Set(judged?.notes.flatMap((note) => note.soundEvents ?? []));
const droppedEvents = new Set(
judged?.shadowedNotes.map((note) => note.event).filter((event) => !layeredEvents.has(event)),
);
const triggers: TimedSampleTrigger[] = [];
for (const item of selectedEvents) {
const event = item.event;
if (endSounds && item.normalizedChannel !== '01' && event.bmson?.up === true && !endSounds.has(event)) continue;
if (droppedEvents.has(event)) continue;
const sampleKey = normalizeObjectKey(event.value, idBase, json.sourceFormat === 'bmson');
const playback = bmsonPlaybackMap?.get(event);
const lrx = json.sourceFormat === 'lrx' ? resolveLRXSamplePlayback(event.lrx) : undefined;
const beat = beatResolver.eventToBeat(event);
triggers.push({
event,
beat,
seconds: resolver.beatToSeconds(beat),
channel: item.normalizedChannel,
sampleKey,
samplePath: wavResources[sampleKey],
sampleOffsetSeconds: playback?.offsetSeconds ?? lrx?.offsetSeconds ?? 0,
sampleDurationSeconds: playback?.durationSeconds ?? lrx?.durationSeconds,
sampleSliceId: playback?.sliceId,
...(lrx?.gain !== undefined ? { sampleGain: lrx.gain } : {}),
...(lrx?.pan !== undefined ? { samplePan: lrx.pan } : {}),
...(lrx?.playbackRate !== undefined ? { samplePlaybackRate: lrx.playbackRate } : {}),
...(lrx?.attackSeconds !== undefined ? { sampleAttackSeconds: lrx.attackSeconds } : {}),
...(lrx?.releaseSeconds !== undefined ? { sampleReleaseSeconds: lrx.releaseSeconds } : {}),
} satisfies TimedSampleTrigger);
}
return triggers;
}
/** Whether two playable-lane events share a lane and beat, the only case where a bmson note can be dropped. */
function hasStackedLaneEvents(
items: ReadonlyArray<{ event: BeMusicEvent; normalizedChannel: string }>,
beatResolver: BeatResolver,
): boolean {
const seen = new Set<string>();
for (const { event, normalizedChannel } of items) {
const side = normalizedChannel.charCodeAt(0);
if (side !== 0x31 && side !== 0x32) continue;
const key = `${normalizedChannel}:${beatResolver.eventToBeat(event)}`;
if (seen.has(key)) return true;
seen.add(key);
}
return false;
}
export function createBmsonSamplePlaybackMap(
json: BeMusicJSON,
resolver: TimingResolver,
sampleEvents: BeMusicEvent[],
beatResolver: BeatResolver,
): Map<BeMusicEvent, { offsetSeconds: number; durationSeconds?: number; sliceId: string }> {
const perSampleKey = new Map<
string,
Array<{ event: BeMusicEvent; beat: number; seconds: number; sampleKey: string }>
>();
const idBase = resolveBMSBase(json);
for (const event of sampleEvents) {
const sampleKey = normalizeObjectKey(event.value, idBase, json.sourceFormat === 'bmson');
let entries = perSampleKey.get(sampleKey);
if (!entries) {
entries = [];
perSampleKey.set(sampleKey, entries);
}
const beat = beatResolver.eventToBeat(event);
entries.push({
event,
beat,
seconds: resolver.beatToSeconds(beat),
sampleKey,
});
}
const playbackMap = new Map<BeMusicEvent, { offsetSeconds: number; durationSeconds?: number; sliceId: string }>();
for (const entries of perSampleKey.values()) {
// Hosts can concatenate playable and BGM lists; source order must not change slice boundaries.
entries.sort((left, right) => left.beat - right.beat);
let anchorSeconds = 0;
let hasAnchor = false;
let sliceIndex = 0;
for (let index = 0; index < entries.length;) {
const firstEntry = entries[index]!;
const currentBeat = firstEntry.beat;
let end = index + 1;
let shouldRestart = firstEntry.event.bmson?.c !== true;
while (end < entries.length && Math.abs(entries[end]!.beat - currentBeat) < 1e-9) {
if (entries[end]!.event.bmson?.c !== true) {
shouldRestart = true;
}
end += 1;
}
if (!hasAnchor || shouldRestart) {
anchorSeconds = firstEntry.seconds;
hasAnchor = true;
}
const offsetSeconds = Math.max(0, firstEntry.seconds - anchorSeconds);
// Every distinct pulse cuts the stem. `c` chooses the source offset, not whether the next note needs a trigger.
const boundary = entries[end];
const durationSeconds = boundary ? Math.max(0, boundary.seconds - firstEntry.seconds) : undefined;
const sliceId = `${firstEntry.sampleKey}:${sliceIndex}`;
sliceIndex += 1;
const playback = {
offsetSeconds: firstEntry.event.bmson?.sampleOffsetSeconds ?? offsetSeconds,
durationSeconds:
firstEntry.event.bmson?.sampleOffsetSeconds !== undefined
? firstEntry.event.bmson.sampleDurationSeconds
: durationSeconds,
sliceId,
};
for (let cursor = index; cursor < end; cursor += 1) {
playbackMap.set(entries[cursor]!.event, playback);
}
index = end;
}
}
return playbackMap;
}
|