c55sound continued
authorPerttu Ahola <celeron55@gmail.com>
Fri, 23 Mar 2012 13:29:30 +0000 (15:29 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sat, 24 Mar 2012 02:24:25 +0000 (04:24 +0200)
src/CMakeLists.txt
src/camera.cpp
src/camera.h
src/client.cpp
src/client.h
src/game.cpp
src/sound.h
src/sound_openal.cpp
src/sound_openal.h

index b8eb9b4e34e90b348d44cda7876c4df35287da86..c852d5b0a34fc05e9465f0f222eae8421bb7f85f 100644 (file)
@@ -42,7 +42,7 @@ if(ENABLE_AUDIO)
                find_package(Vorbis)
                if (VORBIS_FOUND)
                        set(USE_AUDIO 1)
-                       set(audio_SRCS sound.cpp sound_openal.cpp)
+                       set(audio_SRCS sound_openal.cpp)
                        set(AUDIO_INCLUDE_DIRS
                                ${OPENAL_INCLUDE_DIR}
                                ${VORBIS_INCLUDE_DIR}
@@ -125,6 +125,7 @@ configure_file(
 )
 
 set(common_SRCS
+       sound.cpp
        quicktune.cpp
        subgame.cpp
        inventorymanager.cpp
@@ -234,7 +235,7 @@ include_directories(
        ${CMAKE_BUILD_TYPE}
        ${PNG_INCLUDE_DIR}
        ${GETTEXT_INCLUDE_DIR}
-       ${AUDIO_INLCUDE_DIR}
+       ${AUDIO_INLCUDE_DIRS}
        ${JTHREAD_INCLUDE_DIR}
        ${SQLITE3_INCLUDE_DIR}
        ${LUA_INCLUDE_DIR}
index 2a15386d9e150df07a919c5e44b745e57a88d7ed..8dad8dc63e59d7c7ec99414c7237cb073d9a6de0 100644 (file)
@@ -30,8 +30,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "settings.h"
 #include "itemdef.h" // For wield visualization
 #include "noise.h" // easeCurve
+#include "gamedef.h"
+#include "sound.h"
 
-Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control):
+Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
+               IGameDef *gamedef):
        m_smgr(smgr),
        m_playernode(NULL),
        m_headnode(NULL),
@@ -42,6 +45,7 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control):
        m_wieldlight(0),
 
        m_draw_control(draw_control),
+       m_gamedef(gamedef),
 
        m_camera_position(0,0,0),
        m_camera_direction(0,0,0),
@@ -168,7 +172,13 @@ void Camera::step(f32 dtime)
                }
                else
                {
+                       float was = m_view_bobbing_anim;
                        m_view_bobbing_anim = my_modf(m_view_bobbing_anim + offset);
+                       bool step = (was == 0 ||
+                                       (was < 0.5f && m_view_bobbing_anim >= 0.5f) ||
+                                       (was > 0.5f && m_view_bobbing_anim <= 0.5f));
+                       if(step)
+                               m_gamedef->sound()->playSound("default_grass_walk", false, 1.0);
                }
        }
 
@@ -180,6 +190,7 @@ void Camera::step(f32 dtime)
                {
                        m_digging_anim = 0;
                        m_digging_button = -1;
+                       m_gamedef->sound()->playSound("dig", false, 1.0);
                }
        }
 }
@@ -481,9 +492,9 @@ void Camera::setDigging(s32 button)
                m_digging_button = button;
 }
 
-void Camera::wield(const ItemStack &item, IGameDef *gamedef)
+void Camera::wield(const ItemStack &item)
 {
-       IItemDefManager *idef = gamedef->idef();
+       IItemDefManager *idef = m_gamedef->idef();
        scene::IMesh *wield_mesh = item.getDefinition(idef).wield_mesh;
        if(wield_mesh)
        {
index 168863c3ec618af56693576c339c5af6de858d93..0180d99e272ea1256d8483e0b49ad36c23bfdc80 100644 (file)
@@ -39,7 +39,8 @@ class IGameDef;
 class Camera
 {
 public:
-       Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control);
+       Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
+                       IGameDef *gamedef);
        ~Camera();
 
        // Get player scene node.
@@ -116,7 +117,7 @@ public:
        void setDigging(s32 button);
 
        // Replace the wielded item mesh
-       void wield(const ItemStack &item, IGameDef *gamedef);
+       void wield(const ItemStack &item);
 
        // Draw the wielded tool.
        // This has to happen *after* the main scene is drawn.
@@ -136,6 +137,8 @@ private:
 
        // draw control
        MapDrawControl& m_draw_control;
+       
+       IGameDef *m_gamedef;
 
        // Absolute camera position
        v3f m_camera_position;
index 220fd04dda92186acc75186e3db6067f1408e1d9..cb6d2075ec017796fc621514947752cf25f5758a 100644 (file)
@@ -225,11 +225,13 @@ Client::Client(
                MapDrawControl &control,
                IWritableTextureSource *tsrc,
                IWritableItemDefManager *itemdef,
-               IWritableNodeDefManager *nodedef
+               IWritableNodeDefManager *nodedef,
+               ISoundManager *sound
 ):
        m_tsrc(tsrc),
        m_itemdef(itemdef),
        m_nodedef(nodedef),
+       m_sound(sound),
        m_mesh_update_thread(this),
        m_env(
                new ClientMap(this, this, control,
@@ -2326,6 +2328,6 @@ u16 Client::allocateUnknownNodeId(const std::string &name)
 }
 ISoundManager* Client::getSoundManager()
 {
-       return &dummySoundManager;
+       return m_sound;
 }
 
index 6063631dac7c9054fc6c82e717105a0a1bf6b8b4..2a1681e92e9225ab100bd58281fe5cc3293d4877 100644 (file)
@@ -174,7 +174,8 @@ public:
                        MapDrawControl &control,
                        IWritableTextureSource *tsrc,
                        IWritableItemDefManager *itemdef,
-                       IWritableNodeDefManager *nodedef
+                       IWritableNodeDefManager *nodedef,
+                       ISoundManager *sound
        );
        
        ~Client();
@@ -333,6 +334,7 @@ private:
        IWritableTextureSource *m_tsrc;
        IWritableItemDefManager *m_itemdef;
        IWritableNodeDefManager *m_nodedef;
+       ISoundManager *m_sound;
        MeshUpdateThread m_mesh_update_thread;
        ClientEnvironment m_env;
        con::Connection m_con;
index 35a0e2533498abc3c1479ca53637b58fc01cf004..1b24df59bd8b3ccf8f087d43acc95e0d8df9c537 100644 (file)
@@ -56,6 +56,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "clientmap.h"
 #include "sky.h"
 #include "sound.h"
+#if USE_AUDIO
+       #include "sound_openal.h"
+#endif
 #include <list>
 
 /*
@@ -823,6 +826,27 @@ void the_game(
        IWritableItemDefManager *itemdef = createItemDefManager();
        // Create node definition manager
        IWritableNodeDefManager *nodedef = createNodeDefManager();
+       
+       // Sound manager
+       ISoundManager *sound = NULL;
+       bool sound_is_dummy = false;
+#if USE_AUDIO
+       infostream<<"Attempting to use OpenAL audio"<<std::endl;
+       sound = createOpenALSoundManager(NULL);
+       if(!sound)
+               infostream<<"Failed to initialize OpenAL audio"<<std::endl;
+#endif
+       if(!sound){
+               infostream<<"Using dummy audio."<<std::endl;
+               sound = &dummySoundManager;
+               sound_is_dummy = true;
+       }
+       
+       // Test sounds
+       sound->loadSound("default_grass_walk", porting::path_share + DIR_DELIM
+                       + "sounds" + DIR_DELIM + "default_grass_walk3_mono.ogg");
+       //sound->playSound("default_grass_walk", false, 1.0);
+       //sound->playSoundAt("default_grass_walk", true, 1.0, v3f(0,10,0)*BS);
 
        // Add chat log output for errors to be shown in chat
        LogOutputBuffer chat_log_error_buf(LMT_ERROR);
@@ -855,7 +879,7 @@ void the_game(
        MapDrawControl draw_control;
 
        Client client(device, playername.c_str(), password, draw_control,
-                       tsrc, itemdef, nodedef);
+                       tsrc, itemdef, nodedef, sound);
        
        // Client acts as our GameDef
        IGameDef *gamedef = &client;
@@ -1020,7 +1044,7 @@ void the_game(
        /*
                Create the camera node
        */
-       Camera camera(smgr, draw_control);
+       Camera camera(smgr, draw_control, gamedef);
        if (!camera.successfullyCreated(error_message))
                return;
 
@@ -1923,9 +1947,12 @@ void the_game(
                        client.getEnv().getClientMap().updateCamera(camera_position,
                                camera_direction, camera_fov);
                }
-
-               //timer2.stop();
-               //TimeTaker //timer3("//timer3");
+               
+               // Update sound listener
+               sound->updateListener(camera.getCameraNode()->getPosition(),
+                               v3f(0,0,0), // velocity
+                               camera.getCameraNode()->getTarget(),
+                               camera.getCameraNode()->getUpVector());
 
                /*
                        Calculate what block is the crosshair pointing to
@@ -2536,7 +2563,7 @@ void the_game(
                        ItemStack item;
                        if(mlist != NULL)
                                item = mlist->getItem(client.getPlayerItem());
-                       camera.wield(item, gamedef);
+                       camera.wield(item);
                }
                
                /*
@@ -2729,10 +2756,12 @@ void the_game(
 
        // Client scope (client is destructed before destructing *def and tsrc)
        }while(0);
-
-       delete tsrc;
+       
+       if(!sound_is_dummy)
+               delete sound;
        delete nodedef;
        delete itemdef;
+       delete tsrc;
 }
 
 
index 6b20bbd321ebcce199c8d806648bb600cf60595e..72764345e641ae2eb8c1fc268b303320075d6971 100644 (file)
@@ -28,8 +28,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 class OnDemandSoundFetcher
 {
 public:
-       virtual void getSoundFilenames(const std::string &name,
-                       std::set<std::string> &dst);
+       virtual void fetchSounds(const std::string &name,
+                       std::set<std::string> &dst_paths,
+                       std::set<std::vector<char> > &dst_datas) = 0;
 };
 
 class ISoundManager
@@ -48,10 +49,10 @@ public:
        virtual void updateListener(v3f pos, v3f vel, v3f at, v3f up) = 0;
        // playSound functions return -1 on failure, otherwise a handle to the
        // sound
-       virtual int playSound(const std::string &name, int loopcount,
+       virtual int playSound(const std::string &name, bool loop,
                        float volume) = 0;
-       virtual int playSoundAt(const std::string &name, int loopcount,
-                       v3f pos, float volume) = 0;
+       virtual int playSoundAt(const std::string &name, bool loop,
+                       float volume, v3f pos) = 0;
        virtual void stopSound(int sound) = 0;
 };
 
@@ -63,10 +64,10 @@ public:
        virtual bool loadSound(const std::string &name,
                        const std::vector<char> &filedata) {return true;}
        void updateListener(v3f pos, v3f vel, v3f at, v3f up) {}
-       int playSound(const std::string &name, int loopcount,
+       int playSound(const std::string &name, bool loop,
                        float volume) {return 0;}
-       int playSoundAt(const std::string &name, int loopcount,
-                       v3f pos, float volume) {return 0;}
+       int playSoundAt(const std::string &name, bool loop,
+                       float volume, v3f pos) {return 0;}
        void stopSound(int sound) {}
 };
 
index 9566f95c2c5b2167cc5bed71974ce772a51fd8bb..4f056888b0dd56035843cdbbafabf89e1a82d500 100644 (file)
@@ -84,6 +84,14 @@ static const char *alErrorString(ALenum err)
        }
 }
 
+static ALenum warn_if_error(ALenum err, const char *desc)
+{
+       if(err == AL_NO_ERROR)
+               return err;
+       errorstream<<"WARNING: "<<desc<<": "<<alErrorString(err)<<std::endl;
+       return err;
+}
+
 void f3_set(ALfloat *f3, v3f v)
 {
        f3[0] = v.X;
@@ -93,9 +101,9 @@ void f3_set(ALfloat *f3, v3f v)
 
 struct SoundBuffer
 {
-       ALenum  format;
-       ALsizei freq;
-       ALuint  bufferID;
+       ALenum format;
+       ALsizei freq;
+       ALuint buffer_id;
        std::vector<char> buffer;
 };
 
@@ -146,8 +154,8 @@ SoundBuffer* loadOggFile(const std::string &filepath)
                snd->buffer.insert(snd->buffer.end(), array, array + bytes);
        } while (bytes > 0);
 
-       alGenBuffers(1, &snd->bufferID);
-       alBufferData(snd->bufferID, snd->format,
+       alGenBuffers(1, &snd->buffer_id);
+       alBufferData(snd->buffer_id, snd->format,
                        &(snd->buffer[0]), snd->buffer.size(),
                        snd->freq);
 
@@ -168,19 +176,24 @@ SoundBuffer* loadOggFile(const std::string &filepath)
 
 struct PlayingSound
 {
+       ALuint source_id;
+       bool loop;
 };
 
 class OpenALSoundManager: public ISoundManager
 {
 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;
+       v3f m_listener_pos;
 public:
-       OpenALSoundManager():
+       OpenALSoundManager(OnDemandSoundFetcher *fetcher):
+               m_fetcher(fetcher),
                m_device(NULL),
                m_context(NULL),
                m_can_vorbis(false),
@@ -258,6 +271,7 @@ public:
                }
                std::vector<SoundBuffer*> bufs;
                bufs.push_back(buf);
+               m_buffers[name] = bufs;
                return;
        }
 
@@ -272,18 +286,6 @@ public:
                return bufs[j];
        }
 
-       void updateListener(v3f pos, v3f vel, v3f at, v3f up)
-       {
-               ALfloat f[6];
-               f3_set(f, pos);
-               alListenerfv(AL_POSITION, f);
-               f3_set(f, vel);
-               alListenerfv(AL_VELOCITY, f);
-               f3_set(f, at);
-               f3_set(f+3, up);
-               alListenerfv(AL_ORIENTATION, f);
-       }
-       
        bool loadSound(const std::string &name,
                        const std::string &filepath)
        {
@@ -300,23 +302,186 @@ public:
                return false;
        }
 
-       int playSound(const std::string &name, int loopcount,
+       PlayingSound* createPlayingSound(SoundBuffer *buf, bool loop,
                        float volume)
        {
-               return -1;
+               infostream<<"OpenALSoundManager: Creating playing sound"<<std::endl;
+               assert(buf);
+               PlayingSound *sound = new PlayingSound;
+               assert(sound);
+               warn_if_error(alGetError(), "before createPlayingSound");
+               alGenSources(1, &sound->source_id);
+               alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
+               alSourcei(sound->source_id, AL_SOURCE_RELATIVE, true);
+               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);
+               alSourcef(sound->source_id, AL_GAIN, volume);
+               alSourcePlay(sound->source_id);
+               warn_if_error(alGetError(), "createPlayingSound");
+               return sound;
+       }
+
+       PlayingSound* createPlayingSoundAt(SoundBuffer *buf, bool loop,
+                       float volume, v3f pos)
+       {
+               infostream<<"OpenALSoundManager: Creating positional playing sound"
+                               <<std::endl;
+               assert(buf);
+               PlayingSound *sound = new PlayingSound;
+               assert(sound);
+               warn_if_error(alGetError(), "before createPlayingSoundAt");
+               alGenSources(1, &sound->source_id);
+               alSourcei(sound->source_id, AL_BUFFER, buf->buffer_id);
+               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);
+               alSourcePlay(sound->source_id);
+               warn_if_error(alGetError(), "createPlayingSoundAt");
+               return sound;
+       }
+
+       int playSoundRaw(SoundBuffer *buf, bool loop, float volume)
+       {
+               assert(buf);
+               PlayingSound *sound = createPlayingSound(buf, loop, volume);
+               if(!sound)
+                       return -1;
+               int id = m_next_id++;
+               m_sounds_playing[id] = sound;
+               return id;
+       }
+
+       int playSoundRawAt(SoundBuffer *buf, bool loop, float volume, v3f pos)
+       {
+               assert(buf);
+               PlayingSound *sound = createPlayingSoundAt(buf, loop, volume, pos);
+               if(!sound)
+                       return -1;
+               int id = m_next_id++;
+               m_sounds_playing[id] = sound;
+               return id;
        }
-       int playSoundAt(const std::string &name, int loopcount,
-                       v3f pos, float volume)
+       
+       void deleteSound(int id)
+       {
+               std::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;
+               m_sounds_playing.erase(id);
+       }
+
+       /* If buffer does not exist, consult the fetcher */
+       SoundBuffer* getFetchBuffer(const std::string name)
        {
-               return -1;
+               SoundBuffer *buf = getBuffer(name);
+               if(buf)
+                       return buf;
+               if(!m_fetcher)
+                       return NULL;
+               std::set<std::string> paths;
+               std::set<std::vector<char> > datas;
+               m_fetcher->fetchSounds(name, paths, datas);
+               for(std::set<std::string>::iterator i = paths.begin();
+                               i != paths.end(); i++){
+                       loadSound(name, *i);
+               }
+               for(std::set<std::vector<char> >::iterator i = datas.begin();
+                               i != datas.end(); i++){
+                       loadSound(name, *i);
+               }
+               return getBuffer(name);
+       }
+       
+       // Remove stopped sounds
+       void maintain()
+       {
+               verbosestream<<"OpenALSoundManager::maintain(): "
+                               <<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++)
+               {
+                       int id = i->first;
+                       PlayingSound *sound = i->second;
+                       // If not playing, remove it
+                       {
+                               ALint state;
+                               alGetSourcei(sound->source_id, AL_SOURCE_STATE, &state);
+                               if(state != AL_PLAYING){
+                                       del_list.insert(id);
+                               }
+                       }
+               }
+               if(del_list.size() != 0)
+                       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);
+               }
+       }
+
+       /* Interface */
+
+       void updateListener(v3f pos, v3f vel, v3f at, v3f up)
+       {
+               m_listener_pos = pos;
+               alListener3f(AL_POSITION, pos.X, pos.Y, pos.Z);
+               alListener3f(AL_VELOCITY, vel.X, vel.Y, vel.Z);
+               ALfloat f[6];
+               f3_set(f, at);
+               f3_set(f+3, up);
+               alListenerfv(AL_ORIENTATION, f);
+               warn_if_error(alGetError(), "updateListener");
+       }
+
+       int playSound(const std::string &name, bool loop, float volume)
+       {
+               maintain();
+               SoundBuffer *buf = getFetchBuffer(name);
+               if(!buf){
+                       infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
+                                       <<std::endl;
+                       return -1;
+               }
+               return playSoundRaw(buf, loop, volume);
+       }
+       int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
+       {
+               maintain();
+               SoundBuffer *buf = getFetchBuffer(name);
+               if(!buf){
+                       infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
+                                       <<std::endl;
+                       return -1;
+               }
+               return playSoundRawAt(buf, loop, volume, pos);
        }
        void stopSound(int sound)
        {
+               maintain();
+               deleteSound(sound);
        }
 };
 
-ISoundManager *createSoundManager()
+ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher)
 {
-       return new OpenALSoundManager();
+       return new OpenALSoundManager(fetcher);
 };
 
index de1ca3056b698bf583534260d476e6fac20a928f..ce5702a478f25d506af2be05e9d8232a3f73de0e 100644 (file)
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "sound.h"
 
-ISoundManager *createOpenALSoundManager();
+ISoundManager *createOpenALSoundManager(OnDemandSoundFetcher *fetcher);
 
 #endif