Skip to main content
Version: latest - a1.172.0 โš–๏ธ

๐Ÿ”Š Sound

Class for playing in-game 2D and 3D sounds

Client Only๐Ÿ”Not Distributable๐Ÿง‘โ€๐Ÿ’ปAPI Source
๐Ÿ‘ชInheritance
This class shares methods and events from Base Entity, Base Actor.

tip

You can also load raw .ogg files from disk! Please check SpecialPath.

๐ŸŽ’ Examplesโ€‹

Client/Index.lua
local my_sound = Sound(
Vector(-510, 145, 63), -- Location (if a 3D sound)
"nanos-world::A_VR_Confirm", -- Asset Path
false, -- Is 2D Sound
true, -- Auto Destroy (if to destroy after finished playing)
SoundType.SFX,
1, -- Volume
1 -- Pitch
)
Client/Index.lua
-- Spawning a ogg sound located in a Package
local ogg_sound = Sound(Vector(), "package://my-package/Client/awesome_beep.ogg", true)

๐Ÿ›  Constructorsโ€‹

Default Constructorโ€‹

No description provided

local my_sound = Sound(location, asset, is_2d_sound?, auto_destroy?, sound_type?, volume?, pitch?, inner_radius?, falloff_distance?, attenuation_function?, keep_playing_when_silent?, loop_mode?, auto_play?)
Parameters
NameTypeDescription
locationVectorOnly used if 3D sound
assetSound ReferenceThe Sound Asset to load
is_2d_sound?booleanWhether to spawn it as 2D or 3D Default false
auto_destroy?booleanWhether to destroy after finished playing Default true
sound_type?SoundTypeUsed to apply user's volume settings Default SoundType.SFX
volume?floatVolume multiplier Default 1
pitch?floatPitch multiplier Default 1
inner_radius?floatOnly used if 3D sound Default 400
falloff_distance?floatOnly used if 3D sound Default 3600
attenuation_function?AttenuationFunctionOnly used if 3D sound Default AttenuationFunction.Linear
keep_playing_when_silent?booleanOnly used if 3D sound - Use with caution, it may cause performance issues! Default false
loop_mode?SoundLoopModeWhether should force sound to loop Default SoundLoopMode.Default
auto_play?booleanWhether it should immediately play when loaded Default true
Note

auto_destroy means the Entity will be immediately destroyed after spawned, losing references to the Sound spawned in-game. So if the Sound itself loops indefinitely, it will keep playing until the Player reconnects.

๐Ÿ—ฟ Static Functionsโ€‹

This class doesn't have own static functions.

Inherited static functions

๐Ÿฆ  Functionsโ€‹

Attenuation
GetFalloffDistance: floatGets the distance at which the sound becomes inaudible
GetInnerRadius: floatGets the radius within which the volume is 100%
Is2D: booleanGets if the sound is 2D
SetAttenuationShapeIf a 3D Sound, sets the shape of the volume within which the volume is 100%
SetFalloffDistanceIf a 3D Sound, sets the distance which the sound is inaudible
SetInnerRadiusIf a 3D Sound, sets the radius within which the volume is 100%
SetOcclusionIf a 3D Sound, configures its automatic occlusion
Mixing
GetLowPassFilter: floatGets the low pass filter frequency
GetPitch: floatGets the pitch multiplier
GetSoundType: SoundTypeGets the Sound Type, used to apply the user's volume settings
GetVolume: floatGets the volume multiplier
SetLowPassFilterSets low-pass filter frequency. Sets 0 to disable it
SetPitchSets the Sound's pitch
SetVolumeSets the Sound's volume
Playback
FadeInPlays the sound with a fade effect
FadeOutStops the sound with a fade effect
GetDuration: floatGets the duration of the Sound
GetPath: stringGets the asset path of this Sound
IsPlaying: booleanGets if the sound is playing
PlayStarts the sound
SetPausedPauses or resumes the sound
StopStops the sound
StopDelayedStops the sound after the provided delay

Attenuationfunctionsโ€‹

GetFalloffDistanceOptimalโ€‹

Gets the distance at which the sound becomes inaudible

local falloff_distance = my_sound:GetFalloffDistance()
Returns
TypeDescription
floatThe falloff distance

See also SetFalloffDistance.

GetInnerRadiusOptimalโ€‹

Gets the radius within which the volume is 100%

local inner_radius = my_sound:GetInnerRadius()
Returns
TypeDescription
floatThe inner radius

See also SetInnerRadius.

Is2DOptimalโ€‹

Gets if the sound is 2D

local is_2d_sound = my_sound:Is2D()
Returns
TypeDescription
booleanWhether the sound is 2D

SetAttenuationShapeOptimalโ€‹

If a 3D Sound, sets the shape of the volume within which the volume is 100%

Outside the shape, the volume fades over the falloff distance. The meaning of the extents depends on the shape: Sphere uses X as the radius, Capsule uses X as the half height and Y as the radius, Box uses X, Y and Z as its extents, and Cone uses X as the radius, Y as the angle and Z as the falloff angle (both in degrees). The Capsule, Box and Cone follow the Sound's rotation. For a Sphere, this is the same as SetInnerRadius

my_sound:SetAttenuationShape(shape, extents, cone_offset?)
Parameters
NameTypeDescription
shapeAttenuationShapeThe new attenuation shape
extentsVectorThe dimensions of the shape, read differently by each shape
cone_offset?floatCone only: the distance back from the Sound's location at which the cone begins Default 0

See also SetInnerRadius, SetFalloffDistance.

SetFalloffDistanceOptimalโ€‹

If a 3D Sound, sets the distance which the sound is inaudible

my_sound:SetFalloffDistance(falloff_distance)
Parameters
NameTypeDescription
falloff_distancefloatDistance from the inner radius at which the sound becomes inaudible

See also GetFalloffDistance.

SetInnerRadiusOptimalโ€‹

If a 3D Sound, sets the radius within which the volume is 100%

my_sound:SetInnerRadius(inner_radius)
Parameters
NameTypeDescription
inner_radiusfloatRadius within which the volume is 100%

See also GetInnerRadius.

SetOcclusionOptimalโ€‹

If a 3D Sound, configures its automatic occlusion

When something blocks the line between the listener and the Sound, like a wall or a floor, the Sound gets a low-pass filter and a volume multiplier. It is disabled by default

my_sound:SetOcclusion(enabled, low_pass_frequency?, volume_multiplier?)
Parameters
NameTypeDescription
enabledbooleanIf the Sound is automatically occluded
low_pass_frequency?floatThe low-pass filter threshold frequency applied when occluded, in Hz Default 3500
volume_multiplier?floatThe volume multiplier applied when occluded Default 0.85

See also SetLowPassFilter, SetVolume.

Mixingfunctionsโ€‹

GetLowPassFilterOptimalโ€‹

Gets the low pass filter frequency

local frequency = my_sound:GetLowPassFilter()
Returns
TypeDescription
floatThe low pass filter frequency

See also SetLowPassFilter.

GetPitchOptimalโ€‹

Gets the pitch multiplier

local pitch = my_sound:GetPitch()
Returns
TypeDescription
floatThe pitch multiplier

See also SetPitch.

GetSoundTypeOptimalโ€‹

Gets the Sound Type, used to apply the user's volume settings

local sound_type = my_sound:GetSoundType()
Returns
TypeDescription
SoundTypeThe Sound Type

GetVolumeOptimalโ€‹

Gets the volume multiplier

local volume = my_sound:GetVolume()
Returns
TypeDescription
floatThe volume multiplier

See also SetVolume.

SetLowPassFilterOptimalโ€‹

Sets low-pass filter frequency. Sets 0 to disable it

my_sound:SetLowPassFilter(frequency)
Parameters
NameTypeDescription
frequencyfloatThe new low-pass filter frequency. Sets 0 to disable it

See also GetLowPassFilter.

SetPitchOptimalโ€‹

Sets the Sound's pitch

my_sound:SetPitch(new_pitch)
Parameters
NameTypeDescription
new_pitchfloatThe new Sound's pitch

See also GetPitch.

SetVolumeOptimalโ€‹

Sets the Sound's volume

my_sound:SetVolume(new_volume)
Parameters
NameTypeDescription
new_volumefloatVolume multiplier, 1 is the original volume

See also GetVolume.

Playbackfunctionsโ€‹

FadeInOptimalโ€‹

Plays the sound with a fade effect

my_sound:FadeIn(fade_in_duration, fade_volume_level?, start_time?)
Parameters
NameTypeDescription
fade_in_durationfloatSeconds to fade in
fade_volume_level?floatVolume multiplier to fade to Default 1.0
start_time?floatSeconds into the sound to start playing from Default 0.0

FadeOutOptimalโ€‹

Stops the sound with a fade effect

my_sound:FadeOut(fade_out_duration, fade_volume_level?, destroy_after_fadeout?)
Parameters
NameTypeDescription
fade_out_durationfloatSeconds to fade out
fade_volume_level?floatVolume multiplier to fade to Default 0.0
destroy_after_fadeout?booleanWhether to destroy the Sound after fading out Default false

GetDurationOptimalโ€‹

Gets the duration of the Sound

local duration = my_sound:GetDuration()
Returns
TypeDescription
floatDuration of the Sound

GetPathOptimalโ€‹

Gets the asset path of this Sound

local path = my_sound:GetPath()
Returns
TypeDescription
stringThe asset path

IsPlayingOptimalโ€‹

Gets if the sound is playing

local is_playing = my_sound:IsPlaying()
Returns
TypeDescription
booleanWhether the sound is playing

PlayOptimalโ€‹

Starts the sound

my_sound:Play(start_time?)
Parameters
NameTypeDescription
start_time?floatSeconds into the sound to start playing from Default 0.0

SetPausedOptimalโ€‹

Pauses or resumes the sound

my_sound:SetPaused(pause?)
Parameters
NameTypeDescription
pause?booleanWhether to pause (true) or resume (false) Default true

StopOptimalโ€‹

Stops the sound

my_sound:Stop()

StopDelayedOptimalโ€‹

Stops the sound after the provided delay

my_sound:StopDelayed(delay)
Parameters
NameTypeDescription
delayfloatSeconds to wait before stopping

๐Ÿš€ Eventsโ€‹

This class doesn't have own events.

๐Ÿ”Š Sound Attenuationโ€‹

This property defines the function that determines the rate of attenuation over distance. There are a number of different functions to choose from:

๐Ÿ”ˆ Linearโ€‹

This is the default function. When using this function the volume will attenuate linearly and so the changes in volume are constant as the listener moves towards and away from the source. This function is good for crossfading between large background-type sounds that don't need tightly focussed 3D spatial falloff settings.

๐Ÿ”ˆ Logarithmicโ€‹

When using this function the volume attenuates such that the changes in volume are greater at close distances, and lesser at far distances. This function is good for spot sounds that need good 3D positioning, while still being audible at reasonable distances.

๐Ÿ”ˆ Inverseโ€‹

When using this function, the changes in volume are similar to that of the logarithmic curve, but are more exaggerated. This function is good for sounds that only need to be just audible at far distances, but that gets significantly louder as the listener gets quite close to the source.

๐Ÿ”ˆ Log Reverseโ€‹

When using this function, the changes in volume are lesser at close distances, with more dramatic differences at far distances. This function is good for sounds that need to be loud across larger distances.

๐Ÿ”ˆ Natural Soundโ€‹

This function attempts to model a more 'naturalistic' falloff behavior to produce behaviors that closer match reality. This function is a kind of 'middle ground' between the Logarithmic and Inverse functions.