The getInput()
method of the AudioListener
class in three.js
returns the Web Audio API AnalyserNode
that is connected to the input of the AudioListener
.
AudioListener.getInput()
The getInput()
method returns an instance of the AnalyserNode
Web Audio API interface.
The getInput()
method is used to retrieve the AnalyserNode
that is connected to the input of the AudioListener
. This node can be used to perform real-time frequency analysis on the audio signal that is being received by the listener.
const listener = new THREE.AudioListener();
const analyser = listener.getInput();
// Connect an audio source to the listener
const audio = new THREE.Audio( listener );
const loader = new THREE.AudioLoader();
loader.load( 'path/to/audio.mp3', function( buffer ) {
audio.setBuffer( buffer );
audio.play();
});
// Perform real-time frequency analysis on the input signal
const dataArray = new Float32Array( analyser.frequencyBinCount );
analyser.getFloatFrequencyData(dataArray);
In this example, we create an AudioListener
and retrieve the AnalyserNode
for real-time frequency analysis. We then load an audio file and connect it to the listener. Finally, we use the AnalyserNode
to animate a mesh based on the audio frequency data.
const listener = new THREE.AudioListener();
const analyser = listener.getInput();
const audio = new THREE.Audio( listener );
const loader = new THREE.AudioLoader();
loader.load( 'path/to/audio.mp3', function( buffer ) {
audio.setBuffer( buffer );
audio.play();
});
const geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
const material = new THREE.MeshStandardMaterial({ color: 0xffffff });
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
const dataArray = new Float32Array( analyser.frequencyBinCount );
const animate = function () {
requestAnimationFrame( animate );
analyser.getFloatFrequencyData(dataArray);
const frequency = dataArray[0] || 0;
mesh.scale.set( 1+frequency/100, 1+frequency/100, 1+frequency/100 );
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
renderer.render( scene, camera );
};
animate();
The getInput()
method is supported in all modern browsers that support the Web Audio API.
The getInput()
method of the AudioListener
class in three.js
is used to retrieve the AnalyserNode
that is connected to the input of the listener. This node can be used to perform real-time frequency analysis on the audio signal that is being received by the listener.