]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/sound_openal.cpp
Sound: Add pitch option (#5960)
[dragonfireclient.git] / src / sound_openal.cpp
index 66faf40c1a9f2e2cb488a6f6659fd29a26d99f9f..0b53572c428b645a8d87e7c3798db8f1959a88d7 100644 (file)
@@ -1,22 +1,22 @@
 /*
-Minetest-c55
-Copyright (C) 2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 OpenAL support based on work by:
 Copyright (C) 2011 Sebastian 'Bahamada' Rühl
 Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
 Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; ifnot, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
@@ -30,18 +30,20 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
 #elif defined(__APPLE__)
        #include <OpenAL/al.h>
        #include <OpenAL/alc.h>
-       #include <OpenAL/alext.h>
+       //#include <OpenAL/alext.h>
 #else
        #include <AL/al.h>
        #include <AL/alc.h>
        #include <AL/alext.h>
 #endif
 #include <vorbis/vorbisfile.h>
+#include <assert.h>
 #include "log.h"
-#include <map>
+#include "util/numeric.h" // myrand()
+#include "porting.h"
 #include <vector>
-#include "utility.h" // myrand()
-#include "filesys.h"
+#include <fstream>
+#include <unordered_map>
 
 #define BUFFER_SIZE 30000
 
@@ -89,7 +91,7 @@ static ALenum warn_if_error(ALenum err, const char *desc)
 {
        if(err == AL_NO_ERROR)
                return err;
-       errorstream<<"WARNING: "<<desc<<": "<<alErrorString(err)<<std::endl;
+       warningstream<<desc<<": "<<alErrorString(err)<<std::endl;
        return err;
 }
 
@@ -108,31 +110,19 @@ struct SoundBuffer
        std::vector<char> buffer;
 };
 
-SoundBuffer* loadOggFile(const std::string &filepath)
+SoundBuffer *load_opened_ogg_file(OggVorbis_File *oggFile,
+               const std::string &filename_for_logging)
 {
        int endian = 0; // 0 for Little-Endian, 1 for Big-Endian
        int bitStream;
        long bytes;
        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(nonconst, &oggFile) != 0)
-       {
-               infostream<<"Audio: Error opening "<<filepath<<" for decoding"<<std::endl;
-               return NULL;
-       }
 
        SoundBuffer *snd = new SoundBuffer;
 
        // Get some information about the OGG file
-       pInfo = ov_info(&oggFile, -1);
+       pInfo = ov_info(oggFile, -1);
 
        // Check the number of channels... always use 16-bit samples
        if(pInfo->channels == 1)
@@ -147,12 +137,14 @@ SoundBuffer* loadOggFile(const std::string &filepath)
        do
        {
                // Read up to a buffer's worth of decoded sound data
-               bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
+               bytes = ov_read(oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);
 
                if(bytes < 0)
                {
-                       ov_clear(&oggFile);
-                       infostream<<"Audio: Error decoding "<<filepath<<std::endl;
+                       ov_clear(oggFile);
+                       infostream << "Audio: Error decoding "
+                               << filename_for_logging << std::endl;
+                       delete snd;
                        return NULL;
                }
 
@@ -172,14 +164,100 @@ SoundBuffer* loadOggFile(const std::string &filepath)
                                <<"preparing sound buffer"<<std::endl;
        }
 
-       infostream<<"Audio file "<<filepath<<" loaded"<<std::endl;
+       infostream << "Audio file "
+               << filename_for_logging << " loaded" << std::endl;
 
        // Clean up!
-       ov_clear(&oggFile);
+       ov_clear(oggFile);
 
        return snd;
 }
 
+SoundBuffer *load_ogg_from_file(const std::string &path)
+{
+       OggVorbis_File oggFile;
+
+       // Try opening the given file.
+       // This requires libvorbis >= 1.3.2, as
+       // previous versions expect a non-const char *
+       if (ov_fopen(path.c_str(), &oggFile) != 0) {
+               infostream << "Audio: Error opening " << path
+                       << " for decoding" << std::endl;
+               return NULL;
+       }
+
+       return load_opened_ogg_file(&oggFile, path);
+}
+
+struct BufferSource {
+       const char *buf;
+       size_t cur_offset;
+       size_t len;
+};
+
+size_t buffer_sound_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
+{
+       BufferSource *s = (BufferSource *)datasource;
+       size_t copied_size = MYMIN(s->len - s->cur_offset, size);
+       memcpy(ptr, s->buf + s->cur_offset, copied_size);
+       s->cur_offset += copied_size;
+       return copied_size;
+}
+
+int buffer_sound_seek_func(void *datasource, ogg_int64_t offset, int whence)
+{
+       BufferSource *s = (BufferSource *)datasource;
+       if (whence == SEEK_SET) {
+               if (offset < 0 || (size_t)MYMAX(offset, 0) >= s->len) {
+                       // offset out of bounds
+                       return -1;
+               }
+               s->cur_offset = offset;
+               return 0;
+       } else if (whence == SEEK_CUR) {
+               if ((size_t)MYMIN(-offset, 0) > s->cur_offset
+                               || s->cur_offset + offset > s->len) {
+                       // offset out of bounds
+                       return -1;
+               }
+               s->cur_offset += offset;
+               return 0;
+       }
+       // invalid whence param (SEEK_END doesn't have to be supported)
+       return -1;
+}
+
+long BufferSourceell_func(void *datasource)
+{
+       BufferSource *s = (BufferSource *)datasource;
+       return s->cur_offset;
+}
+
+static ov_callbacks g_buffer_ov_callbacks = {
+       &buffer_sound_read_func,
+       &buffer_sound_seek_func,
+       NULL,
+       &BufferSourceell_func
+};
+
+SoundBuffer *load_ogg_from_buffer(const std::string &buf, const std::string &id_for_log)
+{
+       OggVorbis_File oggFile;
+
+       BufferSource s;
+       s.buf = buf.c_str();
+       s.cur_offset = 0;
+       s.len = buf.size();
+
+       if (ov_open_callbacks(&s, &oggFile, NULL, 0, g_buffer_ov_callbacks) != 0) {
+               infostream << "Audio: Error opening " << id_for_log
+                       << " for decoding" << std::endl;
+               return NULL;
+       }
+
+       return load_opened_ogg_file(&oggFile, id_for_log);
+}
+
 struct PlayingSound
 {
        ALuint source_id;
@@ -192,21 +270,35 @@ class OpenALSoundManager: public ISoundManager
        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;
+       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):
                m_fetcher(fetcher),
                m_device(NULL),
                m_context(NULL),
-               m_can_vorbis(false),
-               m_next_id(1)
+               m_next_id(1),
+               m_fade_delay(0),
+               m_is_initialized(false)
        {
                ALCenum error = ALC_NO_ERROR;
-               
+
                infostream<<"Audio: Initializing..."<<std::endl;
 
                m_device = alcOpenDevice(NULL);
@@ -216,14 +308,6 @@ class OpenALSoundManager: public ISoundManager
                        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);
@@ -247,11 +331,13 @@ class OpenALSoundManager: public ISoundManager
                        return;
                }
 
-               alDistanceModel(AL_EXPONENT_DISTANCE);
+               alDistanceModel(AL_INVERSE_DISTANCE);
 
                infostream<<"Audio: Initialized: OpenAL "<<alGetString(AL_VERSION)
                                <<", using "<<alcGetString(m_device, ALC_DEVICE_SPECIFIER)
                                <<std::endl;
+
+               m_is_initialized = true;
        }
 
        ~OpenALSoundManager()
@@ -264,12 +350,27 @@ class OpenALSoundManager: public ISoundManager
                m_context = NULL;
                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;
+                       }
+                       (*i).second.clear();
+               }
+               m_buffers.clear();
                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 =
+               std::unordered_map<std::string, std::vector<SoundBuffer*>>::iterator i =
                                m_buffers.find(name);
                if(i != m_buffers.end()){
                        i->second.push_back(buf);
@@ -283,7 +384,7 @@ class OpenALSoundManager: public ISoundManager
 
        SoundBuffer* getBuffer(const std::string &name)
        {
-               std::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;
@@ -293,7 +394,7 @@ class OpenALSoundManager: public ISoundManager
        }
 
        PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
-                       float volume)
+                       float volume, float pitch)
        {
                infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
                assert(buf);
@@ -308,13 +409,14 @@ class OpenALSoundManager: public ISoundManager
                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;
@@ -327,20 +429,20 @@ 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_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);
                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++;
@@ -348,25 +450,24 @@ class OpenALSoundManager: public ISoundManager
                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++;
                m_sounds_playing[id] = sound;
                return id;
        }
-       
+
        void deleteSound(int id)
        {
-               std::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;
-               
+
                alDeleteSources(1, &sound->source_id);
 
                delete sound;
@@ -374,7 +475,7 @@ class OpenALSoundManager: public ISoundManager
        }
 
        /* If buffer does not exist, consult the fetcher */
-       SoundBuffer* getFetchBuffer(const std::string name)
+       SoundBuffer* getFetchBuffer(const std::string &name)
        {
                SoundBuffer *buf = getBuffer(name);
                if(buf)
@@ -385,16 +486,16 @@ class OpenALSoundManager: public ISoundManager
                std::set<std::string> datas;
                m_fetcher->fetchSounds(name, paths, datas);
                for(std::set<std::string>::iterator i = paths.begin();
-                               i != paths.end(); i++){
+                               i != paths.end(); ++i){
                        loadSoundFile(name, *i);
                }
                for(std::set<std::string>::iterator i = datas.begin();
-                               i != datas.end(); i++){
+                               i != datas.end(); ++i){
                        loadSoundData(name, *i);
                }
                return getBuffer(name);
        }
-       
+
        // Remove stopped sounds
        void maintain()
        {
@@ -402,10 +503,8 @@ 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::map<int, PlayingSound*>::iterator
-                               i = m_sounds_playing.begin();
-                               i != m_sounds_playing.end(); i++)
-               {
+               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;
                        // If not playing, remove it
@@ -417,11 +516,11 @@ class OpenALSoundManager: public ISoundManager
                                }
                        }
                }
-               if(del_list.size() != 0)
+               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++)
+                               i != del_list.end(); ++i)
                {
                        deleteSound(*i);
                }
@@ -432,26 +531,19 @@ class OpenALSoundManager: public ISoundManager
        bool loadSoundFile(const std::string &name,
                        const std::string &filepath)
        {
-               SoundBuffer *buf = loadOggFile(filepath);
-               if(buf)
+               SoundBuffer *buf = load_ogg_from_file(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);
+               SoundBuffer *buf = load_ogg_from_buffer(filedata, name);
+               if (buf)
+                       addBuffer(name, buf);
+               return false;
        }
 
        void updateListener(v3f pos, v3f vel, v3f at, v3f up)
@@ -466,7 +558,12 @@ class OpenALSoundManager: public ISoundManager
                warn_if_error(alGetError(), "updateListener");
        }
 
-       int playSound(const std::string &name, bool loop, float volume)
+       void setListenerGain(float gain)
+       {
+               alListenerf(AL_GAIN, gain);
+       }
+
+       int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
        {
                maintain();
                if(name == "")
@@ -477,9 +574,17 @@ class OpenALSoundManager: public ISoundManager
                                        <<std::endl;
                        return -1;
                }
-               return playSoundRaw(buf, loop, volume);
+               int handle = -1;
+               if (fade > 0) {
+                       handle = playSoundRaw(buf, loop, 0.0f, 0.0f);
+                       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 == "")
@@ -490,23 +595,61 @@ class OpenALSoundManager: public ISoundManager
                                        <<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)
        {
-               std::map<int, PlayingSound*>::iterator i =
-                               m_sounds_playing.find(id);
-               if(i == m_sounds_playing.end())
+               std::unordered_map<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
+               if (i == m_sounds_playing.end())
                        return;
                PlayingSound *sound = i->second;
 
@@ -515,10 +658,37 @@ class OpenALSoundManager: public ISoundManager
                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)
 {
-       return new OpenALSoundManager(fetcher);
+       OpenALSoundManager *m = new OpenALSoundManager(fetcher);
+       if(m->m_is_initialized)
+               return m;
+       delete m;
+       return NULL;
 };