The AudioListener
interface represents the position and orientation of the unique person listening to the audio scene, and is used in audio spatialization. All PannerNode
s spatialize in relation to the AudioListener
stored in the BaseAudioContext.listener
attribute.
It is important to note that there is only one listener per context and that it isn't an AudioNode
.
Inherits properties from its parent, AudioNode
.
The position, forward, and up value are set and retrieved using different syntaxes. Retrieval is done by accessing, for example, AudioListener.positionX
, while setting the same property is done with AudioListener.positionX.value
. This is why these values are not marked read only, which is how they appear in the specification's IDL.
AudioListener.positionX
AudioListener.positionY
AudioListener.positionZ
AudioListener.forwardX
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is 0.AudioListener.forwardY
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is 0.AudioListener.forwardZ
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is -1.AudioListener.upX
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is 0.AudioListener.upY
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is 1.AudioListener.upZ
positionX
, positionY
, and positionZ
) values. The forward and up values are linearly independent of each other. The default is 0.Inherits methods from its parent, AudioNode
.
AudioListener.dopplerFactor
AudioListener.speedOfSound
AudioListener.setOrientation()
AudioListener.setPosition()
In a previous version of the specification, the dopplerFactor
and speedOfSound
properties and the setPosition()
method could be used to control the doppler effect applied to AudioBufferSourceNode
s connected downstream — these would be pitched up and down according to the relative speed of the PannerNode
and the AudioListener
. These features had a number of problems:
AudioBufferSourceNode
s were pitched up or down, not other source nodes.AudioBufferSourceNode
was connected to multiple PannerNode
s was unclear.AudioParam
s, the pitch modification could not be smoothly applied, resulting in audio glitches.Because of these issues, these properties and methods have been removed.
The setOrientation()
and setPosition()
methods have been replaced by setting their property value equivilents. For example setPosition(x, y, z)
can be achieved by setting positionX.value
, positionY.value
, and positionZ.value
respectively.
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.setValueAtTime(1, audioCtx.currentTime); panner.orientationY.setValueAtTime(0, audioCtx.currentTime); panner.orientationZ.setValueAtTime(0, audioCtx.currentTime); } else { panner.setOrientation(1,0,0); } var listener = audioCtx.listener; if(listener.forwardX) { listener.forwardX.setValueAtTime(0, audioCtx.currentTime); listener.forwardY.setValueAtTime(0, audioCtx.currentTime); listener.forwardZ.setValueAtTime(-1, audioCtx.currentTime); listener.upX.setValueAtTime(0, audioCtx.currentTime); listener.upY.setValueAtTime(1, audioCtx.currentTime); listener.upZ.setValueAtTime(0, audioCtx.currentTime); } 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.setValueAtTime(xPos, audioCtx.currentTime); listener.positionY.setValueAtTime(yPos, audioCtx.currentTime); listener.positionZ.setValueAtTime(300, audioCtx.currentTime); } 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.setValueAtTime(xPos, audioCtx.currentTime); panner.positionY.setValueAtTime(yPos, audioCtx.currentTime); panner.positionZ.setValueAtTime(zPos, audioCtx.currentTime); } 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 'AudioListener' in that specification. | Working Draft |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 14 | Yes | 25 | No | 15 | 6 |
dopplerFactor
|
14 — 56 | 12 | 25 — 63 | No | 15 — 43 | 6 |
forwardX |
52 | Yes | No
|
No | 39 | No |
forwardY |
52 | Yes | No
|
No | 39 | No |
forwardZ |
52 | Yes | No
|
No | 39 | No |
positionX |
52 | Yes | No
|
No | 39 | No |
positionY |
52 | Yes | No
|
No | 39 | No |
positionZ |
52 | Yes | No
|
No | 39 | No |
speedOfSound
|
14 — 56 | 12 | 25 — 63 | No | 15 — 43 | 6 |
upX |
52 | Yes | No
|
No | 39 | No |
upY |
52 | Yes | No
|
No | 39 | No |
upZ |
52 | Yes | No
|
No | 39 | No |
setOrientation
|
14 | 18 | 25 | No | 15 | 6 |
setPosition
|
14 | 12 | 25 | No | 15 | 6 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | 18 | Yes | 26 | 15 | ? | Yes |
dopplerFactor
|
? — 56 | 18 — 56 | Yes | 26 — 63 | 15 — 43 | ? | Yes |
forwardX |
52 | 52 | Yes | No
|
39 | ? | No |
forwardY |
52 | 52 | Yes | No
|
39 | ? | No |
forwardZ |
52 | 52 | Yes | No
|
39 | ? | No |
positionX |
52 | 52 | Yes | No
|
39 | ? | No |
positionY |
52 | 52 | Yes | No
|
39 | ? | No |
positionZ |
52 | 52 | Yes | No
|
39 | ? | No |
speedOfSound
|
? — 56 | 18 — 56 | Yes | 26 — 63 | 15 — 43 | ? | Yes |
upX |
52 | 52 | Yes | No
|
39 | ? | No |
upY |
52 | 52 | Yes | No
|
39 | ? | No |
upZ |
52 | 52 | Yes | No
|
39 | ? | No |
setOrientation
|
Yes | 18 | No | 26 | 15 | ? | Yes |
setPosition
|
Yes | 18 | No | 26 | 15 | ? | Yes |
© 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/AudioListener