Object3D

Audio

Create a non-positional (global) audio object.

This uses the Web Audio API.

Example

webaudio / sandbox
webaudio / visualizer

// create an AudioListener and add it to the camera var listener = new v3d.AudioListener(); camera.add(listener); // create a global audio source var sound = new v3d.Audio(listener); // load a sound and set it as the Audio object's buffer var audioLoader = new v3d.AudioLoader(); audioLoader.load('sounds/ambient.ogg', function(buffer) { sound.setBuffer(buffer); sound.setLoop(true); sound.setVolume(0.5); sound.play(); });

Constructor

Audio(listener : AudioListener)

listener — (required) AudioListener instance.

Properties

.autoplay : Boolean

Whether to start playback automatically. Default is false.

.context : AudioContext

The AudioContext of the listener given in the constructor.

.filters : Array

Represents an array of BiquadFilterNodes. Can be used to apply a variety of low-order filters to create more complex sound effects. Filters are set via Audio.setFilter or Audio.setFilters.

.gain : GainNode

A GainNode created using AudioContext.createGain().

.hasPlaybackControl : Boolean

Whether playback can be controlled using the play(), pause() etc. methods. Default is true.

.isPlaying : Boolean

Whether the audio is currently playing.

.listener : AudioListener

A reference to the listener object of this audio.

.playbackRate : Number

Speed of playback. Default is 1.

.startTime : Number

The time at which the sound should begin to play. Same as the when paramter of AudioBufferSourceNode.start(). Default is 0.

.offset : Number

An offset to the time within the audio buffer that playback should begin. Same as the offset paramter of AudioBufferSourceNode.start(). Default is 0.

.source : String

An AudioBufferSourceNode created using AudioContext.createBufferSource().

.sourceType : String

Type of the audio source. Default is string 'empty'.

.type : String

String denoting the type, set to 'Audio'.

Methods

.connect () : Audio

Connect to the Audio.source. This is used internally on initialisation and when setting / removing filters.

.disconnect () : Audio

Disconnect from the Audio.source. This is used internally when setting / removing filters.

.getFilter () : BiquadFilterNode

Returns the first element of the filters array.

.getFilters () : Array

Returns the filters array.

.getLoop () : Boolean

Return the value of source.loop (whether playback should loop).

.getOutput () : GainNode

Return the gainNode.

.getPlaybackRate () : Float

Return the value of playbackRate.

.getVolume (value) : Float

Return the current volume.

.play () : Audio

If hasPlaybackControl is true, starts playback.

.pause () : Audio

If hasPlaybackControl is true, pauses playback.

.onEnded () : null

Called automatically when playback finished. Sets If isPlaying to false.

.setBuffer (audioBuffer) : Audio

Setup the source to the audioBuffer, and sets sourceType to 'buffer'.
If autoplay, also starts playback.

.setFilter (filter) : Audio

Applies a single BiquadFilterNode to the audio.

.setFilters (value : Array) : Audio

value - arrays of filters.
Applies an array of BiquadFilterNodes to the audio.

.setLoop (value : Boolean) : Audio

Set source.loop to value (whether playback should loop).

.setMediaElementSource (mediaElement) : Audio

Applies the given object of type HTMLMediaElement as the source of this audio.
Also sets hasPlaybackControl to false.

.setNodeSource (audioNode) : Audio

Setup the source to the audioBuffer, and sets sourceType to 'audioNode'.
Also sets hasPlaybackControl to false.

.setPlaybackRate (value : Float) : Audio

If hasPlaybackControl is enabled, set the playbackRate to value.

.setVolume (value : Float) : Audio

Set the volume.

.stop () : Audio

If hasPlaybackControl is enabled, stops playback, resets startTime to 0 and sets isPlaying to false.

Source

For more info on how to obtain the source code of this module see this page.