AudioListener.setOrientation()

The setOrientation() method of the AudioListener interface defines the orientation of the listener.

It consists of two direction vectors:

  • The front vector, defined by the three unitless parameters x, y and z, describes the direction of the face of the listener, that is the direction the nose of the person is pointing towards. The front vector's default value is (0, 0, -1).
  • The up vector, defined by three unitless parameters xUp, yUp and zUp, describes the direction of the top of the listener's head. The up vector's default value is (0, 1, 0).

Both vectors must be separated by an angle of 90° — in linear analysis terms, they must be linearly independent.

Syntax

JavaScript
var audioCtx = new AudioContext();
var myListener = audioCtx.listener;
myListener.setOrientation(0,0,-1,0,1,0);

Returns

Void.

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.

This example also recreates the doppler effect — when movement is initiated in any direction, velocity is set on the panner (again, see positionPanner()), which is cancelled out onmouseup.

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!

js;highlight[6,7,26,30,31,32,33,37,62]
// 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.setOrientation(1,0,0);

var listener = audioCtx.listener;
listener.dopplerFactor = 1;
listener.speedOfSound = 343.3;
listener.setOrientation(0,0,-1,0,1,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;

// Only x and z velocity needed, as the listener only moves left and right and in and out in this example. Never up and down.
var xVel = 0;
var zVel = 0;

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

xIterator = WIDTH/150;

// listener will always be in the same place for this demo
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() {
  panner.setPosition(xPos,yPos,zPos);
  panner.setVelocity(xVel,0,zVel);
  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;
  xVel = 17;

  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);
  xVel = 0;
  panner.setVelocity(0,0,0);
}

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

Parameters

x
The x value of the front vector of the listener.
y
The y value of the front vector of the listener.
z
The z value of the front vector of the listener.
xUp
The x value of the up vector of the listener.
yUp
The y value of the up vector of the listener.
zUp
The z value of the up vector of the listener.

Specifications

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

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 10.0webkit 25.0 (25.0)  Not supported 15.0webkit
22 (unprefixed)
6.0webkit
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 26.0 1.2 ? ? ? 33.0

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/audiolistener/setorientation

API AudioListener Method Reference Référence setOrientation Web Audio API