Skip to main content

Audio

Description​

This component allows entities to play sounds.

Properties​

PropertyTypeDefaultDescription
url (Required)string''Source URL
volumenumber1How loud the audio will be played. A value between 0 and 1
loopbooleanfalseWhether the audio restarts after it finishes
pausedbooleanfalseWhether the audio is currently paused
pitchnumber1The factor used to change the audio's pitch. 1 is the default pitch.
positionalbooleanfalseWhether the audio is played positional
refDistancenumber1Only applied if positional is true. The value indicating at what distance the volume from this source will start reducing as the listener moves away. Must be a non-negative value.
distanceModelstring'inverse'Only applied if positional is true. The algorithm used to reduce volume as the distance increases between this audio source and the listener. Possible values include 'linear', 'inverse', and 'exponential'.
rolloffFactornumber1Only applied if positional is true. How quickly volume is reduced as distance increases between this audio source and the listener. The acceptable range of values changes depending on the distanceModel, shown by the following: linear: 0 to 1, inverse: 0 to Infinity, exponential: 0 to Infinity
maxDistancenumber10000Only applied if positional is true and distanceModel equals linear. The max distance between this audio source and the listener. Volume is not reduced after this point. Must be a positive value.

Functions​

Get​

Returns a read-only reference.

Example

ecs.Audio.get(world, component.eid)

Set​

Ensures the component exists on the entity, then assigns the (optional) data to the component.

Example

ecs.Audio.set(world, component.eid, {
url: '',
volume: 1,
loop: false,
paused: false,
pitch: 1,
positional: false,
refDistance: 1,
distanceModel: ''inverse'',
rolloffFactor: 1,
maxDistance: 10000
})

Mutate​

Perform an update to the component within a callback function. Return true to indicate no changes made.

Example

ecs.Audio.mutate(world, component.eid, (cursor) => {
cursor.volume = 0.5;
cursor.loop = true;
return false;
})

Remove​

Removes the component from the entity.

Example

ecs.Audio.remove(world, component.eid)

Has​

Returns true if the component is present on the entity.

Example

ecs.Audio.has(world, component.eid)

Reset​

Adds, or resets the component to its default state.

Example

ecs.Audio.reset(world, component.eid)

Advanced Functions​

Cursor​

Returns a mutable reference. Cursors are reused so only one cursor for each component can exist at a time.

Example
ecs.Audio.cursor(world, component.eid)

Acquire​

Same behavior as cursor, but commit must be called after the cursor is done being used.

Example
ecs.Audio.acquire(world, component.eid)

Commit​

Called after acquire. An optional third argument determines whether the cursor was mutated or not.

Example
ecs.Audio.commit(world, component.eid)
ecs.Audio.commit(world, component.eid, false)

Dirty​

Mark the entity as having been mutated. Only needed in a specific case where systems are mutating data.

Example
ecs.Audio.dirty(world, component.eid)