aqua-feel sync ramp: bound the start only from raw frames, never claim early or inside

A Soul pass falsified the first ramp detector: the +-60 ms centred average crossed
before the picture did (fast rises called detectably late inside the window), the
smoothing ran over samples (stretched on variable-frame-rate tracks), and the lower
bound was a heuristic presented as a bound (slow-start shapes called early or inside
while in sync). Now: time-based smoothing, hiS from the raw frames, and a ramp can
be audio-late-detectable or borderline only. Adds the Soul's fast-rise, VFR and a
120-case sweep over shapes, rises, decays, rates, noise and offsets as tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Aqua 2026-11-11 03:35:01 +00:00
commit 33af5110ed
3 changed files with 61 additions and 20 deletions

View file

@ -76,15 +76,17 @@ Some visuals answer a sound with a slow swell (a scene brightening over a few hu
(`step` found 0 of 5 hits on `fixtures/perlines-trails-ramp.webm`; `ramp` paired 3). `--shape ramp` takes the track's rise over 300 ms as the novelty and picks its peaks (at least 15% of the track's range, 6 noise sigmas,
at least 500 ms apart). For each it finds the quiet level before the rise (the last sample within 2 noise sigmas of the minimum of the 0.6 s before) and the 20% and 80% crossings of the rise to its top.
**What it claims.** The response started somewhere between the quiet level, backed off by the time from there to the 20% crossing and one frame, and the 20% crossing. The lag interval is that whole range against the audio onset,
so it is far wider than a step's (100 ms and more) and most verdicts are `borderline`. `audio-late-detectable` is sound whatever the ramp's shape (the picture started to move at the latest at the 20% crossing and the sound
still came later than the window); `audio-early-detectable` only if even the widest start estimate lies after the sound. The point value (`visual s`, `lag ms`) is the foot of the line through the 20% and 80% crossings, a
convention that fits a linear ramp and reads early for exponential approach and late for slow-start (smoothstep) shapes; it is not the instant anyone perceives.
**What it claims.** Only this, and only in one direction. The picture had started to change by the first frame whose raw value stood above the quiet level by 20% of the rise (and at least 4 noise sigmas): `hiS`. If the sound still came
later than the window after that frame, the verdict is `audio-late-detectable`; that claim does not depend on the ramp's shape. Everything else is `borderline`, always: **a ramp is never reported `inside-window` or `audio-early-detectable`,**
because the start of a swell cannot be bounded from below by looking at it (a slow start sits under the noise; the same sound can sit 0 to 300 ms after the real start). The report still prints a lag interval, from `hiS` back to the quiet
minimum backed off by the valley-to-crossing time and one frame; the back-off is a heuristic that held on the synthetic sweep, not a bound, and no verdict leans on it. The point value (`visual s`, `lag ms`) is the foot of the line through
the 20% and 80% crossings: it fits a linear ramp, reads early for exponential approach and late for slow-start shapes, and is not the instant anyone perceives. The synthetic sweep (`sync.test.mjs`, 120 shapes x rises 40..800 ms x decays x 24..120 fps x
noise x offsets) never produces a wrong ramp verdict; a Soul pass found two earlier versions that did (a centred 60 ms average crossing before the picture; the lower bound taken as a bound).
**What it cannot claim.** When the viewer sees the swell begin (that is later than the physical start and depends on contrast, size and attention). That the swell was caused by this sound: pairing is by nearness, and a bed that
swells on its own, a scene change or a second sound inside 500 ms gives a false pair. The back-off for a decaying tail is checked on linear, exponential and smoothstep ramps (250 to 450 ms to peak, decay over about a second), not proved:
a response that starts more than one valley-to-20% time before the quiet minimum would escape the interval. Ramps closer than 500 ms merge; ramps under 15% of the track's range, or on a steady drift that rises over the whole clip, are not found.
The 0.15 floor (0.25 missed 7 of 10 Vector trails hits and 0.10 admitted one false onset) and the other constants were set on that one clip.
**What it cannot claim.** That the sound is in sync with a swell (only that it is not detectably late). When the viewer sees the swell begin (later than the physical start; depends on contrast, size and attention). That the swell was caused by this sound: pairing
is by nearness to the foot, and a bed that swells on its own, a scene change or a second sound within 500 ms gives a false pair. Ramps closer than 500 ms merge (so the ambiguity guard, which fires on neighbours closer than twice the pairing window, cannot fire at the default 200 ms
window; the merged hits show up as unpaired audio). Ramps under 15% of the track's range, or on a steady drift that rises over the whole clip, are not found. `--kind flux` with `--shape ramp` means a swell of motion energy; it has not been tried on anything
but one fixture. The constants (300 ms rise window, 15% floor, 6 sigmas, 500 ms gap) were set on Perlines v2's Vector trails and the synthetic shapes only.
## Uncertainty

View file

@ -200,10 +200,11 @@ export function visualOnsets(track, { kind = "luma", k = 8, minGapS = 0.1 } = {}
// [valley - (valley to 20% time) - one frame, crossing20S]: a ramp has no first frame, so the onset carries that span and the verdict is read off it, not off the foot.
export function visualRampOnsets(track, { minGapS = 0.5, riseS = 0.3, k = 6 } = {}) {
const { t, v } = track, n = v.length, dts = []; for (let i = 1; i < n; i++) dts.push(t[i] - t[i - 1]);
const period = median(dts), w = Math.max(1, Math.round(0.06 / period));
const sm = new Float64Array(n); for (let i = 0; i < n; i++) { let a = 0, c = 0; for (let q = Math.max(0, i - w); q <= Math.min(n - 1, i + w); q++) { a += v[q]; c++; } sm[i] = a / c; }
const period = median(dts), at = (x) => { let lo = 0, hi = n - 1; while (lo < hi) { const m = (lo + hi) >> 1; if (t[m] < x) lo = m + 1; else hi = m; } return lo; };
// Moving average over +-60 ms of TIME (not of samples: a variable-frame-rate track must not stretch it), by prefix sums.
const cs = new Float64Array(n + 1); for (let i = 0; i < n; i++) cs[i + 1] = cs[i] + v[i];
const sm = new Float64Array(n); for (let i = 0; i < n; i++) { const a = at(t[i] - 0.06), b = at(t[i] + 0.06 + 1e-9); const hi = t[b] <= t[i] + 0.06 + 1e-9 ? b : b - 1; sm[i] = (cs[Math.max(hi, i) + 1] - cs[a]) / (Math.max(hi, i) + 1 - a); }
const range = Math.max(...v) - Math.min(...v), d1 = Float64Array.from(v.slice(1), (x, i) => Math.abs(x - v[i])), sigma = 1.4826 * median(d1) / Math.SQRT2 + 1e-12;
const at = (x) => { let lo = 0, hi = n - 1; while (lo < hi) { const m = (lo + hi) >> 1; if (t[m] < x) lo = m + 1; else hi = m; } return lo; };
const nov = new Float64Array(n); for (let i = 0; i < n; i++) nov[i] = Math.max(0, sm[Math.min(n - 1, at(t[i] + riseS))] - sm[i]);
const r = pickOnsets(nov, t, { k, floor: Math.max(0.15 * range, k * sigma), minGapS, riseFrac: 1 });
r.framePeriodMs = 1000 * period;
@ -212,9 +213,14 @@ export function visualRampOnsets(track, { minGapS = 0.5, riseS = 0.3, k = 6 } =
let m = p; while (m > b0 && sm[m] > mn + 2 * sigma) m--;
let top = m; for (let i = m; i < Math.min(n, at(o.peakT + riseS + 0.3)); i++) if (sm[i] > sm[top]) top = i;
const A = sm[top] - sm[m], cross = (f) => { let i = m; while (i < top && sm[i] < sm[m] + f * A) i++; return i; };
const i20 = cross(0.2), i80 = cross(0.8), foot = Math.min(t[i20], Math.max(t[m], t[i20] - (t[i80] - t[i20]) / 3));
// Over a decaying tail the minimum of the sum lands after the true start (a slow ramp cancels the decay before it turns the curve): back off by the valley-to-20% time.
return { t: foot, loS: t[m] - (t[i20] - t[m]) - period, hiS: t[i20], peakT: t[top], strength: A };
const i20 = cross(0.2), i80 = cross(0.8), foot0 = t[i20] - (t[i80] - t[i20]) / 3;
// The upper bound on the start comes from the raw samples, not the smoothed ones (a centred average crosses before the picture does): the first raw
// frame at or above the quiet level plus 20% of the rise, and no less than 4 noise sigmas, was stamped after the change began.
let hi = m; while (hi < n - 1 && v[hi] < sm[m] + Math.max(0.2 * A, 4 * sigma)) hi++;
const foot = Math.min(t[hi], Math.max(t[m], foot0));
// Over a decaying tail the minimum of the sum lands after the true start (a slow ramp cancels the decay before it turns the curve): the lower bound backs off
// by the valley-to-crossing time. It is a heuristic, not a bound (a slow start can begin much earlier), and the verdict does not lean on it.
return { t: foot, loS: t[m] - (t[hi] - t[m]) - period, hiS: t[hi], peakT: t[top], strength: A };
});
return r;
}
@ -247,7 +253,9 @@ export function syncAnalyze({ audio, sampleRate, audioStartS = 0, visual, visual
const a = audioOnsets(audio, sampleRate, { startS: audioStartS, ...gap }), v = visualShape === "ramp" ? visualRampOnsets(visual, gap.minGapS === undefined ? {} : { minGapS: gap.minGapS }) : visualOnsets(visual, { kind: visualKind, ...gap });
// A video resampled to constant frame rate by `fps` snaps each frame to the nearest source frame: half a period more slack on each side.
const hopMs = a.resolutionMs, periodMs = Math.max(v.framePeriodMs, visualGapMs), win = pairWindowMs / 1000, snapMs = visualResampled ? periodMs / 2 : 0;
const classify = (lo, hi) => {
const classify = (lo, hi, ramp) => {
// A ramp's start cannot be bounded below by looking at it (a slow start is under the noise), so a ramp is only ever shown late, never early or inside the window.
if (ramp) return lo > prof.lagMs ? "audio-late-detectable" : "borderline";
if (lo >= -prof.leadMs && hi <= prof.lagMs) return "inside-window";
if (lo > prof.lagMs) return "audio-late-detectable";
if (hi < -prof.leadMs) return "audio-early-detectable";
@ -261,7 +269,7 @@ export function syncAnalyze({ audio, sampleRate, audioStartS = 0, visual, visual
const hiT = vo.hiS ?? vo.t, loT = vo.loS ?? vo.t - periodMs / 1000;
r.lagLowMs = (ao.t - hiT) * 1e3 - hopMs / 2 - snapMs; r.lagHighMs = (ao.t - loT) * 1e3 + hopMs / 2 + snapMs;
}
r.verdict = r.lagMs === null ? (ao ? "no-visual-onset" : vo ? "no-audio-onset" : "no-onsets") : ambiguous ? "ambiguous-pairing" : classify(r.lagLowMs, r.lagHighMs);
r.verdict = r.lagMs === null ? (ao ? "no-visual-onset" : vo ? "no-audio-onset" : "no-onsets") : ambiguous ? "ambiguous-pairing" : classify(r.lagLowMs, r.lagHighMs, vo.hiS !== undefined);
return r;
};
if (events) {

View file

@ -212,13 +212,13 @@ t("sourceFrames: the rate is the median frame gap, the stale-frame bound is the
// Ramp-shaped responses: a brightening that starts at the event and takes hundreds of ms to peak, then decays over about a second; a drifting bed and noise.
// shapes: linear ramp, exponential approach (steepest at the start), smoothstep (slowest at the start). Truth is the instant the change begins.
const SHAPES = { linear: (u) => Math.min(1, u), exponential: (u) => 1 - Math.exp(-4 * Math.min(1, u)), smoothstep: (u) => { const x = Math.min(1, u); return x * x * (3 - 2 * x); } };
const rampFixture = ({ shape, rampS, fps, offsetMs = 40, events = [0.5042, 1.9117, 3.2209, 4.6033, 5.9161, 7.2087, 8.5], noise = 0.002 }) => {
const SHAPES = { linear: (u) => Math.min(1, u), exponential: (u) => 1 - Math.exp(-4 * Math.min(1, u)), smoothstep: (u) => { const x = Math.min(1, u); return x * x * (3 - 2 * x); }, quadratic: (u) => Math.min(1, u) ** 2 };
const rampFixture = ({ shape, rampS, fps, offsetMs = 40, events = [0.5042, 1.9117, 3.2209, 4.6033, 5.9161, 7.2087, 8.5], noise = 0.002, decayS = 0.8 }) => {
const fx = makeFixture({ offsetMs, fps, seconds: 10, events }), nf = Math.round(fps * 10), tt = new Float64Array(nf), vv = new Float64Array(nf); let s = 11;
const rand = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return s / 2 ** 32 - 0.5; };
for (let f = 0; f < nf; f++) {
const x = f / fps; let y = 0.1 + 0.005 * Math.sin(x * 0.7) + noise * 2 * rand();
for (const e of events) if (x >= e) { const u = (x - e) / rampS; y += 0.3 * (u < 1 ? SHAPES[shape](u) : Math.exp(-(u - 1) * rampS / 0.8)); }
for (const e of events) if (x >= e) { const u = (x - e) / rampS; y += 0.3 * (u < 1 ? SHAPES[shape](u) : Math.exp(-(u - 1) * rampS / decayS)); }
tt[f] = x; vv[f] = y;
}
return { ...fx, visual: { t: tt, v: vv }, events: events.map((e, i) => ({ t: e, label: `r${i}` })) };
@ -244,6 +244,37 @@ t("ramp: the interval is wide and says so (a ramp has no first frame); it is jud
const late = rampFixture({ shape: "linear", rampS: 0.3, fps: 60, offsetMs: 400 }), rl = syncAnalyze({ audio: late.audio, sampleRate: late.sampleRate, visual: late.visual, visualShape: "ramp", pairWindowMs: 450, events: late.events });
assert.ok(rl.pairs.every((p) => p.verdict === "audio-late-detectable"), "audio 400 ms after the picture starts to move is outside whatever the ramp's shape");
});
// A ramp verdict may be silent but not wrong. Soul found (a) a centred 60 ms average crosses before a fast picture does, so 'late' was claimed at true lags inside the window,
// and (b) slow-start shapes begin far before they look like they do, so 'early' or 'inside' was claimed at true lags of 0 to 40 ms. Truth is known in every case.
t("ramp: a fast rise or a step is not called detectably late when the sound is inside the window", () => {
for (const [rampS, off] of [[0.04, 98], [0.04, 60], [0.001, 60], [0.08, 90]]) for (const fps of [30, 60]) {
const fx = rampFixture({ shape: "linear", rampS, fps, offsetMs: off, events: [0.5042, 1.9117, 3.2209, 4.6033, 5.9161, 7.2087, 8.5] });
const r = syncAnalyze({ audio: fx.audio, sampleRate: fx.sampleRate, visual: fx.visual, visualShape: "ramp", events: fx.events });
for (const p of r.pairs) assert.notEqual(p.verdict, "audio-late-detectable", `${rampS * 1000} ms rise, ${fps} fps, true lag ${off}: [${p.lagLowMs}, ${p.lagHighMs}]`);
}
});
t("ramp: a variable-frame-rate track does not stretch the smoothing or the bound (gaps up to 100 ms)", () => {
const fx = rampFixture({ shape: "linear", rampS: 0.1, fps: 60, offsetMs: 40 }); let s = 3; const rand = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return s / 2 ** 32; };
const keep = []; let next = 0; for (let i = 0; i < fx.visual.t.length; i++) if (fx.visual.t[i] >= next) { keep.push(i); next = fx.visual.t[i] + (rand() < 0.3 ? 0.1 * rand() : 0.017); }
const visual = { t: Float64Array.from(keep, (i) => fx.visual.t[i]), v: Float64Array.from(keep, (i) => fx.visual.v[i]) };
const r = syncAnalyze({ audio: fx.audio, sampleRate: fx.sampleRate, visual, visualShape: "ramp", events: fx.events, allowUnpaired: true });
assert.ok(r.pairs.length >= 5);
for (const p of r.pairs) assert.ok(p.verdict !== "audio-late-detectable", `[${p.lagLowMs}, ${p.lagHighMs}] verdict ${p.verdict} at true lag 40`);
});
t("ramp: sweep of 120 shapes, rises (40-800 ms), decays, frame rates, noise and offsets: a ramp verdict is never wrong", () => {
let s = 19; const rand = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return s / 2 ** 32; }, pick = (a) => a[Math.floor(rand() * a.length)];
let claims = 0;
for (let k = 0; k < 120; k++) {
const shape = pick(Object.keys(SHAPES)), rampS = pick([0.04, 0.08, 0.15, 0.3, 0.5, 0.8]), fps = pick([24, 30, 60, 120]), off = pick([-80, -40, 0, 20, 40, 98, 130, 200]), decayS = pick([0.3, 0.8, 2]), noise = pick([0.001, 0.004, 0.01]);
const fx = rampFixture({ shape, rampS, fps, offsetMs: off, decayS, noise, events: [0.5042, 2.1117, 3.7209, 5.3033, 6.9161, 8.5087] });
const r = syncAnalyze({ audio: fx.audio, sampleRate: fx.sampleRate, visual: fx.visual, visualShape: "ramp", pairWindowMs: 250, allowUnpaired: true, events: fx.events });
for (const p of r.pairs) {
assert.ok(["borderline", "audio-late-detectable", "ambiguous-pairing", "no-visual-onset"].includes(p.verdict), `${shape}/${rampS}/${fps}fps/${off}: a ramp is never shown inside or early, got ${p.verdict} [${p.lagLowMs}, ${p.lagHighMs}]`);
if (p.verdict === "audio-late-detectable") { claims++; assert.ok(off > PROFILES.default.lagMs, `${shape}/${rampS}/${fps}fps decay ${decayS} noise ${noise}: called late at true lag ${off}: [${p.lagLowMs}, ${p.lagHighMs}]`); }
}
}
assert.ok(claims > 0, "the sweep contains lags the detector can call late");
});
t("ramp: noise, a steady drift and a lone step give no ramp onsets from noise or drift", () => {
const nf = 600, tt = Float64Array.from({ length: nf }, (_, i) => i / 60); let s = 5; const rand = () => { s = (Math.imul(s, 1664525) + 1013904223) >>> 0; return s / 2 ** 32 - 0.5; };
assert.equal(visualRampOnsets({ t: tt, v: Float64Array.from(tt, () => 0.2 + 0.01 * rand()) }).onsets.length, 0, "noise");