The setFilter()
method of the AudioListener
class in Three.js sets the AudioListener
's filter
property to the provided filter
parameter. The filter
parameter should be either a null
value or an instance of the AudioNode
interface.
setFilter(filter: AudioNode | null): AudioListener;
filter: AudioNode | null
- The filter to set for the AudioListener
. This can be null
or an instance of the AudioNode
interface.AudioListener
- The AudioListener
object.The AudioListener
object represents the listener, or the person who is listening to the audio. When a sound is played in Three.js, it is connected to an AudioNode
, which represents the individual sound source. This AudioNode
is then connected to the AudioListener
's filter
, which applies audio effects to the sound before it reaches the listener's ears.
The setFilter()
method allows you to set the filter
property of the AudioListener
. If you provide a null
value, the filter will be removed and the sound will play without any effects. If you provide an instance of the AudioNode
interface, the sound will be played with the effects applied by the specified filter.
Here is an example of how to use the setFilter()
method:
const listener = new THREE.AudioListener();
const sound = new THREE.Audio(listener);
const audioLoader = new THREE.AudioLoader();
audioLoader.load('sound.mp3', function(buffer) {
sound.setBuffer(buffer);
sound.setLoop(true);
sound.setVolume(0.5);
const filter = new THREE.BiquadFilterNode(listener.context, {
type: 'lowpass',
frequency: 1000,
Q: 10
});
listener.setFilter(filter);
});
In this example, an AudioListener
object is created, as well as an Audio
object which plays the sound loaded from the 'sound.mp3' file. The setBuffer()
, setLoop()
, and setVolume()
methods of the Audio
object are used to set the sound's buffer, loop setting, and volume, respectively.
A BiquadFilterNode
object is created with a 'lowpass'
filter type, a frequency of 1000, and a Q factor of 10. The setFilter()
method of the AudioListener
object is then called with the filter
parameter set to the BiquadFilterNode
object. This applies the low-pass filter to the sound before it is played back to the listener.