Skip to content
Advertisement

WebAudio panner not working properly with WebRTC audio stream

I have an issue where my audio panner isn’t properly panning with the given values.

Currently, if I set positionX.value to 1000, the audio plays as it was in the middle and not panned at all to the right channel.

Now if I set positionX.value to 0.5 or 0.9 or 1, the audio plays on the right channel, (even though not entirely, as I can still hear a bit on the left, more than usual).

I don’t understand why it only works from -1 to 1, any number higher than that the audio goes back to the center. Any idea why? And I’m sure it should work with higher numbers because I have tested it before in a different situation and nothing says it shouldn’t on the documentation.

JavaScript

Advertisement

Answer

Your recvAudio Audio element is diffusing the raw MediaStream directly, without the PannerNode affecting it.

JavaScript

In this snippet, source.mediaStream is exactly the same object as event.stream.

JavaScript
JavaScript

So when you get outside of the reference distance [-1 ~ 1], your AudioContext’s output sound will get lower than the one of this Audio element, and you will have the impression that the PannerNode doesn’t work anymore, because the Audio element’s output covers it.

To fix it, the best is probably to not use an Audio element at all here, you don’t really need it since you can let the AudioContext output the sound itself.

But if you really need to use an Audio element, instead of connecting the AudioPanner node to the context’s destination, connect it to a MediaStreamDestinationNode and pass this latter as the srcObject of your Audio element:

JavaScript

https://jsfiddle.net/do9xq681/

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement