| 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 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "SoundManager.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include <AL/alut.h> | 
|---|
| 32 |  | 
|---|
| 33 | #include "util/Exception.h" | 
|---|
| 34 | #include "util/Math.h" | 
|---|
| 35 | #include "util/ScopeGuard.h" | 
|---|
| 36 | #include "core/GameMode.h" | 
|---|
| 37 | #include "core/ScopedSingletonManager.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 |     SoundManager* SoundManager::singletonPtr_s = NULL; | 
|---|
| 42 |     ManageScopedSingleton(SoundManager, ScopeID::Graphics, true); | 
|---|
| 43 |  | 
|---|
| 44 |     SoundManager::SoundManager() | 
|---|
| 45 |     { | 
|---|
| 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) | 
|---|
| 53 |         { | 
|---|
| 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."); | 
|---|
| 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; | 
|---|
| 76 |         else | 
|---|
| 77 |             COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl; | 
|---|
| 78 |  | 
|---|
| 79 |         GameMode::setPlaysSound(true); | 
|---|
| 80 |         // Disarm guards | 
|---|
| 81 |         alutExitGuard.Dismiss(); | 
|---|
| 82 |         closeDeviceGuard.Dismiss(); | 
|---|
| 83 |         desroyContextGuard.Dismiss(); | 
|---|
| 84 |     } | 
|---|
| 85 |  | 
|---|
| 86 |     SoundManager::~SoundManager() | 
|---|
| 87 |     { | 
|---|
| 88 |         GameMode::setPlaysSound(false); | 
|---|
| 89 |         alcDestroyContext(this->context_); | 
|---|
| 90 |         alcCloseDevice(this->device_); | 
|---|
| 91 |         alutExit(); | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     void SoundManager::setListenerPosition(const Vector3& position) | 
|---|
| 95 |     { | 
|---|
| 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; | 
|---|
| 100 |     } | 
|---|
| 101 |  | 
|---|
| 102 |     void SoundManager::setListenerOrientation(const Quaternion& orientation) | 
|---|
| 103 |     { | 
|---|
| 104 |         // update listener orientation | 
|---|
| 105 |         Vector3 up = orientation.xAxis(); // just a wild guess | 
|---|
| 106 |         Vector3 at = orientation.zAxis(); | 
|---|
| 107 |  | 
|---|
| 108 |         ALfloat orient[6] = { at.x, at.y, at.z, | 
|---|
| 109 |                               up.x, up.y, up.z }; | 
|---|
| 110 |  | 
|---|
| 111 |         alListenerfv(AL_POSITION, orient); | 
|---|
| 112 |         ALenum error = alGetError(); | 
|---|
| 113 |         if (error == AL_INVALID_VALUE) | 
|---|
| 114 |             COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl; | 
|---|
| 115 |     } | 
|---|
| 116 | } | 
|---|