AudioListener

翻译不完整。 请帮助我们翻译这篇文章!

AudioListener 接口代表了人听音乐场景时声音的位置和方向, 和用于音频空间化。 所有PannerNode 相对于 AudioListener 的空间化储存在BaseAudioContext.listener 属性里.

特别需要注意的是一个环境中只能有一个收听者而且这不是AudioNode.

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

Properties

position,forward和up值是以不同的语法设置和检索的。检索是通过访问来实现的,比如说 AudioListener.positionX ,设置相同属性时可以通过使用 AudioListener.positionX.value 来完成。 这就是为什么他们不被标记为只读, 这在规范的接口定义中就是这么说的.

AudioListener.positionX
在笛卡尔右手坐标系中代表一个收听者的水平坐标。默认值是 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
在相同的笛卡尔坐标系中代表了收听者的面向的方向的水平坐标,就像 (positionX, positionY, 和 positionZ) 值一样。前方和上方值互相线性无关。默认值是 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 -1.
AudioListener.upX
代表了收听者头顶在笛卡尔坐标系的水平位置,就像 (positionX, positionY, 和positionZ) 值一样。 前方和上方值线性无关。默认值是 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 1.
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

AudioListener.setOrientation()
设置收听者的方向。
AudioListener.setPosition()
设置收听者的位置。

Note: Although these methods are deprecated they are currently the only way to set the orientation and position in Firefox, Internet Explorer and Safari.

Deprecated features

AudioListener.dopplerFactor
一个Double值,表示在呈现 多普勒效应 时要使用的音高偏移量。
AudioListener.speedOfSound
一个Double值代表了声音的速度,以米每秒的单位计算。

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.

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.

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!

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.

Specifications

Specification Status Comment
Web Audio API
AudioListener
Working Draft

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
AudioListenerChrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
dopplerFactor
Deprecated
Chrome No support 14 — 56Edge No support 12 — 79Firefox No support 25 — 63IE No support NoOpera No support 15 — 43Safari Full support 6WebView Android No support ? — 56Chrome Android No support 18 — 56Firefox Android No support 26 — 63Opera Android No support 14 — 43Safari iOS ? Samsung Internet Android No support 1.0 — 6.0
forwardXChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
forwardYChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
forwardZChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
positionXChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
positionYChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
positionZChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
setOrientation
DeprecatedNon-standard
Chrome Full support 14Edge Full support 18Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
setPosition
DeprecatedNon-standard
Chrome Full support 14Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 15Safari Full support 6WebView Android Full support YesChrome Android Full support 18Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support YesSamsung Internet Android Full support 1.0
speedOfSound
Deprecated
Chrome No support 14 — 56Edge No support 12 — 79Firefox No support 25 — 63IE No support NoOpera No support 15 — 43Safari Full support 6WebView Android No support ? — 56Chrome Android No support 18 — 56Firefox Android No support 26 — 63Opera Android No support 14 — 43Safari iOS ? Samsung Internet Android No support 1.0 — 6.0
upXChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
upYChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0
upZChrome Full support 52Edge Full support ≤18Firefox No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
IE No support NoOpera Full support 39Safari No support NoWebView Android Full support 52Chrome Android Full support 52Firefox Android No support No
Notes
No support No
Notes
Notes Supports a deprecated way of setting orientation - the setOrientation() method.
Opera Android Full support 41Safari iOS ? Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

See also