Created
June 27, 2025 19:02
-
-
Save whoisryosuke/69f84cad109d5c99d9ad99ae93439fc5 to your computer and use it in GitHub Desktop.
Web Audio - Echo node - @see: https://web.dev/case-studies/jamwithchrome-audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class EchoNode { | |
// We need a node that sits at the front of the chain and receives input data | |
// and sends it directly to output - and to the "echo" node chain | |
input: GainNode; | |
// The final output node for this effect | |
output: GainNode; | |
// The "echo" effect | |
delay: DelayNode; | |
// Amplifies "echo" effect | |
feedback: GainNode; | |
// Controls how much "echo" we hear on top of original audio | |
wet: GainNode; | |
constructor(ctx: AudioContext) { | |
this.input = ctx.createGain(); | |
this.output = ctx.createGain(); | |
this.delay = ctx.createDelay(); | |
this.feedback = ctx.createGain(); | |
this.wet = ctx.createGain(); | |
// Default config for nodes | |
this.delay.delayTime.value = 0.15; | |
this.feedback.gain.value = 0.25; | |
this.wet.gain.value = 0.25; | |
// We connect input to 2 places: | |
// the output (so we hear it unaltered), and the "echo" chain | |
// Connect input to "echo" chain | |
this.input.connect(this.delay); | |
// Connect input directly to the output (unaltered) | |
this.input.connect(this.output); | |
// Setup "echo" chain from the input we received | |
this.delay.connect(this.feedback); | |
this.delay.connect(this.wet); | |
// Loop feedback back into delay | |
this.feedback.connect(this.delay); | |
this.wet.connect(this.output); | |
} | |
connect = (node: AudioNode) => { | |
this.output.connect(node); | |
}; | |
} | |
// using | |
const ctx = new AudioContext(); | |
const echo = new EchoNode(ctx); | |
const osc = ctx..createOscillator(); | |
osc.start(); | |
// Connect sound to the "input" property (aka the inside gain node) | |
osc.connect(echo.input); | |
// Connect echo node to output (or next node in chain) | |
echo.connect(ctx.destination); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment