feat: add independent full-band activity workflow
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user