feat: add independent full-band activity workflow

This commit is contained in:
2026-08-29 13:56:09 +09:00
parent 3ebc0f84e0
commit 51e346a477
8 changed files with 2176 additions and 50 deletions
+12
View File
@@ -191,6 +191,17 @@ export function createCameraAssetRuntime(onChange = () => {}) {
return statuses(scene).filter((status) => !status.ready).map(({ descriptor }) => descriptor.id);
}
function missingAudioAssetIds(scene) {
const audioIds = audioAssetIds(scene);
return statuses(scene)
.filter(
(status) =>
audioIds.has(status.descriptor.id) &&
!status.ready,
)
.map(({ descriptor }) => descriptor.id);
}
async function decodeAudioAssets(scene, signal, onProgress = () => {}) {
const ids = [...audioAssetIds(scene)];
const entries = [...scene.sources, ...(scene.ambient ?? [])];
@@ -248,6 +259,7 @@ export function createCameraAssetRuntime(onChange = () => {}) {
return imageElement;
},
missingAssetIds,
missingAudioAssetIds,
reset,
statuses,
transferListForAudio,
+42
View File
@@ -109,6 +109,48 @@
<div class="band-editor-list" id="band-list"></div>
</div>
<details class="activity-monitor utility-details" id="activity-monitor">
<summary><span>전 대역 활동 검사</span><small>위치추정과 분리 · ADC spectrum</small></summary>
<div class="activity-monitor__body">
<p class="activity-monitor__copy">Scene은 물리 마이크 신호 합성에만 사용하며 음원 위치·주파수·개수 정답은 검출기에 전달하지 않습니다. 위치추정과 독립적으로 ADC의 0…Fs/2만 관측하므로 analog alias order는 사용자가 지정해야 합니다.</p>
<div class="activity-field-grid">
<label class="field"><span>검출 알고리즘</span><select id="activity-algorithm"><option value="os-cfar">OS-CFAR</option><option value="ca-cfar">CA-CFAR</option><option value="dixon-spectral-flux">Dixon spectral flux</option></select></label>
<label class="field"><span>기준 마이크</span><select id="activity-microphone" disabled><option value="">마이크를 불러오세요</option></select></label>
</div>
<div class="activity-option-grid" id="activity-cfar-options">
<label class="field"><span>Nominal Pfa / tested CUT</span><input id="activity-pfa" type="number" min="0.000000001" max="0.999999999" step="0.001" value="0.001" /></label>
<label class="field"><span>Training cells / side</span><input id="activity-training" type="number" min="1" step="1" value="8" /></label>
<label class="field"><span>Guard cells / side</span><input id="activity-guard" type="number" min="0" step="1" value="2" /></label>
<label class="field"><span>OS rank k</span><input id="activity-rank" type="number" min="1" step="1" value="12" /></label>
</div>
<div class="activity-option-grid" id="activity-flux-options" hidden>
<label class="activity-checkbox field--wide"><input id="activity-peak-picker-enabled" type="checkbox" /><span>Böck–Krebs–Schedl causal peak picker 사용</span></label>
<label class="field"><span>Threshold δ</span><input id="activity-delta" type="number" min="0" step="0.01" placeholder="사용자 보정값" /></label>
<label class="field"><span>Pre-max frames</span><input id="activity-pre-max" type="number" min="0" step="1" value="3" /></label>
<label class="field"><span>Pre-mean frames</span><input id="activity-pre-mean" type="number" min="0" step="1" value="8" /></label>
<label class="field"><span>Refractory frames</span><input id="activity-refractory" type="number" min="0" step="1" value="3" /></label>
</div>
<p class="activity-monitor__note activity-monitor__note--cfar">OS/CA의 Pfa는 IID exponential 배경 모델에서 tested CUT 하나당 정한 nominal false-alarm 값입니다. 위치 확률·신뢰도·실측 보정값이 아닙니다.</p>
<p class="activity-monitor__note activity-monitor__note--flux">Spectral flux는 onset novelty이며 지속 activity가 아닙니다. δ는 사용자가 데이터에 맞춰 정하는 보정값입니다.</p>
<div class="activity-actions">
<button class="button button--primary" id="activity-run-button" type="button">전 대역 검사</button>
<button class="button button--ghost" id="activity-export-button" type="button" disabled>Activity JSON</button>
</div>
<p class="activity-status" id="activity-status" role="status" aria-live="polite">검사할 입력과 기준 마이크를 준비하세요.</p>
<label class="activity-frame-control">
<span id="activity-frame-label">Frame 0 / 0</span>
<input id="activity-frame" type="range" min="0" max="0" step="1" value="0" disabled />
</label>
<div class="activity-result-list" id="activity-result-list" role="list" aria-label="전 대역 활동 검사 결과" aria-live="polite">
<p class="activity-result-empty">검사 후 선택 frame의 원시 검출 통계와 candidate band를 표시합니다.</p>
</div>
</div>
</details>
<details class="advanced-settings" id="advanced-settings">
<summary><span>고급 설정</span><small>음향 · 범위 · STFT · 식별자</small></summary>
<div class="advanced-settings__body">
File diff suppressed because it is too large Load Diff
+227 -1
View File
@@ -5,6 +5,11 @@ import { localizationAlgorithmSupportsBackend } from "/packages/acoustics-core/l
import { computeLocalizationWithBackend } from "/packages/webgpu-kernels/compute.js";
import { isMusicUnavailableError } from "/packages/acoustics-core/music.js";
import { getCompleteFrameCount } from "/packages/acoustics-core/stft.js";
import { analyzeCfarActivity } from "/packages/acoustics-core/activity-monitor.js";
import {
computeDixonSpectralFlux,
pickCausalSpectralFluxPeaks,
} from "/packages/acoustics-core/spectral-flux.js";
function post(type, jobId, payload = {}, transfer = []) {
self.postMessage({ type, jobId, ...payload }, transfer);
@@ -50,6 +55,219 @@ function collectBandTransfers(run, transfer) {
}
}
function collectTypedArrayTransfers(value, transfer, seen = new Set(), buffers = new Set()) {
if (value === null || typeof value !== "object" || seen.has(value)) return;
seen.add(value);
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
if (!buffers.has(value.buffer)) {
buffers.add(value.buffer);
transfer.push(value.buffer);
}
return;
}
if (Array.isArray(value)) {
for (const item of value) collectTypedArrayTransfers(item, transfer, seen, buffers);
return;
}
for (const item of Object.values(value)) {
collectTypedArrayTransfers(item, transfer, seen, buffers);
}
}
function activitySignalStats(synthesis, scene) {
const channel = synthesis.channels[0];
let selectedChannelPeak = 0;
for (const sample of channel) {
selectedChannelPeak = Math.max(selectedChannelPeak, Math.abs(sample));
}
const workingSetWithoutSourceIdentity = {
model: synthesis.workingSet.model,
channelBytes: synthesis.workingSet.channelBytes,
maximumTransientBytes: synthesis.workingSet.maximumTransientBytes,
maximumTransientScope: synthesis.workingSet.maximumTransientScope,
pathSummary: synthesis.workingSet.pathSummary,
summaryPathPairEvaluations:
synthesis.workingSet.summaryPathPairEvaluations,
synthesisPathBuildPairEvaluations:
synthesis.workingSet.synthesisPathBuildPairEvaluations,
retainedPathObjects: synthesis.workingSet.retainedPathObjects,
remainingNonToneTransformModel:
synthesis.workingSet.remainingNonToneTransformModel,
};
const windowingWithoutSourceTruth = {
model: synthesis.windowing.model,
renderStartSample: synthesis.windowing.renderStartSample,
outputStartSample: synthesis.windowing.outputStartSample,
outputSampleCount: synthesis.windowing.outputSampleCount,
preRollSamples: synthesis.windowing.preRollSamples,
renderMaximumPropagationDelaySeconds:
synthesis.windowing.renderMaximumPropagationDelaySeconds,
activePathMaximumDelaySeconds:
synthesis.windowing.activePathMaximumDelaySeconds,
requestedMaximumPropagationDelaySeconds:
synthesis.windowing.requestedMaximumPropagationDelaySeconds,
geometryModel: synthesis.windowing.geometryModel,
sourcePositionSampleTime: synthesis.windowing.sourcePositionSampleTime,
dopplerMode: synthesis.windowing.dopplerMode,
delayedTailPolicy: synthesis.windowing.delayedTailPolicy,
arrivalOverlapModel: synthesis.windowing.arrivalOverlapModel,
};
const peakNormalization = scene.mix?.normalization === "peak";
return {
preNormalizationPeak: synthesis.preNormalizationPeak,
appliedScale: synthesis.appliedScale,
clippedSampleCount: synthesis.clippedSampleCount,
selectedChannelPeak,
acousticModel: synthesis.acousticModel,
acousticPathCount: synthesis.acousticPathCount,
acousticPathCounts: synthesis.acousticPathCounts,
workingSet: workingSetWithoutSourceIdentity,
windowing: windowingWithoutSourceTruth,
preprocessing: {
configuredNormalization: scene.mix?.normalization ?? "none",
normalizationScope: peakNormalization
? "selected-physical-microphone-whole-render"
: "none",
futureDependentNormalization: peakNormalization,
detectorCausalAfterPreprocessing: true,
endToEndCausal: !peakNormalization,
},
samplingModel: "ideal-real-sampler-no-antialias-filter",
sampleRate: synthesis.sampleRate,
nyquistHz: synthesis.sampleRate / 2,
explicitSceneSourceIdentitiesAndArrivalsIncluded: false,
sourceDerivedAggregatePhysicalAuditIncluded: true,
};
}
async function runActivity({
jobId,
scene,
arrayDefinition,
experiment,
assetBuffers,
activityConfig,
}) {
if (!activityConfig || typeof activityConfig !== "object") {
throw new TypeError("activityConfig must be an object.");
}
const channelIndex = activityConfig.channelIndex;
if (
!Number.isInteger(channelIndex) ||
channelIndex < 0 ||
channelIndex >= arrayDefinition.microphones.length
) {
throw new RangeError("activityConfig.channelIndex is outside the microphone array.");
}
const microphone = arrayDefinition.microphones[channelIndex];
const totalStartedAt = performance.now();
post("progress", jobId, {
stage: "activity-signal",
message: "전 대역 감시용 마이크 수신 신호를 합성하고 있습니다…",
});
const signalStartedAt = performance.now();
const selectedMicrophoneArray = {
...arrayDefinition,
microphones: [microphone],
};
const synthesis = synthesizeMicrophoneSignals({
scene,
arrayDefinition: selectedMicrophoneArray,
assetBuffers,
minimumDistance: experiment.beamforming.minimumDistance,
});
const signalGeneration = performance.now() - signalStartedAt;
const samples = synthesis.channels[0];
const detectorStartedAt = performance.now();
let analysis;
let peakPicking = null;
if (
activityConfig.algorithm === "ca-cfar" ||
activityConfig.algorithm === "os-cfar"
) {
analysis = await analyzeCfarActivity({
samples,
sampleRate: synthesis.sampleRate,
fftSize: experiment.stft.fftSize,
hopSize: experiment.stft.hopSize,
window: experiment.stft.window,
channelId: microphone.id,
detector: activityConfig.algorithm,
trainingCellsPerSide: activityConfig.trainingCellsPerSide,
guardCellsPerSide: activityConfig.guardCellsPerSide,
nominalFalseAlarmProbability:
activityConfig.nominalFalseAlarmProbability,
...(activityConfig.algorithm === "os-cfar"
? { rank: activityConfig.rank }
: {}),
onProgress: (progress) => post("progress", jobId, {
stage: progress.stage,
progress,
}),
});
} else if (activityConfig.algorithm === "dixon-spectral-flux") {
post("progress", jobId, {
stage: "activity-spectral-flux",
message: "Dixon spectral flux 원시 onset novelty를 계산하고 있습니다…",
});
analysis = computeDixonSpectralFlux({
samples,
sampleRate: synthesis.sampleRate,
fftSize: experiment.stft.fftSize,
hopSize: experiment.stft.hopSize,
channelId: microphone.id,
maximumOutputBytes: activityConfig.maximumOutputBytes,
});
if (activityConfig.peakPicker) {
peakPicking = pickCausalSpectralFluxPeaks(
analysis.rawOdf,
activityConfig.peakPicker,
);
}
} else {
throw new RangeError(
'activityConfig.algorithm must be "ca-cfar", "os-cfar", or "dixon-spectral-flux".',
);
}
const detectorTime = performance.now() - detectorStartedAt;
const report = {
kind: "micarrsim.activity-worker-report/1",
algorithm: activityConfig.algorithm,
channel: {
index: channelIndex,
id: microphone.id,
position: [...microphone.position],
selection: "user-selected-single-physical-microphone",
microphoneFusion: "none",
},
input: {
sampleRate: synthesis.sampleRate,
sampleCount: samples.length,
fftSize: experiment.stft.fftSize,
hopSize: experiment.stft.hopSize,
experimentWindow: experiment.stft.window,
adcRangeHz: [0, synthesis.sampleRate / 2],
analogAliasOrderResolved: false,
sceneSourceTruthUsedByPhysicalSimulator: true,
sceneSourceTruthPassedToDetector: false,
synthesizedMicrophoneCount: 1,
},
signalStats: activitySignalStats(synthesis, scene),
analysis,
...(peakPicking ? { peakPicking } : {}),
timingMs: {
total: performance.now() - totalStartedAt,
signalGeneration,
detector: detectorTime,
},
};
const transfer = [];
collectTypedArrayTransfers(report, transfer);
post("activity-result", jobId, { activityReport: report }, transfer);
}
async function computeLocalizationOrUnavailable(options) {
try {
return await computeLocalizationWithBackend(options);
@@ -255,10 +473,18 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
}
self.addEventListener("message", async (event) => {
if (event.data?.type !== "run" && event.data?.type !== "run-timeline") return;
if (
event.data?.type !== "run" &&
event.data?.type !== "run-timeline" &&
event.data?.type !== "detect-activity"
) return;
const { jobId, scene, arrayDefinition, experiment, assetBuffers } = event.data;
const totalStartedAt = performance.now();
try {
if (event.data.type === "detect-activity") {
await runActivity(event.data);
return;
}
if (event.data.type === "run-timeline") {
await runTimeline(event.data);
return;
+202 -2
View File
@@ -300,6 +300,193 @@
.band-editor input[type="color"] { padding: 3px; }
.band-editor__remove { border: 0; border-radius: 7px; color: var(--danger); background: rgb(255 123 115 / 8%); cursor: pointer; }
.activity-monitor__body {
display: grid;
min-width: 0;
gap: 9px;
padding: 10px;
border-top: 1px solid var(--line);
}
.activity-monitor__copy,
.activity-monitor__note,
.activity-status,
.activity-result-empty {
margin: 0;
overflow-wrap: anywhere;
font-size: .6rem;
line-height: 1.5;
}
.activity-monitor__copy { color: var(--muted); }
.activity-field-grid,
.activity-option-grid,
.activity-actions {
display: grid;
min-width: 0;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 8px;
}
.activity-option-grid {
padding: 9px;
border: 1px solid var(--line);
border-radius: 9px;
background: #0b1714;
}
.activity-option-grid[hidden] { display: none; }
.activity-checkbox {
display: flex;
min-width: 0;
align-items: center;
gap: 7px;
color: var(--muted);
cursor: pointer;
font-size: .61rem;
font-weight: 650;
}
.activity-checkbox input {
width: 16px;
height: 16px;
flex: 0 0 auto;
margin: 0;
accent-color: var(--mint);
}
.activity-monitor__note {
padding: 8px 9px;
border-left: 2px solid rgb(88 199 232 / 45%);
border-radius: 0 7px 7px 0;
color: var(--muted);
background: rgb(88 199 232 / 4%);
}
.activity-actions { margin-top: 1px; }
.activity-status {
min-width: 0;
padding: 7px 8px;
border-radius: 7px;
color: var(--muted);
background: #0b1714;
}
.activity-frame-control {
display: grid;
min-width: 0;
gap: 5px;
color: var(--muted);
font-size: .59rem;
font-weight: 650;
}
.activity-frame-control input { width: 100%; min-width: 0; accent-color: var(--cyan); }
.activity-result-list {
display: grid;
overflow-y: auto;
min-width: 0;
min-height: 140px;
max-height: min(52vh, 520px);
gap: 6px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 8px;
background: #07110f;
overscroll-behavior: contain;
scrollbar-width: thin;
resize: vertical;
}
.activity-result-empty { color: var(--muted); text-align: center; }
.activity-result-item {
min-width: 0;
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 7px;
background: #0b1714;
overflow-wrap: anywhere;
}
.activity-message,
.activity-caveat,
.activity-frame-summary,
.activity-empty,
.activity-event-selected {
margin: 0;
padding: 7px 8px;
border-radius: 7px;
overflow-wrap: anywhere;
font-size: .6rem;
line-height: 1.5;
}
.activity-message,
.activity-frame-summary {
color: var(--text);
background: #0b1714;
}
.activity-message.is-stale {
color: var(--amber);
border: 1px solid rgb(255 197 110 / 24%);
}
.activity-caveat {
color: var(--muted);
border-left: 2px solid rgb(88 199 255 / 42%);
background: rgb(88 199 255 / 5%);
}
.activity-empty { color: var(--muted); text-align: center; }
.activity-event-selected {
color: var(--mint);
border: 1px solid rgb(101 242 191 / 24%);
background: rgb(101 242 191 / 5%);
}
.activity-result-row {
display: grid;
min-width: 0;
gap: 5px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 8px;
background: #0b1714;
}
.activity-result-row > strong {
min-width: 0;
overflow-wrap: anywhere;
color: var(--text);
font-size: .64rem;
}
.activity-result-row > span {
min-width: 0;
overflow-wrap: anywhere;
color: var(--muted);
font: 550 .57rem/1.45 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.activity-hypothesis {
display: grid;
min-width: 0;
grid-template-columns: minmax(0, 1fr) auto;
gap: 5px;
}
.activity-hypothesis > span {
grid-column: 1 / -1;
color: var(--muted);
font-size: .56rem;
}
.activity-hypothesis input {
width: 100%;
min-width: 0;
min-height: 32px;
padding: 6px 7px;
border: 1px solid var(--line);
border-radius: 7px;
color: var(--text);
background: #07110f;
font-size: .62rem;
}
.activity-event-button {
width: 100%;
min-width: 0;
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 7px;
color: var(--text);
background: #0b1714;
cursor: pointer;
overflow-wrap: anywhere;
text-align: left;
font: 550 .59rem/1.45 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.activity-event-button:hover { border-color: var(--cyan); }
details > summary { list-style: none; }
details > summary::-webkit-details-marker { display: none; }
.inline-details, .advanced-settings, .utility-details, .mode-help {
@@ -470,22 +657,35 @@ html[data-run-state="running"] #snapshot-mode { border-color: var(--mint); box-s
.header-status { grid-column: 1 / -1; }
.run-status { max-width: 100%; }
.workflow-strip { top: 88px; }
.input-file-grid, .quick-field-grid, .algorithm-options, .advanced-field-grid, .output-actions, .band-editor, .live-controls { grid-template-columns: 1fr; }
.input-file-grid, .quick-field-grid, .algorithm-options, .advanced-field-grid, .output-actions, .band-editor, .live-controls, .activity-field-grid, .activity-option-grid, .activity-actions { grid-template-columns: 1fr; }
.field--wide, .live-scrubber { grid-column: auto; }
.band-editor__remove { min-height: 32px; }
.live-actions .button { flex: 1 1 calc(50% - 4px); }
.metric-grid { grid-template-columns: 1fr 1fr; }
.overlay-toolbar { justify-content: flex-start; }
.activity-hypothesis { grid-template-columns: 1fr; }
.activity-hypothesis > span { grid-column: auto; }
}
@container camera-controls (max-width: 350px) {
.input-file-grid,
.quick-field-grid,
.advanced-field-grid,
.output-actions {
.output-actions,
.activity-field-grid,
.activity-option-grid,
.activity-actions {
grid-template-columns: minmax(0, 1fr);
}
.activity-hypothesis {
grid-template-columns: minmax(0, 1fr);
}
.activity-hypothesis > span {
grid-column: auto;
}
.field--wide {
grid-column: auto;
}
+103 -45
View File
@@ -4,7 +4,8 @@ const DIXON_2006_URL =
"https://www.dafx.de/paper-archive/2006/papers/p_133.pdf";
const BOECK_KREBS_SCHEDL_2012_URL =
"https://www.cp.jku.at/research/papers/Boeck_etal_ISMIR_2012.pdf";
const DEFAULT_MAXIMUM_OUTPUT_BYTES = 64 * 1024 * 1024;
export const DEFAULT_MAXIMUM_SPECTRAL_FLUX_OUTPUT_BYTES =
64 * 1024 * 1024;
function requireConfigurationObject(value, label) {
if (value === null || typeof value !== "object" || Array.isArray(value)) {
@@ -92,6 +93,90 @@ function checkedByteCount(elementCount, bytesPerElement, label) {
return byteCount;
}
export function estimateDixonSpectralFluxWorkload(options) {
const configuration = requireConfigurationObject(options, "options");
const sampleCount = requireNonNegativeSafeInteger(
requireOwn(configuration, "sampleCount"),
"sampleCount",
);
const fftSize = requirePowerOfTwo(
requireOwn(configuration, "fftSize"),
"fftSize",
);
const hopSize = requireNonNegativeSafeInteger(
requireOwn(configuration, "hopSize"),
"hopSize",
);
if (hopSize < 1 || hopSize > fftSize) {
throw new RangeError("hopSize must be between 1 and fftSize, inclusive.");
}
const maximumOutputBytes =
configuration.maximumOutputBytes ??
DEFAULT_MAXIMUM_SPECTRAL_FLUX_OUTPUT_BYTES;
if (!Number.isSafeInteger(maximumOutputBytes) || maximumOutputBytes <= 0) {
throw new RangeError("maximumOutputBytes must be a positive safe integer.");
}
const frameCount = sampleCount < fftSize
? 0
: 1 + Math.floor((sampleCount - fftSize) / hopSize);
const binCount = fftSize;
const positiveDifferenceElements =
checkedElementCount(frameCount, binCount);
const positiveDifferenceBytes = checkedByteCount(
positiveDifferenceElements,
Float64Array.BYTES_PER_ELEMENT,
"positiveDifferences",
);
const rawOdfBytes = checkedByteCount(
frameCount,
Float64Array.BYTES_PER_ELEMENT,
"rawOdf",
);
const frameTimingBytes = checkedByteCount(
frameCount * 3,
Float64Array.BYTES_PER_ELEMENT,
"frame timing",
);
const outputBytes =
positiveDifferenceBytes + rawOdfBytes + frameTimingBytes;
if (!Number.isSafeInteger(outputBytes)) {
throw new RangeError(
"spectral-flux output byte count exceeds safe integer range.",
);
}
if (outputBytes > maximumOutputBytes) {
throw new RangeError(
`spectral flux requires ${outputBytes.toLocaleString()} output bytes; ` +
`maximumOutputBytes is ${maximumOutputBytes.toLocaleString()}.`,
);
}
const workspaceElements = frameCount === 0 ? 0 : fftSize * 4;
const workspaceBytes = checkedByteCount(
workspaceElements,
Float64Array.BYTES_PER_ELEMENT,
"spectral-flux workspace",
);
const estimatedPeakTypedArrayBytes = outputBytes + workspaceBytes;
if (!Number.isSafeInteger(estimatedPeakTypedArrayBytes)) {
throw new RangeError(
"spectral-flux peak typed-array byte count exceeds safe integer range.",
);
}
return Object.freeze({
sampleCount,
frameCount,
binCount,
positiveDifferenceElements,
positiveDifferenceBytes,
rawOdfBytes,
frameTimingBytes,
outputBytes,
maximumOutputBytes,
workspaceBytes,
estimatedPeakTypedArrayBytes,
});
}
/**
* Compute Simon Dixon's 2006 magnitude spectral-flux onset detection function.
*
@@ -123,51 +208,24 @@ export function computeDixonSpectralFlux(options) {
if (typeof channelId !== "string" || channelId.length === 0) {
throw new TypeError("channelId must be a non-empty string.");
}
const maximumOutputBytes = configuration.maximumOutputBytes ?? DEFAULT_MAXIMUM_OUTPUT_BYTES;
if (!Number.isSafeInteger(maximumOutputBytes) || maximumOutputBytes <= 0) {
throw new RangeError("maximumOutputBytes must be a positive safe integer.");
}
const frameCount = samples.length < fftSize
? 0
: 1 + Math.floor((samples.length - fftSize) / hopSize);
const binCount = fftSize;
const positiveDifferenceElements = checkedElementCount(frameCount, binCount);
const positiveDifferenceBytes = checkedByteCount(
positiveDifferenceElements,
Float64Array.BYTES_PER_ELEMENT,
"positiveDifferences",
);
const rawOdfBytes = checkedByteCount(
const workload = estimateDixonSpectralFluxWorkload({
sampleCount: samples.length,
fftSize,
hopSize,
maximumOutputBytes: configuration.maximumOutputBytes,
});
const {
frameCount,
Float64Array.BYTES_PER_ELEMENT,
"rawOdf",
);
const frameTimingBytes = checkedByteCount(
frameCount * 3,
Float64Array.BYTES_PER_ELEMENT,
"frame timing",
);
const outputBytes = positiveDifferenceBytes + rawOdfBytes + frameTimingBytes;
if (!Number.isSafeInteger(outputBytes)) {
throw new RangeError("spectral-flux output byte count exceeds safe integer range.");
}
if (outputBytes > maximumOutputBytes) {
throw new RangeError(
`spectral flux requires ${outputBytes.toLocaleString()} output bytes; ` +
`maximumOutputBytes is ${maximumOutputBytes.toLocaleString()}.`,
);
}
const workspaceElements = frameCount === 0 ? 0 : fftSize * 4;
const workspaceBytes = checkedByteCount(
workspaceElements,
Float64Array.BYTES_PER_ELEMENT,
"spectral-flux workspace",
);
const estimatedPeakTypedArrayBytes = outputBytes + workspaceBytes;
if (!Number.isSafeInteger(estimatedPeakTypedArrayBytes)) {
throw new RangeError("spectral-flux peak typed-array byte count exceeds safe integer range.");
}
binCount,
positiveDifferenceElements,
positiveDifferenceBytes,
rawOdfBytes,
frameTimingBytes,
outputBytes,
maximumOutputBytes,
workspaceBytes,
estimatedPeakTypedArrayBytes,
} = workload;
const positiveDifferences = new Float64Array(positiveDifferenceElements);
const rawOdf = new Float64Array(frameCount);
+576
View File
@@ -853,3 +853,579 @@ test("Acoustic Camera prepares, scrubs, plays, and pauses the moving-source exam
frame.remove();
}
});
test("Acoustic Camera full-band Activity keeps raw ADC evidence separate from localization", async () => {
const frame = document.createElement("iframe");
frame.src = "/apps/acoustic-camera/?integration-test=activity-full-band-1";
frame.style.cssText =
"position:fixed;left:-20000px;top:0;width:1400px;height:1000px;border:0";
document.body.append(frame);
await new Promise((resolve, reject) => {
frame.addEventListener("load", resolve, { once: true });
frame.addEventListener(
"error",
() => reject(new Error("Acoustic Camera Activity iframe load failed")),
{ once: true },
);
});
const windowObject = frame.contentWindow;
const documentObject = frame.contentDocument;
const downloads = [];
const objectUrls = [];
let confirmCount = 0;
const originalAnchorClick = windowObject.HTMLAnchorElement.prototype.click;
const originalRevoke = windowObject.URL.revokeObjectURL.bind(windowObject.URL);
const originalConfirm = windowObject.confirm.bind(windowObject);
windowObject.HTMLAnchorElement.prototype.click = function captureDownload() {
downloads.push({ fileName: this.download, url: this.href });
};
windowObject.URL.revokeObjectURL = (url) => objectUrls.push(url);
windowObject.confirm = () => {
confirmCount += 1;
return true;
};
try {
await waitFor(
() => documentObject.documentElement.dataset.appReady === "true",
"Acoustic Camera Activity initialization",
);
await waitFor(
() => documentObject.documentElement.dataset.cameraValid === "true",
"Acoustic Camera Activity initial validation",
);
assert(
documentObject.querySelector("#activity-export-button").disabled,
"Activity export starts disabled without a report",
);
assert(
documentObject.querySelector("#activity-frame").disabled,
"Activity frame scrubber starts disabled without a report",
);
documentObject.querySelector("#load-music-example-button").click();
await waitFor(
() =>
documentObject.documentElement.dataset.inputBusy === "false" &&
documentObject.documentElement.dataset.cameraValid === "true" &&
documentObject.querySelector("#algorithm").value === "music" &&
documentObject.querySelector("#microphone-count").textContent === "12 ch",
"Activity quick MUSIC example load",
);
const sceneWithMissingBackground = await loadJson(
"/assets/sample-scenes/music-single-tone.scene.json",
);
sceneWithMissingBackground.mix.normalization = "peak";
sceneWithMissingBackground.assets = [
{
id: "unbound-background",
kind: "image",
fileName: "unbound-background.png",
mimeType: "image/png",
},
];
sceneWithMissingBackground.image = {
assetId: "unbound-background",
widthPixels: 640,
heightPixels: 480,
plane: {
z: 2,
xMin: -1,
xMax: 1,
yMin: -0.75,
yMax: 0.75,
},
};
dispatchFiles(
windowObject,
documentObject.querySelector("#scene-input"),
[
new windowObject.File(
[JSON.stringify(sceneWithMissingBackground)],
"music-with-unbound-background.scene.json",
{ type: "application/json" },
),
],
);
await waitFor(
() =>
documentObject.documentElement.dataset.inputBusy === "false" &&
documentObject.documentElement.dataset.cameraValid === "false" &&
documentObject.querySelector("#validation-message").textContent.includes(
"unbound-background",
) &&
!documentObject.querySelector("#activity-run-button").disabled,
"missing background image does not block audio Activity analysis",
);
const activityMonitor = documentObject.querySelector("#activity-monitor");
activityMonitor.open = true;
assertEqual(
documentObject.querySelector("#activity-algorithm").value,
"os-cfar",
"default full-band detector is OS-CFAR",
);
assert(
activityMonitor.querySelector(".activity-monitor__copy").textContent.includes(
"Scene",
) &&
activityMonitor.querySelector(".activity-monitor__copy").textContent.includes(
"검출기에 전달하지",
) &&
activityMonitor.querySelector(".activity-monitor__copy").textContent.includes(
"ADC",
) &&
activityMonitor.querySelector(".activity-monitor__copy").textContent.includes(
"alias order",
),
"Activity copy separates ADC observation from Scene truth and analog alias order",
);
const musicBandMaximum = documentObject.querySelector(
'#band-list .band-editor input[data-band-key="maxHz"]',
);
const originalMusicBandMaximum = musicBandMaximum.valueAsNumber;
dispatchValue(
windowObject,
musicBandMaximum,
originalMusicBandMaximum + 2 * (48000 / 512),
);
await waitFor(
() =>
documentObject.documentElement.dataset.cameraValid === "false" &&
documentObject.querySelector("#run-button").disabled &&
!documentObject.querySelector("#activity-run-button").disabled,
"Activity remains ready when an unused MUSIC band is invalid",
);
documentObject.querySelector("#activity-run-button").click();
await waitFor(
() =>
documentObject.documentElement.dataset.activityState === "done" &&
documentObject.documentElement.dataset.activityAlgorithm === "os-cfar",
"default OS-CFAR Activity run",
45000,
);
dispatchValue(
windowObject,
documentObject.querySelector(
'#band-list .band-editor input[data-band-key="maxHz"]',
),
originalMusicBandMaximum,
);
await waitFor(
() =>
documentObject.documentElement.dataset.cameraValid === "false" &&
documentObject.documentElement.dataset.activityState === "done" &&
documentObject.querySelector("#validation-message").textContent.includes(
"unbound-background",
),
"restore localization-only MUSIC band after independent Activity run",
);
assertEqual(
documentObject.documentElement.dataset.runState,
"idle",
"Activity run does not create a localization result",
);
assert(
documentObject.querySelector("#export-result-button").disabled,
"localization Result export remains unavailable",
);
const caveatText =
documentObject.querySelector("#activity-result-list .activity-caveat")
?.textContent ?? "";
assert(
caveatText.includes("IID exponential") &&
caveatText.includes("tested CUT") &&
caveatText.includes("confidence") &&
caveatText.includes("frame 전체"),
"UI states the nominal per-CUT statistical limits",
);
const activityResultText =
documentObject.querySelector("#activity-result-list").textContent;
assert(
documentObject.querySelector("#activity-frame-label").textContent.includes(
"start",
) &&
documentObject.querySelector("#activity-frame-label").textContent.includes(
"available",
),
"Activity frame distinguishes descriptive start from causal availability",
);
assert(
activityResultText.includes("CFAR tested 범위") &&
activityResultText.includes("full reference window") &&
activityResultText.includes("untested"),
"UI discloses the actual CFAR coverage and untouched edge bins",
);
assert(
activityResultText.includes("end-to-end 전처리는 미래 sample에 의존"),
"UI discloses whole-render peak-normalization causality",
);
const toneHypothesisInput = await waitFor(
() => documentObject.querySelector(
'#activity-result-list input[data-activity-analog-hypothesis="true"]' +
'[data-activity-bin="12"]',
),
"detected 1125 Hz raw Activity row",
15000,
);
const toneRow = toneHypothesisInput.closest(".activity-result-row");
assert(
toneRow.querySelector("strong").textContent.includes("ADC bin 12") &&
toneRow.querySelector("strong").textContent.includes("1125"),
"detected tone is expressed as an observed ADC bin",
);
const rawText = [...toneRow.children]
.find((child) => child.tagName === "SPAN")?.textContent ?? "";
assert(
rawText.includes("CUT") &&
rawText.includes("ref") &&
rawText.includes("threshold") &&
rawText.includes("ratio"),
"detected row exposes raw CUT, reference, threshold, and ratio",
);
assertEqual(
toneHypothesisInput.valueAsNumber,
1125,
"analog hypothesis starts at the observed ADC center without claiming alias order",
);
dispatchValue(windowObject, toneHypothesisInput, 49125, "input");
toneRow.querySelector("button[data-add-activity-band]").click();
const addedBandEditor = await waitFor(
() => [...documentObject.querySelectorAll("#band-list .band-editor")]
.find((editor) =>
editor.querySelector('input[data-band-key="id"]')?.value ===
"activity-1"),
"above-Nyquist Activity hypothesis band addition",
);
const addedMinimumHz = Number(
addedBandEditor.querySelector('input[data-band-key="minHz"]').value,
);
const addedMaximumHz = Number(
addedBandEditor.querySelector('input[data-band-key="maxHz"]').value,
);
assert(
Math.abs((addedMinimumHz + addedMaximumHz) / 2 - 49125) <= 1e-9,
"explicit analog hypothesis center",
);
assert(
Math.abs(addedMaximumHz - addedMinimumHz - 48000 / 512) <= 1e-9,
"one FFT-cell candidate band width",
);
assert(
(addedMinimumHz + addedMaximumHz) / 2 > 48000 / 2,
"above-Nyquist analog hypothesis is retained",
);
await waitFor(
() =>
documentObject.documentElement.dataset.cameraValid === "false" &&
documentObject.documentElement.dataset.activityState === "done" &&
documentObject.querySelector("#validation-message").textContent.includes(
"unbound-background",
),
"above-Nyquist Activity band with unrelated missing image",
);
assert(
documentObject.querySelector("#run-status").textContent.includes("ADC bin 12") &&
documentObject.querySelector("#run-status").textContent.includes(
"다른 alias order",
),
"band addition labels ADC folding and keeps alias-order choice explicit",
);
const activityDownloadStart = downloads.length;
documentObject.querySelector("#activity-export-button").click();
const activityDownload = await waitFor(
() => downloads.slice(activityDownloadStart)
.find((download) => download.fileName.endsWith(".activity.json")),
"OS-CFAR Activity JSON download",
);
const activityDocument = await fetch(activityDownload.url).then(
(response) => response.json(),
);
assertEqual(activityDocument.kind, "micarrsim.activity-report", "Activity document kind");
assertEqual(activityDocument.provenanceOnlyIds, true, "Scene/Array/Experiment are provenance IDs");
assertEqual(
activityDocument.detectorSceneTruthInput,
false,
"outer Activity report denies Scene-truth detector input",
);
const workerReport = activityDocument.report;
const analysis = workerReport.analysis;
assertEqual(workerReport.algorithm, "os-cfar", "exported detector identity");
assertEqual(
workerReport.channel.selection,
"user-selected-single-physical-microphone",
"one explicit physical microphone",
);
assertEqual(workerReport.channel.microphoneFusion, "none", "no microphone fusion");
assertEqual(
workerReport.input.sceneSourceTruthPassedToDetector,
false,
"worker does not receive Scene source truth as detector input",
);
assertEqual(
workerReport.input.sceneSourceTruthUsedByPhysicalSimulator,
true,
"report distinguishes physical Scene synthesis from detector input",
);
assertEqual(
workerReport.input.synthesizedMicrophoneCount,
1,
"only the explicitly selected microphone is synthesized for Activity",
);
assertEqual(
workerReport.input.analogAliasOrderResolved,
false,
"worker reports ADC observation without resolving analog alias order",
);
assertEqual(
workerReport.input.adcRangeHz.join(","),
"0,24000",
"Activity export reports only the observable ADC range",
);
assertEqual(workerReport.localization, undefined, "worker report has no localization");
assertEqual(workerReport.scene, undefined, "worker report contains no Scene source list");
assertEqual(activityDocument.localization, undefined, "document has no localization payload");
assertEqual(
workerReport.signalStats.windowing.arrivingSourceIds,
undefined,
"Activity artifact omits arriving source identities",
);
assertEqual(
workerReport.signalStats
.explicitSceneSourceIdentitiesAndArrivalsIncluded,
false,
"report narrows its truth-free claim to explicit identities and arrivals",
);
assertEqual(
workerReport.signalStats.sourceDerivedAggregatePhysicalAuditIncluded,
true,
"report discloses aggregate source-derived physical audit statistics",
);
assertEqual(
workerReport.signalStats.preprocessing.normalizationScope,
"selected-physical-microphone-whole-render",
"peak normalization is scoped to the selected microphone",
);
assertEqual(
workerReport.signalStats.preprocessing.futureDependentNormalization,
true,
"whole-render peak normalization is marked future-dependent",
);
assertEqual(
workerReport.signalStats.preprocessing.endToEndCausal,
false,
"peak-normalized Activity pipeline does not claim end-to-end causality",
);
assertEqual(
workerReport.signalStats.windowing.sourceArrivals,
undefined,
"Activity artifact omits source-arrival truth",
);
assertEqual(
workerReport.signalStats.workingSet.maximumTransientId,
undefined,
"Activity artifact omits source-scoped working-set identity",
);
const model = analysis.detectorModel;
assertEqual(
model.parameters.nominalPfaPerTestedCutUnderIidExponentialModel,
0.001,
"nominal Pfa remains explicitly model-qualified",
);
assertEqual(
model.calibration.status,
"nominal-model-only-not-empirically-calibrated",
"nominal Pfa is not represented as empirical calibration",
);
assertEqual(
model.calibration.globalFrameFalseAlarmProbability,
null,
"no invented frame-global false-alarm probability",
);
assert(
model.calibration.notCalibratedReasons.includes(
"hann-window-bin-independence-is-not-guaranteed",
),
"Hann bin dependence limitation is exported",
);
assertEqual(model.processing.clustering, false, "no bin clustering");
assertEqual(model.processing.temporalPersistence, false, "no temporal persistence");
assertEqual(model.processing.detectorFusion, false, "no detector fusion");
assertEqual(model.processing.microphoneFusion, false, "no microphone fusion metadata");
assertEqual(model.processing.bandFusion, false, "no band fusion");
const expectedFrameCount =
1 + Math.floor(
(workerReport.input.sampleCount - workerReport.input.fftSize) /
workerReport.input.hopSize,
);
const expectedBinCount = workerReport.input.fftSize / 2 + 1;
assertEqual(analysis.frames.length, expectedFrameCount, "every complete frame retained");
assertEqual(analysis.summary.frameCount, expectedFrameCount, "frame summary is lossless");
let exportedBinCount = 0;
for (const exportedFrame of analysis.frames) {
assertEqual(exportedFrame.bins.length, expectedBinCount, "every ADC bin retained");
assertEqual(
exportedFrame.testedBinCount + exportedFrame.untestedBinCount,
expectedBinCount,
"tested and untested states cover a whole frame",
);
for (let binIndex = 0; binIndex < exportedFrame.bins.length; binIndex += 1) {
const bin = exportedFrame.bins[binIndex];
assertEqual(bin.adcBin, binIndex, "ADC bin order is preserved");
assert(Number.isFinite(bin.cutPower) && bin.cutPower >= 0, "raw CUT power is finite");
assert(
bin.status === "detected" ||
bin.status === "not-detected" ||
bin.status === "untested",
"every bin has an explicit decision state",
);
}
exportedBinCount += exportedFrame.bins.length;
}
assertEqual(
exportedBinCount,
expectedFrameCount * expectedBinCount,
"Activity JSON preserves the full frame/bin Cartesian product",
);
assertEqual(
analysis.summary.binRecordCount,
exportedBinCount,
"raw bin-record summary matches the payload",
);
assertEqual(analysis.frames[0].bins[0].reason, "real-only-dc-bin", "DC remains untested");
assertEqual(
analysis.frames[0].bins.at(-1).reason,
"real-only-nyquist-bin",
"Nyquist remains untested",
);
const exportedTone = analysis.frames
.map((exportedFrame) => exportedFrame.bins[12])
.find((bin) => bin.status === "detected");
assert(exportedTone, "the UI row comes from a retained raw detected-bin record");
assert(
Number.isFinite(exportedTone.referencePower) &&
Number.isFinite(exportedTone.thresholdPower),
"detected tone retains raw reference and threshold statistics",
);
assert(
exportedTone.thresholdRatio === null
? exportedTone.thresholdRatioStatus ===
"positive-infinity-zero-threshold"
: Number.isFinite(exportedTone.thresholdRatio) &&
exportedTone.thresholdRatio > 1,
"threshold ratio is raw and JSON-safe",
);
const experimentDownloadStart = downloads.length;
documentObject.querySelector("#export-experiment-button").click();
const experimentDownload = await waitFor(
() => downloads.slice(experimentDownloadStart)
.find((download) => download.fileName.endsWith(".experiment.json")),
"Activity hypothesis Experiment JSON download",
);
const experimentDocument = await fetch(experimentDownload.url).then(
(response) => response.json(),
);
const addedBand = experimentDocument.frequencyBands.find(
(band) => band.id === "activity-1",
);
assert(addedBand, "explicit Activity hypothesis is exported as an Experiment band");
assert(
Math.abs((addedBand.minHz + addedBand.maxHz) / 2 - 49125) <= 1e-9,
"exported analog center",
);
assert(
(addedBand.minHz + addedBand.maxHz) / 2 > workerReport.input.adcRangeHz[1],
"Experiment retains the above-Nyquist analog hypothesis",
);
const [musicArray, musicScene] = await Promise.all([
loadJson("/assets/array-geometries/rectangular-4x3-450mm.array.json"),
loadJson("/assets/sample-scenes/music-single-tone.scene.json"),
]);
assertEqual(
(await validateExperiment(experimentDocument, {
arrayDefinition: musicArray,
scene: musicScene,
})).length,
0,
"above-Nyquist explicit hypothesis remains a valid Experiment",
);
assertEqual(confirmCount, 0, "saved Activity report is replaced without a dirty-report prompt");
dispatchValue(
windowObject,
documentObject.querySelector("#activity-algorithm"),
"dixon-spectral-flux",
);
assert(
!documentObject.querySelector("#activity-peak-picker-enabled").checked,
"spectral-flux peak picker remains opt-in",
);
await waitFor(
() =>
!documentObject.querySelector("#activity-run-button").disabled &&
!documentObject.querySelector("#activity-flux-options").hidden,
"raw-only spectral flux readiness",
);
documentObject.querySelector("#activity-run-button").click();
await waitFor(
() =>
documentObject.documentElement.dataset.activityState === "done" &&
documentObject.documentElement.dataset.activityAlgorithm ===
"dixon-spectral-flux",
"raw-only spectral flux Activity run",
45000,
);
const fluxText = documentObject.querySelector("#activity-result-list").textContent;
assert(
fluxText.includes("raw onset novelty") &&
fluxText.includes("probability") &&
fluxText.includes("Peak picker를 적용하지 않아"),
"spectral flux UI remains raw novelty without an invented event threshold",
);
const fluxDownloadStart = downloads.length;
documentObject.querySelector("#activity-export-button").click();
const fluxDownload = await waitFor(
() => downloads.slice(fluxDownloadStart)
.find((download) => download.fileName.endsWith(".activity.json")),
"raw spectral-flux Activity JSON download",
);
const fluxDocument = await fetch(fluxDownload.url).then(
(response) => response.json(),
);
assertEqual(
fluxDocument.report.algorithm,
"dixon-spectral-flux",
"raw spectral-flux identity",
);
assertEqual(
fluxDocument.report.peakPicking,
undefined,
"no implicit spectral-flux peak picker",
);
assertEqual(
fluxDocument.report.analysis.rawOdf.length,
fluxDocument.report.analysis.frameCount,
"all raw spectral-flux frame values retained",
);
assertEqual(
fluxDocument.report.analysis.positiveDifferences.length,
fluxDocument.report.analysis.frameCount *
fluxDocument.report.analysis.binCount,
"all raw per-frame/per-bin positive differences retained",
);
assertEqual(confirmCount, 0, "Activity exports clear report-dirty state");
} finally {
windowObject.confirm = originalConfirm;
windowObject.HTMLAnchorElement.prototype.click = originalAnchorClick;
windowObject.URL.revokeObjectURL = originalRevoke;
for (const url of objectUrls) originalRevoke(url);
frame.remove();
}
});
+14
View File
@@ -1,5 +1,6 @@
import {
computeDixonSpectralFlux,
estimateDixonSpectralFluxWorkload,
pickCausalSpectralFluxPeaks,
} from "/packages/acoustics-core/spectral-flux.js";
@@ -90,6 +91,19 @@ test("Dixon flux는 Hamming linear magnitude의 양의 차이를 full DFT에서
assertEqual(result.workload.positiveDifferenceBytes, 64, "raw bin bytes");
assertEqual(result.workload.outputBytes, 128, "total returned typed-array bytes");
assertEqual(result.workload.maximumOutputBytes, 128, "caller output limit");
const preflight = estimateDixonSpectralFluxWorkload({
sampleCount: 8,
fftSize: 4,
hopSize: 4,
maximumOutputBytes: 128,
});
assertEqual(preflight.frameCount, result.frameCount, "preflight frame count");
assertEqual(preflight.outputBytes, result.workload.outputBytes, "preflight output bytes");
assertEqual(
preflight.estimatedPeakTypedArrayBytes,
result.workload.estimatedPeakTypedArrayBytes,
"preflight peak typed-array bytes",
);
});
test("Dixon flux는 silence와 incomplete tail을 패딩 없이 처리한다", () => {