| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Erwin 'vaiursch' Herrsche |
|---|
| 24 | * Kevin Young |
|---|
| 25 | * Reto Grieder |
|---|
| 26 | * Co-authors: |
|---|
| 27 | * ... |
|---|
| 28 | */ |
|---|
| 29 | |
|---|
| 30 | #ifndef _SoundManager_H__ |
|---|
| 31 | #define _SoundManager_H__ |
|---|
| 32 | |
|---|
| 33 | #include "OrxonoxPrereqs.h" |
|---|
| 34 | |
|---|
| 35 | #include <list> |
|---|
| 36 | #include <map> |
|---|
| 37 | #include <string> |
|---|
| 38 | #include <boost/shared_ptr.hpp> |
|---|
| 39 | |
|---|
| 40 | #include "util/Singleton.h" |
|---|
| 41 | #include "core/config/Configurable.h" |
|---|
| 42 | #include "core/object/StrongPtr.h" |
|---|
| 43 | #include "core/UpdateListener.h" |
|---|
| 44 | |
|---|
| 45 | // tolua_begin |
|---|
| 46 | namespace orxonox |
|---|
| 47 | { |
|---|
| 48 | //! Enum for the sound type. |
|---|
| 49 | namespace SoundType |
|---|
| 50 | { |
|---|
| 51 | enum Value |
|---|
| 52 | { |
|---|
| 53 | All = 0, |
|---|
| 54 | Music = 1, |
|---|
| 55 | Effects = 2 |
|---|
| 56 | }; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | //! The SoundManager class manages the OpenAL device, context and listener position. |
|---|
| 60 | class _OrxonoxExport SoundManager |
|---|
| 61 | // tolua_end |
|---|
| 62 | : public Singleton<SoundManager>, public Configurable, public UpdateListener |
|---|
| 63 | { // tolua_export |
|---|
| 64 | friend class Singleton<SoundManager>; |
|---|
| 65 | |
|---|
| 66 | public: |
|---|
| 67 | SoundManager(); |
|---|
| 68 | ~SoundManager(); |
|---|
| 69 | |
|---|
| 70 | void preUpdate(const Clock& time); |
|---|
| 71 | void postUpdate(const Clock& time) { /*no action*/ } |
|---|
| 72 | void setConfigValues(); |
|---|
| 73 | |
|---|
| 74 | // tolua_begin |
|---|
| 75 | static SoundManager& getInstance() { return Singleton<SoundManager>::getInstance(); } |
|---|
| 76 | static bool exists() { return Singleton<SoundManager>::exists(); } |
|---|
| 77 | |
|---|
| 78 | std::string getDeviceName(unsigned int index) const |
|---|
| 79 | { return index < this->deviceNames_.size() ? this->deviceNames_[index] : std::string(); } |
|---|
| 80 | // tolua_end |
|---|
| 81 | |
|---|
| 82 | void setListenerPosition(const Vector3& position); |
|---|
| 83 | void setListenerOrientation(const Quaternion& orientation); |
|---|
| 84 | |
|---|
| 85 | void registerAmbientSound(AmbientSound* newAmbient); |
|---|
| 86 | void unregisterAmbientSound(AmbientSound* oldAmbient); |
|---|
| 87 | void pauseAmbientSound(AmbientSound* ambient); |
|---|
| 88 | |
|---|
| 89 | // tolua_begin |
|---|
| 90 | void setVolume(float vol, SoundType::Value type); |
|---|
| 91 | float getVolume(SoundType::Value type); |
|---|
| 92 | float getRealVolume(SoundType::Value type); |
|---|
| 93 | |
|---|
| 94 | void toggleMute(SoundType::Value type); |
|---|
| 95 | bool getMute(SoundType::Value type); |
|---|
| 96 | // tolua_end |
|---|
| 97 | |
|---|
| 98 | shared_ptr<SoundBuffer> getSoundBuffer(const std::string& filename); |
|---|
| 99 | void releaseSoundBuffer(const shared_ptr<SoundBuffer>& buffer, bool bPoolBuffer); |
|---|
| 100 | |
|---|
| 101 | ALuint getSoundSource(BaseSound* object); |
|---|
| 102 | void releaseSoundSource(ALuint source); |
|---|
| 103 | |
|---|
| 104 | static std::string getALErrorString(ALenum error); |
|---|
| 105 | |
|---|
| 106 | private: |
|---|
| 107 | void processCrossFading(float dt); |
|---|
| 108 | void fadeIn(AmbientSound* sound); |
|---|
| 109 | void fadeOut(AmbientSound* sound); |
|---|
| 110 | |
|---|
| 111 | void checkFadeStepValidity(); |
|---|
| 112 | |
|---|
| 113 | void checkVolumeValidity(SoundType::Value type); |
|---|
| 114 | void checkSoundVolumeValidity() { this->checkVolumeValidity(SoundType::All); } |
|---|
| 115 | void checkAmbientVolumeValidity() { this->checkVolumeValidity(SoundType::Music); } |
|---|
| 116 | void checkEffectsVolumeValidity() { this->checkVolumeValidity(SoundType::Effects); } |
|---|
| 117 | void updateVolume(SoundType::Value type); |
|---|
| 118 | |
|---|
| 119 | unsigned int createSoundSources(unsigned int n); |
|---|
| 120 | |
|---|
| 121 | // OpenAL device/context related |
|---|
| 122 | std::vector<std::string> deviceNames_; |
|---|
| 123 | ALCdevice* device_; |
|---|
| 124 | ALCcontext* context_; |
|---|
| 125 | |
|---|
| 126 | // Ambient sound related |
|---|
| 127 | typedef std::list<std::pair<AmbientSound*, bool> > AmbientList; |
|---|
| 128 | AmbientList ambientSounds_; |
|---|
| 129 | //! Absolute change per second (0.1 means 10% of the nominal volume) for cross fading |
|---|
| 130 | float crossFadeStep_; |
|---|
| 131 | std::list<StrongPtr<AmbientSound> > fadeInList_; |
|---|
| 132 | std::list<StrongPtr<AmbientSound> > fadeOutList_; |
|---|
| 133 | |
|---|
| 134 | // Volume related |
|---|
| 135 | float volume_[3]; |
|---|
| 136 | float mute_[3]; |
|---|
| 137 | |
|---|
| 138 | // Sound buffer related |
|---|
| 139 | static const unsigned int maxEffectsPoolSize_s = 40 * 1024 * 1024; |
|---|
| 140 | unsigned int effectsPoolSize_; |
|---|
| 141 | typedef std::list<shared_ptr<SoundBuffer> > EffectsPoolList; |
|---|
| 142 | EffectsPoolList effectsPool_; |
|---|
| 143 | typedef std::map<std::string, shared_ptr<SoundBuffer> > SoundBufferMap; |
|---|
| 144 | SoundBufferMap soundBuffers_; |
|---|
| 145 | |
|---|
| 146 | // Sound source related |
|---|
| 147 | unsigned int minSources_; |
|---|
| 148 | unsigned int maxSources_; |
|---|
| 149 | std::vector<ALuint> availableSoundSources_; |
|---|
| 150 | std::vector<std::pair<ALuint, BaseSound*> > usedSoundSources_; |
|---|
| 151 | |
|---|
| 152 | bool bDestructorCalled_; ///< Becomes true if the destructor is called - used to prevent ambient sounds from registering after the lists were cleared |
|---|
| 153 | |
|---|
| 154 | static SoundManager* singletonPtr_s; |
|---|
| 155 | }; // tolua_export |
|---|
| 156 | } // tolua_export |
|---|
| 157 | |
|---|
| 158 | #endif /* _SoundManager_H__ */ |
|---|