The Distance Attenuation Model - Hangman/TuningFork GitHub Wiki
Taken from the OpenAL 1.1 specification:
Samples usually use the entire dynamic range of the chosen format/encoding, independent of their real world intensity. For example, a jet engine and a clockwork both will have samples with full amplitude. The application will then have to adjust source volume accordingly to account for relative differences. Source volume is then attenuated by distance. The effective attenuation of a source depends on many factors, among which distance attenuation and source and listener volume are only some of the contributing factors. Even if the source and listener volume exceed 1.0 (amplification beyond the guaranteed dynamic range), distance and other attenuation might ultimately limit the overall volume to a value below 1.0. OpenAL currently supports three modes of operation with respect to distance attenuation, including one that is similar to the IASIG I3DL2 model. The application can choose one of these models (or chooses to disable distance-dependent attenuation) on a per-context basis.
Setting The Distance Attenuation Model
You can specify the model in the AudioConfig before initialization:
AudioConfig config = new AudioConfig();
config.setDistanceAttenuationModel(DistanceAttenuationModel.INVERSE_DISTANCE_CLAMPED);
audio = Audio.init(config);
or at runtime:
audio.setDistanceAttenuationModel(DistanceAttenuationModel.INVERSE_DISTANCE_CLAMPED);
Setting The Min & Max Distance
The min distance value defines how far away the listener must be from the sound source for the attenuation to kick in.
audio.setDefaultAttenuationMinDistance(1f);
The max distance value defines how far away the listener must be from the sound source for the attenuation to stop. This only works with CLAMPED distance models and does not mean that the sound is no longer audible from there on. The volume of the sound merely remains the same. With the linear model, however, it means that the sound is no longer audible.
audio.setDefaultAttenuationMaxDistance(100f);
The Attenuation Factor
This factor determines how slowly or how quickly the sound source loses volume as the listener moves away from the source. A factor of 0.5 reduces the volume loss by half. With a factor of 2, the source loses volume twice as fast.
audio.setDefaultAttenuationFactor(0.5f);
Note: Setting a DistanceAttenuationModel overwrites the default min and max attenuation distance as well as the attenuation factor that is used for new sound sources. It won't affect existing ones.
Available Distance Attenuation Models
- NONE
- INVERSE_DISTANCE
- INVERSE_DISTANCE_CLAMPED
- LINEAR_DISTANCE
- LINEAR_DISTANCE_CLAMPED
- EXPONENT_DISTANCE
- EXPONENT_DISTANCE_CLAMPED
TuningFork uses the INVERSE_DISTANCE_CLAMPED model by default.
NONE
It's dead simple, no attenuation!
INVERSE_DISTANCE
The following formula describes the distance attenuation defined by the Inverse Distance Attenuation Model.
volume = attenuationMinDistance / (attenuationMinDistance + attenuationFactor * (distance – attenuationMinDistance));
The attenuationMinDistance parameter used here is a per-source attribute. attenuationMinDistance is the distance at which the listener will experience volume changes (unless the implementation had to clamp effective volume to the available dynamic range). attenuationFactor is per-source parameter the application can use to increase or decrease the range of a source by decreasing or increasing the attenuation, respectively. The default value is 1.
INVERSE_DISTANCE_CLAMPED
This is like the INVERSE_DISTANCE model, extended to guarantee that for distances below attenuationMinDistance, volume is clamped. This mode is equivalent to the IASIG I3DL2 distance model.
distance = max(distance, attenuationMinDistance);
distance = min(distance, attenuationMaxDistance);
volume = attenuationMinDistance / (attenuationMinDistance + attenuationFactor * (distance – attenuationMinDistance));
LINEAR_DISTANCE
This models a linear drop-off in volume as distance increases between the source and listener.
distance = min(distance, attenuationMaxDistance) // avoid negative volume
volume = (1 – attenuationFactor * (distance – attenuationMinDistance) / (attenuationMaxDistance – attenuationMinDistance))
LINEAR_DISTANCE_CLAMPED
This is the linear model, extended to guarantee that for distances below attenuationMinDistance, volume is clamped.
distance = max(distance, attenuationMinDistance)
distance = min(distance, attenuationMaxDistance)
volume = (1 – attenuationFactor * (distance – attenuationMinDistance) / (attenuationMaxDistance – attenuationMinDistance))
EXPONENT_DISTANCE
This models an exponential dropoff in volume as distance increases between the source and listener.
volume = (distance / attenuationMinDistance) ^ (- attenuationFactor)
where the ^ operation raises its first operand to the power of its second operand.
EXPONENT_DISTANCE_CLAMPED
This is the exponential model, extended to guarantee that for distances below attenuationMinDistance, volume is clamped.
distance = max(distance, attenuationMinDistance)
distance = min(distance, attenuationMaxDistance)
volume = (distance / attenuationMinDistance) ^ (- attenuationFactor)