Class Sound
Index
Constructors
Properties
Accessors
Methods
Constructors
constructor
-
new Sound(
name: string,
urlOrArrayBuffer: any,
scene?: Nullable<Scene>,
readyToPlayCallback?: Nullable<() => void>,
options?: ISoundOptions,
): SoundCreate a sound and attach it to a scene
Parameters
- name: string
Name of your sound
- urlOrArrayBuffer: any
Url to the sound to load async or ArrayBuffer, it also works with MediaStreams and AudioBuffers
Optionalscene: Nullable<Scene>defines the scene the sound belongs to
- readyToPlayCallback: Nullable<() => void> = null
Provide a callback function if you'd like to load your code once the sound is ready to be played
Optionaloptions: ISoundOptionsObjects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming
Returns Sound
- name: string
Properties
metadata
Gets or sets an object used to store user defined information for the sound.
on Ended Observable
Observable event when the current playing sound finishes.
ref Distance
Define the reference distance the sound should be heard perfectly.
rolloff Factor
Define the roll off factor of spatial sounds.
sound Track Id
The sound track id this sound belongs to.
use Custom Attenuation
Does the sound use a custom attenuation curve to simulate the falloff happening when the source gets further away from the camera.
Accessors
autoplay
-
get autoplay(): boolean
Does the sound autoplay once loaded.
Returns boolean
-
set autoplay(value: boolean): void
Parameters
- value: boolean
Returns void
current Time
-
get currentTime(): number
Gets the current time for the sound.
Returns number
directional Cone Inner Angle
-
get directionalConeInnerAngle(): number
Gets or sets the inner angle for the directional cone.
Returns number
-
set directionalConeInnerAngle(value: number): void
Gets or sets the inner angle for the directional cone.
Parameters
- value: number
Returns void
directional Cone Outer Angle
-
get directionalConeOuterAngle(): number
Gets or sets the outer angle for the directional cone.
Returns number
-
set directionalConeOuterAngle(value: number): void
Gets or sets the outer angle for the directional cone.
Parameters
- value: number
Returns void
distance Model
-
get distanceModel(): "linear" | "inverse" | "exponential"
Define the distance attenuation model the sound will follow.
Returns "linear" | "inverse" | "exponential"
-
set distanceModel(value: "linear" | "inverse" | "exponential"): void
Parameters
- value: "linear" | "inverse" | "exponential"
Returns void
is Paused
-
get isPaused(): boolean
Is this sound currently paused.
Returns boolean
is Playing
-
get isPlaying(): boolean
Is this sound currently played.
Returns boolean
loop
-
get loop(): boolean
Does the sound loop after it finishes playing once.
Returns boolean
-
set loop(value: boolean): void
Parameters
- value: boolean
Returns void
max Distance
-
get maxDistance(): number
Define the max distance the sound should be heard (intensity just became 0 at this point).
Returns number
-
set maxDistance(value: number): void
Parameters
- value: number
Returns void
name
-
get name(): string
The name of the sound in the scene.
Returns string
-
set name(value: string): void
Parameters
- value: string
Returns void
spatial Sound
-
get spatialSound(): boolean
Does this sound enables spatial sound.
Returns boolean
-
set spatialSound(newValue: boolean): void
Does this sound enables spatial sound.
Parameters
- newValue: boolean
Returns void
Methods
attach To Mesh
-
Attach the sound to a dedicated mesh
Parameters
- transformNode: TransformNode
The transform node to connect the sound with
Returns void
- transformNode: TransformNode
clone
connect To Sound Track Audio Node
-
Connect this sound to a sound track audio node like gain...
Parameters
- soundTrackAudioNode: AudioNode
the sound track audio node to connect to
Returns void
- soundTrackAudioNode: AudioNode
detach From Mesh
-
Detach the sound from the previously attached mesh
Returns void
dispose
-
Release the sound and its associated resources
Returns void
get Audio Buffer
-
Gets the current underlying audio buffer containing the data
Returns Nullable<AudioBuffer>
the audio buffer
get Class Name
-
Get the current class name.
Returns string
current class name
get Playback Rate
-
Gets the sound play back rate.
Returns number
the play back rate of the sound
get Sound Gain
-
Gets the WebAudio GainNode, gives you precise control over the gain of instances of this Sound.
Returns Nullable<GainNode>
the gain node
get Sound Source
-
Gets the WebAudio AudioBufferSourceNode, lets you keep track of and stop instances of this Sound.
Returns Nullable<AudioBufferSourceNode>
the source node
get Volume
-
Gets the volume of the sound.
Returns number
the volume of the sound
is Ready
-
Gets if the sounds is ready to be played or not.
Returns boolean
true if ready, otherwise false
pause
-
Put the sound in pause
Returns void
play
-
Play the sound
Parameters
Optionaltime: number(optional) Start the sound after X seconds. Start immediately (0) by default.
Optionaloffset: number(optional) Start the sound at a specific time in seconds
Optionallength: number(optional) Sound duration (in seconds)
Returns void
serialize
-
Serializes the Sound in a JSON representation
Returns any
the JSON representation of the sound
set Attenuation Function
-
setAttenuationFunction(
callback: (
currentVolume: number,
currentDistance: number,
maxDistance: number,
refDistance: number,
rolloffFactor: number,
) => number,
): voidSets a new custom attenuation function for the sound.
Parameters
- callback: (
currentVolume: number,
currentDistance: number,
maxDistance: number,
refDistance: number,
rolloffFactor: number,
) => numberDefines the function used for the attenuation
Returns void
- callback: (
set Audio Buffer
-
Sets the data of the sound from an audiobuffer
Parameters
- audioBuffer: AudioBuffer
The audioBuffer containing the data
Returns void
- audioBuffer: AudioBuffer
set Directional Cone
-
Transform this sound into a directional source
Parameters
- coneInnerAngle: number
Size of the inner cone in degree
- coneOuterAngle: number
Size of the outer cone in degree
- coneOuterGain: number
Volume of the sound outside the outer cone (between 0.0 and 1.0)
Returns void
- coneInnerAngle: number
set Local Direction To Mesh
-
Sets the local direction of the emitter if spatial sound is enabled
Parameters
- newLocalDirection: Vector3
Defines the new local direction
Returns void
- newLocalDirection: Vector3
set Playback Rate
-
Set the sound play back rate
Parameters
- newPlaybackRate: number
Define the playback rate the sound should be played at
Returns void
- newPlaybackRate: number
set Position
-
Sets the position of the emitter if spatial sound is enabled
Parameters
- newPosition: Vector3
Defines the new position
Returns void
- newPosition: Vector3
set Volume
-
Sets a dedicated volume for this sounds
Parameters
- newVolume: number
Define the new volume of the sound
Optionaltime: numberDefine time for gradual change to new volume
Returns void
- newVolume: number
stop
-
Stop the sound
Parameters
Optionaltime: number(optional) Stop the sound after X seconds. Stop immediately (0) by default.
Returns void
switch Panning Model To Equal Power
-
Switch the panning model to Equal Power: Represents the equal-power panning algorithm, generally regarded as simple and efficient. equalpower is the default value.
Returns void
switch Panning Model To HRTF
-
Switch the panning model to HRTF: Renders a stereo output of higher quality than equalpower — it uses a convolution with measured impulse responses from human subjects.
Returns void
update Options
-
Updates the current sounds options such as maxdistance, loop...
Parameters
- options: ISoundOptions
A JSON object containing values named as the object properties
Returns void
- options: ISoundOptions
StaticParse
-
Parse a JSON representation of a sound to instantiate in a given scene
Parameters
- parsedSound: any
Define the JSON representation of the sound (usually coming from the serialize method)
- scene: Scene
Define the scene the new parsed sound should be created in
- rootUrl: string
Define the rooturl of the load in case we need to fetch relative dependencies
OptionalsourceSound: SoundDefine a sound place holder if do not need to instantiate a new one
Returns Sound
the newly parsed sound
- parsedSound: any
Defines a sound that can be played in the application. The sound can either be an ambient track or a simple sound played in reaction to a user action.
See
https://doc.babylonjs.com/legacy/audio