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);""

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

Leave a Comment