feat: compare localization algorithms in camera

This commit is contained in:
2026-08-28 20:57:38 +09:00
parent e8dac792d9
commit fe0f70ab11
6 changed files with 362 additions and 70 deletions
+60 -18
View File
@@ -1,6 +1,7 @@
import { synthesizeMicrophoneSignals } from "/packages/acoustics-core/propagation.js";
import { sampleQuasiStaticWindow } from "/packages/acoustics-core/trajectory.js";
import { computeDasWithBackend } from "/packages/webgpu-kernels/compute.js";
import { createLocalizationAnalysisPlan } from "/packages/acoustics-core/localization.js";
import { computeLocalizationWithBackend } from "/packages/webgpu-kernels/compute.js";
function post(type, jobId, payload = {}, transfer = []) {
self.postMessage({ type, jobId, ...payload }, transfer);
@@ -46,7 +47,20 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
const frames = [];
const transfer = [];
let signalGeneration = 0;
let beamforming = 0;
let localizationTime = 0;
const localizationScene = { speedOfSound: scene.speedOfSound };
const algorithm = experiment.beamforming.algorithm ?? "das";
const requestedBackend = experiment.beamforming.backend ?? "auto";
const reuseCpuPlan = algorithm === "gcc-phat" || requestedBackend === "cpu";
let localizationExperiment = experiment;
let analysisPlan = reuseCpuPlan
? createLocalizationAnalysisPlan({
sampleRate: scene.sampleRate,
arrayDefinition,
scene: localizationScene,
experiment: localizationExperiment,
})
: undefined;
post("progress", jobId, {
stage: "timeline-start",
@@ -82,13 +96,14 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
const frameSignalGeneration = performance.now() - signalStartedAt;
signalGeneration += frameSignalGeneration;
const beamformingStartedAt = performance.now();
const das = await computeDasWithBackend({
const localizationStartedAt = performance.now();
const localization = await computeLocalizationWithBackend({
channels: synthesis.channels,
sampleRate: synthesis.sampleRate,
arrayDefinition,
scene: synthesisWindow.scene,
experiment,
scene: localizationScene,
experiment: localizationExperiment,
...(analysisPlan ? { analysisPlan } : {}),
onFallback: ({ error }) =>
post("fallback", jobId, {
code: error.code,
@@ -105,9 +120,32 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
progress,
}),
});
const frameBeamforming = performance.now() - beamformingStartedAt;
beamforming += frameBeamforming;
collectBandTransfers(das, transfer);
const frameLocalization = performance.now() - localizationStartedAt;
localizationTime += frameLocalization;
collectBandTransfers(localization, transfer);
if (
!analysisPlan &&
localization.backend === "cpu" &&
localization.fallback?.from === "webgpu"
) {
// Auto has established that WebGPU is unavailable for this worker. Keep
// the first frame's honest fallback metadata, then avoid retrying the GPU
// and rebuilding CPU geometry/workspaces on every remaining live frame.
localizationExperiment = {
...experiment,
beamforming: {
...experiment.beamforming,
backend: "cpu",
},
};
analysisPlan = createLocalizationAnalysisPlan({
sampleRate: scene.sampleRate,
arrayDefinition,
scene: localizationScene,
experiment: localizationExperiment,
});
}
frames.push({
index: plannedFrame.index ?? frameIndex,
@@ -116,12 +154,13 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
sampleTime: synthesisWindow.sampleTime,
duration: synthesisWindow.duration,
run: {
...das,
...localization,
signalStats: signalStats(synthesis),
timingMs: {
total: performance.now() - frameStartedAt,
signalGeneration: frameSignalGeneration,
beamforming: frameBeamforming,
localization: frameLocalization,
beamforming: frameLocalization,
},
},
});
@@ -145,6 +184,7 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
{
timeline: {
backend: backends.size === 1 ? frames[0].run.backend : "mixed",
algorithm: experiment.beamforming.algorithm ?? "das",
requestedBackend: experiment.beamforming.backend ?? "auto",
duration: scene.duration,
frameCount: frames.length,
@@ -153,7 +193,8 @@ async function runTimeline({ jobId, scene, arrayDefinition, experiment, assetBuf
timingMs: {
total: performance.now() - totalStartedAt,
signalGeneration,
beamforming,
localization: localizationTime,
beamforming: localizationTime,
},
},
},
@@ -184,8 +225,8 @@ self.addEventListener("message", async (event) => {
message: `마이크 신호 합성 완료 · ${signalGeneration.toFixed(0)} ms`,
});
const beamformingStartedAt = performance.now();
const das = await computeDasWithBackend({
const localizationStartedAt = performance.now();
const localization = await computeLocalizationWithBackend({
channels: synthesis.channels,
sampleRate: synthesis.sampleRate,
arrayDefinition,
@@ -198,20 +239,21 @@ self.addEventListener("message", async (event) => {
}),
onProgress: (progress) => post("progress", jobId, { stage: progress.stage, progress }),
});
const beamforming = performance.now() - beamformingStartedAt;
const localizationTime = performance.now() - localizationStartedAt;
const transfer = [];
collectBandTransfers(das, transfer);
collectBandTransfers(localization, transfer);
post(
"result",
jobId,
{
run: {
...das,
...localization,
signalStats: signalStats(synthesis),
timingMs: {
total: performance.now() - totalStartedAt,
signalGeneration,
beamforming,
localization: localizationTime,
beamforming: localizationTime,
},
},
},