Changeset 5929 for code/trunk/src/orxonox/sound
- Timestamp:
- Oct 12, 2009, 8:20:07 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 4 deleted
- 4 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core5 (added) merged: 5768-5769,5772,5775-5780,5783-5785,5791-5792,5795-5807,5809-5814,5816-5832,5836-5839,5842-5853,5855-5899,5904-5922,5924-5928
- Property svn:mergeinfo changed
-
code/trunk/src/orxonox/sound/CMakeLists.txt
r5781 r5929 1 1 ADD_SOURCE_FILES(ORXONOX_SRC_FILES 2 AmbientSound.cc 3 BaseSound.cc 2 4 SoundManager.cc 3 SoundBase.cc 4 SoundMainMenu.cc 5 WorldSound.cc 5 6 ) 6 7 -
code/trunk/src/orxonox/sound/SoundManager.cc
r5781 r5929 31 31 #include <AL/alut.h> 32 32 33 #include "util/Exception.h" 33 34 #include "util/Math.h" 34 #include " CameraManager.h"35 #include " graphics/Camera.h"36 #include " SoundBase.h"35 #include "util/ScopeGuard.h" 36 #include "core/GameMode.h" 37 #include "core/ScopedSingletonManager.h" 37 38 38 39 namespace orxonox 39 40 { 40 41 SoundManager* SoundManager::singletonPtr_s = NULL; 42 ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); 41 43 42 /**43 * Default constructor44 */45 44 SoundManager::SoundManager() 46 45 { 47 this->device_ = NULL; 48 this->soundavailable_ = true; 49 if(!alutInitWithoutContext(NULL,NULL)) 46 if (!alutInitWithoutContext(NULL,NULL)) 47 ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError())); 48 Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit); 49 50 COUT(3) << "OpenAL: Opening sound device..." << std::endl; 51 this->device_ = alcOpenDevice(NULL); 52 if (this->device_ == NULL) 50 53 { 51 COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; 52 this->soundavailable_ = false; 54 COUT(0) << "OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl; 55 #ifdef ORXONOX_PLATFORM_WINDOWS 56 COUT(0) << "Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl; 57 #endif 58 ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device."); 53 59 } 60 Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_); 61 62 COUT(3) << "OpenAL: Sound device opened" << std::endl; 63 this->context_ = alcCreateContext(this->device_, NULL); 64 if (this->context_ == NULL) 65 ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context"); 66 Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_); 67 68 if (alcMakeContextCurrent(this->context_) == AL_TRUE) 69 COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl; 70 71 COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; 72 73 const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); 74 if (str == NULL) 75 COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl; 54 76 else 55 { 56 assert(this->device_ == NULL); 57 COUT(3) << "Sound: OpenAL: Open sound device..." << std::endl; 58 this->device_ = alcOpenDevice(NULL); 77 COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl; 59 78 60 if(this->device_ == NULL) 61 { 62 COUT(2) << "Sound: OpenAL: Could not open sound device" << std::endl; 63 this->soundavailable_ = false; 64 } 65 else 66 { 67 COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl; 68 this->context_ = alcCreateContext(this->device_, NULL); 69 if(this->context_ == NULL) 70 { 71 COUT(2) << "Sound: OpenAL: Could not create sound context" << std::endl; 72 this->soundavailable_ = false; 73 } 74 else 75 { 76 if(alcMakeContextCurrent(this->context_) == AL_TRUE) 77 COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl; 78 79 COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl; 80 const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER); 81 if (str == NULL) 82 COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl; 83 else 84 COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl; 85 } 86 } 87 } 79 GameMode::setPlaysSound(true); 80 // Disarm guards 81 alutExitGuard.Dismiss(); 82 closeDeviceGuard.Dismiss(); 83 desroyContextGuard.Dismiss(); 88 84 } 89 85 90 86 SoundManager::~SoundManager() 91 87 { 88 GameMode::setPlaysSound(false); 92 89 alcDestroyContext(this->context_); 93 90 alcCloseDevice(this->device_); … … 95 92 } 96 93 97 /** 98 * Add a SoundBase object to the list. Every SoundBase object should be in 99 * this list. 100 * 101 * @param sound Pointer to the SoundBase object to add 102 */ 103 void SoundManager::addSound(SoundBase* sound) 94 void SoundManager::setListenerPosition(const Vector3& position) 104 95 { 105 this->soundlist_.push_back(sound); 96 alListener3f(AL_POSITION, position.x, position.y, position.z); 97 ALenum error = alGetError(); 98 if (error == AL_INVALID_VALUE) 99 COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; 106 100 } 107 101 108 /** 109 * Remove a SoundBase object from the list and destroy it. 110 */ 111 void SoundManager::removeSound(SoundBase* sound) 102 void SoundManager::setListenerOrientation(const Quaternion& orientation) 112 103 { 113 std::list<SoundBase*>::iterator pos = this->soundlist_.end(); 114 for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++) 115 { 116 if((*i) == sound) 117 pos = i; 118 } 104 // update listener orientation 105 Vector3 up = orientation.xAxis(); // just a wild guess 106 Vector3 at = orientation.zAxis(); 119 107 120 delete (*pos); 121 this->soundlist_.erase(pos); 122 } 108 ALfloat orient[6] = { at.x, at.y, at.z, 109 up.x, up.y, up.z }; 123 110 124 /** 125 * Tick function, updates listener and registred SoundBase objects 126 * 127 * @param dt @see Orxonox::Tickable 128 */ 129 void SoundManager::tick(float dt) 130 { 131 if (!CameraManager::getInstancePtr()) 132 return; 133 134 // update listener position 135 Camera* camera = CameraManager::getInstance().getActiveCamera(); 136 if(camera == NULL) return; 137 Vector3 pos = camera->getPosition(); 138 alListener3f(AL_POSITION, pos.x, pos.y, pos.z); 111 alListenerfv(AL_POSITION, orient); 139 112 ALenum error = alGetError(); 140 if(error == AL_INVALID_VALUE) 141 COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl; 142 143 // update listener orientation 144 const Quaternion& orient = camera->getOrientation(); 145 Vector3 up = orient.xAxis(); // just a wild guess 146 Vector3 at = orient.zAxis(); 147 148 ALfloat orientation[6] = { at.x, at.y, at.z, 149 up.x, up.y, up.z }; 150 151 alListenerfv(AL_POSITION, orientation); 152 error = alGetError(); 153 if(error == AL_INVALID_VALUE) 113 if (error == AL_INVALID_VALUE) 154 114 COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; 155 156 // update sounds157 for(std::list<SoundBase*>::iterator i = this->soundlist_.begin(); i != this->soundlist_.end(); i++)158 (*i)->update();159 }160 161 /**162 * Check if sound is available163 */164 bool SoundManager::isSoundAvailable()165 {166 return this->soundavailable_;167 115 } 168 116 } -
code/trunk/src/orxonox/sound/SoundManager.h
r5781 r5929 39 39 /** 40 40 * The SoundManager class manages the OpenAL device, context and listener 41 * position. It has a list of all SoundBase objects and calls their update 42 * function every tick. It is a singleton. 41 * position. It is a singleton. 43 42 * 44 43 */ 45 class _OrxonoxExport SoundManager : public Singleton<SoundManager> , public Tickable44 class _OrxonoxExport SoundManager : public Singleton<SoundManager> 46 45 { 47 46 friend class Singleton<SoundManager>; … … 49 48 SoundManager(); 50 49 ~SoundManager(); 51 void addSound(SoundBase* sound); 52 void removeSound(SoundBase* sound); 53 void tick(float dt); 54 bool isSoundAvailable(); 50 51 void setListenerPosition(const Vector3& position); 52 void setListenerOrientation(const Quaternion& orientation); 55 53 56 54 private: 57 55 ALCdevice* device_; 58 56 ALCcontext* context_; 59 std::list<SoundBase*> soundlist_;60 bool soundavailable_;61 57 62 58 static SoundManager* singletonPtr_s; 63 }; // class SoundManager64 } // namespace orxonox59 }; 60 } 65 61 66 62 #endif /* _SoundManager_H__ */
Note: See TracChangeset
for help on using the changeset viewer.