title: Webrtc Web Audio add Delay Node to incoming stream
body: <p>I'm adding delay to an incoming audio-only WebRTC stream using the Web Audio API's <a href="https://developer.mozilla.org/en-US/docs/Web/API/DelayNode" rel="nofollow noreferrer"><code>DelayNode</code></a> in Google Chrome.</p>
  
  <pre class="lang-js prettyprint-override"><code>connectionRecv.onaddstream = (event) =&gt; {
    const recvAudio = new Audio();
    recvAudio.srcObject = event.stream.clone();
    recvAudio.autoplay = true;
  
    recvAudio.onloadedmetadata = () =&gt; {
  
      // controls if original stream should also be played
      // true causes WebRTC getStats() receive track audioLevel == 0
      recvAudio.muted = muteOriginalStream;
  
      const recvAudioSource = audioContext.createMediaStreamSource(recvAudio.srcObject as MediaStream);
      const delayNode = audioContext.createDelay();
      delayNode.delayTime.value = 1; // delay by 1 second
      recvAudioSource.connect(delayNode);
      delayNode.connect(audioContext.destination);
    };
  };
  </code></pre>
  
  <p>This is working with one small problem. I want to mute the original stream so that I don't hear double audio (original stream &amp; delayed stream). But, when I mute the original stream so I only hear the delayed stream, the <code>RTCPeerConnection</code> <code>getStats()</code> returns 0 for the receive track <code>audioLevel</code>.</p>
  
  <p>I've tried many different ways of piping around the received stream so I only hear the delayed version, but either I can't hear any audio at all or the <code>getStats()</code> <code>audioLevel</code> is 0. I'm looking for an implementation that preserves the non-zero <code>audioLevel</code> from WebRTC <code>getStats()</code>, while only the delayed stream is playing through the audio output device. To put it simply:</p>
  
  <p><strong>How can I play only the delayed stream without zeroing-out the WebRTC <code>getStats()</code> receive track <code>audioLevel</code>?</strong></p>
  
  <p>I've created a minimum reproduction of the issue on stackblitz <a href="https://stackblitz.com/edit/typescript-uw7e8f" rel="nofollow noreferrer">here</a> where I create a loopback WebRTC connection where the sender and receiver are the same browser. Inspect the console logs to see the receive track <code>audioLevel</code> returned from <code>getStats()</code>.</p>
  
  <p>Note: if you use my stackblitz, I suggest you use headphones to avoid a feedback loop.</p>