]> git.lizzy.rs Git - minetest.git/blobdiff - src/sound_openal.cpp
Improvements/fixes for noise parameter input in advanced settings
[minetest.git] / src / sound_openal.cpp
index 1772ee817f8fc81e8675215ea9421d280ab1da5e..b33a85703f340255e1c61ef9110c5e50f3e78410 100644 (file)
@@ -36,8 +36,9 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
        #include <AL/alc.h>
        #include <AL/alext.h>
 #endif
+#include <cmath>
 #include <vorbis/vorbisfile.h>
-#include <assert.h>
+#include <cassert>
 #include "log.h"
 #include "util/numeric.h" // myrand()
 #include "porting.h"
@@ -275,7 +276,8 @@ class OpenALSoundManager: public ISoundManager
        std::unordered_map<int, PlayingSound*> m_sounds_playing;
        v3f m_listener_pos;
        struct FadeState {
-               FadeState() {}
+               FadeState() = default;
+
                FadeState(float step, float current_gain, float target_gain):
                        step(step),
                        current_gain(current_gain),
@@ -331,7 +333,7 @@ class OpenALSoundManager: public ISoundManager
                        return;
                }
 
-               alDistanceModel(AL_INVERSE_DISTANCE);
+               alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
 
                infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
                                <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
@@ -351,13 +353,11 @@ class OpenALSoundManager: public ISoundManager
                alcCloseDevice(m_device);
                m_device = NULL;
 
-               for (std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
-                               m_buffers.begin(); i != m_buffers.end(); ++i) {
-                       for (std::vector<SoundBuffer*>::iterator iter = (*i).second.begin();
-                                       iter != (*i).second.end(); ++iter) {
-                               delete *iter;
+               for (auto &buffer : m_buffers) {
+                       for (SoundBuffer *sb : buffer.second) {
+                               delete sb;
                        }
-                       (*i).second.clear();
+                       buffer.second.clear();
                }
                m_buffers.clear();
                infostream<<"Audio: Deinitialized."<<std::endl;
@@ -379,7 +379,6 @@ class OpenALSoundManager: public ISoundManager
                std::vector<SoundBuffer*> bufs;
                bufs.push_back(buf);
                m_buffers[name] = bufs;
-               return;
        }
 
        SoundBuffer* getBuffer(const std::string &name)
@@ -407,7 +406,7 @@ class OpenALSoundManager: public ISoundManager
                alSource3f(sound->source_id, AL_POSITION, 0, 0, 0);
                alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
                alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
-               volume = MYMAX(0.0, volume);
+               volume = std::fmax(0.0f, volume);
                alSourcef(sound->source_id, AL_GAIN, volume);
                alSourcef(sound->source_id, AL_PITCH, pitch);
                alSourcePlay(sound->source_id);
@@ -429,9 +428,14 @@ class OpenALSoundManager: public ISoundManager
                alSourcei(sound->source_id, AL_SOURCE_RELATIVE, false);
                alSource3f(sound->source_id, AL_POSITION, pos.X, pos.Y, pos.Z);
                alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
-               alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
+               // Use alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED) and set reference
+               // distance to clamp gain at <1 node distance, to avoid excessive
+               // volume when closer
+               alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 10.0f);
                alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
-               volume = MYMAX(0.0, volume);
+               // Multiply by 3 to compensate for reducing AL_REFERENCE_DISTANCE from
+               // the previous value of 30 to the new value of 10
+               volume = std::fmax(0.0f, volume * 3.0f);
                alSourcef(sound->source_id, AL_GAIN, volume);
                alSourcef(sound->source_id, AL_PITCH, pitch);
                alSourcePlay(sound->source_id);
@@ -450,7 +454,8 @@ class OpenALSoundManager: public ISoundManager
                return id;
        }
 
-       int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch)
+       int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, const v3f &pos,
+                       float pitch)
        {
                assert(buf);
                PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
@@ -485,13 +490,11 @@ class OpenALSoundManager: public ISoundManager
                std::set<std::string> paths;
                std::set<std::string> datas;
                m_fetcher->fetchSounds(name, paths, datas);
-               for(std::set<std::string>::iterator i = paths.begin();
-                               i != paths.end(); ++i){
-                       loadSoundFile(name, *i);
+               for (const std::string &path : paths) {
+                       loadSoundFile(name, path);
                }
-               for(std::set<std::string>::iterator i = datas.begin();
-                               i != datas.end(); ++i){
-                       loadSoundData(name, *i);
+               for (const std::string &data : datas) {
+                       loadSoundData(name, data);
                }
                return getBuffer(name);
        }
@@ -503,10 +506,9 @@ class OpenALSoundManager: public ISoundManager
                                <<m_sounds_playing.size()<<" playing sounds, "
                                <<m_buffers.size()<<" sound names loaded"<<std::endl;
                std::set<int> del_list;
-               for(std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.begin();
-                               i != m_sounds_playing.end(); ++i) {
-                       int id = i->first;
-                       PlayingSound *sound = i->second;
+               for (auto &sp : m_sounds_playing) {
+                       int id = sp.first;
+                       PlayingSound *sound = sp.second;
                        // If not playing, remove it
                        {
                                ALint state;
@@ -519,10 +521,8 @@ class OpenALSoundManager: public ISoundManager
                if(!del_list.empty())
                        verbosestream<<"OpenALSoundManager::maintain(): deleting "
                                        <<del_list.size()<<" playing sounds"<<std::endl;
-               for(std::set<int>::iterator i = del_list.begin();
-                               i != del_list.end(); ++i)
-               {
-                       deleteSound(*i);
+               for (int i : del_list) {
+                       deleteSound(i);
                }
        }
 
@@ -566,7 +566,7 @@ class OpenALSoundManager: public ISoundManager
        int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
        {
                maintain();
-               if(name == "")
+               if (name.empty())
                        return 0;
                SoundBuffer *buf = getFetchBuffer(name);
                if(!buf){
@@ -587,7 +587,7 @@ class OpenALSoundManager: public ISoundManager
        int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
        {
                maintain();
-               if(name == "")
+               if (name.empty())
                        return 0;
                SoundBuffer *buf = getFetchBuffer(name);
                if(!buf){