Class Sound

The base interface for any sound-producing entity, including individual sounds, groups, and playbacks. BaseSound

Hierarchy

  • PlaybackContainer<typeof FilterManager, this> & FilterManager<this>
    • Sound

Implements

Constructors

  • Parameters

    • url: string
    • buffer: undefined | IAudioBuffer
    • context: IAudioContext
    • globalGainNode: GainNode
    • soundType: SoundType = SoundType.Buffer
    • panType: PanType = "HRTF"
    • Optional _cacophony: Cacophony

    Returns Sound

Properties

_cacophony?: Cacophony
_filters: BiquadFilterNode[] = []
_playbackRate: number = 1
_position: Position = ...
_stereoPan: number = 0
_threeDOptions: PannerOptions = ...
_volume: number = 1
buffer?: IAudioBuffer
context: IAudioContext
eventEmitter: TypedEventEmitter<SoundEvents> = ...
globalGainNode: GainNode
loopCount: LoopCount = 0
panType: PanType = "HRTF"
playbacks: Playback[]
soundType: SoundType = SoundType.Buffer
url: string

Accessors

  • get duration(): number
  • Retrieves the duration of the sound in seconds. If the sound is based on an AudioBuffer, it returns the duration of the buffer. Otherwise, if the sound has not been played and is a MediaElementSource, it returns NaN, indicating that the duration is unknown or not applicable.

    Returns number

    The duration of the sound in seconds.

  • get filters(): BiquadFilterNode[]
  • Returns BiquadFilterNode[]

  • get isPlaying(): boolean
  • Returns a boolean indicating whether the object is currently playing. an object is playing if any of its playbacks are currently playing.

    Returns boolean

    True if the object is playing, false otherwise.

  • get playbackRate(): number
  • Returns number

  • set playbackRate(rate): void
  • Parameters

    • rate: number

    Returns void

  • get position(): Position
  • Retrieves the current 3D spatial position of the sound in the audio context. The position is returned as an array of three values[x, y, z].

    Returns Position

    The current position of the sound.

  • set position(position): void
  • Sets the 3D spatial position of the sound in the audio context. The position is an array of three values[x, y, z]. This method updates the position of all active playbacks of the sound.

    Parameters

    • position: Position

      The new position of the sound.

    Returns void

  • get stereoPan(): null | number
  • Returns null | number

  • set stereoPan(value): void
  • Parameters

    • value: number

    Returns void

  • get threeDOptions(): PannerOptions
  • Returns PannerOptions

  • set threeDOptions(options): void
  • Parameters

    • options: Partial<PannerOptions>

    Returns void

  • get volume(): number
  • Returns number

  • set volume(volume): void
  • Parameters

    • volume: number

    Returns void

Methods

  • Adds a BiquadFilterNode to the container's filter chain. Filters are applied in the order they are added.

    Parameters

    • filter: BiquadFilterNode

      The filter to add to the chain.

    Returns void

  • Parameters

    • filters: BiquadFilterNode[]

    Returns void

  • Parameters

    • connection: any

    Returns any

  • Returns void

  • Clones the current Sound instance, creating a deep copy with the option to override specific properties. This method allows for the creation of a new, independent Sound instance based on the current one, with the flexibility to modify certain attributes through the overrides parameter. This is particularly useful for creating variations of a sound without affecting the original instance. The cloned instance includes all properties, playback settings, and filters of the original, unless explicitly overridden.

    Parameters

    • overrides: Partial<SoundCloneOverrides> = {}

      An object specifying properties to override in the cloned instance. This can include audio settings like volume, playback rate, and spatial positioning, as well as more complex configurations like 3D audio options and filter adjustments.

    Returns Sound

    A new Sound instance that is a clone of the current sound.

  • Type Parameters

    • K extends keyof SoundEvents

    Parameters

    • event: K
    • data: SoundEvents[K]

    Returns void

  • Type Parameters

    • K extends keyof SoundEvents

    Parameters

    • event: K
    • data: SoundEvents[K]

    Returns Promise<void>

  • Sets or retrieves the loop behavior for the sound. If loopCount is provided, the sound will loop the specified number of times. If loopCount is 'infinite', the sound will loop indefinitely until stopped. If no argument is provided, the method returns the current loop count setting.

    Parameters

    • Optional loopCount: LoopCount

      The number of times to loop or 'infinite' for indefinite looping.

    Returns LoopCount

    The current loop count setting if no argument is provided.

  • Remove event listener.

    Type Parameters

    • K extends keyof SoundEvents

    Parameters

    • event: K
    • listener: ((data) => void)
        • (data): void
        • Parameters

          • data: SoundEvents[K]

          Returns void

    Returns void

  • Register event listener.

    Type Parameters

    • K extends keyof SoundEvents

    Parameters

    • event: K
    • listener: ((data) => void)
        • (data): void
        • Parameters

          • data: SoundEvents[K]

          Returns void

    Returns void

    Cleanup function

  • Generates a Playback instance for the sound without starting playback. This allows for pre-configuration of playback properties such as volume and position before the sound is actually played.

    Returns Playback[]

    An array of Playback instances that are ready to be played.

  • Removes a BiquadFilterNode from the container's filter chain. If the filter is not part of the chain, the method has no effect.

    Parameters

    • filter: BiquadFilterNode

      The filter to remove from the chain.

    Returns void

  • Parameters

    • filters: BiquadFilterNode[]

    Returns void

  • Seeks to a specific time within the sound's playback.

    Parameters

    • time: number

      The time in seconds to seek to. This method iterates through all active Playback instances and calls their seek() method with the specified time.

    Returns void

  • Returns void