Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/sound/SoundManager.cc @ 5896

Last change on this file since 5896 was 5896, checked in by rgrieder, 15 years ago

Added some basic sound classes: WorldSound (to be used for 3D sound, is a WorldEntity) and AmbientSound (BaseObject, just add it somewhere and it will play with playOnLoad=true).
Also moved the sound listener device updating to the HumanController. We might want to modify this again so that the listener is at the camera position again, not at the space ship.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
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
39namespace 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            ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device.");
54        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
55
56        COUT(3) << "OpenAL: Sound device opened" << std::endl;
57        this->context_ = alcCreateContext(this->device_, NULL);
58        if (this->context_ == NULL)
59            ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context");
60        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
61
62        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
63            COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl;
64
65        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
66
67        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
68        if (str == NULL)
69            COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
70        else
71            COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl;
72
73        GameMode::setPlaysSound(true);
74        // Disarm guards
75        alutExitGuard.Dismiss();
76        closeDeviceGuard.Dismiss();
77        desroyContextGuard.Dismiss();
78    }
79
80    SoundManager::~SoundManager()
81    {
82        GameMode::setPlaysSound(false);
83        alcDestroyContext(this->context_);
84        alcCloseDevice(this->device_);
85        alutExit();
86    }
87
88    void SoundManager::setListenerPosition(const Vector3& position)
89    {
90        alListener3f(AL_POSITION, position.x, position.y, position.z);
91        ALenum error = alGetError();
92        if (error == AL_INVALID_VALUE)
93            COUT(2) << "Sound: OpenAL: Invalid listener position" << std::endl;
94    }
95
96    void SoundManager::setListenerOrientation(const Quaternion& orientation)
97    {
98        // update listener orientation
99        Vector3 up = orientation.xAxis(); // just a wild guess
100        Vector3 at = orientation.zAxis();
101
102        ALfloat orient[6] = { at.x, at.y, at.z,
103                              up.x, up.y, up.z };
104
105        alListenerfv(AL_POSITION, orient);
106        ALenum error = alGetError();
107        if (error == AL_INVALID_VALUE)
108            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
109    }
110}
Note: See TracBrowser for help on using the repository browser.