Start Page » DragonScript Scripting Language » Abstraction Layers: How you want to build your Game » Behavior Elements » ECBehaviorSpeaker
Behavior element behavior adding speaker support.
Behavior adds a Speaker resource to the the behavior element.
If the ECBehaviorCollider is present in the behavior element before this behavior is added the speaker is attached. The speaker is attached to the named bone if defined otherwise it is attached statically.
Speakers can play either a static Sound resource or use a Synthesizer. By using a synthesizer dynamic sound can be played. To use a synthesizer use the speaker{(id)}.synthesizer
property instead of the speaker{(id)}.sound
property. If both properties are present the synthesizer one is used if valid otherwise sound.
If synthesizer is valid a SynthesizerInstance will be created. Subclasses of ECBehaviorSpeaker or other behaviors can then obtain SynthesizerController from the SynthesizerInstance to modify the played sound. Since controllers use curves behaviors have to set the value ahead of time before playing back the speaker. It is possible to change the controller curves while sound is playing. In this case though care has to be taken to be set the curve early enough since audio modules typically render sound into buffered blocks. If the curve is changed too late the changes can be missed or result in strange sound jumping.
See also:
This behavior can be added multiple times to an element. Each instance creates one speaker attached to the element collider which can be individually modified. To distinguish the speakers each instance has an identifier which can be used to retrieve a specific instance. Hence to use more than one speaker use code like this in your subclass constructor:
class MyElement extends BehaviorElementClass func new() ECBehaviorSpeaker.new(this) ECBehaviorSpeaker.new(this, "subSpeaker") end end
You can now define the parameters to use for both speakers using for example the properties speaker.volume
for the first speaker and speaker(subSpeaker).volume
for the second speaker.
It is recommended to always specify an identifier even if only one speaker is used. This avoids potentially name conflicts especially if other behaviors are added attaching other resources too.
Element class properties have the prefix speaker.
or speaker({id}).
if id is not empty.
Speaker type.
speaker.type
or speaker({id}).type
Allowed Values:
Value | Description |
---|---|
point | Omnidirectional. |
directed | Directed. |
point
<string name='speaker.type'>directed</string>
Path of sound resource to use.
speaker.sound
or speaker({id}).sound
*.ogg
(all sound modules)<string name='speaker.sound'>click.ogg</string>
Path of synthesizer resource to use.
speaker.synthesizer
or speaker({id}).synthesizer
*.desynth
<string name='speaker.synthesizer'>sound.desynth</string>
Speaker volume.
speaker.volume
or speaker({id}).volume
1
0
<float name='speaker.volume'>0.8</float>
Speaker range in meters. Speaker is inaudible beyond range.
speaker.range
or speaker({id}).range
30
0
<float name='speaker.range'>20</float>
Roll off. Value 1
is realistic (normal) roll-off. Values larger than 1
reduce volume faster near the sound source. Values smaller than 1
reduce volume faster near the sound range.
speaker.rollOff
or speaker({id}).rollOff
1
0
<float name='speaker.rollOff'>1.5</float>
Distance offset for attenuation calculation. For use by distance sounds. Offsets the true distance to the sound source for attenuation calculation to make the sound appear coming from far away. Requires increasing the volume to compensate for the distance increased attenuation.
speaker.distanceOffset
or speaker({id}).distanceOffset
0
0
<float name='speaker.distanceOffset'>500</float>
Play speed. Value of 1
is normal play speed. Values larger than 1
are faster (1.5 for example 150% play speed). Values less than 1
are slower (0.75 for example 75% play speed).
speaker.playSpeed
or speaker({id}).playSpeed
1
<float name='speaker.playSpeed'>1.5</float>
Speaker is muted. Affects only volume not play state.
speaker.muted
or speaker({id}).muted
false
<boolean name='speaker.muted'>true</boolean>
Speaker plays back looping. If fase playback stops after end of sound or synthesizer.
speaker.looping
or speaker({id}).looping
true
<boolean name='speaker.looping'>false</boolean>
Speaker is playing back after being created.
speaker.playing
or speaker({id}).playing
true
<boolean name='speaker.playing'>false</boolean>
Shape of speaker.
speaker.shape
or speaker({id}).shape
<string name='speaker.shape'>box:position,0,0.5,0:extends,2,1,0.5</string>
Position to attach resource to collider.
speaker.position
or speaker({id}).position
<vector name='speaker.position' x='0' y='0' z='0.1'/>
Orientation to attach resource to collider in degrees.
speaker.orientation
or speaker({id}).orientation
<vector name='speaker.orientation' x='30' y='0' z='0'/>
Bone to attach resource to. If empty string attach to collider.
speaker.bone
or speaker({id}).bone
<string name='speaker.bone'>attach</string>
Playback trigger. If no trigger is set the state of playing
property is used.
speaker.trigger
or speaker({id}).trigger
playing
<string name='speaker.trigger'>@switchOnVent & @powerEnabled</string>
Muted trigger. If no trigger is set the state of muted
property is used.
speaker.trigger(muted)
or speaker({id}).trigger(muted)
muted
<string name='speaker.trigger(muted)'>@switchOnVent & @powerEnabled</string>
Speaker started playing back.
Speaker stopped playing. This can be either manual or trigger based stop of speaker or a non-looping speaker finished playing back the sound/synthesizer.
Speaker has been muted.
Speaker has been unmuted.
This behavior requires no other behaviors.
This behavior does support element class to be persistable (setPersistable).
Since DragonScript Module Version 1.0
This example defines an element which can play back sound using a speaker.
class MyElement extends BehaviorElementClass public var ECBehaviorComponent component public var ECBehaviorCollider collider public var ECBehaviorSpeaker speaker func new() component = ECBehaviorComponent.new(this, null) collider = ECBehaviorCollider.new(this, component) speaker = ECBehaviorSpeaker.new(this, component, collider) end end
Using element class supporting adding behaviors the behavior can be added like this:
<?xml version='1.0' encoding='UTF-8'?> <elementClass name='MyClass' class='GenericBehaviorElement'> <behavior type='ECBehaviorComponent'/> <behavior type='ECBehaviorCollider'/> <behavior type='ECBehaviorSpeaker'> <!-- optional: set layer mask (default '2' meaning BaseGameApp.WorldLayerBit.audio). list of bits to set. --> <string name='layerMask'>0 1 4</string> <!-- optional: use BaseGameApp trigger table. game can add more supported values --> <string name='triggerTable'>default</string> <!-- set element properties. omit property prefix if used inside behavior tag --> <string name='.sound'>ambience.ogg</string> </behavior> <!-- for adding multiple behaviors use unique identifiers --> <behavior type='ECBehaviorSpeaker' id='second'/> </elementClass>