natiny.audio.source_set_attenuation

Lua
natiny.audio.source_set_attenuation(source, model [, min_distance [, max_distance [, rolloff]]])

Sets distance attenuation for a spatial source.

Parameters

Name Type Default Description
source number Source handle
model integer natiny.audio.ATTENUATION_* constant
min_distance number 1.0 Distance at which attenuation begins
max_distance number 100.0 Distance beyond which gain stops changing
rolloff number 1.0 Attenuation strength

Example

Lua
natiny.audio.source_set_attenuation(source, natiny.audio.ATTENUATION_LINEAR,
                             1.0, 8.0, 1.0)

Notes

  • With distance d clamped between min_distance and max_distance, the models calculate gain as follows:
Constant Gain
ATTENUATION_NONE 1
ATTENUATION_INVERSE min / (min + rolloff * (d - min))
ATTENUATION_LINEAR max(0, 1 - rolloff * (d - min) / (max - min))
ATTENUATION_EXPONENTIAL (d / min) ^ -rolloff
  • min_distance is clamped to at least 0.0001, max_distance to at least min_distance, and rolloff to at least 0.0.
  • An unknown model uses inverse attenuation. Non-spatial sources ignore these settings.