Expose audio attenuation model
Prerequisite Checklist
- I searched for existing issues to prevent duplicates
- I searched for existing discussions on the forum to prevent duplicates
- I understand SFML's scope and believe the feature fits it
Describe your feature request here
Feature request: expose audio attenuation model
Description
SFML 3 uses miniaudio as its audio backend, which provides multiple attenuation models for spatialized sounds.
Currently, sf::SoundSource exposes parameters such as:
setAttenuation()setMinDistance()setMaxDistance()setMinGain()setMaxGain()
However, there is no way to explicitly select the attenuation model itself.
For applications that require a specific spatial audio behavior, this is limiting because the attenuation model is an important part of the spatialization algorithm.
Proposed API
The preferred solution would be to expose the attenuation model through the public SFML API, for example:
enum class AttenuationModel
{
None,
Inverse,
Linear,
Exponential
};and:
void setAttenuationModel(AttenuationModel model);
[[nodiscard]] AttenuationModel getAttenuationModel() const;This would allow users to explicitly choose the desired attenuation behavior without depending on the underlying audio backend.
For example:
sound.setAttenuationModel(sf::AttenuationModel::Linear);Alternative solution
If exposing the model as part of the public API is not desirable, another option would be to make the underlying sound handle accessible to derived classes.
Currently sf::SoundSource internally has access to the underlying sound object, but it is not available to users extending SFML classes.
Making the corresponding getSound() functionality protected would allow advanced users to configure backend-specific properties themselves, for example:
class MySound : public sf::Sound
{
public:
void setMyAttenuationModel(...)
{
auto* sound = getSound();
// Configure the underlying miniaudio sound.
}
};This would be useful not only for attenuation models but also for other miniaudio features that are not currently exposed by SFML.
Compatibility
This should be backwards compatible because the existing default attenuation behavior can remain unchanged. The new API would simply allow applications to explicitly select another model when needed.
I believe exposing the attenuation model would make the SFML 3 audio spatialization API more complete while still keeping the miniaudio implementation detail hidden from normal users.
Use Cases
- Allow games to choose a suitable attenuation model for different types of sounds, such as linear attenuation for environmental audio.
- Allow developers to achieve different spatial audio behaviors without manually calculating volume or modifying SFML.
API Example
#include <SFML/Audio.hpp>
int main()
{
sf::SoundBuffer buffer("sound.wav");
sf::Sound sound(buffer);
// Explicitly select the attenuation model.
sound.setAttenuationModel(sf::AttenuationModel::Linear);
sound.setMinDistance(5.f);
sound.setMaxDistance(100.f);
sound.play();
}Source: SFML/SFML