Adding a convolver reverb to an audio file

I’m sorry, I’m really a newbie in Javascript and web audio api. I’m trying to add a reverb to an audio file, with the possibility to regulate the amount of the reverb to be added, but the code doesn’t work…can you please help me? Thanks a lot in advance!

   const track1 = audioContext.createMediaElementSource(audioElement1);
   const playButton1 = document.getElementById('player1');
   var sub1 = document.getElementById("sub1");

   var impulseResponseBuffer;
   var getSound = new XMLHttpRequest ();
   getSound.open("get", "minster.wav", true);
   getSound.responseType = "arraybuffer";
   const gainNode1 = audioContext.createGain();
   var convolver = audioContext.createConvolver();
   const volumeControl1 = document.getElementById('volume1');

   getSound.onload = function () {
       audioContext.decodeAudioData(getSound.response, function(buffer) {
          impulseResponseBuffer = buffer;
       });
   };

   getSound.send();

   playButton1.addEventListener('click', function() {
   if (audioContext.state === 'suspended') {
       audioContext.resume();
   }
   if (this.dataset.playing === 'false') {
       audioElement1.currentTime = 0;
       audioElement1.play();
       audioElement1.loop = 'true';
       this.dataset.playing = 'true';
       sub1.innerHTML = "Stop";
   } else if (this.dataset.playing === 'true') {
       audioElement1.pause();
       this.dataset.playing = 'false';
       sub1.innerHTML = "Play";
   }
   }, false);
   
   audioElement1.addEventListener('ended', () => {
   playButton1.dataset.playing = 'false';
   }, false);

   convolver.buffer = impulseResponseBuffer;
   track1.connect(convolver);
   convolver.connect(gainNode1);
   gainNode1.connect(audioContext.destination);
   track1.connect(audioContext.destination);
   
   volumeControl1.addEventListener('input', function() {
   gainNode1.gain.value = this.value;
   }, false);""

8 thoughts on “Adding a convolver reverb to an audio file”

  1. First, you really need to describe in what way it doesn’t work. Is there no sound? Sound isn’t what you expected? Error messages in the developer console?

    Second, consider looking at Figure 2 and Example 2 in Modular Routing. There’s lot of extraneous things there, but I think what you want is two gain nodes: dry gain and wet gain, where the dry gain is for the audio you’re playing (unmodified), and the wet gain is for the output of the convolver. You an adjust the gains to control how much of the convolver effect is in the final output.

    Reply

Leave a Comment