Class: SoundInstance

pc.SoundInstance

A pc.SoundInstance plays a pc.Sound

Constructor

new SoundInstance(manager, sound, options)

Parameters:
Name Type Description
manager pc.SoundManager The sound manager
sound pc.Sound The sound to play
options Object Options for the instance
Properties
Name Type Attributes Default Description
volume Number <optional>
1 The playback volume, between 0 and 1.
pitch Number <optional>
1 The relative pitch, default of 1, plays at normal pitch.
loop Boolean <optional>
false Whether the sound should loop when it reaches the end or not.
startTime Number <optional>
0 The time from which the playback will start in seconds. Default is 0 to start at the beginning.
duration Number <optional>
null The total time after the startTime in seconds when playback will stop or restart if loop is true.
onPlay function <optional>
null Function called when the instance starts playing.
onPause function <optional>
null Function called when the instance is paused.
onResume function <optional>
null Function called when the instance is resumed.
onStop function <optional>
null Function called when the instance is stopped.
onEnd function <optional>
null Function called when the instance ends.
Properties:
Name Type Description
volume Number The volume modifier to play the sound with. In range 0-1.
pitch Number The pitch modifier to play the sound with. Must be larger than 0.01
startTime Number The start time from which the sound will start playing.
currentTime Number Gets or sets the current time of the sound that is playing. If the value provided is bigger than the duration of the instance it will wrap from the beginning.
duration Number The duration of the sound that the instance will play starting from startTime.
loop Boolean If true the instance will restart when it finishes playing
isPlaying Boolean Returns true if the instance is currently playing.
isPaused Boolean Returns true if the instance is currently paused.
isStopped Boolean Returns true if the instance is currently stopped.
isSuspended Boolean Returns true if the instance is currently suspended because the window is not focused.
source AudioBufferSourceNode Gets the source that plays the sound resource. If the Web Audio API is not supported the type of source is Audio. Source is only available after calling play.
sound pc.Sound The sound resource that the instance will play.
Source:

Methods

(private) _initializeNodes()

Creates internal audio nodes and connects them
Source:

(private) _onManagerDestroy()

Handle the manager's 'destroy' event.
Source:

(private) _onManagerDestroy()

Handle the manager's 'destroy' event.
Source:

(private) _onManagerResume()

Handle the manager's 'resume' event.
Source:

(private) _onManagerSuspend()

Handle the manager's 'suspend' event.
Source:

(private) _onManagerVolumeChange()

Handle the manager's 'volumechange' event.
Source:

(private) _updateCurrentTime()

Sets the current time taking into account the time the instance started playing, the current pitch and the current time offset.
Source:

clearExternalNodes()

Clears any external nodes set by pc.SoundInstance#setExternalNodes.
Source:

getExternalNodes() → {Array.<AudioNode>}

Gets any external nodes set by pc.SoundInstance#setExternalNodes.
Source:
Returns:
Returns an array that contains the two nodes set by pc.SoundInstance#setExternalNodes.
Type
Array.<AudioNode>

pause() → {Boolean}

Pauses playback of sound. Call resume() to resume playback from the same position.
Source:
Returns:
Returns true if the sound was paused
Type
Boolean

play() → {Boolean}

Begins playback of sound. If the sound is not loaded this will return false. If the sound is already playing this will restart the sound.
Source:
Returns:
True if the sound was started.
Type
Boolean

resume() → {Boolean}

Resumes playback of the sound. Playback resumes at the point that the audio was paused
Source:
Returns:
Returns true if the sound was resumed.
Type
Boolean

setExternalNodes(firstNode, lastNodeopt)

Connects external Web Audio API nodes. You need to pass the first node of the node graph that you created externally and the last node of that graph. The first node will be connected to the audio source and the last node will be connected to the destination of the AudioContext (e.g. speakers). Requires Web Audio API support.
Parameters:
Name Type Attributes Description
firstNode AudioNode The first node that will be connected to the audio source of sound instances.
lastNode AudioNode <optional>
The last node that will be connected to the destination of the AudioContext. If unspecified then the firstNode will be connected to the destination instead.
Source:
Example
var context = app.systems.sound.context;
var analyzer = context.createAnalyzer();
var distortion = context.createWaveShaper();
var filter = context.createBiquadFilter();
analyzer.connect(distortion);
distortion.connect(filter);
instance.setExternalNodes(analyzer, filter);

stop() → {Boolean}

Stops playback of sound. Calling play() again will restart playback from the beginning of the sound.
Source:
Returns:
Returns true if the sound was stopped.
Type
Boolean

Events

end

Fired when the sound currently played by the instance ends.
Source:

pause

Fired when the instance is paused.
Source:

play

Fired when the instance starts playing its source
Source:

resume

Fired when the instance is resumed.
Source:

stop

Fired when the instance is stopped.
Source: