Reference Source
public class | source

ResonanceSingleton

You can directly use an instance of this class. Resonance

Class for managing sounds

Static Member Summary

Static Public Members
public static

The frame rate at which to fade the sounds

public static

The max speed this sound can be played at

public static

The max number of sounds that can be recycled

public static

The max volume to be used

public static

The min volume to be used

public static

clamp(pVal: number, pMin: number, pMax: number): *: *

public static

normalize(pValue: number, pMin: number, pMax: number, pRange: number): *: *

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

The checker function to resolve to true or false denoting if a sound can be played Master control over which sounds can play

public

audioCtx: AudioContext

The audio context all audio will dervie from

public

An object that stores fade information

public
public

gainNode: GainNode

Gain node

public

An object that stores the buffer data of a sound so it does not have to be loaded each time

public

Mute status

public

Array of sounds that are currently paused

public

Array of sounds that are queued for fading

public

Array of sounds that are queued for playing

public
public
public

Array of sounds that can be resused

public

Array of sounds that are currently playing

public

Current state of the library

public

version: *

The version of the module.

public

The master volume

Private Members
private
private

The logger module this module uses to log errors / logs.

Method Summary

Public Methods
public

addFilter(pSound: Sound, pFilter: Object)

Adds a filter to be applied to sounds.

public

Function to adjust master volume

public

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

public

createSound(pSoundPath: number, pVolume: number, pStartTime: number, pEndTime: number, pSave: boolean, pPlayUnfocused: boolean, pPlaybackRate: number, pLoop: boolean): Sound

public

emit(pSoundPath: string, pVolume: number, pStartTime: number, pEndTime: number, pPlaybackRate: number, -: Function): SoundSource

Plays a sound that is not stored.

public

API for the developer to define when a sound can and cannot be played.

public

fade(pVolume: number, pDuration: number, pEase: function, pCallback: function): ResonanceSingleton

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

public

Checks if the sound manager is currently muted

public

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

public

Pauses all sounds in the game

public

removeFilter(pSound: Sound, pFilter: Object)

Removes a filter from being applied to sounds.

public

Resumes all sounds in games

public

This effectively stops all sounds in the game except those specified in the pException array

public

Master mute

public

Removes the loaded audio buffer data for this sound

Private Methods
private
private

Recycles the sound for reuse later instead of deleting it.

private

This will start playing sounds that were initially blocked by not having a user gesture.

Static Public Members

public static FRAME_RATE: number source

The frame rate at which to fade the sounds

public static MAX_PLAYBACK_RATE: number source

The max speed this sound can be played at

public static MAX_RECYCLED_SOUNDS: number source

The max number of sounds that can be recycled

public static MAX_VOLUME: number source

The max volume to be used

public static MIN_VOLUME: number source

The min volume to be used

public static clamp(pVal: number, pMin: number, pMax: number): *: * source

Return:

*

The clamped number

public static normalize(pValue: number, pMin: number, pMax: number, pRange: number): *: * source

Return:

*

The normalized number

Public Constructors

public constructor() source

Public Members

public abilityToPlaySound: Function source

The checker function to resolve to true or false denoting if a sound can be played Master control over which sounds can play

public audioCtx: AudioContext source

The audio context all audio will dervie from

public fader: Object source

An object that stores fade information

public focused: boolean source

public gainNode: GainNode source

Gain node

public loadedBuffers: Object source

An object that stores the buffer data of a sound so it does not have to be loaded each time

public muted: boolean source

Mute status

public pausedSounds: Array source

Array of sounds that are currently paused

public queuedSoundsToFade: Array source

Array of sounds that are queued for fading

public queuedSoundsToPlay: Array source

Array of sounds that are queued for playing

public queuedToStop: boolean source

public ready: boolean source

public recycledSounds: Array source

Array of sounds that can be resused

public soundsPlaying: Array source

Array of sounds that are currently playing

public state: string source

Current state of the library

public version: * source

The version of the module.

public volume: number source

The master volume

Private Members

private _previousGainNodeValue: * source

private logger: Object source

The logger module this module uses to log errors / logs.

Public Methods

public addFilter(pSound: Sound, pFilter: Object) source

Adds a filter to be applied to sounds.

Params:

NameTypeAttributeDescription
pSound Sound

The sound to apply add the filter to.

pFilter Object

The filter to add.

public adjustVolume(pVolume: number): ResonanceSingleton source

Function to adjust master volume

Params:

NameTypeAttributeDescription
pVolume number

The number to change the volume to

Return:

ResonanceSingleton

This instance

public canPlaySound(): boolean source

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

Return:

boolean

Whether a sound can be played or not.

public createSound(pSoundPath: number, pVolume: number, pStartTime: number, pEndTime: number, pSave: boolean, pPlayUnfocused: boolean, pPlaybackRate: number, pLoop: boolean): Sound source

Params:

NameTypeAttributeDescription
pSoundPath number

The path of the sound file

pVolume number
  • optional
  • default: 100

The volume of the sound

pStartTime number
  • optional
  • default: 0

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

pEndTime number
  • optional
  • default: duration

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

pSave boolean
  • optional
  • default: false

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

pPlayUnfocused boolean
  • optional
  • default: false

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

pPlaybackRate number
  • optional
  • default: 1

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

pLoop boolean
  • optional
  • default: false

Whether this sound should loop or not

Return:

Sound

A sound object that has vast API on controlling it

public emit(pSoundPath: string, pVolume: number, pStartTime: number, pEndTime: number, pPlaybackRate: number, -: Function): SoundSource source

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

Params:

NameTypeAttributeDescription
pSoundPath string

The path to the file

pVolume number
  • optional
  • default: 100

The volume to play the sound

pStartTime number
  • optional
  • default: 0

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

pEndTime number
  • optional
  • default: duration

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

pPlaybackRate number
  • optional
  • default: 1

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

- Function

A callback to call when this sound has finished playing.

Return:

SoundSource

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

public enableChecker(pCheckerFunction: Function): ResonanceSingleton source

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

Params:

NameTypeAttributeDescription
pCheckerFunction Function

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

Return:

ResonanceSingleton

This instance

public fade(pVolume: number, pDuration: number, pEase: function, pCallback: function): ResonanceSingleton source

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

Params:

NameTypeAttributeDescription
pVolume number
  • optional
  • default: 100

The volume to fade to

pDuration number
  • optional
  • default: 5000

The duration of the fade in ms

pEase function
  • optional
  • default: 'easeOutCubic'

Easing function

pCallback function

Callback to be called when the fade is over

Return:

ResonanceSingleton

This instance

public isMuted(): boolean source

Checks if the sound manager is currently muted

Return:

boolean

Whether the sound manager is muted

public killAllSounds(): ResonanceSingleton source

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

Return:

ResonanceSingleton

This instance

public pauseAllSounds(pFocus: boolean): ResonanceSingleton source

Pauses all sounds in the game

Params:

NameTypeAttributeDescription
pFocus boolean

If this function was called automatically by the game being unfocused

Return:

ResonanceSingleton

This instance

public removeFilter(pSound: Sound, pFilter: Object) source

Removes a filter from being applied to sounds.

Params:

NameTypeAttributeDescription
pSound Sound

The sound to apply remove the filter from.

pFilter Object

The filter to remove.

public resumeAllSounds(pFocus: boolean): ResonanceSingleton source

Resumes all sounds in games

Params:

NameTypeAttributeDescription
pFocus boolean

If this function was called automatically by the game being focused

Return:

ResonanceSingleton

This instance

public stopAllSounds(pException: Array): ResonanceSingleton source

This effectively stops all sounds in the game except those specified in the pException array

Params:

NameTypeAttributeDescription
pException Array

an array of sounds that should not stop

Return:

ResonanceSingleton

This instance

public toggleMute(): ResonanceSingleton source

Master mute

Return:

ResonanceSingleton

This instance

public unloadSound(pSoundPath: string): ResonanceSingleton source

Removes the loaded audio buffer data for this sound

Params:

NameTypeAttributeDescription
pSoundPath string

The path of the sound file

Return:

ResonanceSingleton

This instance

Private Methods

private playQueuedSounds() source

private recycleSound(pSound: Sound): ResonanceSingleton source

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

Params:

NameTypeAttributeDescription
pSound Sound

The sound to recycle

Return:

ResonanceSingleton

This instance

private resumeAudioCtx() source

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