Reputation: 581
trying to wire up a delay effect for a little Web Audio app and can't seem to get it to sound good. Seems simple enough but not getting the results I'd like. Would like to have the delay note repeat over and over and slowly die away. I can implement the gain part of that but can't seem to get the feedback loop part so the delay continues. Can someone point me in the right direction?
Here's the relevant code. Added a second delay in hopes of extending but not quite what I was looking for.
var osc = ctx.createOscillator();
var gainNode = ctx.createGain();
if (addDelay === false) {
osc.connect(gainNode);
gainNode.connect(ctx.destination);
}
else if (addDelay === true) {
var delay = ctx.createDelay();
var delay2 = ctx.createDelay();
delay.delayTime.value = 0.35;
delay2.delayTime.value = 0.5;
osc.connect(gainNode);
gainNode.connect(ctx.destination);
gainNode.connect(delay);
gainNode.connect(delay2);
delay.connect(gainNode);
delay2.connect(gainNode);
delay.connect(ctx.destination);
delay2.connect(ctx.destination);
}
gainNode.gain.value = 0.0;
gainNode.gain.setTargetAtTime(0.75, ctx.currentTime, 0.1);
gainNode.gain.setTargetAtTime(0.0, ctx.currentTime + sustainFnl, 0.01);
osc.frequency.value = hz * octave / 4;
osc.type = waveType;
osc.start();
osc.stop(ctx.currentTime + sustainFnl + 0.01);
}
Thanks!
Upvotes: 1
Views: 1271
Reputation: 581
Found the answer in this article by Chris Lowis: http://blog.chrislowis.co.uk/2014/07/23/dub-delay-web-audio-api.html
var ctx = new AudioContext();
var audioElement = $('#feedback audio')[0];
audioElement.addEventListener('play', function(){
var source = ctx.createMediaElementSource(audioElement);
var delay = ctx.createDelay();
delay.delayTime.value = 0.5;
var feedback = ctx.createGain();
feedback.gain.value = 0.8;
delay.connect(feedback);
feedback.connect(delay);
source.connect(delay);
source.connect(ctx.destination);
delay.connect(ctx.destination);
Exactly what I was looking for.
Upvotes: 2
Reputation: 6048
I did not try this out, but in the addDelay === true
case, maybe you want to delete this line:
gainNode.connect(ctx.destination);
I'm also not sure why you have
gainNode.connect(delay);
gainNode.connect(delay2);
Did you mean to cascade the two delay nodes together? And if you did, you could just use one delay node with a longer delay time.
Here is a small example that seems to work: https://jsfiddle.net/n6782bpf/ Not sure if this is what you wanted.
Upvotes: 0