[page:Object3D] →

[name]

Create a non-positional ( global ) audio object.

This uses the [link:https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API 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

[name]( [param:AudioListener listener] )

listener — (required) [page:AudioListener AudioListener] instance.

Properties

[property:Boolean autoplay]

Whether to start playback automatically. Default is `false`.

[property:AudioContext context]

The [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext AudioContext] of the [page:AudioListener listener] given in the constructor.

[property:Number detune]

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

[property:Array filters]

Represents an array of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioNode 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 [link:https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode BiquadFilterNodes]. Filters are set via [page:Audio.setFilter] or [page:Audio.setFilters].

[property:GainNode gain]

A [link:https://developer.mozilla.org/en-US/docs/Web/API/GainNode GainNode] created using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain AudioContext.createGain]().

[property:Boolean hasPlaybackControl]

Whether playback can be controlled using the [page:Audio.play play](), [page:Audio.pause pause]() etc. methods. Default is `true`.

[property:Boolean isPlaying]

Whether the audio is currently playing.

[property:AudioListener listener]

A reference to the listener object of this audio.

[property:Number playbackRate]

Speed of playback. Default is `1`.

[property:Number offset]

An offset to the time within the audio buffer that playback should begin. Same as the `offset` parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is `0`.

[property:Number duration]

Overrides the duration of the audio. Same as the `duration` parameter of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start AudioBufferSourceNode.start](). Default is `undefined` to play the whole buffer.

[property:String source]

An [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode AudioBufferSourceNode] created using [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource AudioContext.createBufferSource]().

[property:String sourceType]

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

[property:String type]

String denoting the type, set to 'Audio'.

Methods

[method:this connect]()

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

[method:this disconnect]()

Disconnect from the [page:Audio.source]. This is used internally when setting / removing filters.

[method:Float getDetune]()

Returns the detuning of oscillation in cents.

[method:BiquadFilterNode getFilter]()

Returns the first element of the [page:Audio.filters filters] array.

[method:Array getFilters]()

Returns the [page:Audio.filters filters] array.

[method:Boolean getLoop]()

Return the value of [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] (whether playback should loop).

[method:GainNode getOutput]()

Return the [page:Audio.gain gainNode].

[method:Float getPlaybackRate]()

Return the value of [page:Audio.playbackRate playbackRate].

[method:Float getVolume]( value )

Return the current volume.

[method:this play]( delay )

If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, starts playback.

[method:this pause]()

If [page:Audio.hasPlaybackControl hasPlaybackControl] is true, pauses playback.

[method:undefined onEnded]()

Called automatically when playback finished.

[method:this setBuffer]( audioBuffer )

Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'buffer'.
If [page:Audio.autoplay autoplay], also starts playback.

[method:this setDetune]( [param:Float value] )

Defines the detuning of oscillation in cents.

[method:this setFilter]( filter )

Applies a single filter node to the audio.

[method:this setFilters]( [param:Array value] )

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

[method:this setLoop]( [param:Boolean value] )

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop source.loop] to `value` (whether playback should loop).

[method:this setLoopStart]( [param:Float value] )

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart source.loopStart] to `value`.

[method:this setLoopEnd]( [param:Float value] )

Set [link:https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd source.loopEnd] to `value`.

[method:this setMediaElementSource]( mediaElement )

Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement HTMLMediaElement] as the source of this audio.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

[method:this setMediaStreamSource]( mediaStream )

Applies the given object of type [link:https://developer.mozilla.org/en-US/docs/Web/API/MediaStream MediaStream] as the source of this audio.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

[method:this setNodeSource]( audioNode )

Setup the [page:Audio.source source] to the audioBuffer, and sets [page:Audio.sourceType sourceType] to 'audioNode'.
Also sets [page:Audio.hasPlaybackControl hasPlaybackControl] to false.

[method:this setPlaybackRate]( [param:Float value] )

If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, set the [page:Audio.playbackRate playbackRate] to `value`.

[method:this setVolume]( [param:Float value] )

Set the volume.

[method:this stop]()

If [page:Audio.hasPlaybackControl hasPlaybackControl] is enabled, stops playback.

Source

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]