Reference Source
import {Sound} from 'resonance/src/sound.mjs'
public class | source

Sound

A small class to represent a sound. Has useful API to handle the sound.

Constructor Summary

Public Constructor
public

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

Member Summary

Public Members
public set

info(pNewInfo: Object)

Set the info of this sound The new value is not used, as this is a "read-only" variable.

public get

info: Objecct: {...undefined: Object}

Get the info object of this sound

public get

Get the loop status of this sound

public set

loop(pLoopValue: boolean)

Set the loop status of this sound

public

onEnded: *

public
public
public
public
public get

Get the playback status of this sound

public set

playbackRate(pNewPlaybackRate: number)

Set the playback status of this sound

public get

Gets the current volume of this sound

public set

volume(pNewVolume: number)

Sets the volume of this sound

Private Members
private

Array of filters that are currently applied on this sound.

private

Info tied to this sound.

private

If this sound is to loop.

private

The playback rate of this sound.

private

The volume of this sound.

private

The duration of this sound.

private

The end time of ththise sound.

private

Events tied to this sound.

private

Object holding the fading information for this sound.

private

The gain node of this sound.

private

Whether this sound is loaded.

private

If this sound is muted.

private

The timestamp of this sound when it was paused so it can be resumed at the same timestamp.

private

If this sound is to play after it is loaded.

private

If this sound plays without window focus.

private

Whether to save this sound or not.

private

The path of the sound.

private

The source of this sound.

private

The start time of this sound.

private

The start timestamp of this sound.

private

The state of this sound.

private

Whether this sound was sent a stop signal.

Method Summary

Public Methods
public

addFilter(pFilter: Object)

Adds a filter to be applied to this sound.

public

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

public

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

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

public

Get the current timestamp of the sound playing

public

on(pEvent: Object, pCallback: Function): this

Attaches a callback to the specified event.

public

pause(): this

Pauses this sound

public

play(pResume: boolean): this

Plays this sound

public

Start the queued fade

public

Removes all filters from this sound.

public

removeFilter(pFilter: Object)

Removes a filter from being applied to this sound.

public

resume(): this

Resumes playing this sound

public

stop(pState: string): this

Stops this sound from playing.

public

toggleLoop(): this

Toggleable loop feature for this sound.

public

toggleMute(): this

Toggleable mute feature for this sound.

Private Methods
private

Get whether this sound will play when the window is unfocused

private

kill()

Kills this sound.

private

load()

Loads this sound and stores its data so future sounds can use the same buffer

private

restart(): this

Restarts this sound

private

wipe()

Resets this sound to default state

Public Constructors

public constructor(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

The volume of the sound

pStartTime number

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

pEndTime number

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

pSave boolean

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

pPlayUnfocused boolean

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

pPlaybackRate number

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

pLoop boolean

Whether this sound should loop or not

Return:

Sound

A sound object that has vast API on controlling it

Public Members

public set info(pNewInfo: Object) source

Set the info of this sound The new value is not used, as this is a "read-only" variable. So any attempts to set it will not work

public get info: Objecct: {...undefined: Object} source

Get the info object of this sound

Return:

Objecct

The info object of this sound

public get loop: boolean: * source

Get the loop status of this sound

Return:

boolean

Whether this sound is set to loop or not

public set loop(pLoopValue: boolean) source

Set the loop status of this sound

public onEnded: * source

public onResumed: * source

public onStarted: * source

public onStopped: * source

public onSuspended: * source

public get playbackRate: number: * source

Get the playback status of this sound

Return:

number

The playback rate of this sound

public set playbackRate(pNewPlaybackRate: number) source

Set the playback status of this sound

public get volume: number: * source

Gets the current volume of this sound

Return:

number

The current volume of this sound

public set volume(pNewVolume: number) source

Sets the volume of this sound

Private Members

private _filters: Array source

Array of filters that are currently applied on this sound.

private _info: boolean source

Info tied to this sound.

Properties:

NameTypeAttributeDescription
soundPath string

The path this sound uses for its source

duration number

The duration of this sound

private _loop: boolean source

If this sound is to loop.

private _playbackRate: boolean source

The playback rate of this sound.

private _volume: boolean source

The volume of this sound.

private duration: number source

The duration of this sound.

private endTime: number source

The end time of ththise sound.

private events: Object source

Events tied to this sound. start | end | pause | resume

private fader: Object source

Object holding the fading information for this sound.

private gainNode: Object source

The gain node of this sound.

private loaded: boolean source

Whether this sound is loaded.

private muted: boolean source

If this sound is muted.

private pausedTimeStamp: number source

The timestamp of this sound when it was paused so it can be resumed at the same timestamp.

private playAfterLoad: boolean source

If this sound is to play after it is loaded.

private playUnfocused: boolean source

If this sound plays without window focus.

private save: boolean source

Whether to save this sound or not.

private soundPath: string source

The path of the sound.

private source: Object source

The source of this sound.

private startTime: number source

The start time of this sound.

private startedTimeStamp: number source

The start timestamp of this sound.

private state: string source

The state of this sound.

private stopSignal: boolean source

Whether this sound was sent a stop signal.

Public Methods

public addFilter(pFilter: Object) source

Adds a filter to be applied to this sound.

Params:

NameTypeAttributeDescription
pFilter Object

The filter to add.

TODO:

  • Allow more than one filter. Loop through the filters array and connect all filters after disconnecting.

public build(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

The volume of the sound

pStartTime number

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

pEndTime number

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

pSave boolean

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

pPlayUnfocused boolean

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

pPlaybackRate number

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

pLoop boolean

Whether this sound should loop or not

Return:

Sound

A sound object that has vast API on controlling it

public fade(pVolume: number, pDuration: number, pEase: function, pCallback: function): this 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:

this

This sound instance

public getCurrentTime(): number source

Get the current timestamp of the sound playing

Return:

number

The current timestamp into the sound

public on(pEvent: Object, pCallback: Function): this source

Attaches a callback to the specified event.

Params:

NameTypeAttributeDescription
pEvent Object

The event to attach the callback to

pCallback Function

The function to be called when the event is triggered

Return:

this

The Sound instance

public pause(): this source

Pauses this sound

Return:

this

This sound instance

public play(pResume: boolean): this source

Plays this sound

Params:

NameTypeAttributeDescription
pResume boolean

If this is being played from a paused state

Return:

this

This sound instance

public queuedFade() source

Start the queued fade

public removeAllFilters() source

Removes all filters from this sound.

public removeFilter(pFilter: Object) source

Removes a filter from being applied to this sound.

Params:

NameTypeAttributeDescription
pFilter Object

The filter to remove.

public resume(): this source

Resumes playing this sound

Return:

this

This sound instance

public stop(pState: string): this source

Stops this sound from playing. If any filters are utilized on this sound they are removed.

Params:

NameTypeAttributeDescription
pState string

The current state of this sound. It's used to figure out if a callback should be dispatched

Return:

this

This sound instance

public toggleLoop(): this source

Toggleable loop feature for this sound. Flips between loop and unlooped

Return:

this

This sound instance

public toggleMute(): this source

Toggleable mute feature for this sound. Flips between muted and unmuted

Return:

this

This sound instance

Private Methods

private canPlayUnfocused(): boolean source

Get whether this sound will play when the window is unfocused

Return:

boolean

Whether or not this sound will play when the window is unfocused

private kill() source

Kills this sound. Wipes it, and recycles it if the recycle manager isn't full. If the recycle manager is full, this sound will become an empty class instance.

private load() source

Loads this sound and stores its data so future sounds can use the same buffer

private restart(): this source

Restarts this sound

Return:

this

This sound instance

private wipe() source

Resets this sound to default state