Modernize client code (#6250)
[oweals/minetest.git] / src / sound_openal.cpp
index b9af9e3a919cd86f66fc8e84108e0d999fd0b962..1772ee817f8fc81e8675215ea9421d280ab1da5e 100644 (file)
@@ -43,7 +43,7 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
 #include "porting.h"
 #include <vector>
 #include <fstream>
-#include "util/cpp11_container.h"
+#include <unordered_map>
 
 #define BUFFER_SIZE 30000
 
@@ -271,9 +271,22 @@ private:
        ALCdevice *m_device;
        ALCcontext *m_context;
        int m_next_id;
-       UNORDERED_MAP<std::string, std::vector<SoundBuffer*> > m_buffers;
-       UNORDERED_MAP<int, PlayingSound*> m_sounds_playing;
+       std::unordered_map<std::string, std::vector<SoundBuffer*>> m_buffers;
+       std::unordered_map<int, PlayingSound*> m_sounds_playing;
        v3f m_listener_pos;
+       struct FadeState {
+               FadeState() {}
+               FadeState(float step, float current_gain, float target_gain):
+                       step(step),
+                       current_gain(current_gain),
+                       target_gain(target_gain) {}
+               float step;
+               float current_gain;
+               float target_gain;
+       };
+
+       std::unordered_map<int, FadeState> m_sounds_fading;
+       float m_fade_delay;
 public:
        bool m_is_initialized;
        OpenALSoundManager(OnDemandSoundFetcher *fetcher):
@@ -281,6 +294,7 @@ public:
                m_device(NULL),
                m_context(NULL),
                m_next_id(1),
+               m_fade_delay(0),
                m_is_initialized(false)
        {
                ALCenum error = ALC_NO_ERROR;
@@ -337,8 +351,8 @@ public:
                alcCloseDevice(m_device);
                m_device = NULL;
 
-               for (UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i = m_buffers.begin();
-                               i != m_buffers.end(); ++i) {
+               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;
@@ -349,9 +363,14 @@ public:
                infostream<<"Audio: Deinitialized."<<std::endl;
        }
 
+       void step(float dtime)
+       {
+               doFades(dtime);
+       }
+
        void addBuffer(const std::string &name, SoundBuffer *buf)
        {
-               UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
+               std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
                                m_buffers.find(name);
                if(i != m_buffers.end()){
                        i->second.push_back(buf);
@@ -365,7 +384,7 @@ public:
 
        SoundBuffer* getBuffer(const std::string &name)
        {
-               UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
+               std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
                                m_buffers.find(name);
                if(i == m_buffers.end())
                        return NULL;
@@ -375,7 +394,7 @@ public:
        }
 
        PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
-                       float volume)
+                       float volume, float pitch)
        {
                infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
                assert(buf);
@@ -390,13 +409,14 @@ public:
                alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
                volume = MYMAX(0.0, volume);
                alSourcef(sound->source_id, AL_GAIN, volume);
+               alSourcef(sound->source_id, AL_PITCH, pitch);
                alSourcePlay(sound->source_id);
                warn_if_error(alGetError(), "createPlayingSound");
                return sound;
        }
 
        PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
-                       float volume, v3f pos)
+                       float volume, v3f pos, float pitch)
        {
                infostream<<"OpenALSoundManager: Creating positional playing sound"
                                <<std::endl;
@@ -413,15 +433,16 @@ public:
                alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
                volume = MYMAX(0.0, volume);
                alSourcef(sound->source_id, AL_GAIN, volume);
+               alSourcef(sound->source_id, AL_PITCH, pitch);
                alSourcePlay(sound->source_id);
                warn_if_error(alGetError(), "createPlayingSoundAt");
                return sound;
        }
 
-       int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
+       int playSoundRaw(SoundBuffer *buf, bool loop, float volume, float pitch)
        {
                assert(buf);
-               PlayingSound *sound = createPlayingSound(buf, loop, volume);
+               PlayingSound *sound = createPlayingSound(buf, loop, volume, pitch);
                if(!sound)
                        return -1;
                int id = m_next_id++;
@@ -429,10 +450,10 @@ public:
                return id;
        }
 
-       int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
+       int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos, float pitch)
        {
                assert(buf);
-               PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
+               PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos, pitch);
                if(!sound)
                        return -1;
                int id = m_next_id++;
@@ -442,7 +463,7 @@ public:
 
        void deleteSound(int id)
        {
-               UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
                if(i == m_sounds_playing.end())
                        return;
                PlayingSound *sound = i->second;
@@ -482,7 +503,7 @@ public:
                                <<m_sounds_playing.size()<<" playing sounds, "
                                <<m_buffers.size()<<" sound names loaded"<<std::endl;
                std::set<int> del_list;
-               for(UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.begin();
+               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;
@@ -515,6 +536,7 @@ public:
                        addBuffer(name, buf);
                return false;
        }
+
        bool loadSoundData(const std::string &name,
                        const std::string &filedata)
        {
@@ -541,7 +563,7 @@ public:
                alListenerf(AL_GAIN, gain);
        }
 
-       int playSound(const std::string &name, bool loop, float volume)
+       int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
        {
                maintain();
                if(name == "")
@@ -552,9 +574,17 @@ public:
                                        <<std::endl;
                        return -1;
                }
-               return playSoundRaw(buf, loop, volume);
+               int handle = -1;
+               if (fade > 0) {
+                       handle = playSoundRaw(buf, loop, 0.0f, pitch);
+                       fadeSound(handle, fade, volume);
+               } else {
+                       handle = playSoundRaw(buf, loop, volume, pitch);
+               }
+               return handle;
        }
-       int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
+
+       int playSoundAt(const std::string &name, bool loop, float volume, v3f pos, float pitch)
        {
                maintain();
                if(name == "")
@@ -565,21 +595,60 @@ public:
                                        <<std::endl;
                        return -1;
                }
-               return playSoundRawAt(buf, loop, volume, pos);
+               return playSoundRawAt(buf, loop, volume, pos, pitch);
        }
+
        void stopSound(int sound)
        {
                maintain();
                deleteSound(sound);
        }
+
+       void fadeSound(int soundid, float step, float gain)
+       {
+               m_sounds_fading[soundid] = FadeState(step, getSoundGain(soundid), gain);
+       }
+
+       void doFades(float dtime)
+       {
+               m_fade_delay += dtime;
+
+               if (m_fade_delay < 0.1f)
+                       return;
+
+               float chkGain = 0;
+               for (std::unordered_map<int, FadeState>::iterator i = m_sounds_fading.begin();
+                               i != m_sounds_fading.end();) {
+                       if (i->second.step < 0.f)
+                               chkGain = -(i->second.current_gain);
+                       else
+                               chkGain = i->second.current_gain;
+
+                       if (chkGain < i->second.target_gain) {
+                               i->second.current_gain += (i->second.step * m_fade_delay);
+                               i->second.current_gain = rangelim(i->second.current_gain, 0, 1);
+
+                               updateSoundGain(i->first, i->second.current_gain);
+                               ++i;
+                       } else {
+                               if (i->second.target_gain <= 0.f)
+                                       stopSound(i->first);
+
+                               m_sounds_fading.erase(i++);
+                       }
+               }
+               m_fade_delay = 0;
+       }
+
        bool soundExists(int sound)
        {
                maintain();
                return (m_sounds_playing.count(sound) != 0);
        }
+
        void updateSoundPosition(int id, v3f pos)
        {
-               UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
                if (i == m_sounds_playing.end())
                        return;
                PlayingSound *sound = i->second;
@@ -589,6 +658,29 @@ public:
                alSource3f(sound->source_id, AL_VELOCITY, 0, 0, 0);
                alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
        }
+
+       bool updateSoundGain(int id, float gain)
+       {
+               std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               if (i == m_sounds_playing.end())
+                       return false;
+
+               PlayingSound *sound = i->second;
+               alSourcef(sound->source_id, AL_GAIN, gain);
+               return true;
+       }
+
+       float getSoundGain(int id)
+       {
+               std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               if (i == m_sounds_playing.end())
+                       return 0;
+
+               PlayingSound *sound = i->second;
+               ALfloat gain;
+               alGetSourcef(sound->source_id, AL_GAIN, &gain);
+               return gain;
+       }
 };
 
 ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)