src/resonance-audio.js
function ResonanceAudio(context, options) {
  // Public variables.
  /**
   * Binaurally-rendered stereo (2-channel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}.
   * @member {AudioNode} output
   * @memberof ResonanceAudio
   * @instance
   */
  /**
   * Ambisonic (multichannel) input {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For rendering input soundfields).
   * @member {AudioNode} ambisonicInput
   * @memberof ResonanceAudio
   * @instance
   */
  /**
   * Ambisonic (multichannel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For allowing external rendering / post-processing).
   * @member {AudioNode} ambisonicOutput
   * @memberof ResonanceAudio
   * @instance
   */

  // Use defaults for undefined arguments.
  if (options == undefined) {
    options = {};
  }
  if (options.ambisonicOrder == undefined) {
    options.ambisonicOrder = Utils.DEFAULT_AMBISONIC_ORDER;
  }
  if (options.listenerPosition == undefined) {
    options.listenerPosition = Utils.DEFAULT_POSITION.slice();
  }
  if (options.listenerForward == undefined) {
    options.listenerForward = Utils.DEFAULT_FORWARD.slice();
  }
  if (options.listenerUp == undefined) {
    options.listenerUp = Utils.DEFAULT_UP.slice();
  }
  if (options.dimensions == undefined) {
    options.dimensions = {};
    Object.assign(options.dimensions, Utils.DEFAULT_ROOM_DIMENSIONS);
  }
  if (options.materials == undefined) {
    options.materials = {};
    Object.assign(options.materials, Utils.DEFAULT_ROOM_MATERIALS);
  }
  if (options.speedOfSound == undefined) {
    options.speedOfSound = Utils.DEFAULT_SPEED_OF_SOUND;
  }

  // Create member submodules.
  this._ambisonicOrder = Encoder.validateAmbisonicOrder(options.ambisonicOrder);
  this._sources = [];
  this._room = new Room(context, {
    listenerPosition: options.listenerPosition,
    dimensions: options.dimensions,
    materials: options.materials,
    speedOfSound: options.speedOfSound,
  });
  this._listener = new Listener(context, {
    ambisonicOrder: options.ambisonicOrder,
    position: options.listenerPosition,
    forward: options.listenerForward,
    up: options.listenerUp,
  });

  // Create auxillary audio nodes.
  this._context = context;
  this.output = context.createGain();
  this.ambisonicOutput = context.createGain();
  this.ambisonicInput = this._listener.input;

  // Connect audio graph.
  this._room.output.connect(this._listener.input);
  this._listener.output.connect(this.output);
  this._listener.ambisonicOutput.connect(this.ambisonicOutput);
}
function ResonanceAudio(context, options) {
  // Public variables.
  /**
   * Binaurally-rendered stereo (2-channel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}.
   * @member {AudioNode} output
   * @memberof ResonanceAudio
   * @instance
   */
  /**
   * Ambisonic (multichannel) input {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For rendering input soundfields).
   * @member {AudioNode} ambisonicInput
   * @memberof ResonanceAudio
   * @instance
   */
  /**
   * Ambisonic (multichannel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For allowing external rendering / post-processing).
   * @member {AudioNode} ambisonicOutput
   * @memberof ResonanceAudio
   * @instance
   */

  // Use defaults for undefined arguments.
  if (options == undefined) {
    options = {};
  }
  if (options.ambisonicOrder == undefined) {
    options.ambisonicOrder = Utils.DEFAULT_AMBISONIC_ORDER;
  }
  if (options.listenerPosition == undefined) {
    options.listenerPosition = Utils.DEFAULT_POSITION.slice();
  }
  if (options.listenerForward == undefined) {
    options.listenerForward = Utils.DEFAULT_FORWARD.slice();
  }
  if (options.listenerUp == undefined) {
    options.listenerUp = Utils.DEFAULT_UP.slice();
  }
  if (options.dimensions == undefined) {
    options.dimensions = {};
    Object.assign(options.dimensions, Utils.DEFAULT_ROOM_DIMENSIONS);
  }
  if (options.materials == undefined) {
    options.materials = {};
    Object.assign(options.materials, Utils.DEFAULT_ROOM_MATERIALS);
  }
  if (options.speedOfSound == undefined) {
    options.speedOfSound = Utils.DEFAULT_SPEED_OF_SOUND;
  }

  // Create member submodules.
  this._ambisonicOrder = Encoder.validateAmbisonicOrder(options.ambisonicOrder);
  this._sources = [];
  this._room = new Room(context, {
    listenerPosition: options.listenerPosition,
    dimensions: options.dimensions,
    materials: options.materials,
    speedOfSound: options.speedOfSound,
  });
  this._listener = new Listener(context, {
    ambisonicOrder: options.ambisonicOrder,
    position: options.listenerPosition,
    forward: options.listenerForward,
    up: options.listenerUp,
  });

  // Create auxillary audio nodes.
  this._context = context;
  this.output = context.createGain();
  this.ambisonicOutput = context.createGain();
  this.ambisonicInput = this._listener.input;

  // Connect audio graph.
  this._room.output.connect(this._listener.input);
  this._listener.output.connect(this.output);
  this._listener.ambisonicOutput.connect(this.ambisonicOutput);
}
function ResonanceAudio ( context , options ) { // Public variables. /**
   * Binaurally-rendered stereo (2-channel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}.
   * @member {AudioNode} output
   * @memberof ResonanceAudio
   * @instance
   */ /**
   * Ambisonic (multichannel) input {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For rendering input soundfields).
   * @member {AudioNode} ambisonicInput
   * @memberof ResonanceAudio
   * @instance
   */ /**
   * Ambisonic (multichannel) output {@link
   * https://developer.mozilla.org/en-US/docs/Web/API/AudioNode AudioNode}
   * (For allowing external rendering / post-processing).
   * @member {AudioNode} ambisonicOutput
   * @memberof ResonanceAudio
   * @instance
   */ // Use defaults for undefined arguments. if ( options == undefined ) { options = { } ; } if ( options . ambisonicOrder == undefined ) { options . ambisonicOrder = Utils . DEFAULT_AMBISONIC_ORDER ; } if ( options . listenerPosition == undefined ) { options . listenerPosition = Utils . DEFAULT_POSITION . slice ( ) ; } if ( options . listenerForward == undefined ) { options . listenerForward = Utils . DEFAULT_FORWARD . slice ( ) ; } if ( options . listenerUp == undefined ) { options . listenerUp = Utils . DEFAULT_UP . slice ( ) ; } if ( options . dimensions == undefined ) { options . dimensions = { } ; Object . assign ( options . dimensions , Utils . DEFAULT_ROOM_DIMENSIONS ) ; } if ( options . materials == undefined ) { options . materials = { } ; Object . assign ( options . materials , Utils . DEFAULT_ROOM_MATERIALS ) ; } if ( options . speedOfSound == undefined ) { options . speedOfSound = Utils . DEFAULT_SPEED_OF_SOUND ; } // Create member submodules. this . _ambisonicOrder = Encoder . validateAmbisonicOrder ( options . ambisonicOrder ) ; this . _sources = [ ] ; this . _room = new Room ( context , { listenerPosition : options . listenerPosition , dimensions : options . dimensions , materials : options . materials , speedOfSound : options . speedOfSound , } ) ; this . _listener = new Listener ( context , { ambisonicOrder : options . ambisonicOrder , position : options . listenerPosition , forward : options . listenerForward , up : options . listenerUp , } ) ; // Create auxillary audio nodes. this . _context = context ; this . output = context . createGain ( ) ; this . ambisonicOutput = context . createGain ( ) ; this . ambisonicInput = this . _listener . input ; // Connect audio graph. this . _room . output . connect ( this . _listener . input ) ; this . _listener . output . connect ( this . output ) ; this . _listener . ambisonicOutput . connect ( this . ambisonicOutput ) ; }
Options for constructing a new ResonanceAudio scene.
@typedef {Object} ResonanceAudio~ResonanceAudioOptions
@property {Number} ambisonicOrder
Desired ambisonic Order. Defaults to
{@linkcode Utils.DEFAULT_AMBISONIC_ORDER DEFAULT_AMBISONIC_ORDER}.
@property {Float32Array} listenerPosition
The listener's initial position (in meters), where origin is the center of
the room. Defaults to {@linkcode Utils.DEFAULT_POSITION DEFAULT_POSITION}.
@property {Float32Array} listenerForward
The listener's initial forward vector.
Defaults to {@linkcode Utils.DEFAULT_FORWARD DEFAULT_FORWARD}.
@property {Float32Array} listenerUp
The listener's initial up vector.
Defaults to {@linkcode Utils.DEFAULT_UP DEFAULT_UP}.
@property {Utils~RoomDimensions} dimensions Room dimensions (in meters). Defaults to
{@linkcode Utils.DEFAULT_ROOM_DIMENSIONS DEFAULT_ROOM_DIMENSIONS}.
@property {Utils~RoomMaterials} materials Named acoustic materials per wall.
Defaults to {@linkcode Utils.DEFAULT_ROOM_MATERIALS DEFAULT_ROOM_MATERIALS}.
@property {Number} speedOfSound
(in meters/second). Defaults to
{@linkcode Utils.DEFAULT_SPEED_OF_SOUND DEFAULT_SPEED_OF_SOUND}.
 
@class ResonanceAudio
@description Main class for managing sources, room and listener models.
@param {AudioContext} context
Associated {@link
https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext}.
@param {ResonanceAudio~ResonanceAudioOptions} options
Options for constructing a new ResonanceAudio scene.
Options for constructing a new ResonanceAudio scene .