The PannerNode
interface represents the position and behavior of an audio source signal in space. It is an AudioNode
audio-processing module describing its position with right-hand Cartesian coordinates, its movement using a velocity vector and its directionality using a directionality cone.
A PannerNode
always has exactly one input and one output: the input can be mono or stereo but the output is always stereo (2 channels); you can't have panning effects without at least two audio channels!
Number of inputs | 1 |
---|---|
Number of outputs | 1 |
Channel count mode | "clamped-max" |
Channel count | 2 |
Channel interpretation | "speakers" |
PannerNode.PannerNode
PannerNode
object instance.Inherits properties from its parent, AudioNode
.
The orientation and position value are set and retrieved using different syntaxes, since they're stored as AudioParam
values. Retrieval is done by accessing, for example, PannerNode.positionX
. While setting the same property is done with PannerNode.positionX.value
. This is why these values are not marked read only, which is how they appear in the WebIDL.
PannerNode.coneInnerAngle
PannerNode.coneOuterAngle
coneOuterGain
attribute.PannerNode.coneOuterGain
coneOuterAngle
attribute. Its default value is 0
, meaning that no sound can be heard.PannerNode.distanceModel
PannerNode.maxDistance
PannerNode.orientationX
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 1.PannerNode.orientationY
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 0.PannerNode.orientationZ
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 0.PannerNode.panningModel
PannerNode.positionX
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 0.PannerNode.positionY
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 0.PannerNode.positionZ
AudioParam
cannot be directly changed, its value can be altered using its value
property. The default is value is 0.PannerNode.refDistance
PannerNode.rolloffFactor
Inherits methods from its parent, AudioNode
.
PannerNode.setPosition()
AudioListener
object stored in the AudioContext.listener
attribute.)PannerNode.setOrientation()
PannerNode.setVelocity()
PannerNode
had a velocity that could pitch up or down AudioBufferSourceNode
s connected downstream. This feature was not clearly specified and had a number of issues, so it was removed from the specification.In the following example, you can see an example of how the createPanner()
method, AudioListener
and PannerNode
would be used to control audio spatialisation. Generally you will define the position in 3D space that your audio listener and panner (source) occupy initially, and then update the position of one or both of these as the application is used. You might be moving a character around inside a game world for example, and wanting delivery of audio to change realistically as your character moves closer to or further away from a music player such as a stereo. In the example you can see this being controlled by the functions moveRight()
, moveLeft()
, etc., which set new values for the panner position via the PositionPanner()
function.
To see a complete implementation, check out our panner-node example (view the source code) — this demo transports you to the 2.5D "Room of metal", where you can play a track on a boom box and then walk around the boom box to see how the sound changes!
Note how we have used some feature detection to either give the browser the newer property values (like AudioListener.forwardX
) for setting position, etc. if it supports those, or older methods (like AudioListener.setOrientation()
) if it still supports those but not the new properties.
// set up listener and panner position information var WIDTH = window.innerWidth; var HEIGHT = window.innerHeight; var xPos = Math.floor(WIDTH/2); var yPos = Math.floor(HEIGHT/2); var zPos = 295; // define other variables var AudioContext = window.AudioContext || window.webkitAudioContext; var audioCtx = new AudioContext(); var panner = audioCtx.createPanner(); panner.panningModel = 'HRTF'; panner.distanceModel = 'inverse'; panner.refDistance = 1; panner.maxDistance = 10000; panner.rolloffFactor = 1; panner.coneInnerAngle = 360; panner.coneOuterAngle = 0; panner.coneOuterGain = 0; if(panner.orientationX) { panner.orientationX.value = 1; panner.orientationY.value = 0; panner.orientationZ.value = 0; } else { panner.setOrientation(1,0,0); } var listener = audioCtx.listener; if(listener.forwardX) { listener.forwardX.value = 0; listener.forwardY.value = 0; listener.forwardZ.value = -1; listener.upX.value = 0; listener.upY.value = 1; listener.upZ.value = 0; } else { listener.setOrientation(0,0,-1,0,1,0); } var source; var play = document.querySelector('.play'); var stop = document.querySelector('.stop'); var boomBox = document.querySelector('.boom-box'); var listenerData = document.querySelector('.listener-data'); var pannerData = document.querySelector('.panner-data'); leftBound = (-xPos) + 50; rightBound = xPos - 50; xIterator = WIDTH/150; // listener will always be in the same place for this demo if(listener.positionX) { listener.positionX.value = xPos; listener.positionY.value = yPos; listener.positionZ.value = 300; } else { listener.setPosition(xPos,yPos,300); } listenerData.innerHTML = 'Listener data: X ' + xPos + ' Y ' + yPos + ' Z ' + 300; // panner will move as the boombox graphic moves around on the screen function positionPanner() { if(panner.positionX) { panner.positionX.value = xPos; panner.positionY.value = yPos; panner.positionZ.value = zPos; } else { panner.setPosition(xPos,yPos,zPos); } pannerData.innerHTML = 'Panner data: X ' + xPos + ' Y ' + yPos + ' Z ' + zPos; }
Note: In terms of working out what position values to apply to the listener and panner, to make the sound appropriate to what the visuals are doing on screen, there is quite a bit of math involved, but you will soon get used to it with a bit of experimentation.
Specification | Status | Comment |
---|---|---|
Web Audio API The definition of 'PannerNode' in that specification. | Working Draft |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 14 | Yes | 25 | No | 15 | 6 |
PannerNode() constructor |
551 | ? | 53 | No | 42 | ? |
coneInnerAngle |
14 | Yes | 25 | No | 15 | 6 |
coneOuterAngle |
14 | Yes | 25 | No | 15 | 6 |
coneOuterGain |
14 | Yes | 25 | No | 15 | 6 |
distanceModel |
14 | Yes | 25 | No | 15 | 6 |
maxDistance |
14 | Yes | 25 | No | 15 | 6 |
orientationX |
Yes | Yes | 50 | No | Yes | No |
orientationY |
Yes | Yes | 50 | No | Yes | No |
orientationZ |
Yes | Yes | 50 | No | Yes | No |
panningModel |
14 | Yes | 25 | No | 15 | 6 |
positionX |
Yes | Yes | 50 | No | Yes | No |
positionY |
Yes | Yes | 50 | No | Yes | No |
positionZ |
Yes | Yes | 50 | No | Yes | No |
refDistance |
14 | Yes | 25 | No | 15 | 6 |
rolloffFactor |
14 | Yes | 25 | No | 15 | 6 |
setOrientation |
14 | Yes | 25 | No | 15 | 6 |
setPosition |
14 | Yes | 25 | No | 15 | 6 |
setVelocity |
14 — 56 | No | 25 | No | 15 — 43 | 6 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | 14 | 14 | Yes | 26 | No | 15 | ? |
PannerNode() constructor |
551 | 551 | ? | 53 | No | 42 | ? |
coneInnerAngle |
Yes | 14 | Yes | 26 | No | 15 | ? |
coneOuterAngle |
Yes | 14 | Yes | 26 | No | 15 | ? |
coneOuterGain |
Yes | 14 | Yes | 26 | No | 15 | ? |
distanceModel |
Yes | 14 | Yes | 26 | No | 15 | ? |
maxDistance |
Yes | 14 | Yes | 26 | No | 15 | ? |
orientationX |
Yes | Yes | Yes | 50 | No | Yes | No |
orientationY |
Yes | Yes | Yes | 50 | No | Yes | No |
orientationZ |
Yes | Yes | Yes | 50 | No | Yes | No |
panningModel |
Yes | 14 | Yes | 26 | No | 15 | ? |
positionX |
Yes | Yes | Yes | 50 | No | Yes | No |
positionY |
Yes | Yes | Yes | 50 | No | Yes | No |
positionZ |
Yes | Yes | Yes | 50 | No | Yes | No |
refDistance |
Yes | 14 | Yes | 26 | No | 15 | ? |
rolloffFactor |
Yes | 14 | Yes | 26 | No | 15 | ? |
setOrientation |
Yes | 14 | Yes | 26 | No | 15 | ? |
setPosition |
Yes | 14 | Yes | 26 | No | 15 | ? |
setVelocity |
14 — 56 | 14 — 56 | No | 26 | No | 15 — 43 | ? |
1. Before Chrome 59, the default values were not supported.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/PannerNode