Class ResonanceSingleton

Class for managing sounds ResonanceSingleton

License

Resonance does not have a license at this time. For licensing contact the author

Author

https://github.com/doubleactii

Safari does not support .ogg files, so if you are using safari, do not use .ogg files or this library will not play that sound on safari. Otherwise, you can use .ogg on any other platform. Cordova included. This supports mp3, wav, ogg, etc.

Constructors

Properties

MAX_VOLUME: number = 200

The max volume to be used

MIN_VOLUME: number = -200

The min volume to be used

MAX_RECYCLED_SOUNDS: number = 500

The max number of sounds that can be recycled

MAX_PLAYBACK_RATE: number = 10

The max speed this sound can be played at

FRAME_RATE: number = 60

The frame rate at which to fade the sounds

version: string = "VERSION_REPLACE_ME"

The version of the module.

logger: any
audioCtx: AudioContext
gainNode: GainNode
soundsPlaying: Sound[]
pausedSounds: Sound[]
recycledSounds: Sound[]
queuedSoundsToPlay: Sound[]
queuedSoundsToFade: Sound[]
loadedBuffers: {
    [key: string]: AudioBuffer;
}

Type declaration

  • [key: string]: AudioBuffer
fader: any
volume: number
muted: boolean
state: null | string
focused: boolean
abilityToPlaySound: null | (() => boolean)

Type declaration

    • (): boolean
    • Returns boolean

_previousGainNodeValue?: number
ready?: boolean

Methods

  • Parameters

    • pVal: number

      Value to clamp

    • pMin: number

      Minimum value

    • pMax: number

      Maximum value

    Returns number

    The clamped number

  • Parameters

    • pValue: number

      The number to normalize within a range

    • pMin: number = 0

      The min number

    • pMax: number = 100

      The max number

    • pRange: number = 1

      The range to normalize within

    Returns number

    The normalized number

  • Function to check whether this library can play a sound at a given moment.

    Returns boolean

    Whether a sound can be played or not.

  • API for the developer to define when a sound can and cannot be played. Any sound that tries to play while this returns false will not play Enables a checker to become the master checker over whether a sound can play If their defined conditions return false, then the sound cannot be played

    Parameters

    • pCheckerFunction: (() => boolean)

      The function that will resolve to true or false denoting if a sound can be played

        • (): boolean
        • Returns boolean

    Returns ResonanceSingleton

    This instance

  • Function to adjust master volume

    Parameters

    • pVolume: number

      The number to change the volume to

    Returns ResonanceSingleton

    This instance

  • Adds a filter to be applied to sounds.

    Parameters

    • pSound: Sound

      The sound to apply add the filter to.

    • pFilter: any

      The filter to add.

    Returns void

  • Removes a filter from being applied to sounds.

    Parameters

    • pSound: Sound

      The sound to apply remove the filter from.

    • pFilter: any

      The filter to remove.

    Returns void

  • Fades this sound to the specified volume in the specified duration via the specified ease

    Parameters

    • Optional pVolume: number = 100

      The volume to fade to

    • Optional pDuration: number = 5000

      The duration of the fade in ms

    • Optional pEase: string = 'easeOutCubic'

      Easing function

    • Optional pCallback: (() => void)

      Callback to be called when the fade is over

        • (): void
        • Returns void

    Returns ResonanceSingleton

    This instance

  • Checks if the sound manager is currently muted

    Returns boolean

    Whether the sound manager is muted

  • Plays a sound that is not stored. Plays the sound only. You can only kill this sound, no repeat, no changing volume, etc

    Parameters

    • pSoundPath: string

      The path to the file

    • Optional pVolume: number = 100

      The volume to play the sound

    • Optional pStartTime: number = 0

      The start time of this sound (to play a clipped version)

    • Optional pEndTime: number

      The end time of this sound (to play a clipped version)

    • Optional pPlaybackRate: number = 1

      The rate at which the sound is played, Higher numbers for faster playback (MAX 10)

    • Optional pCallback: (() => void)
        • (): void
        • Returns void

    Returns any

    The source to this emitted sound. Call source.kill() to stop this sound while its playing. This is the only API this sound has

  • Parameters

    • pSoundPath: string

      The path of the sound file

    • Optional pVolume: number

      The volume of the sound

    • Optional pStartTime: number

      The start time of this sound (to play a clipped version)

    • Optional pEndTime: number

      The end time of this sound (to play a clipped version)

    • Optional pSave: boolean

      Whether to save this sound, or recycle it when it's completed

    • Optional pPlayUnfocused: boolean

      If this sound is set to true then it will not be paused automatically when the game screen is not focused

    • Optional pPlaybackRate: number

      The rate at which the sound is played, Higher numbers for faster playback (MAX 10)

    • Optional pLoop: boolean

      Whether this sound should loop or not

    Returns Sound

    • A sound object that has vast API on controlling it
  • This effectively stops all sounds in the game except those specified in the pException array

    Parameters

    • Optional pException: Sound[]

      an array of sounds that should not stop

    Returns ResonanceSingleton

    This instance

  • This effectively kills all sounds in the game and subjects them to be recycled

    Returns ResonanceSingleton

    This instance

  • Pauses all sounds in the game

    Parameters

    • Optional pFocus: boolean

      If this function was called automatically by the game being unfocused

    Returns ResonanceSingleton

    This instance

  • Resumes all sounds in games

    Parameters

    • Optional pFocus: boolean

      If this function was called automatically by the game being focused

    Returns ResonanceSingleton

    This instance

  • Finds all queued sounds (sounds that were played when the game was minimized or out of focus and plays them)

    Returns void

  • Removes the loaded audio buffer data for this sound

    Parameters

    • pSoundPath: string

      The path of the sound file

    Returns ResonanceSingleton

    This instance

  • Recycles the sound for reuse later instead of deleting it. All binding info on the sound is removed

    Parameters

    • pSound: Sound

      The sound to recycle

    Returns ResonanceSingleton

    This instance

  • This will start playing sounds that were initially blocked by not having a user gesture. This will also restart sounds when a mobile device backs out of an app, and then rejoins the app

    Returns void