UNPKG

@babylonjs/core

Version:

Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.

158 lines (157 loc) 5.59 kB
import { Observable } from "../../Misc/observable.js"; import type { Nullable } from "../../types.js"; import { SoundState } from "../soundState.js"; import { AbstractNamedAudioNode } from "./abstractAudioNode.js"; import type { _AbstractSoundInstance } from "./abstractSoundInstance.js"; import type { PrimaryAudioBus } from "./audioBus.js"; import type { AudioEngineV2 } from "./audioEngineV2.js"; import type { _AbstractAudioSubGraph } from "./subNodes/abstractAudioSubGraph.js"; import type { IVolumeAudioOptions } from "./subNodes/volumeAudioSubNode.js"; import type { AbstractAudioAnalyzer, IAudioAnalyzerOptions } from "./subProperties/abstractAudioAnalyzer.js"; import type { AbstractSpatialAudio, ISpatialAudioOptions } from "./subProperties/abstractSpatialAudio.js"; import type { AbstractStereoAudio, IStereoAudioOptions } from "./subProperties/abstractStereoAudio.js"; /** @internal */ export interface IAbstractSoundOptionsBase { /** * Whether the sound should start playing automatically. Defaults to `false`. */ autoplay: boolean; /** * The maximum number of instances that can play at the same time. Defaults to `Infinity`. */ maxInstances: number; } /** @internal */ export interface IAbstractSoundPlayOptionsBase { /** * Whether the sound should loop. Defaults to `false`. */ loop: boolean; /** * The time within the sound buffer to start playing at, in seconds. Defaults to `0`. */ startOffset: number; } /** * Options for creating a sound. */ export interface IAbstractSoundOptions extends IAbstractSoundOptionsBase, IAbstractSoundPlayOptions, IAudioAnalyzerOptions, ISpatialAudioOptions, IStereoAudioOptions { /** * The output bus for the sound. Defaults to `null`. * - If not set or `null`, the sound is automatically connected to the audio engine's default main bus. * @see {@link AudioEngineV2.defaultMainBus} */ outBus: Nullable<PrimaryAudioBus>; } /** * Options for playing a sound. */ export interface IAbstractSoundPlayOptions extends IAbstractSoundPlayOptionsBase, IVolumeAudioOptions { } /** * Options stored in a sound. * @internal */ export interface IAbstractSoundStoredOptions extends IAbstractSoundOptionsBase, IAbstractSoundPlayOptionsBase { } /** * Abstract class representing a sound in the audio engine. */ export declare abstract class AbstractSound extends AbstractNamedAudioNode { private _analyzer; private _newestInstance; private _outBus; private _privateInstances; private _state; protected _instances: ReadonlySet<_AbstractSoundInstance>; protected abstract readonly _options: IAbstractSoundStoredOptions; protected abstract _subGraph: _AbstractAudioSubGraph; /** * Observable for when the sound stops playing. */ readonly onEndedObservable: Observable<AbstractSound>; protected constructor(name: string, engine: AudioEngineV2); /** * The analyzer features of the sound. */ get analyzer(): AbstractAudioAnalyzer; /** * Whether the sound should start playing automatically. Defaults to `false`. */ get autoplay(): boolean; /** * The current playback time of the sound, in seconds. */ get currentTime(): number; set currentTime(value: number); /** * Whether the sound should loop. Defaults to `false`. */ get loop(): boolean; set loop(value: boolean); /** * The maximum number of instances that can play at the same time. Defaults to `Infinity`. */ get maxInstances(): number; set maxInstances(value: number); /** * The output bus for the sound. Defaults to `null`. * - If not set or `null`, the sound is automatically connected to the audio engine's default main bus. * @see {@link AudioEngineV2.defaultMainBus} */ get outBus(): Nullable<PrimaryAudioBus>; set outBus(outBus: Nullable<PrimaryAudioBus>); /** * The spatial features of the sound. */ abstract spatial: AbstractSpatialAudio; /** * The time within the sound buffer to start playing at, in seconds. Defaults to `0`. */ get startOffset(): number; set startOffset(value: number); /** * The state of the sound. */ get state(): SoundState; /** * The stereo features of the sound. */ abstract stereo: AbstractStereoAudio; /** * The output volume of the sound. */ get volume(): number; set volume(value: number); /** * Releases associated resources. */ dispose(): void; /** * Plays the sound. * - Triggers `onEndedObservable` if played for the full duration and the `loop` option is not set. * @param options The options to use when playing the sound. Options set here override the sound's options. */ abstract play(options?: Partial<IAbstractSoundPlayOptions>): void; /** * Pauses the sound. */ pause(): void; /** * Resumes the sound. */ resume(): void; /** * Stops the sound. * - Triggers `onEndedObservable` if the sound is playing. */ abstract stop(): void; protected _beforePlay(instance: _AbstractSoundInstance): void; protected _afterPlay(instance: _AbstractSoundInstance): void; protected _getNewestInstance(): Nullable<_AbstractSoundInstance>; protected _setState(state: SoundState): void; protected abstract _createInstance(): _AbstractSoundInstance; protected _stopExcessInstances(): void; private _onInstanceEnded; private _onOutBusDisposed; }