Skip to main content

Object3D

Audio

Create a non-positional ( global ) audio object.

This uses the Web Audio API.

Code Example

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

Examples

[example:webaudio_sandbox webaudio / sandbox ]
[example:webaudio_visualizer webaudio / visualizer ]

Constructor

Audio

function Audio( listener: AudioListener ): void;  

listener — (required) AudioListener instance.

Properties

autoplay

autoplay: Boolean;  

Whether to start playback automatically. Default is false.

context

context: AudioContext;  

The AudioContext of the listener given in the constructor.

detune

detune: Number;  

Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave. Default is 0.

filters

filters: Array;  

Represents an array of AudioNodes. Can be used to apply a variety of low-order filters to create more complex sound effects. In most cases, the array contains instances of BiquadFilterNodes. Filters are set via Audio.setFilter or Audio.setFilters.

gain

gain: GainNode;  

A GainNode created using AudioContext.createGain().

hasPlaybackControl

hasPlaybackControl: Boolean;  

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

isPlaying

isPlaying: Boolean;  

Whether the audio is currently playing.

listener

listener: AudioListener;  

A reference to the listener object of this audio.

playbackRate

playbackRate: Number;  

Speed of playback. Default is 1.

offset

offset: Number;  

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

duration

duration: Number;  

Overrides the duration of the audio. Same as the duration parameter of AudioBufferSourceNode.start(). Default is undefined to play the whole buffer.

source

source: AudioNode;  

An AudioBufferSourceNode created using AudioContext.createBufferSource().

sourceType

sourceType: String;  

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

type

type: String;  

String denoting the type, set to 'Audio'.

Methods

connect

function connect( ): this;  

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

disconnect

function disconnect( ): this;  

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

getDetune

function getDetune( ): Float;  

Returns the detuning of oscillation in cents.

getFilter

function getFilter( ): BiquadFilterNode;  

Returns the first element of the filters array.

getFilters

function getFilters( ): Array;  

Returns the filters array.

getLoop

function getLoop( ): Boolean;  

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

getOutput

function getOutput( ): GainNode;  

Return the gainNode.

getPlaybackRate

function getPlaybackRate( ): Float;  

Return the value of playbackRate.

getVolume

function getVolume( ): Float;  

Return the current volume.

play

function play( ): this;  

If hasPlaybackControl is true, starts playback.

pause

function pause( ): this;  

If hasPlaybackControl is true, pauses playback.

onEnded

function onEnded( ): undefined;  

Called automatically when playback finished.

setBuffer

function setBuffer( ): this;  

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

setDetune

function setDetune( value: Float ): this;  

Defines the detuning of oscillation in cents.

setFilter

function setFilter( ): this;  

Applies a single filter node to the audio.

setFilters

function setFilters( value: Array ): this;  

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

setLoop

function setLoop( value: Boolean ): this;  

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

setLoopStart

function setLoopStart( value: Float ): this;  

Set source.loopStart to value.

setLoopEnd

function setLoopEnd( value: Float ): this;  

Set source.loopEnd to value.

setMediaElementSource

function setMediaElementSource( ): this;  

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

setMediaStreamSource

function setMediaStreamSource( ): this;  

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

setNodeSource

function setNodeSource( ): this;  

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

setPlaybackRate

function setPlaybackRate( value: Float ): this;  

If hasPlaybackControl is enabled, set the playbackRate to value.

setVolume

function setVolume( value: Float ): this;  

Set the volume.

stop

function stop( ): this;  

If hasPlaybackControl is enabled, stops playback.

Source

src/audio/Audio.js