Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Registrati per tradurlo tu.

The AudioListener interface represents the position and orientation of the unique person listening to the audio scene, and is used in audio spatialization. All PannerNodes spatialize in relation to the AudioListener stored in the AudioContext.listener attribute.

It is important to note that there is only one listener per context and that it isn't an AudioNode.

We see the position, up and front vectors of an AudioListener, with the up and front vectors at 90° from the other

Properties

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.dopplerFactor
A double value representing the amount of pitch shift to use when rendering a doppler effect. See Deprecated features to learn why this property was removed.
AudioListener.positionX
Represents the horizontal position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.positionY
Represents the vertical position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.positionZ
Represents the longitudinal (back and forth) position of the listener in a right-hand cartesian coordinate sytem. The default is 0.
AudioListener.forwardX
Represents the horizontal position of the listener's forward direction in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.forwardY
Represents the vertical position of the listener's forward direction in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.forwardZ
Represents the longitudinal (back and forth) position of the listener's forward direction in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.speedOfSound
Is a double value representing the speed of sound, in meters per second. See Deprecated features to learn why this property was removed.
AudioListener.upX
Represents the horizontal position of the top of the listener's head in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.upY
Represents the vertical position of the top of the listener's head in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.
AudioListener.upZ
Represents the longitudinal (back and forth) position of the top of the listener's head in the same cartesian coordinate sytem as the position (positionX, positionY, and positionZ) values. The forward and up values are linearly independent of each other. The default is 0.

Methods

Inherits methods from its parent, AudioNode.

AudioListener.setOrientation()
Sets the orientation of the listener.
AudioListener.setPosition()
Sets the position of the listener. See Deprecated features to learn why this method was removed.

Deprecated features

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 AudioBufferSourceNodes 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:

  • Only AudioBufferSourceNodes were pitched up or down, not other source nodes.
  • The behavior to adopt when an AudioBufferSourceNode was connected to multiple PannerNodes was unclear.
  • Because the velocity of the panner and the listener were not AudioParams, the pitch modification could not be smoothly applied, resulting in audio glitches.

Because of these issues, these properties and methods have been removed.

Example

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!

// Define Web Audio API objects

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;
panner.orientationX.value = 1;
panner.orientationY.value = 0;
panner.orientationZ.value = 0;

var listener = audioCtx.listener;
listener.orientationX.value = 0;
listener.orientationY.value = 0;
listener.orientationZ.value = -1;
listener.upX.value = 0;
listener.upY.value = 1;
listener.upZ.value = 0;

var source;

var boomBox = document.querySelector('.boom-box');

// set up listener and panner position information
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;

var xPos = WIDTH/2;
var yPos = HEIGHT/2;
var zPos = 295;

leftBound = (-xPos) + 50;
rightBound = xPos - 50;

xIterator = WIDTH/150;

// listener will always be in the same place for this demo

listener.positionX.value = xPos;
listener.positionY.value = yPos;
listener.positionZ.value = zPos;
listenerData.innerHTML = 'Listener data: X ' + xPos + ' Y ' + yPos + ' Z ' + 300;

// panner will move as the boombox graphic moves around on the screen
function positionPanner() {
  panner.positionX.value = xPos;
  panner.positionY.value = yPos;
  panner.positionZ.value = zPos;
  pannerData.innerHTML = 'Panner data: X ' + xPos + ' Y ' + yPos + ' Z ' + zPos;
}

// controls to move left and right past the boom box
// and zoom in and out
// only right movement code shown in this listing to save space
// look at the source code for full listing

var leftButton = document.querySelector('.left');
var rightButton = document.querySelector('.right');
var zoomInButton = document.querySelector('.zoom-in');
var zoomOutButton = document.querySelector('.zoom-out');

var boomX = 0;
var boomY = 0;
var boomZoom = 0.25;

var zoomInLoop;
var zoomOutLoop

function moveRight() {
  boomX += -xIterator;
  xPos += -0.066;

  if(boomX <= leftBound) {
    boomX = leftBound;
    xPos = (WIDTH/2) - 5;
  }

  boomBox.style.transform = "translate(" + boomX + "px , " + boomY + "px) scale(" + boomZoom + ")";
  positionPanner();
  rightLoop = requestAnimationFrame(moveRight);
  return rightLoop;
}

rightButton.onmousedown = moveRight;
rightButton.onmouseup = function () {
  window.cancelAnimationFrame(rightLoop);
}

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 fiddly math involved, but you will soon get used to it with a bit of experimentation.

Specifications

Specification Status Comment
Web Audio API
The definition of 'AudioListener' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 10.0webkit
(Yes) (unprefixed)
25.0 (25.0)  No support 15.0webkit
22 (unprefixed)
6.0webkit
Position, forward, and up properties 52.0     39  
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? (Yes)
(Yes) (unprefixed)
26.0 1.2 ? ? ? 33.0
(Yes) (unprefixed)
Position, forward, and up properties No support 52.0       39   52.0

See also

Tag del documento e collaboratori

 Hanno collaborato alla realizzazione di questa pagina: Sheppy, jpmedley, teoli, chrisdavidmills, padenotmoz, fscholz, JesseTG, anirudh_venkatesh, sabrish, kscarfone
 Ultima modifica di: Sheppy,