Skip to content

Commit

Permalink
SOUND: Expose attenuation function parameters. Use less aggressive de…
Browse files Browse the repository at this point in the history
…faults.

The default, (0.5, 0, -1, 0) -> -6db and 0.5m ref distance, resulted in too strong attenuation.
  • Loading branch information
logzero committed Sep 26, 2014
1 parent ae4a6a1 commit 7e224c0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,7 @@ void Game::ProcessNewSettings()

sound.SetVolume(settings.GetSoundVolume());
sound.SetMaxActiveSources(settings.GetMaxSoundSources());
sound.SetAttenuation(settings.GetSoundAttenuation());
}

void Game::ShowHUD(bool value)
Expand Down
9 changes: 9 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ Settings::Settings() :
{
resolution[0] = 800;
resolution[1] = 600;

sound_attenuation[0] = 0.9146065;
sound_attenuation[1] = 0.2729276;
sound_attenuation[2] = -0.2313740;
sound_attenuation[3] = -0.2884304;
}

void Settings::SetFailsafeSettings()
Expand Down Expand Up @@ -181,6 +186,10 @@ void Settings::Serialize(bool write, Config & config)
Param(config, write, section, "sky_time_speed", sky_time_speed);

config.get("sound", section);
Param(config, write, section, "attenuation_scale", sound_attenuation[0]);
Param(config, write, section, "attenuation_shift", sound_attenuation[1]);
Param(config, write, section, "attenuation_exponent", sound_attenuation[2]);
Param(config, write, section, "attenuation_offset", sound_attenuation[3]);
Param(config, write, section, "sources", sound_sources);
Param(config, write, section, "volume", sound_volume);
Param(config, write, section, "music_volume", music_volume);
Expand Down
7 changes: 7 additions & 0 deletions src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ class Settings
return sound_sources;
}

// get sound attenuation[4] coefficients
const float * GetSoundAttenuation() const
{
return sound_attenuation;
}

bool GetMPH() const
{
return mph;
Expand Down Expand Up @@ -400,6 +406,7 @@ class Settings
float music_volume;
float sound_volume;
int sound_sources;
float sound_attenuation[4];
bool mph; //if false, KPH
std::string track;
int antialiasing; //0 or 1 mean off
Expand Down
22 changes: 17 additions & 5 deletions src/sound/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ Sound::Sound() :
samplers_num(0),
samplers_pause(true)
{
attenuation[0] = 0.9146065;
attenuation[1] = 0.2729276;
attenuation[2] = -0.2313740;
attenuation[3] = -0.2884304;

sources.reserve(64);
samplers.reserve(64);
}
Expand Down Expand Up @@ -255,6 +260,14 @@ void Sound::SetMaxActiveSources(size_t value)
max_active_sources = value;
}

void Sound::SetAttenuation(const float nattenuation[4])
{
attenuation[0] = nattenuation[0];
attenuation[1] = nattenuation[1];
attenuation[2] = nattenuation[2];
attenuation[3] = nattenuation[3];
}

size_t Sound::AddSource(std::tr1::shared_ptr<SoundBuffer> buffer, float offset, bool is3d, bool loop)
{
Source src;
Expand Down Expand Up @@ -441,11 +454,10 @@ void Sound::ProcessSources()
float len = relvec.Magnitude();
if (len < 0.1f) len = 0.1f;

// distance attenuation 1/r
float attenuation = 1.0f;
float mindist = 0.5f;
float dist = std::max(mindist, len);
float cgain = mindist / (mindist + attenuation * (dist - mindist));
// distance attenuation
// y = a * (x - b)^c + d
float cgain = attenuation[0] * powf(len - attenuation[1], attenuation[2]) + attenuation[3];
cgain = clamp(cgain, 0.0f, 1.0f);

// directional attenuation
// maximum at 0.75 (source on opposite side)
Expand Down
4 changes: 4 additions & 0 deletions src/sound/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Sound
// active sources limit can be adjusted at runtime
void SetMaxActiveSources(size_t value);

// attenuation: y = a * (x - b)^c + d
void SetAttenuation(const float attenuation[4]);

size_t AddSource(std::tr1::shared_ptr<SoundBuffer> buffer, float offset, bool is3d, bool loop);

void RemoveSource(size_t id);
Expand Down Expand Up @@ -87,6 +90,7 @@ class Sound
Vec3 listener_pos;
Vec3 listener_vel;
Quat listener_rot;
float attenuation[4];
float sound_volume;
bool initdone;
bool disable;
Expand Down

0 comments on commit 7e224c0

Please sign in to comment.