Class Playback

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

Hierarchy

  • BasePlayback
    • Playback

Implements

Constructors

  • Creates an instance of the Playback class.

    Parameters

    • origin: Sound

      The Sound instance that the Playback is associated with.

    • source: SourceNode

      The audio source node.

    • gainNode: GainNode

      The gain node for controlling volume.

    Returns Playback

    Throws

    Throws an error if an invalid pan type is provided.

Properties

_filters: BiquadFilterNode[] = []
_offset: number = 0
_panType: PanType = 'stereo'
_playbackRate: number = 1
_playing: boolean = false
_startTime: number = 0
_state: PlaybackState = PlaybackState.Unplayed
buffer?: IAudioBuffer
context: IAudioContext
currentLoop: number = 0
gainNode?: GainNode
loopCount: LoopCount = 0
origin: Sound

The Sound instance that the Playback is associated with.

panner?: PannerNode | StereoPannerNode
source?: SourceNode

Accessors

  • get duration(): number
  • Gets the duration of the audio in seconds.

    Returns number

    The duration of the audio or NaN if the duration is unknown.

    Throws

    Throws an error if the sound has been cleaned up.

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

  • get playbackRate(): number
  • Gets the current playback rate of the audio.

    Returns number

    The current playback rate.

  • set playbackRate(rate): void
  • Sets the playback rate of the audio.

    Parameters

    • rate: number

      The playback rate to set.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up or if the source type is unsupported.

  • get position(): Position
  • Gets the position of the audio source in 3D space (HRTF panning only).

    Returns Position

    The [x, y, z] coordinates of the audio source.

    Throws

    Throws an error if the sound has been cleaned up or if HRTF panning is not used.

  • set position(position): void
  • Sets the position of the audio source in 3D space (HRTF panning only).

    Parameters

    • position: Position

      The [x, y, z] coordinates of the audio source.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up or if HRTF panning is not used.

  • set sourceLoop(loop): void
  • Sets whether the audio source should loop.

    Parameters

    • loop: boolean

      Whether the audio should loop.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up.

  • get stereoPan(): null | number
  • Gets the stereo panning value.

    Returns null | number

    The current stereo pan value, or null if stereo panning is not applicable.

    Throws

    Throws an error if stereo panning is not available or if the sound has been cleaned up.

  • set stereoPan(value): void
  • Sets the stereo panning value.

    Parameters

    • value: number

      The stereo pan value to set, between -1 (left) and 1 (right).

    Returns void

    Throws

    Throws an error if stereo panning is not available, if the sound has been cleaned up, or if the value is out of bounds.

  • get threeDOptions(): PannerOptions
  • Gets the 3D audio options if HRTF panning is used.

    Returns PannerOptions

    The current 3D audio options.

    Throws

    Throws an error if the sound has been cleaned up or if HRTF panning is not used.

  • set threeDOptions(options): void
  • Sets the 3D audio options for HRTF panning.

    Parameters

    • options: Partial<PannerOptions>

      The 3D audio options to set.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up or if HRTF panning is not used.

  • get volume(): number
  • Gets the current volume of the audio.

    Returns number

    The current volume.

    Throws

    Throws an error if the sound has been cleaned up.

  • set volume(v): void
  • Sets the volume of the audio.

    Parameters

    • v: number

      The volume to set.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up.

Methods

  • Adds a filter to the audio signal chain.

    Parameters

    • filter: BiquadFilterNode

      The filter to add.

    Returns void

  • Parameters

    • filters: BiquadFilterNode[]

    Returns void

  • Parameters

    • connection: any

    Returns any

  • Cleans up resources used by the Playback instance. This method should be called when the audio is no longer needed to free up resources.

    Returns void

  • Creates a clone of the current Playback instance with optional overrides for certain properties. This method allows for the creation of a new Playback instance that shares the same audio context and source node but can have different settings such as loop count or pan type.

    Parameters

    • overrides: Partial<PlaybackCloneOverrides> = {}

      An object containing properties to override in the cloned instance.

    Returns Playback

    A new Playback instance cloned from the current one with the specified overrides applied.

    Throws

    Throws an error if the sound has been cleaned up.

  • Sets or gets the loop count for the audio.

    Parameters

    • Optional loopCount: LoopCount

      The number of times the audio should loop. 'infinite' for endless looping.

    Returns LoopCount

    The loop count if no parameter is provided.

    Throws

    Throws an error if the sound has been cleaned up or if the source type is unsupported.

  • Handles the loop event when the audio ends. This method is bound to the 'onended' event of the audio source. It manages looping logic and restarts playback if necessary.

    Returns void

  • Starts playing the audio.

    Returns [Playback]

    Returns the instance of the Playback class for chaining.

    Throws

    Throws an error if the sound has been cleaned up.

  • Refreshes the audio filters by re-applying them to the audio signal chain. This method is called internally whenever filters are added or removed.

    Returns void

    Throws

    Throws an error if the sound has been cleaned up.

  • Removes a filter from the audio signal chain.

    Parameters

    • filter: BiquadFilterNode

      The filter to remove.

    Returns void

  • Parameters

    • filters: BiquadFilterNode[]

    Returns void

  • Parameters

    • gainNode: GainNode

    Returns void

  • Parameters

    • panType: PanType
    • audioContext: IAudioContext

    Returns void

  • Parameters

    • pannerNode: PannerNode

    Returns void

  • Parameters

    • source: SourceNode

    Returns void