// The interactive panel of an oscillator slide, built into a container // element: the animated diagram (slowed down by SLOWDOWN) with Start, reset, // Play and volume in its corner, a slider for each parameter (see // controls.js), and an optional live spectrogram of the sound. // // Changing x0 resets the animation to rest at the new x0 and scales the // playing sound. The other parameters change both live, keeping the current // size of the swing (see retune in duffing.js). import { createLoop } from './canvas.js'; import { bindSliders, createPresetSelect, physicsOf, sliderSpecs, slidersHTML, volumeHTML } from './controls.js'; import { amplitude, omegaBound, retune, step } from './duffing.js'; import { drawOscillator } from './oscillator-diagram.js'; import { createSound } from './sound.js'; import { createSpectrogram, SPEC_FFT } from './spectrogram.js'; const SLOWDOWN = 100; function buildPanel(container, names, spec) { container.innerHTML = `
${volumeHTML()}
${slidersHTML(names, spec)} `; } // container: the element to build into. params: initial values, whose keys // pick the sliders (in SLIDERS order). sliders: per-slider overrides of // SLIDERS, by name. labels, damper: see drawOscillator. presets (optional): // [{ name, group?, params }] for a dropdown built into presetContainer; // choosing one sets the sliders and restarts the motion from rest at x0 (a // running animation, or one stopped by an escape, keeps running; a playing // sound keeps playing), and moving a slider by hand shows "Custom". // Returns the page's sim for deck.js: { draw, pause }. export function createOscillatorPage({ container, params, sliders = {}, labels, damper = false, presets = null, presetContainer = null }) { const { names, spec } = sliderSpecs(params, sliders); buildPanel(container, names, spec); const q = (selector) => container.querySelector(selector); const role = (name) => q(`[data-role="${name}"]`); const canvas = q('canvas.oscillator'); const specCanvas = q('canvas.spectrogram'); const toggle = role('toggle'); const physics = () => physicsOf(params); let p = physics(); const state = { x: params.x0, v: 0, phase: 0, escaped: false }; // the sound's normalization (see oscillator-worklet.js): without a drive, // the largest |x| of the motion from rest at x0, which is x0 itself unless // x0 is on the inner side of one of a double well's wells const driven = 'F' in params; const ref = () => (driven ? spec('x0').max : Math.max(Math.abs(params.x0), amplitude(p, params.x0, 0))); const spectrogram = createSpectrogram(specCanvas); // the driving force as an arrow on the block, longer for a stronger force const drive = () => (driven && params.F > 0 ? Math.cos(state.phase) * Math.sqrt(params.F / (spec('F').max * spec('F').scale)) : null); const drawDiagram = () => drawOscillator(canvas, state.x, { labels, damper, drive: drive(), escaped: state.escaped }); // Back to rest at x0, with the driving force at phase 0. function restart() { state.x = params.x0; state.v = 0; state.phase = 0; state.escaped = false; } // If the block escapes, the animation stops with a note on the diagram; // Start, or changing any slider, restarts it from x0 (a slider change // keeps it running if it was). let runAfterEscape = false; // registered before createLoop's own handler, so it runs first toggle.addEventListener('click', () => { if (state.escaped) restart(); }); const loop = createLoop({ button: toggle, step: (dt) => { if (state.escaped) return; // the rest of the frame after an escape step(p, state, dt, omegaBound(p, amplitude(p, state.x, state.v))); if (state.escaped) { loop.pause(); toggle.textContent = 'Start'; runAfterEscape = true; } }, draw: drawDiagram, speed: 1 / SLOWDOWN, }); const sound = createSound({ button: role('play'), volume: role('volume'), params: () => ({ ...p, x0: params.x0, ref: ref() }), fftSize: SPEC_FFT, onPlay: spectrogram.attach, onStop: spectrogram.detach, }); function reset() { restart(); runAfterEscape = false; loop.reset(); } role('reset').addEventListener('click', reset); const specToggle = role('spectrogram-toggle'); specToggle.addEventListener('click', () => { specCanvas.hidden = !specCanvas.hidden; specToggle.textContent = specCanvas.hidden ? 'Show spectrogram' : 'Hide spectrogram'; spectrogram.draw(); }); let presets_ = null; const sliderControls = bindSliders({ root: container, names, spec, params, onInput: (name) => { presets_?.markCustom(); const old = p; p = physics(); if (state.escaped) { restart(); if (runAfterEscape) loop.start(); runAfterEscape = false; } else if (name === 'x0') { reset(); } else { retune(old, p, state); } drawDiagram(); sound.update(); }, }); if (presets) { presets_ = createPresetSelect({ container: presetContainer, presets, params, names, onChoose: () => { sliderControls.showAll(); p = physics(); const wasRunning = loop.isRunning() || runAfterEscape; reset(); if (wasRunning) loop.start(); sound.restart(); }, }); } // the sliders may have raised alpha to its bound p = physics(); restart(); return { draw: () => { drawDiagram(); spectrogram.draw(); }, pause: () => { loop.pause(); sound.stop(); }, }; }