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.

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

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!

The PannerNode brings a spatial position and velocity and a directionality for a given signal.

Number of inputs 1
Number of outputs 1
Channel count mode "clamped-max"
Channel count 2
Channel interpretation "speakers"

Properties

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
Is a double value describing the angle, in degrees, of a cone inside of which there will be no volume reduction.
PannerNode.coneOuterAngle
A double value describing the angle, in degrees, of a cone outside of which the volume will be reduced by a constant value, defined by the coneOuterGain attribute.
PannerNode.coneOuterGain
A double value describing the amount of volume reduction outside the cone defined by the coneOuterAngle attribute. Its default value is 0, meaning that no sound can be heard.
PannerNode.distanceModel
An enumerated value determining which algorithm to use to reduce the volume of the audio source as it moves away from the listener.
PannerNode.maxDistance
A double value representing the maximum distance between the audio source and the listener, after which the volume is not reduced any further.
PannerNode.orientationX
Represents the horizontal position of the audio source's vector in a right-hand cartesian coordinate sytem. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 1.
PannerNode.orientationY
Represents the vertical position of the audio source's vector in a right-hand cartesian coordinate sytem. The default is 0. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 0.
PannerNode.orientationZ
Represents the longitudinal (back and forth) position of the audio source's vector in a right-hand cartesian coordinate sytem. The default is 0. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 0.
PannerNode.panningModel
An enumerated value determining which spatialisation algorithm to use to position the audio in 3D space.
PannerNode.positionX
Represents the horizontal position of the audio in a right-hand cartesian coordinate sytem. The default is 0. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 0.
PannerNode.positionY
Represents the vertical position of the audio in a right-hand cartesian coordinate sytem. The default is 0. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 0.
PannerNode.positionZ
Represents the longitudinal (back and forth) position of the audio in a right-hand cartesian coordinate sytem. The default is 0. While this AudioParam cannot be directly changed, its value can be altered using its value property. The default is value is 0.
PannerNode.refDistance
A double value representing the reference distance for reducing volume as the audio source moves further from the listener.
PannerNode.rolloffFactor
A double value describing how quickly the volume is reduced as the source moves away from the listener. This value is used by all distance models.

Methods

Inherits methods from its parent, AudioNode.

PannerNode.setPosition()
Defines the position of the audio source relative to the listener (represented by an AudioListener object stored in the AudioContext.listener attribute.)
PannerNode.setOrientation()
Defines the direction the audio source is playing in.
PannerNode.setVelocity()
Defines the velocity vector of the audio source — how fast it is moving and in what direction. In a previous version of the specification, the PannerNode had a velocity that could pitch up or down AudioBufferSourceNodes connected downstream. This feature was not clearly specified and had a number of issues, so it was removed from the specification.

Examples

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 'PannerNode' in that specification.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 10.0webkit 25 (25)  No support 15.0webkit
22 (unprefixed)
6.0webkit
orientationX, orientationY, and orientationZ ? 50 (50) No support ? ?
positionX, positionY, and positionZ ? 50 (50) No support ? ?
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 26.0 (26) 1.2 No support ? ? 33.0
orientationX, orientationY, and orientationZ ? 50.0 (50) ? No support ? ? ?
positionX, positionY, and positionZ ? 50.0 (50) ? No support ? ? ?

See also

文档标签和贡献者

 最后编辑者: Sheppy,