UNPKG

9.33 kBTypeScriptView Raw
1import { Object3D } from "../core/Object3D.js";
2import { AudioContext } from "./AudioContext.js";
3import { AudioListener } from "./AudioListener.js";
4
5// Extras / Audio /////////////////////////////////////////////////////////////////////
6
7/**
8 * Create a non-positional ( global ) {@link Audio} object.
9 * This uses the {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API | Web {@link Audio} API}.
10 * @example
11 * ```typescript
12 * // create an AudioListener and add it to the camera
13 * const listener = new THREE.AudioListener();
14 * camera.add(listener);
15 * // create a global {@link Audio} source
16 * const sound = new THREE.Audio(listener);
17 * // load a sound and set it as the {@link Audio} object's buffer
18 * const audioLoader = new THREE.AudioLoader();
19 * audioLoader.load('sounds/ambient.ogg', function (buffer) {
20 * sound.setBuffer(buffer);
21 * sound.setLoop(true);
22 * sound.setVolume(0.5);
23 * sound.play();
24 * });
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/Audio | Official Documentation}
29 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/audio/Audio.js | Source}
30 */
31export class Audio<NodeType extends AudioNode = GainNode> extends Object3D {
32 /**
33 * Create a new instance of {@link Audio}
34 * @param listener (required) {@link AudioListener | AudioListener} instance.
35 */
36 constructor(listener: AudioListener);
37
38 /**
39 * A Read-only _string_ to check if `this` object type.
40 * @remarks Sub-classes will update this value.
41 * @defaultValue `Audio`
42 */
43 readonly type: string | "Audio";
44
45 /**
46 * A reference to the listener object of this audio.
47 */
48 listener: AudioListener;
49
50 /**
51 * The {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext | AudioContext} of the {@link AudioListener | listener} given in the constructor.
52 */
53 context: AudioContext;
54
55 /**
56 * A {@link https://developer.mozilla.org/en-US/docs/Web/API/GainNode | GainNode} created using
57 * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createGain | AudioContext.createGain}().
58 */
59 gain: GainNode;
60
61 /**
62 * Whether to start playback automatically.
63 * @defaultValue `false`
64 */
65 autoplay: boolean;
66
67 buffer: AudioBuffer | null;
68
69 /**
70 * Modify pitch, measured in cents. +/- 100 is a semitone. +/- 1200 is an octave.
71 * @defaultValue `0`
72 */
73 detune: number;
74
75 /**
76 * @default false
77 */
78 loop: boolean;
79
80 /**
81 * @default 0
82 */
83 loopStart: number;
84
85 /**
86 * @default 0
87 */
88 loopEnd: number;
89
90 /**
91 * An offset to the time within the {@link Audio} buffer that playback should begin.
92 * Same as the {@link Audio.offset | offset} parameter of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}.
93 * @defaultValue `0`
94 */
95 offset: number;
96
97 /**
98 * Overrides the duration of the audio. Same as the {@link Audio.duration | duration} parameter of
99 * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/start | AudioBufferSourceNode.start()}.
100 * @defaultValue `undefined` _to play the whole buffer_.
101 */
102 duration: number | undefined;
103
104 /**
105 * Speed of playback.
106 * @defaultValue `1`
107 */
108 playbackRate: number;
109
110 /**
111 * Whether the {@link Audio} is currently playing.
112 * @defaultValue `false`
113 */
114 isPlaying: boolean;
115
116 /**
117 * Whether playback can be controlled using the {@link Audio.play | play}(), {@link Audio.pause | pause}() etc. methods.
118 * @defaultValue `true`
119 */
120 hasPlaybackControl: boolean;
121
122 /**
123 * Type of the {@link Audio} source.
124 * @defaultValue 'empty'.
125 */
126 sourceType: string;
127
128 /**
129 * An {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode | AudioBufferSourceNode} created using
130 * {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/createBufferSource | AudioContext.createBufferSource()}.
131 */
132 source: AudioScheduledSourceNode | null;
133
134 /**
135 * Represents an array of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioNode | AudioNodes}.
136 * Can be used to apply a variety of low-order filters to create more complex sound effects.
137 * In most cases, the array contains instances of {@link https://developer.mozilla.org/en-US/docs/Web/API/BiquadFilterNode | BiquadFilterNodes}.
138 * Filters are set via {@link THREE.Audio.setFilter | Audio.setFilter} or {@link THREE.Audio.setFilters | Audio.setFilters}.
139 * @defaultValue `[]`
140 */
141 filters: AudioNode[];
142
143 /**
144 * Return the {@link Audio.gain | gainNode}.
145 */
146 getOutput(): NodeType;
147
148 /**
149 * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'audioNode'.
150 * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false.
151 */
152 setNodeSource(audioNode: AudioScheduledSourceNode): this;
153
154 /**
155 * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement | HTMLMediaElement} as the source of this audio.
156 * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false.
157 */
158 setMediaElementSource(mediaElement: HTMLMediaElement): this;
159
160 /**
161 * Applies the given object of type {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaStream | MediaStream} as the source of this audio.
162 * @remarks Also sets {@link Audio.hasPlaybackControl | hasPlaybackControl} to false.
163 */
164 setMediaStreamSource(mediaStream: MediaStream): this;
165
166 /**
167 * Setup the {@link Audio.source | source} to the audioBuffer, and sets {@link Audio.sourceType | sourceType} to 'buffer'.
168 * @remarks If {@link Audio.autoplay | autoplay}, also starts playback.
169 */
170 setBuffer(audioBuffer: AudioBuffer): this;
171
172 /**
173 * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, starts playback.
174 */
175 play(delay?: number): this;
176
177 /**
178 * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is true, pauses playback.
179 */
180 pause(): this;
181
182 /**
183 * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, stops playback.
184 * @param delay (optional) - The delay, in seconds, at which the audio should start playing.
185 */
186 stop(delay?: number): this;
187
188 /**
189 * Called automatically when playback finished.
190 */
191 onEnded(): void;
192
193 /**
194 * Connect to the {@link THREE.Audio.source | Audio.source}
195 * @remarks This is used internally on initialisation and when setting / removing filters.
196 */
197 connect(): this;
198 /**
199 * Disconnect from the {@link THREE.Audio.source | Audio.source}
200 * @remarks This is used internally when setting / removing filters.
201 */
202 disconnect(): this;
203
204 /**
205 * Returns the detuning of oscillation in cents.
206 */
207 getDetune(): number;
208 /**
209 * Defines the detuning of oscillation in cents.
210 * @param value Expects a `Float`
211 */
212 setDetune(value: number): this;
213
214 /**
215 * Returns the first element of the {@link Audio.filters | filters} array.
216 */
217 getFilter(): AudioNode;
218 /**
219 * Applies a single filter node to the audio.
220 */
221 setFilter(filter: AudioNode): this;
222
223 /**
224 * Returns the {@link Audio.filters | filters} array.
225 */
226 getFilters(): AudioNode[];
227 /**
228 * Applies an array of filter nodes to the audio.
229 * @param value Arrays of filters.
230 */
231 setFilters(value: AudioNode[]): this;
232
233 /**
234 * Return the value of {@link Audio.playbackRate | playbackRate}.
235 */
236 getPlaybackRate(): number;
237 /**
238 * If {@link Audio.hasPlaybackControl | hasPlaybackControl} is enabled, set the {@link Audio.playbackRate | playbackRate} to `value`.
239 * @param value Expects a `Float`
240 */
241 setPlaybackRate(value: number): this;
242
243 /**
244 * Return the value of {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} (whether playback should loop).
245 */
246 getLoop(): boolean;
247 /**
248 * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loop | source.loop} to `value` (whether playback should loop).
249 * @param value
250 */
251 setLoop(value: boolean): this;
252
253 /**
254 * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopStart | source.loopStart} to `value`.
255 * @param value Expects a `Float`
256 */
257 setLoopStart(value: number): this;
258 /**
259 * Set {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBufferSourceNode/loopEnd | source.loopEnd} to `value`.
260 * @param value Expects a `Float`
261 */
262 setLoopEnd(value: number): this;
263
264 /**
265 * Return the current volume.
266 */
267 getVolume(): number;
268 /**
269 * Set the volume.
270 * @param value Expects a `Float`
271 */
272 setVolume(value: number): this;
273}