Audio

This module provides a description of the Audio component, used for audio processing, playback, and listening.

  • AudioClipSource: A variant type representing audio sources. It contains information about the audio resource (with a ResourceId) and the data of the audio source (rate, channels, samples).

  • AudioSource: A component for processing and playing audio sources. It includes functions for playing, stopping, and pausing/resuming the current sound.

  • AudioListener: A component for listening, with the ability to update its position and direction.

Add these components like any others 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.)))

// create a node at position float3(2, 0, 0)
var listenerNode = create_node(NodeData(name="listener_node", position=float3(2, 0, 0)))
// add a component for audio listening
add_component(listenerNode, new AudioListener())

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))

previous position of sound for updating it if it is 3D

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 if source is not resource (with SoundId), it is played as data with rate, channels and samples

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
}
AudioListener : Component

audio listener component

Usage example:

add_component(node, new AudioListener())
AudioListener.on_update()

update audio listener position and direction