API Reference

Class List

pc.SoundComponent

Extends: pc.Component

The Sound Component controls playback of pc.Sounds.

Summary

Properties

distanceModelDetermines which algorithm to use to reduce the volume of the sound as it moves away from the listener.
maxDistanceThe maximum distance from the listener at which audio falloff stops.
pitchThe pitch modifier to play the audio with.
positionalIf true the audio will play back at the location of the Entity in space, so the audio will be affect by the position of the pc.AudioListenerComponent.
refDistanceThe reference distance for reducing volume as the sound source moves further from the listener.
rollOffFactorThe factor used in the falloff equation.
slotsA dictionary that contains the pc.SoundSlots managed by this Component.
volumeThe volume modifier to play the audio with.

Methods

addSlotCreates a new pc.SoundSlot with the specified name.
pausePauses playback of the slot with the specified name.
playBegins playing the sound slot with the specified name.
removeSlotRemoves the pc.SoundSlot with the specified name.
resumeResumes playback of the sound slot with the specified name if it's paused.
slotReturns the slot with the specified name
stopStops playback of the sound slot with the specified name if it's paused.

Events

endFired when a sound instance stops playing because it reached its ending.
pauseFired when a sound instance is paused.
playFired when a sound instance starts playing
resumeFired when a sound instance is resumed.
stopFired when a sound instance is stopped.

Inherited

Properties

enabledEnables or disables the component.

Details

Constructor

SoundComponent(system, entity)

Create a new Sound Component

Parameters

systempc.SoundComponentSystemThe ComponentSystem that created this Component
entitypc.EntityThe entity that the Component is attached to

Properties

StringdistanceModel

Determines which algorithm to use to reduce the volume of the sound as it moves away from the listener. Can be one of pc.DISTANCE_LINEAR, pc.DISTANCE_INVERSE or pc.DISTANCE_EXPONENTIAL. Default is pc.DISTANCE_LINEAR.

NumbermaxDistance

The maximum distance from the listener at which audio falloff stops. Note the volume of the audio is not 0 after this distance, but just doesn't fall off anymore.

Numberpitch

The pitch modifier to play the audio with. Must be larger than 0.01

Booleanpositional

If true the audio will play back at the location of the Entity in space, so the audio will be affect by the position of the pc.AudioListenerComponent.

NumberrefDistance

The reference distance for reducing volume as the sound source moves further from the listener.

NumberrollOffFactor

The factor used in the falloff equation.

Objectslots

A dictionary that contains the pc.SoundSlots managed by this Component.

Numbervolume

The volume modifier to play the audio with. In range 0-1.

Methods

addSlot(name, options)

Creates a new pc.SoundSlot with the specified name.

// get an asset by id
var asset = app.assets.get(10);
// add a slot
this.entity.sound.addSlot('beep', {
    asset: asset
});
// play
this.entity.sound.play('beep');

Parameters

nameStringThe name of the slot
optionsObjectSettings for the slot
options.volumeNumberThe playback volume, between 0 and 1.
options.pitchNumberThe relative pitch, default of 1, plays at normal pitch.
options.loopBooleanIf true the sound will restart when it reaches the end.
options.startTimeNumberThe start time from which the sound will start playing.
options.durationNumberThe duration of the sound that the slot will play starting from startTime.
options.overlapBooleanIf true then sounds played from slot will be played independently of each other. Otherwise the slot will first stop the current sound before starting the new one.
options.autoPlayBooleanIf true the slot will start playing as soon as its audio asset is loaded.
options.assetNumberThe asset id of the audio asset that is going to be played by this slot.

Returns

pc.SoundSlot The new slot.

pause([name])

Pauses playback of the slot with the specified name. If the name is undefined then all slots currently played will be paused. The slots can be resumed by calling pc.SoundComponent#resume.

// pause all sounds
this.entity.sound.pause();
// pause a specific sound
this.entity.sound.pause('beep');

Parameters

nameStringThe name of the slot to pause. Leave undefined to pause everything.

play(name)

Begins playing the sound slot with the specified name. The slot will restart playing if it is already playing unless the overlap field is true in which case a new sound will be created and played.

// get asset by id
var asset = app.assets.get(10);
// create a slot and play it
this.entity.sound.addSlot('beep', {
    asset: asset
});
this.entity.sound.play('beep');

Parameters

nameStringThe name of the pc.SoundSlot to play

Returns

pc.SoundInstance The sound instance that will be played.

removeSlot(name)

Removes the pc.SoundSlot with the specified name.

// remove a slot called 'beep'
this.entity.sound.removeSlot('beep');

Parameters

nameStringThe name of the slot

resume(name)

Resumes playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be resumed.

// resume all sounds
this.entity.sound.resume();
// resume a specific sound
this.entity.sound.resume('beep');

Parameters

nameStringThe name of the slot to resume. Leave undefined to resume everything.

slot(name)

Returns the slot with the specified name

// get a slot and set its volume
this.entity.sound.slot('beep').volume = 0.5;

Parameters

nameStringThe name of the slot

Returns

pc.SoundSlot The slot

stop(name)

Stops playback of the sound slot with the specified name if it's paused. If no name is specified all slots will be stopped.

// stop all sounds
this.entity.sound.stop();
// stop a specific sound
this.entity.sound.stop('beep');

Parameters

nameStringThe name of the slot to stop. Leave undefined to stop everything.

Events

end

Fired when a sound instance stops playing because it reached its ending.

Parameters

slotpc.SoundSlotThe slot whose instance ended
instancepc.SoundInstanceThe instance that ended

pause

Fired when a sound instance is paused.

Parameters

slotpc.SoundSlotThe slot whose instance was paused
instancepc.SoundInstanceThe instance that was paused created to play the sound

play

Fired when a sound instance starts playing

Parameters

slotpc.SoundSlotThe slot whose instance started playing
instancepc.SoundInstanceThe instance that started playing

resume

Fired when a sound instance is resumed..

Parameters

slotpc.SoundSlotThe slot whose instance was resumed
instancepc.SoundInstanceThe instance that was resumed

stop

Fired when a sound instance is stopped.

Parameters

slotpc.SoundSlotThe slot whose instance was stopped
instancepc.SoundInstanceThe instance that was stopped

Inherited

Properties

Booleanenabled

Enables or disables the component.