AudioSource

This module provides the AudioSource component, which plays a sound on a node: playing, stopping, and pausing or resuming the current sound. A 3D sound is heard from the node of an AudioListener.

Add it like any other component using the add_component function:

// create a node at position float3(-2, 0, 0)
var sourceNode = create_node(NodeData(name="source_node", position=float3(-2, 0, 0)))
// add a component for 3D sound with the given attenuation
add_component(sourceNode, new AudioSource(is3d=true, attenuation=inverse_distance_attenuation(5.)))

Classes

AudioSource : Component

audio component for processing and playing audio sources

Usage example:

let node : NodeId = ...
add_component(node, new AudioSource(isLoop=true, is3d=true))
Fields:
  • source : SoundId - id of Sound resource (can be generated by request_sound for example)

  • isLoop : bool = false - if sound should be looped

  • is3d : bool = false - if sound should be played as 3D sound

  • autoPlay : bool = false - if sound should be played automatically on start

  • attenuation : Attenuation = default_attenuation() - attenuation of sound (default: 1 / (d + 1))

  • currentSound : SoundHandle - handle of currently playing sound

  • pan : float = 0f - pan of sound (left -1 to right 1)

  • pitch : float = 1f - pitch of sound (1 is normal)

  • volume : float = 1f - volume of sound (0 is silent, 1 is normal)

AudioSource.on_initialize()

play sound if autoPlay is true

AudioSource.on_update()

update 3D sound position if it is 3D

AudioSource.play()

stop previous sound if any and plays new sound based on the input source

AudioSource.playSource(src: SoundId)

delete previous source if any and plays new source

Arguments:

Usage example:

add_component(node, new AudioSource())
get_component(node) $(var audio : AudioSource?) {
    audio.playSource(request_sound("my_sound.wav"))
}
AudioSource.stop()

stop playing current sound and sets it to invalid id

Usage example:

add_component(node, new AudioSource())
get_component(node) $(var audio : AudioSource?) {
    audio.stop()
}
AudioSource.pause(val: bool = true)

pause or resume playing current sound

Arguments:
  • val : bool - true to pause, false to resume

Usage example:

add_component(node, new AudioSource())
get_component(node) $(var audio : AudioSource?) {
    audio.pause() // pause
    audio.pause(false) // resume
}