// The oscillators on these slides are all of the form // x'' + delta x' + omega2 x + alpha x^3 = F cos(Omega t) // (harmonic: alpha = delta = F = 0), with x the displacement from equilibrium // as a fraction of the spring's rest length and t in seconds. p is // { omega2, alpha, delta, F, Omega } (F and Omega optional, default 0); a // state s is { x, v, phase, escaped }, where phase = Omega t (mod 2 pi) is // the driving force's phase, kept so that changing Omega does not jump it. // Shared by the animation (main thread) and the sound (oscillator-worklet.js). // A softening spring (alpha < 0) can let the block escape: past |x| = X_MAX // (the spring fully compressed, or stretched to twice its length) the motion // stops and s.escaped is set. export const X_MAX = 1; // The conservative part of x'', and its potential. export const force = (p, x) => -p.omega2 * x - p.alpha * x * x * x; export const potential = (p, x) => 0.5 * p.omega2 * x * x + 0.25 * p.alpha * x ** 4; // Amplitude of the undamped orbit through (x, v): the largest A > 0 with // V(A) = E, the farthest the orbit reaches. With b = omega2 / 2 and // s = sqrt(b^2 + alpha E), A^2 = (s - b) / (alpha / 2). For b > 0 (positive // linear stiffness) this is computed as 2 E / (b + s), which avoids // cancellation when alpha is small; for b < 0 (a double well) there is no // cancellation. Falls back to |x| when there is no such turning point (no // restoring force, or an orbit over a softening barrier). export function amplitude(p, x, v) { const E = 0.5 * v * v + potential(p, x); const b = 0.5 * p.omega2; const disc = b * b + p.alpha * E; let a2 = NaN; if (disc >= 0) { const s = Math.sqrt(disc); a2 = b >= 0 ? (2 * E) / (b + s) : (s - b) / (0.5 * p.alpha); } return Number.isFinite(a2) && a2 > 0 ? Math.sqrt(a2) : Math.abs(x); } // A bound on the local angular frequency sqrt(|d force / dx|) for motion of // amplitude at most A, for choosing step sizes. With alpha < 0 the stiffness // can grow (negatively) up to |x| = X_MAX. export function omegaBound(p, A) { const xm = p.alpha > 0 ? Math.min(Math.abs(A), X_MAX) : X_MAX; return Math.sqrt(Math.abs(p.omega2) + 3 * Math.abs(p.alpha) * xm * xm); } // Advance s by dt, in substeps short enough to stay accurate at audio // frequencies (omega h <= 0.02, omega from omegaBound, and Omega h <= 0.02 // when there is a driving force). // Each substep is velocity Verlet for the spring and driving forces, between // two half steps of the damping v' = -delta v solved exactly (Strang // splitting), so with delta = F = 0 it is symplectic. export function step(p, s, dt, omega) { if (s.escaped) return; const F = p.F ?? 0; const W = p.Omega ?? 0; const n = Math.max(1, Math.ceil((Math.max(omega, F ? W : 0) * Math.abs(dt)) / 0.02)); const h = dt / n; const decay = Math.exp(-0.5 * p.delta * h); let { x, v } = s; let phase = s.phase ?? 0; let a = force(p, x) + F * Math.cos(phase); for (let i = 0; i < n; i++) { v *= decay; x += v * h + 0.5 * a * h * h; if (Math.abs(x) > X_MAX) { x = Math.sign(x) * X_MAX; v = 0; s.escaped = true; break; } phase += W * h; const a1 = force(p, x) + F * Math.cos(phase); v += 0.5 * (a + a1) * h; a = a1; v *= decay; } s.x = x; s.v = v; s.phase = phase % (2 * Math.PI); } // Move s onto the undamped orbit with amplitude A under p, keeping x where // possible and setting v for that orbit's energy. Used when parameters change // during the motion, so the size of the swing carries over and the motion is // not knocked over a softening barrier by a sudden change. If the force at A // points outward (A past a softening barrier), that orbit escapes, so start // from rest at A. export function ontoOrbit(p, s, A) { const outward = A !== 0 && force(p, A) * A >= 0; const room = potential(p, A) - potential(p, s.x); if (outward || Math.abs(s.x) >= Math.abs(A) || room < 0) { s.x = Math.abs(s.x) >= Math.abs(A) && !outward ? Math.sign(s.x) * Math.abs(A) : A; s.v = 0; } else { s.v = (s.v < 0 ? -1 : 1) * Math.sqrt(2 * room); } } // Change the parameters of a moving state from p to q (see ontoOrbit). export function retune(p, q, s) { ontoOrbit(q, s, amplitude(p, s.x, s.v)); }