UNPKG

2.59 kBTypeScriptView Raw
1import { Audio } from "./Audio.js";
2
3/**
4 * Create a {@link AudioAnalyser} object, which uses an {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} to analyse audio data.
5 * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web Audio API}.
6 * @example
7 * ```typescript
8 * // create an AudioListener and add it to the camera
9 * const listener = new THREE.AudioListener();
10 * camera.add(listener);
11 * // create an Audio source
12 * const sound = new THREE.Audio(listener);
13 * // load a sound and set it as the Audio object's buffer
14 * const audioLoader = new THREE.AudioLoader();
15 * audioLoader.load('sounds/ambient.ogg', function (buffer) {
16 * sound.setBuffer(buffer);
17 * sound.setLoop(true);
18 * sound.setVolume(0.5);
19 * sound.play();
20 * });
21 * // create an AudioAnalyser, passing in the sound and desired fftSize
22 * const analyser = new THREE.AudioAnalyser(sound, 32);
23 * // get the average frequency of the sound
24 * const data = analyser.getAverageFrequency();
25 * ```
26 * @see Example: {@link https://threejs.org/examples/#webaudio_sandbox | webaudio / sandbox }
27 * @see Example: {@link https://threejs.org/examples/#webaudio_visualizer | webaudio / visualizer }
28 * @see {@link https://threejs.org/docs/index.html#api/en/audio/AudioAnalyser | Official Documentation}
29 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/AudioAnalyser.js | Source}
30 */
31export class AudioAnalyser {
32 /**
33 * Create a new {@link {@link AudioAnalyser} | AudioAnalyser}.
34 * @param audio
35 * @param fftSize See {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/fftSize | AnalyserNode.fftSize }. Expects a `unsigned integer`. Default `2048`.
36 */
37 constructor(audio: Audio<AudioNode>, fftSize?: number);
38
39 /**
40 * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode | AnalyserNode} used to analyze audio.
41 */
42 analyser: AnalyserNode;
43
44 /**
45 * A Uint8Array with size determined by {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/frequencyBinCount | analyser.frequencyBinCount} used to hold analysis data.
46 */
47 data: Uint8Array;
48
49 /**
50 * Uses the Web Audio's {@link https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData | getByteFrequencyData} method
51 */
52 getFrequencyData(): Uint8Array;
53
54 /**
55 * Get the average of the frequencies returned by the {@link AudioAnalyser.getFrequencyData | getFrequencyData} method.
56 */
57 getAverageFrequency(): number;
58}