API

Class Sound

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.

Index
  • Create 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: ISoundOptions

      Objects to provide with the current available options: autoplay, loop, volume, spatialSound, maxDistance, rolloffFactor, refDistance, distanceModel, panningModel, streaming

    Returns Sound

metadata: any = null

Gets or sets an object used to store user defined information for the sound.

onEndedObservable: Observable<Sound> = ...

Observable event when the current playing sound finishes.

refDistance: number = 1

Define the reference distance the sound should be heard perfectly.

rolloffFactor: number = 1

Define the roll off factor of spatial sounds.

soundTrackId: number = -1

The sound track id this sound belongs to.

useCustomAttenuation: boolean = false

Does the sound use a custom attenuation curve to simulate the falloff happening when the source gets further away from the camera.

  • Connect this sound to a sound track audio node like gain...

    Parameters

    • soundTrackAudioNode: AudioNode

      the sound track audio node to connect to

    Returns void

  • 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

  • Sets a new custom attenuation function for the sound.

    Parameters

    • callback: (
          currentVolume: number,
          currentDistance: number,
          maxDistance: number,
          refDistance: number,
          rolloffFactor: number,
      ) => number

      Defines the function used for the attenuation

    Returns void

  • Sets the data of the sound from an audiobuffer

    Parameters

    • audioBuffer: AudioBuffer

      The audioBuffer containing the data

    Returns void

  • 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

  • Set the sound play back rate

    Parameters

    • newPlaybackRate: number

      Define the playback rate the sound should be played at

    Returns void

  • Sets a dedicated volume for this sounds

    Parameters

    • newVolume: number

      Define the new volume of the sound

    • Optionaltime: number

      Define time for gradual change to new volume

    Returns void

  • 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: Sound

      Define a sound place holder if do not need to instantiate a new one

    Returns Sound

    the newly parsed sound