Capítulo 299 de 859

Chapter 299: Audio

Core Idea

Represents a non-positional ( global ) audio object.

This and related audio modules make use of the Web Audio API.

Key Concepts

  • autoplay : boolean: Whether to start playback automatically or not.
  • buffer : AudioBuffer (readonly): A reference to an audio buffer.
  • context : AudioContext (readonly): The audio context.
  • detune : number (readonly): Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave.
  • duration : undefined | number: Overrides the default duration of the audio.
  • filters : Array.<AudioNode> (readonly): Can be used to apply a variety of low-order filters to create more complex sound effects e.g. via BiquadFilterNode.
  • gain : GainNode (readonly): The gain node used for volume control.
  • hasPlaybackControl : boolean (readonly): Indicates whether the audio playback can be controlled with method like Audio#play or Audio#pause.
  • isPlaying : boolean (readonly): Indicates whether the audio is playing or not.
  • listener : AudioListener (readonly): The global audio listener.

Code Examples

// 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();
});
  • What it demonstrates: Typical usage of Audio.

Reference Tables

Constructor

Signature
new Audio( listener : AudioListener )

Properties

PropertyDescription
.autoplay : booleanWhether to start playback automatically or not.
.buffer : AudioBuffer (readonly)A reference to an audio buffer.
.context : AudioContext (readonly)The audio context.
.detune : number (readonly)Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave.
`.duration : undefinednumber`
.filters : Array.<AudioNode> (readonly)Can be used to apply a variety of low-order filters to create more complex sound effects e.g. via BiquadFilterNode.
.gain : GainNode (readonly)The gain node used for volume control.
.hasPlaybackControl : boolean (readonly)Indicates whether the audio playback can be controlled with method like Audio#play or Audio#pause.
.isPlaying : boolean (readonly)Indicates whether the audio is playing or not.
.listener : AudioListener (readonly)The global audio listener.
.loop : boolean (readonly)Whether the audio should loop or not.
.loopEnd : numberDefines where in the audio buffer the replay should stop, in seconds.
.loopStart : numberDefines where in the audio buffer the replay should start, in seconds.
.offset : numberAn offset to the time within the audio buffer the playback should begin, in seconds.
.playbackRate : number (readonly)The playback speed.
.source : AudioNode (readonly)Holds a reference to the current audio source.
`.sourceType : 'empty''audioNode'

Methods

MethodDescription
.connect() : AudioConnects to the audio source. This is used internally on initialisation and when setting / removing filters.
`.disconnect() : Audioundefined`
.getDetune() : numberReturns the detuning of oscillation in cents.
`.getFilter() : AudioNodeundefined`
.getFilters() : Array.<AudioNode>Returns the current set filters.
.getLoop() : booleanReturns the loop flag.
.getOutput() : GainNodeReturns the output audio node.
.getPlaybackRate() : numberReturns the current playback rate.
.getVolume() : numberReturns the volume.
.onEnded()Automatically called when playback finished.
`.pause() : Audioundefined`
`.play( delay : number ) : Audioundefined`
.setBuffer( audioBuffer : AudioBuffer ) : AudioSets the given audio buffer as the source of this instance.
.setDetune( value : number ) : AudioDefines the detuning of oscillation in cents.
.setFilter( filter : AudioNode ) : AudioApplies a single filter node to the audio.
.setFilters( value : Array.<AudioNode> ) : AudioSets an array of filters and connects them with the audio source.
`.setLoop( value : boolean ) : Audioundefined`
.setLoopEnd( value : number ) : AudioSets the loop end value which defines where in the audio buffer the replay should stop, in seconds.
.setLoopStart( value : number ) : AudioSets the loop start value which defines where in the audio buffer the replay should start, in seconds.
.setMediaElementSource( mediaElement : HTMLMediaElement ) : AudioSets the given media element as the source of this instance.
.setMediaStreamSource( mediaStream : MediaStream ) : AudioSets the given media stream as the source of this instance.
.setNodeSource( audioNode : AudioNode ) : AudioSets the given audio node as the source of this instance.
`.setPlaybackRate( value : number ) : Audioundefined`
.setVolume( value : number ) : AudioSets the volume.
`.stop( delay : number ) : Audioundefined`

Key Takeaways

  1. Audio extends: EventDispatcher → Object3D
  2. Most relevant properties: autoplay, buffer, context, detune.
  3. Key methods: connect, disconnect, getDetune, getFilter.

Connects To