Tooltips: Unify the tooltip[] and list[] description tooltip display functions (...
[oweals/minetest.git] / src / sound_openal.cpp
index ca1023413305cd7419279191f9fe909166085c54..a425af8274374f34be56380171b5bab5c93ca7fd 100644 (file)
@@ -39,12 +39,11 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
 #include <vorbis/vorbisfile.h>
 #include <assert.h>
 #include "log.h"
-#include "filesys.h"
 #include "util/numeric.h" // myrand()
 #include "porting.h"
-#include <map>
 #include <vector>
 #include <fstream>
+#include "util/cpp11_container.h"
 
 #define BUFFER_SIZE 30000
 
@@ -145,6 +144,7 @@ SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
                        ov_clear(oggFile);
                        infostream << "Audio: Error decoding "
                                << filename_for_logging << std::endl;
+                       delete snd;
                        return NULL;
                }
 
@@ -270,19 +270,31 @@ private:
        OnDemandSoundFetcher *m_fetcher;
        ALCdevice *m_device;
        ALCcontext *m_context;
-       bool m_can_vorbis;
        int m_next_id;
-       std::map<std::string, std::vector<SoundBuffer*> > m_buffers;
-       std::map<int, PlayingSound*> m_sounds_playing;
+       UNORDERED_MAP<std::string, std::vector<SoundBuffer*> > m_buffers;
+       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;
+       };
+
+       UNORDERED_MAP<int, FadeState> m_sounds_fading;
+       float m_fade_delay;
 public:
        bool m_is_initialized;
        OpenALSoundManager(OnDemandSoundFetcher *fetcher):
                m_fetcher(fetcher),
                m_device(NULL),
                m_context(NULL),
-               m_can_vorbis(false),
                m_next_id(1),
+               m_fade_delay(0),
                m_is_initialized(false)
        {
                ALCenum error = ALC_NO_ERROR;
@@ -296,14 +308,6 @@ public:
                        return;
                }
 
-               if(alcIsExtensionPresent(m_device, "EXT_vorbis")){
-                       infostream<<"Audio: Vorbis extension present"<<std::endl;
-                       m_can_vorbis = true;
-               } else{
-                       infostream<<"Audio: Vorbis extension NOT present"<<std::endl;
-                       m_can_vorbis = false;
-               }
-
                m_context = alcCreateContext(m_device, NULL);
                if(!m_context){
                        error = alcGetError(m_device);
@@ -327,7 +331,7 @@ public:
                        return;
                }
 
-               alDistanceModel(AL_EXPONENT_DISTANCE);
+               alDistanceModel(AL_INVERSE_DISTANCE);
 
                infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
                                <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
@@ -347,7 +351,7 @@ public:
                alcCloseDevice(m_device);
                m_device = NULL;
 
-               for (std::map<std::string, std::vector<SoundBuffer*> >::iterator i = m_buffers.begin();
+               for (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) {
@@ -359,9 +363,14 @@ public:
                infostream<<"Audio: Deinitialized."<<std::endl;
        }
 
+       void step(float dtime)
+       {
+               doFades(dtime);
+       }
+
        void addBuffer(const std::string &name, SoundBuffer *buf)
        {
-               std::map<std::string, std::vector<SoundBuffer*> >::iterator i =
+               UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
                                m_buffers.find(name);
                if(i != m_buffers.end()){
                        i->second.push_back(buf);
@@ -375,7 +384,7 @@ public:
 
        SoundBuffer* getBuffer(const std::string &name)
        {
-               std::map<std::string, std::vector<SoundBuffer*> >::iterator i =
+               UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
                                m_buffers.find(name);
                if(i == m_buffers.end())
                        return NULL;
@@ -419,7 +428,6 @@ public:
                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_ROLLOFF_FACTOR, 0.7);
                alSourcef(sound->source_id, AL_REFERENCE_DISTANCE, 30.0);
                alSourcei(sound->source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
                volume = MYMAX(0.0, volume);
@@ -453,8 +461,7 @@ public:
 
        void deleteSound(int id)
        {
-               std::map<int, PlayingSound*>::iterator i =
-                               m_sounds_playing.find(id);
+               UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
                if(i == m_sounds_playing.end())
                        return;
                PlayingSound *sound = i->second;
@@ -494,10 +501,8 @@ public:
                                <<m_sounds_playing.size()<<" playing sounds, "
                                <<m_buffers.size()<<" sound names loaded"<<std::endl;
                std::set<int> del_list;
-               for(std::map<int, PlayingSound*>::iterator
-                               i = m_sounds_playing.begin();
-                               i != m_sounds_playing.end(); ++i)
-               {
+               for(UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.begin();
+                               i != m_sounds_playing.end(); ++i) {
                        int id = i->first;
                        PlayingSound *sound = i->second;
                        // If not playing, remove it
@@ -529,6 +534,7 @@ public:
                        addBuffer(name, buf);
                return false;
        }
+
        bool loadSoundData(const std::string &name,
                        const std::string &filedata)
        {
@@ -555,7 +561,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)
        {
                maintain();
                if(name == "")
@@ -566,8 +572,16 @@ public:
                                        <<std::endl;
                        return -1;
                }
-               return playSoundRaw(buf, loop, volume);
+               int handle = -1;
+               if (fade > 0) {
+                       handle = playSoundRaw(buf, loop, 0);
+                       fadeSound(handle, fade, volume);
+               } else {
+                       handle = playSoundRaw(buf, loop, volume);
+               }
+               return handle;
        }
+
        int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
        {
                maintain();
@@ -581,21 +595,59 @@ public:
                }
                return playSoundRawAt(buf, loop, volume, pos);
        }
+
        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 (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)
        {
-               std::map<int, PlayingSound*>::iterator i =
-                               m_sounds_playing.find(id);
-               if(i == m_sounds_playing.end())
+               UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               if (i == m_sounds_playing.end())
                        return;
                PlayingSound *sound = i->second;
 
@@ -604,6 +656,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)
+       {
+               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)
+       {
+               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)