Options
All
  • Public
  • Public/Protected
  • All
Menu

The Sound class is used to control the playback of audio sources using the Web Audio API.

Hierarchy

  • Sound

Index

Constructors

  • new Sound(src: any, __namedParameters?: { container: any }): Sound
  • Parameters

    • src: any
    • __namedParameters: { container: any } = {}
      • container: any

    Returns Sound

Properties

id: number

The numeric identifier for accessing this node

src: string

The audio source path

container: AudioContainer

The AudioContainer which controls playback

startTime: number = undefined

The time in seconds at which playback was started

pausedTime: number = undefined

The time in seconds at which playback was paused

events: { stop: {}; start: {}; end: {}; pause: {}; load: {} } = ...

Registered event callbacks

Type declaration

  • stop: {}
    • start: {}
      • end: {}
        • pause: {}
          • load: {}
            loading: Promise<any> = undefined

            If this Sound source is currently in the process of loading, this attribute contains a Promise that will resolve when the loading process completes.

            _eventHandlerId: number = 1

            The registered event handler id for this Sound. Incremented each time a callback is registered.

            _scheduledEvents: Set<number> = ...

            A collection of scheduled events recorded as window timeout IDs

            _nodeId: number = 0

            A global audio node ID used to quickly reference a specific audio node

            Accessors

            • get context(): AudioContext
            • A convenience reference to the sound context used by the application

              Returns AudioContext

            • get node(): AudioBufferSourceNode | MediaElementAudioSourceNode
            • A reference to the audio source node being used by the AudioContainer

              Returns AudioBufferSourceNode | MediaElementAudioSourceNode

            • get gain(): AudioParam
            • A reference to the GainNode parameter which controls volume

              Returns AudioParam

            • get currentTime(): number
            • The current playback time of the sound

              Returns number

            • get duration(): number
            • The total sound duration, in seconds

              Returns number

            • get loaded(): boolean
            • Is the contained audio node loaded and ready for playback?

              Returns boolean

            • get failed(): boolean
            • Did the contained audio node fail to load?

              Returns boolean

            • get playing(): boolean
            • Is the audio source currently playing?

              Returns boolean

            • get loop(): boolean
            • set loop(looping: boolean): void
            • Is the Sound current looping?

              Returns boolean

            • Is the Sound current looping?

              Parameters

              • looping: boolean

              Returns void

            • get volume(): number
            • set volume(value: number): void
            • The volume at which the Sound is playing

              Returns number

            • The volume at which the Sound is playing

              Parameters

              • value: number

              Returns void

            Methods

            • fade(volume: number, [options={}]?: { duration: number; from: number; type: string }): Promise<void>
            • Fade the volume for this sound between its current level and a desired target volume

              Parameters

              • volume: number

                The desired target volume level between 0 and 1

              • [options={}]: { duration: number; from: number; type: string } = {}

                Additional options that configure the fade operation

                • duration: number
                • from: number
                • type: string

              Returns Promise<void>

              A Promise that resolves after the requested fade duration

            • load([options={}]?: { autoplay: boolean; autoplayOptions: any }): Promise<Sound>
            • Load the audio source, creating an AudioBuffer. Audio loading is idempotent, it can be requested multiple times but only the first load request will be honored.

              Parameters

              • [options={}]: { autoplay: boolean; autoplayOptions: any } = {}

                Additional options which affect resource loading

                • autoplay: boolean
                • autoplayOptions: any

              Returns Promise<Sound>

              The Sound once its source audio buffer is loaded

            • play([options={}]?: { loop: boolean; offset: number; volume: number; fade: number }): number | void | Promise<void>
            • Begin playback for the sound node

              Parameters

              • [options={}]: { loop: boolean; offset: number; volume: number; fade: number } = {}

                Options which configure playback

                • loop: boolean
                • offset: number
                • volume: number
                • fade: number

              Returns number | void | Promise<void>

            • pause(): void
            • Pause playback, remembering the playback position in order to resume later.

              Returns void

            • stop(): void
            • Stop playback, fully resetting the Sound to a non-playing state.

              Returns void

            • schedule(fn: Function, playbackTime: number): Promise<null>
            • Schedule a function to occur at the next occurrence of a specific playbackTime for this Sound.

              example

              Schedule audio playback changes

              sound.schedule(() => console.log("Do something exactly 30 seconds into the track"), 30);
              sound.schedule(() => console.log("Do something next time the track loops back to the beginning"), 0);
              sound.schedule(() => console.log("Do something 5 seconds before the end of the track"), sound.duration - 5);

              Parameters

              • fn: Function

                A function that will be called with this Sound as its single argument

              • playbackTime: number

                The desired playback time at which the function should be called

              Returns Promise<null>

              A Promise which resolves once the scheduled function has been called

            • emit(eventName: string): void
            • Trigger registered callback functions for a specific event name.

              Parameters

              • eventName: string

                The event name being emitted

              Returns void

            • off(eventName: string, fn: number | Function): void
            • Deactivate an event handler which was previously registered for a specific event

              Parameters

              • eventName: string

                The event name being deactivated

              • fn: number | Function

                The callback ID or callback function being un-registered

              Returns void

            • on(eventName: string, fn: Function, [options={}]?: { once: boolean }): number
            • Register an event handler to take actions for a certain Sound event.

              Parameters

              • eventName: string

                The event name being deactivated

              • fn: Function

                The callback function to trigger when the event occurs

              • [options={}]: { once: boolean } = {}

                Additional options that affect callback registration

                • once: boolean

              Returns number

            • _onEnd(): void
            • Called when playback concludes naturally

              Returns void

            • _onLoad(): void
            • Called when the audio buffer is first loaded

              Returns void

            • _onPause(): void
            • Called when playback is paused

              Returns void

            • _onStart(): void
            • Called when the sound begins playing

              Returns void

            • _onStop(): void
            • Called when playback is stopped (prior to naturally reaching the end)

              Returns void

            • _registerForEvent(eventName: any, callback: any): number
            • Register a new callback function for a certain event. For internal use only.

              Parameters

              • eventName: any
              • callback: any

              Returns number

            • _clearEvents(): void
            • Cancel all pending scheduled events.

              Returns void