// Audio thread for the oscillator pages: integrates the oscillator of // duffing.js sample by sample, so the sound plays for as long as wanted and // follows parameter changes as they happen. sound.js sends messages: // { type: 'params', omega2, alpha, delta, F, Omega, x0, ref } // applied from now on // { type: 'start', x?, v?, phase? } // start from the given state, by default from rest at // x0 with the driving force at phase 0 // and receives { type: 'ended' } when damping without a driving force has // brought the sound 80 dB below ref. If the block escapes, the sound fades // to silence and the next parameter change restarts it from rest at x0. // // The output is x / ref; sound.js sets the volume after this. Without a // driving force ref = x0, so the loudness does not depend on the // displacement (an undamped orbit from x0 peaks at 1). A change of x0 then // scales the motion by the same factor, keeping its phase, so the output stays // continuous. With a driving force the swing is set by the drive more than by // x0, so ref is fixed (the largest x0) and loudness follows the swing: a // resonance is louder. x0 is then only the starting point and a change does // not touch the playing motion. Other changes keep the size of the swing (see // retune in duffing.js), so they do not click. import { amplitude, omegaBound, ontoOrbit, retune, step } from './duffing.js'; const FADE = 0.005; // seconds, for fading in const END_LEVEL = 1e-4; // -80 dB class OscillatorProcessor extends AudioWorkletProcessor { constructor() { super(); this.p = null; this.x0 = 0; this.ref = 0; this.gain = 0; // follows 1 / ref, smoothed (see process) this.s = { x: 0, v: 0, phase: 0, escaped: false }; this.env = 0; this.ended = true; // one-pole DC blocker, so a constant x (f0 = 0) is silent this.dcIn = 0; this.dcOut = 0; this.port.onmessage = (e) => this.message(e.data); } message(m) { if (m.type === 'params') { const q = { omega2: m.omega2, alpha: m.alpha, delta: m.delta, F: m.F, Omega: m.Omega }; const follow = !m.F; // whether x0 changes move the playing motion if (this.s.escaped) { // restart after an escape, fading in this.s = { x: m.x0, v: 0, phase: 0, escaped: false }; this.env = 0; } else if (this.p && follow && this.x0 === 0 && m.x0 !== 0) { // from no motion at all: start from rest at the new x0 this.s = { x: m.x0, v: 0, phase: 0, escaped: false }; this.env = 0; } else if (this.p) { if (follow && this.x0 !== 0 && m.x0 !== this.x0) { // scale the swing by new / old x0, keeping the phase const ratio = m.x0 / this.x0; const A = ratio * amplitude(this.p, this.s.x, this.s.v); this.s.x *= ratio; this.s.v *= ratio; ontoOrbit(q, this.s, A); // the gain changes at once with the motion, so the output does not jump this.gain = m.ref !== 0 ? 1 / m.ref : 0; } else { retune(this.p, q, this.s); } } this.p = q; this.x0 = m.x0; this.ref = m.ref; } else if (m.type === 'start') { this.s = { x: m.x ?? this.x0, v: m.v ?? 0, phase: m.phase ?? 0, escaped: false }; this.gain = this.ref !== 0 ? 1 / this.ref : 0; this.env = 0; this.ended = false; } } end() { if (this.ended) return; this.ended = true; this.port.postMessage({ type: 'ended' }); } process(inputs, outputs) { const out = outputs[0][0]; if (!this.p) return true; const h = 1 / sampleRate; const k = 1 - Math.exp(-h / FADE); const { p, s } = this; const A = amplitude(p, s.x, s.v); const omega = omegaBound(p, A); // ref can change with other parameters (for a double well), so the gain // moves to it smoothly const gainTarget = this.ref !== 0 ? 1 / this.ref : 0; for (let i = 0; i < out.length; i++) { step(p, s, h, omega); this.env += k * ((s.escaped ? 0 : 1) - this.env); this.gain += k * (gainTarget - this.gain); const y = s.x * this.gain * this.env; this.dcOut = y - this.dcIn + 0.999 * this.dcOut; this.dcIn = y; out[i] = this.dcOut; } if (p.delta > 0 && !p.F && A < END_LEVEL * Math.abs(this.ref)) this.end(); return true; } } registerProcessor('oscillator-processor', OscillatorProcessor);