Remove no-op mapgen::add_random_objects
[oweals/minetest.git] / src / sound_openal.cpp
index 4f056888b0dd56035843cdbbafabf89e1a82d500..8d76b69e16621105b6fdb0cbb8048fa1e8721589 100644 (file)
@@ -23,10 +23,10 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
 
 #include "sound_openal.h"
 
-#if defined(_MSC_VER)
+#if defined(_WIN32)
        #include <al.h>
        #include <alc.h>
-       #include <alext.h>
+       //#include <alext.h>
 #elif defined(__APPLE__)
        #include <OpenAL/al.h>
        #include <OpenAL/alc.h>
@@ -41,6 +41,7 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
 #include <map>
 #include <vector>
 #include "utility.h" // myrand()
+#include "filesys.h"
 
 #define BUFFER_SIZE 30000
 
@@ -115,9 +116,14 @@ SoundBuffer* loadOggFile(const std::string &filepath)
        char array[BUFFER_SIZE]; // Local fixed size array
        vorbis_info *pInfo;
        OggVorbis_File oggFile;
-
+       
+       // Do a dumb-ass static string copy for old versions of ov_fopen
+       // because they expect a non-const char*
+       char nonconst[10000];
+       snprintf(nonconst, 10000, "%s", filepath.c_str());
        // Try opening the given file
-       if(ov_fopen(filepath.c_str(), &oggFile) != 0)
+       //if(ov_fopen(filepath.c_str(), &oggFile) != 0)
+       if(ov_fopen(nonconst, &oggFile) != 0)
        {
                infostream<<"Audio: Error opening "<<filepath<<" for decoding"<<std::endl;
                return NULL;
@@ -286,22 +292,6 @@ public:
                return bufs[j];
        }
 
-       bool loadSound(const std::string &name,
-                       const std::string &filepath)
-       {
-               SoundBuffer *buf = loadOggFile(filepath);
-               if(buf)
-                       addBuffer(name, buf);
-               return false;
-       }
-       bool loadSound(const std::string &name,
-                       const std::vector<char> &filedata)
-       {
-               errorstream<<"OpenALSoundManager: Loading from filedata not"
-                               " implemented"<<std::endl;
-               return false;
-       }
-
        PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
                        float volume)
        {
@@ -392,15 +382,15 @@ public:
                if(!m_fetcher)
                        return NULL;
                std::set<std::string> paths;
-               std::set<std::vector<char> > datas;
+               std::set<std::string> datas;
                m_fetcher->fetchSounds(name, paths, datas);
                for(std::set<std::string>::iterator i = paths.begin();
                                i != paths.end(); i++){
-                       loadSound(name, *i);
+                       loadSoundFile(name, *i);
                }
-               for(std::set<std::vector<char> >::iterator i = datas.begin();
+               for(std::set<std::string>::iterator i = datas.begin();
                                i != datas.end(); i++){
-                       loadSound(name, *i);
+                       loadSoundData(name, *i);
                }
                return getBuffer(name);
        }
@@ -439,6 +429,31 @@ public:
 
        /* Interface */
 
+       bool loadSoundFile(const std::string &name,
+                       const std::string &filepath)
+       {
+               SoundBuffer *buf = loadOggFile(filepath);
+               if(buf)
+                       addBuffer(name, buf);
+               return false;
+       }
+       bool loadSoundData(const std::string &name,
+                       const std::string &filedata)
+       {
+               // The vorbis API sucks; just write it to a file and use vorbisfile
+               // TODO: Actually load it directly from memory
+               std::string basepath = porting::path_user + DIR_DELIM + "cache" +
+                               DIR_DELIM + "tmp";
+               std::string path = basepath + DIR_DELIM + "tmp.ogg";
+               verbosestream<<"OpenALSoundManager::loadSoundData(): Writing "
+                               <<"temporary file to ["<<path<<"]"<<std::endl;
+               fs::CreateAllDirs(basepath);
+               std::ofstream of(path.c_str(), std::ios::binary);
+               of.write(filedata.c_str(), filedata.size());
+               of.close();
+               return loadSoundFile(name, path);
+       }
+
        void updateListener(v3f pos, v3f vel, v3f at, v3f up)
        {
                m_listener_pos = pos;
@@ -454,6 +469,8 @@ public:
        int playSound(const std::string &name, bool loop, float volume)
        {
                maintain();
+               if(name == "")
+                       return 0;
                SoundBuffer *buf = getFetchBuffer(name);
                if(!buf){
                        infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
@@ -465,6 +482,8 @@ public:
        int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
        {
                maintain();
+               if(name == "")
+                       return 0;
                SoundBuffer *buf = getFetchBuffer(name);
                if(!buf){
                        infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
@@ -478,6 +497,24 @@ public:
                maintain();
                deleteSound(sound);
        }
+       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())
+                       return;
+               PlayingSound *sound = i->second;
+
+               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);
+       }
 };
 
 ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)