I really found simple left / right panning to be complex using the web audio API. It is really tuned for volumetric / spatial material, and I honestly don't understand it very well.
The way I usually pan is as follows:
var panLeft = context.createGain(); var panRight = context.createGain(); var merger = context.createMerger(2); source.connect(panLeft); source.connect(panRight); panLeft.connect(merger, 0, 0); panRight.connect(merger, 0, 1); merger.connect(context.destination); document.getElementById('panInput').addEventListener('change', function () { var val = this.value; panLeft.gain.value = ( val * -0.5 ) + 0.5; panRight.gain.value = ( val * 0.5 ) + 0.5; });
Basically, you send a signal to two gain nodes that you intend to use as the left and right channels. Then you take a value from your range element and use it to set the gain on each node.
This is a kind of lazy version. In serious audio applications, there is usually a bit more math involved with panning to make sure there are no changes in the overall level - but hopefully this is enough to get you started.
source share