/ interactive_tutorials / hardening_softening
Sign in
interactive_tutorials / hardening_softening
hardening_softening / app / js / oscillator-worklet.js
114 lines · 4.4 KBCodeBlameHistory
7212d95Hardening and softening tutorialJeremy Magland 1// Audio thread for the oscillator pages: integrates the oscillator of
2// duffing.js sample by sample, so the sound plays for as long as wanted and
3// follows parameter changes as they happen. sound.js sends messages:
4// { type: 'params', omega2, alpha, delta, F, Omega, x0, ref }
5// applied from now on
6// { type: 'start', x?, v?, phase? }
7// start from the given state, by default from rest at
8// x0 with the driving force at phase 0
9// and receives { type: 'ended' } when damping without a driving force has
10// brought the sound 80 dB below ref. If the block escapes, the sound fades
11// to silence and the next parameter change restarts it from rest at x0.
12//
13// The output is x / ref; sound.js sets the volume after this. Without a
14// driving force ref = x0, so the loudness does not depend on the
15// displacement (an undamped orbit from x0 peaks at 1). A change of x0 then
16// scales the motion by the same factor, keeping its phase, so the output stays
17// continuous. With a driving force the swing is set by the drive more than by
18// x0, so ref is fixed (the largest x0) and loudness follows the swing: a
19// resonance is louder. x0 is then only the starting point and a change does
20// not touch the playing motion. Other changes keep the size of the swing (see
21// retune in duffing.js), so they do not click.
23import { amplitude, omegaBound, ontoOrbit, retune, step } from './duffing.js';
25const FADE = 0.005; // seconds, for fading in
26const END_LEVEL = 1e-4; // -80 dB
28class OscillatorProcessor extends AudioWorkletProcessor {
29 constructor() {
30 super();
31 this.p = null;
32 this.x0 = 0;
33 this.ref = 0;
34 this.gain = 0; // follows 1 / ref, smoothed (see process)
35 this.s = { x: 0, v: 0, phase: 0, escaped: false };
36 this.env = 0;
37 this.ended = true;
38 // one-pole DC blocker, so a constant x (f0 = 0) is silent
39 this.dcIn = 0;
40 this.dcOut = 0;
41 this.port.onmessage = (e) => this.message(e.data);
42 }
44 message(m) {
45 if (m.type === 'params') {
46 const q = { omega2: m.omega2, alpha: m.alpha, delta: m.delta, F: m.F, Omega: m.Omega };
47 const follow = !m.F; // whether x0 changes move the playing motion
48 if (this.s.escaped) {
49 // restart after an escape, fading in
50 this.s = { x: m.x0, v: 0, phase: 0, escaped: false };
51 this.env = 0;
52 } else if (this.p && follow && this.x0 === 0 && m.x0 !== 0) {
53 // from no motion at all: start from rest at the new x0
54 this.s = { x: m.x0, v: 0, phase: 0, escaped: false };
55 this.env = 0;
56 } else if (this.p) {
57 if (follow && this.x0 !== 0 && m.x0 !== this.x0) {
58 // scale the swing by new / old x0, keeping the phase
59 const ratio = m.x0 / this.x0;
60 const A = ratio * amplitude(this.p, this.s.x, this.s.v);
61 this.s.x *= ratio;
62 this.s.v *= ratio;
63 ontoOrbit(q, this.s, A);
64 // the gain changes at once with the motion, so the output does not jump
65 this.gain = m.ref !== 0 ? 1 / m.ref : 0;
66 } else {
67 retune(this.p, q, this.s);
68 }
69 }
70 this.p = q;
71 this.x0 = m.x0;
72 this.ref = m.ref;
73 } else if (m.type === 'start') {
74 this.s = { x: m.x ?? this.x0, v: m.v ?? 0, phase: m.phase ?? 0, escaped: false };
75 this.gain = this.ref !== 0 ? 1 / this.ref : 0;
76 this.env = 0;
77 this.ended = false;
78 }
79 }
81 end() {
82 if (this.ended) return;
83 this.ended = true;
84 this.port.postMessage({ type: 'ended' });
85 }
87 process(inputs, outputs) {
88 const out = outputs[0][0];
89 if (!this.p) return true;
90 const h = 1 / sampleRate;
91 const k = 1 - Math.exp(-h / FADE);
92 const { p, s } = this;
93 const A = amplitude(p, s.x, s.v);
94 const omega = omegaBound(p, A);
95 // ref can change with other parameters (for a double well), so the gain
96 // moves to it smoothly
97 const gainTarget = this.ref !== 0 ? 1 / this.ref : 0;
99 for (let i = 0; i < out.length; i++) {
100 step(p, s, h, omega);
101 this.env += k * ((s.escaped ? 0 : 1) - this.env);
102 this.gain += k * (gainTarget - this.gain);
103 const y = s.x * this.gain * this.env;
104 this.dcOut = y - this.dcIn + 0.999 * this.dcOut;
105 this.dcIn = y;
106 out[i] = this.dcOut;
107 }
109 if (p.delta > 0 && !p.F && A < END_LEVEL * Math.abs(this.ref)) this.end();
110 return true;
111 }
114registerProcessor('oscillator-processor', OscillatorProcessor);
↑↓ move↵openescclose