|
| 1 | +import { AudioModule } from "../../audio/AudioModule.js"; |
| 2 | +import { dbToLin, ENV_PEAK_BOOST_DB } from "../../audio/constants.js"; |
| 3 | +import { |
| 4 | + MODULE_KIND, PORT_TYPE, PORT_DIR, CV_POLARITY, |
| 5 | + CONTROL_KIND, CONTROL_CURVE, |
| 6 | +} from "../../audio/graph/types.js"; |
| 7 | + |
| 8 | +// AR envelope — a simplified ADSR with no decay and an implicit sustain |
| 9 | +// at peak. Gate high → attack ramp 0 → peak, then hold; gate low → |
| 10 | +// release ramp from current value → 0. Useful for plucks, claps, and |
| 11 | +// percussive bursts where the sustain plateau is just "fully open". |
| 12 | +// |
| 13 | +// Mirrors EnvelopeModule's contract (cv-out shape, internal gain node for |
| 14 | +// meter readout, gate-source dedup) so it composes with the same drawEnv + |
| 15 | +// amp.level wiring as the ADSR envelope. The CV-out value uses the same |
| 16 | +// dB-space normalisation: 0 = release floor, 1 = peak (= 0 dB at the amp |
| 17 | +// with knob at -48). |
| 18 | +export class ArEnvelopeModule extends AudioModule { |
| 19 | + static KIND = MODULE_KIND.CONTROL; |
| 20 | + static PORTS = [ |
| 21 | + { name: "trigger", dir: PORT_DIR.IN, type: PORT_TYPE.GATE }, |
| 22 | + { name: "env", dir: PORT_DIR.OUT, type: PORT_TYPE.CV, polarity: CV_POLARITY.UNIPOLAR }, |
| 23 | + ]; |
| 24 | + static CONTROLS = [ |
| 25 | + { name: "a", kind: CONTROL_KIND.KNOB, range: [0.001, 4], curve: CONTROL_CURVE.EXP, cvRange: 4, cvPolarity: CV_POLARITY.UNIPOLAR }, |
| 26 | + { name: "r", kind: CONTROL_KIND.KNOB, range: [0.001, 4], curve: CONTROL_CURVE.EXP, cvRange: 4, cvPolarity: CV_POLARITY.UNIPOLAR }, |
| 27 | + ]; |
| 28 | + |
| 29 | + constructor(ctx, params) { |
| 30 | + super(ctx); |
| 31 | + this.params = { ...params }; |
| 32 | + this.node = ctx.createGain(); |
| 33 | + this.node.gain.value = 1; |
| 34 | + this.envPhase = "idle"; |
| 35 | + this.envStart = 0; |
| 36 | + |
| 37 | + this.cvOut = ctx.createConstantSource(); |
| 38 | + this.cvOut.offset.value = 0; |
| 39 | + this.cvOut.start(); |
| 40 | + this._gateSources = new Set(); |
| 41 | + |
| 42 | + this._registerCvOut("env", this.cvOut); |
| 43 | + this._registerGateInput("trigger", (sourceId, active) => this._handleGate(sourceId, active)); |
| 44 | + this._makeCvInput("a", 4, null); |
| 45 | + this._makeCvInput("r", 4, null); |
| 46 | + } |
| 47 | + |
| 48 | + setParams(partial) { this.params = { ...this.params, ...partial }; } |
| 49 | + getValue() { return this.node.gain.value; } |
| 50 | + getPhase() { return this.envPhase; } |
| 51 | + getStart() { return this.envStart; } |
| 52 | + |
| 53 | + noteOn() { |
| 54 | + const t = this.ctx.currentTime; |
| 55 | + const e = this.params; |
| 56 | + const g = this.node.gain; |
| 57 | + g.cancelScheduledValues(t); |
| 58 | + g.setValueAtTime(Math.max(1e-5, g.value), t); |
| 59 | + // Attack to peak boost; then hold at peak (no decay, no sustain knob). |
| 60 | + g.exponentialRampToValueAtTime(dbToLin(ENV_PEAK_BOOST_DB), t + e.a); |
| 61 | + |
| 62 | + const cv = this.cvOut.offset; |
| 63 | + cv.cancelScheduledValues(t); |
| 64 | + cv.setValueAtTime(cv.value, t); |
| 65 | + cv.linearRampToValueAtTime(1.0, t + e.a); |
| 66 | + this.envStart = performance.now(); |
| 67 | + // Reuse drawEnv's "ad" phase tag — with d=0 + sustainDb=0 it animates |
| 68 | + // the dot along the attack ramp and then across the flat top, which is |
| 69 | + // exactly the AR shape. |
| 70 | + this.envPhase = "ad"; |
| 71 | + } |
| 72 | + noteOff() { |
| 73 | + const t = this.ctx.currentTime; |
| 74 | + const e = this.params; |
| 75 | + const g = this.node.gain; |
| 76 | + g.cancelScheduledValues(t); |
| 77 | + g.setValueAtTime(Math.max(1e-5, g.value), t); |
| 78 | + g.exponentialRampToValueAtTime(1.0, t + e.r); |
| 79 | + |
| 80 | + const cv = this.cvOut.offset; |
| 81 | + cv.cancelScheduledValues(t); |
| 82 | + cv.setValueAtTime(cv.value, t); |
| 83 | + cv.linearRampToValueAtTime(0, t + e.r); |
| 84 | + this.envStart = performance.now(); |
| 85 | + this.envPhase = "rel"; |
| 86 | + } |
| 87 | + reset() { |
| 88 | + const t = this.ctx.currentTime; |
| 89 | + this.node.gain.cancelScheduledValues(t); |
| 90 | + this.node.gain.setValueAtTime(1, t); |
| 91 | + this.cvOut.offset.cancelScheduledValues(t); |
| 92 | + this.cvOut.offset.setValueAtTime(0, t); |
| 93 | + this.envPhase = "idle"; |
| 94 | + this._gateSources.clear(); |
| 95 | + } |
| 96 | + |
| 97 | + _handleGate(sourceId, active) { |
| 98 | + const wasOpen = this._gateSources.size > 0; |
| 99 | + if (active) this._gateSources.add(sourceId); |
| 100 | + else this._gateSources.delete(sourceId); |
| 101 | + const nowOpen = this._gateSources.size > 0; |
| 102 | + if (!wasOpen && nowOpen) this.noteOn(); |
| 103 | + else if (wasOpen && !nowOpen) this.noteOff(); |
| 104 | + } |
| 105 | + |
| 106 | + setParam(name, value) { |
| 107 | + if (name === "a" || name === "r") { |
| 108 | + this.setParams({ [name]: value }); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + dispose() { |
| 113 | + try { this.cvOut.stop(); } catch {} |
| 114 | + try { this.cvOut.disconnect(); } catch {} |
| 115 | + try { this.node.disconnect(); } catch {} |
| 116 | + super.dispose(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments