jevstrudel.git / packages / superdough / feedbackdelay.mjs
1if (typeof DelayNode !== 'undefined') {
2  class FeedbackDelayNode extends DelayNode {
3    constructor(ac, wet, time, feedback) {
4      // The Web Audio default maxDelayTime is 1 s: without this, any
5      // delaytime over a second silently played at one.
6      super(ac, { maxDelayTime: 10 });
7      wet = Math.abs(wet);
8      this.delayTime.value = time;
9
10      this.feedbackGain = ac.createGain();
11      this.feedbackGain.gain.value = Math.min(Math.abs(feedback), 0.995);
12      this.feedback = this.feedbackGain.gain;
13
14      this.delayGain = ac.createGain();
15      this.delayGain.gain.value = wet;
16
17      this.connect(this.feedbackGain);
18      this.connect(this.delayGain);
19      this.feedbackGain.connect(this);
20
21      this.connect = (target) => this.delayGain.connect(target);
22      return this;
23    }
24    start(t) {
25      this.delayGain.gain.setValueAtTime(this.delayGain.gain.value, t + this.delayTime.value);
26    }
27  }
28
29  BaseAudioContext.prototype.createFeedbackDelay = function (wet, time, feedback) {
30    return new FeedbackDelayNode(this, wet, time, feedback);
31  };
32}