summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-18 18:44:21 +0100
committerskal <pascal.massimino@gmail.com>2026-02-18 18:44:21 +0100
commiteff75e6d15eaebccec814b37504a6eeff53fb9e8 (patch)
tree6b57a7336bd40be09f58d3ef4a9fcf6e3aedfc05
parent9b1439582853c24d240d1e62290f33809097bbe8 (diff)
feat(mq_editor): switch curve interpolation to Lagrange through all control points
Replace cubic Bezier with Lagrange interpolation so P1/P2 are actual points on the curve. Eval uses stored t1/t2 as arbitrary knot positions. fitBezier keeps least-squares but with Lagrange basis at u=1/3,2/3. Remove endpoint companion drag (no longer tangent handles). TODO: support arbitrary number of inner control points. handoff(Claude): Lagrange interpolation replaces Bezier in mq_editor curve eval/fit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
-rw-r--r--tools/mq_editor/editor.js15
-rw-r--r--tools/mq_editor/mq_extract.js45
-rw-r--r--tools/mq_editor/utils.js43
-rw-r--r--tools/mq_editor/viewer.js13
4 files changed, 54 insertions, 62 deletions
diff --git a/tools/mq_editor/editor.js b/tools/mq_editor/editor.js
index 0854ec0..a7d0879 100644
--- a/tools/mq_editor/editor.js
+++ b/tools/mq_editor/editor.js
@@ -25,8 +25,7 @@ class PartialEditor {
// Private state
this._selectedIndex = -1;
- this._dragPointIndex = -1;
- this._dragCompanionOff = null; // {dt, dv} offset of companion handle relative to anchor
+ this._dragPointIndex = -1;
this._amp = { tMin: 0, tMax: 1, ampTop: 1 };
this._setupButtons();
@@ -496,12 +495,7 @@ class PartialEditor {
for (let i = 0; i < 4; ++i) {
if (Math.hypot(this._tToX(curve['t' + i]) - x, this._ampToY(curve['a' + i]) - y) <= 8) {
if (this.onBeforeChange) this.onBeforeChange();
- this._dragPointIndex = i;
- this._dragCompanionOff = null;
- if (i === 0)
- this._dragCompanionOff = { da: curve.a1 - curve.a0 };
- else if (i === 3)
- this._dragCompanionOff = { da: curve.a2 - curve.a3 };
+ this._dragPointIndex = i;
canvas.style.cursor = 'grabbing';
e.preventDefault();
return;
@@ -516,11 +510,6 @@ class PartialEditor {
const curve = this.partials[this._selectedIndex].freqCurve;
const i = this._dragPointIndex;
curve['a' + i] = Math.max(0, this._yToAmp(y));
- if (this._dragCompanionOff) {
- const off = this._dragCompanionOff;
- if (i === 0) { curve.a1 = curve.a0 + off.da; }
- else { curve.a2 = curve.a3 + off.da; }
- }
this._renderAmpEditor();
if (this.viewer) this.viewer.render();
e.preventDefault();
diff --git a/tools/mq_editor/mq_extract.js b/tools/mq_editor/mq_extract.js
index ff38d63..18897fb 100644
--- a/tools/mq_editor/mq_extract.js
+++ b/tools/mq_editor/mq_extract.js
@@ -526,7 +526,10 @@ function trackIsoContour(stftCache, seedTime, seedFreq, params) {
};
}
-// Fit cubic bezier to trajectory using least-squares for inner control points
+// Fit interpolating curve to trajectory via least-squares for inner control point values.
+// Inner knots fixed at u=1/3 and u=2/3 (t = t0+dt/3, t0+2*dt/3).
+// The curve passes through all 4 control points (Lagrange interpolation).
+// TODO: support arbitrary number of inner control points
function fitBezier(times, values) {
const n = times.length - 1;
const t0 = times[0], v0 = values[0];
@@ -534,43 +537,35 @@ function fitBezier(times, values) {
const dt = t3 - t0;
if (dt <= 1e-9 || n < 2) {
- // Linear fallback for too few points or zero duration
return {t0, v0, t1: t0 + dt / 3, v1: v0 + (v3 - v0) / 3, t2: t0 + 2 * dt / 3, v2: v0 + 2 * (v3 - v0) / 3, t3, v3};
}
- // Least squares solve for v1, v2
- // Bezier: B(u) = (1-u)^3*v0 + 3(1-u)^2*u*v1 + 3(1-u)*u^2*v2 + u^3*v3
- // Target_i = val_i - (1-u)^3*v0 - u^3*v3
- // Model_i = A_i*v1 + B_i*v2
- // A_i = 3(1-u)^2*u
- // B_i = 3(1-u)*u^2
+ // Lagrange basis with inner knots at u1=1/3, u2=2/3
+ // l1(u) = u*(u-2/3)*(u-1) / ((1/3)*(1/3-2/3)*(1/3-1)) = 13.5*u*(u-2/3)*(u-1)
+ // l2(u) = u*(u-1/3)*(u-1) / ((2/3)*(2/3-1/3)*(2/3-1)) = -13.5*u*(u-1/3)*(u-1)
+ // l0(u) = (u-1/3)*(u-2/3)*(u-1) / ((-1/3)*(-2/3)*(-1)) = -4.5*(u-1/3)*(u-2/3)*(u-1)
+ // l3(u) = u*(u-1/3)*(u-2/3) / ((2/3)*(1/3)) = 4.5*u*(u-1/3)*(u-2/3)
+ // Least-squares: minimize Σ(l1*v1 + l2*v2 - target_i)^2
+ // target_i = values[i] - l0*v0 - l3*v3
let sA2 = 0, sB2 = 0, sAB = 0, sAT = 0, sBT = 0;
for (let i = 0; i <= n; ++i) {
- const u = (times[i] - t0) / dt;
- const u2 = u * u;
- const u3 = u2 * u;
- const invU = 1.0 - u;
- const invU2 = invU * invU;
- const invU3 = invU2 * invU;
-
- const A = 3 * invU2 * u;
- const B = 3 * invU * u2;
- const target = values[i] - (invU3 * v0 + u3 * v3);
-
- sA2 += A * A;
- sB2 += B * B;
- sAB += A * B;
- sAT += A * target;
- sBT += B * target;
+ const u = (times[i] - t0) / dt;
+ const l0 = -4.5 * (u - 1/3) * (u - 2/3) * (u - 1);
+ const l1 = 13.5 * u * (u - 2/3) * (u - 1);
+ const l2 = -13.5 * u * (u - 1/3) * (u - 1);
+ const l3 = 4.5 * u * (u - 1/3) * (u - 2/3);
+ const A = l1, B = l2;
+ const target = values[i] - l0 * v0 - l3 * v3;
+ sA2 += A * A; sB2 += B * B; sAB += A * B;
+ sAT += A * target; sBT += B * target;
}
const det = sA2 * sB2 - sAB * sAB;
let v1, v2;
if (Math.abs(det) < 1e-9) {
- // Fallback to simple 1/3, 2/3 heuristic if matrix is singular
const idx1 = Math.round(n / 3);
const idx2 = Math.round(2 * n / 3);
v1 = values[idx1];
diff --git a/tools/mq_editor/utils.js b/tools/mq_editor/utils.js
index 96d807c..2c6b2f5 100644
--- a/tools/mq_editor/utils.js
+++ b/tools/mq_editor/utils.js
@@ -1,29 +1,48 @@
// Shared utilities for mq_editor
-// Evaluate cubic bezier curve at time t (robust: handles dt<=0)
+// Evaluate interpolating curve at time t via Lagrange interpolation.
+// The curve passes through all 4 control points at their stored time positions.
+// Knot positions: u_k = (t_k - t0) / (t3 - t0), k=0..3.
+// TODO: support arbitrary number of inner control points
function evalBezier(curve, t) {
const dt = curve.t3 - curve.t0;
if (dt <= 0) return curve.v0;
let u = (t - curve.t0) / dt;
u = Math.max(0, Math.min(1, u));
- const u1 = 1.0 - u;
- return u1*u1*u1 * curve.v0 +
- 3*u1*u1*u * curve.v1 +
- 3*u1*u*u * curve.v2 +
- u*u*u * curve.v3;
+ const u1 = (curve.t1 - curve.t0) / dt;
+ const u2 = (curve.t2 - curve.t0) / dt;
+ const d0 = (-u1) * (-u2) * (-1);
+ const d1 = u1 * (u1 - u2) * (u1 - 1);
+ const d2 = u2 * (u2 - u1) * (u2 - 1);
+ const d3 = (1 - u1) * (1 - u2);
+ if (Math.abs(d0) < 1e-9 || Math.abs(d1) < 1e-9 || Math.abs(d2) < 1e-9 || Math.abs(d3) < 1e-9)
+ return curve.v0 + (curve.v3 - curve.v0) * u;
+ const l0 = (u - u1) * (u - u2) * (u - 1) / d0;
+ const l1 = u * (u - u2) * (u - 1) / d1;
+ const l2 = u * (u - u1) * (u - 1) / d2;
+ const l3 = u * (u - u1) * (u - u2) / d3;
+ return l0 * curve.v0 + l1 * curve.v1 + l2 * curve.v2 + l3 * curve.v3;
}
-// Evaluate amplitude component of unified bezier curve at time t
+// Evaluate amplitude component of interpolating curve at time t
function evalBezierAmp(curve, t) {
const dt = curve.t3 - curve.t0;
if (dt <= 0) return curve.a0;
let u = (t - curve.t0) / dt;
u = Math.max(0, Math.min(1, u));
- const u1 = 1.0 - u;
- return u1*u1*u1 * curve.a0 +
- 3*u1*u1*u * curve.a1 +
- 3*u1*u*u * curve.a2 +
- u*u*u * curve.a3;
+ const u1 = (curve.t1 - curve.t0) / dt;
+ const u2 = (curve.t2 - curve.t0) / dt;
+ const d0 = (-u1) * (-u2) * (-1);
+ const d1 = u1 * (u1 - u2) * (u1 - 1);
+ const d2 = u2 * (u2 - u1) * (u2 - 1);
+ const d3 = (1 - u1) * (1 - u2);
+ if (Math.abs(d0) < 1e-9 || Math.abs(d1) < 1e-9 || Math.abs(d2) < 1e-9 || Math.abs(d3) < 1e-9)
+ return curve.a0 + (curve.a3 - curve.a0) * u;
+ const l0 = (u - u1) * (u - u2) * (u - 1) / d0;
+ const l1 = u * (u - u2) * (u - 1) / d1;
+ const l2 = u * (u - u1) * (u - 1) / d2;
+ const l3 = u * (u - u1) * (u - u2) / d3;
+ return l0 * curve.a0 + l1 * curve.a1 + l2 * curve.a2 + l3 * curve.a3;
}
// Get canvas-relative {x, y} from a mouse event
diff --git a/tools/mq_editor/viewer.js b/tools/mq_editor/viewer.js
index c69d9e7..d7b5ac1 100644
--- a/tools/mq_editor/viewer.js
+++ b/tools/mq_editor/viewer.js
@@ -626,14 +626,8 @@ class SpectrogramViewer {
if (this.selectedPartial >= 0 && this.selectedPartial < this.partials.length) {
const ptIdx = this.hitTestControlPoint(x, y, this.partials[this.selectedPartial]);
if (ptIdx >= 0) {
- const curve = this.partials[this.selectedPartial].freqCurve;
- let companionOff = null;
- if (ptIdx === 0)
- companionOff = { dt: curve.t1 - curve.t0, dv: curve.v1 - curve.v0 };
- else if (ptIdx === 3)
- companionOff = { dt: curve.t2 - curve.t3, dv: curve.v2 - curve.v3 };
if (this.onBeforeChange) this.onBeforeChange();
- this.dragState = { pointIndex: ptIdx, companionOff };
+ this.dragState = { pointIndex: ptIdx };
canvas.style.cursor = 'grabbing';
e.preventDefault();
return;
@@ -655,11 +649,6 @@ class SpectrogramViewer {
const i = this.dragState.pointIndex;
partial.freqCurve['t' + i] = t;
partial.freqCurve['v' + i] = v;
- if (this.dragState.companionOff) {
- const off = this.dragState.companionOff;
- if (i === 0) { partial.freqCurve.t1 = t + off.dt; partial.freqCurve.v1 = v + off.dv; }
- else { partial.freqCurve.t2 = t + off.dt; partial.freqCurve.v2 = v + off.dv; }
- }
this.render();
e.preventDefault();
return;