The setFilters()
method is a function in Three.js that allows users to apply a set of custom filters to an Audio
object. This enables you to modify the sound wave in real-time, resulting in a more immersive listening experience for your users.
audio.setFilters(filters);
filters
(Array): An array of AudioNode
objects representing the filters to be applied to the Audio
object.This method does not return a value.
// Create an AudioContext
const context = new AudioContext();
// Create an oscillator node
const oscillator = context.createOscillator();
oscillator.frequency.value = 440;
// Create a low-pass filter node
const lowPassFilter = context.createBiquadFilter();
lowPassFilter.type = 'lowpass';
lowPassFilter.frequency.value = 2000;
// Create a high-pass filter node
const highPassFilter = context.createBiquadFilter();
highPassFilter.type = 'highpass';
highPassFilter.frequency.value = 500;
// Create an Audio object with the oscillator as the source
const audio = new THREE.Audio(context);
audio.setNodeSource(oscillator);
// Apply the filters to the Audio object
audio.setFilters([lowPassFilter, highPassFilter]);
In this example, we create an AudioContext
and an OscillatorNode
to generate a simple sound wave. We create two BiquadFilterNode
s to serve as filters, one low-pass and one high-pass. We then create a new Audio
object and set its node source to the OscillatorNode
using the setNodeSource()
method.
Finally, we apply the filters to the Audio
object using the setFilters()
method, passing in an array of the two filter nodes created earlier.
setFilters()
method should be called after you have set the source node for the Audio
object using the setNodeSource()
method.setFilters()
method will affect the order in which they are applied to the sound wave. For example, if you want to apply a low-pass filter followed by a high-pass filter, you should add the low-pass filter node to the array before the high-pass filter node.createScriptProcessor()
method of the AudioContext
object. This allows you to apply complex signal processing algorithms to your sound wave in real-time.