Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/sound/WorldSound.cc @ 6381

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

Added new XML parameter for the Scene: soundReferenceDistance.
This value represents the distance to a sound source where the gain is still 1.0. Further away it will decrease with 1/(distance - soundReferenceDistance).
Default value is 20.

  • Property svn:eol-style set to native
File size: 4.5 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 *      Reto Grieder
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "WorldSound.h"
31
32#include <AL/alut.h>
33#include "util/Math.h"
34#include "core/CoreIncludes.h"
35#include "core/EventIncludes.h"
36#include "core/XMLPort.h"
37#include "Scene.h"
38#include "SoundManager.h"
39#include <core/ConsoleCommandCompilation.h>
40
41namespace orxonox
42{
43    CreateFactory(WorldSound);
44
45    WorldSound::WorldSound(BaseObject* creator)
46        : StaticEntity(creator)
47    {
48        RegisterObject(WorldSound);
49        // WorldSound buffers should be pooled when they're not used anymore
50        this->bPooling_ = true;
51        this->registerVariables();
52    }
53
54    WorldSound::~WorldSound()
55    {
56    }
57   
58    void WorldSound::registerVariables()
59    {
60        registerVariable(volume_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::volumeChanged));
61        registerVariable(source_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::sourceChanged));
62        registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::loopingChanged));
63        registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::stateChanged));
64        registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<WorldSound>(this, &WorldSound::pitchChanged));
65    }
66
67    void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
68    {
69        SUPER(WorldSound, XMLPort, xmlelement, mode);
70        BaseSound::XMLPortExtern(xmlelement, mode);
71    }
72
73    void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(WorldSound, XMLEventPort, xmlelement, mode);
76        XMLPortEventState(WorldSound, BaseObject, "play", play, xmlelement, mode);
77    }
78
79    void WorldSound::initialiseSource()
80    {
81        BaseSound::initialiseSource();
82        if (this->getScene())
83        {
84            float refDist = this->getScene()->getSoundReferenceDistance();
85            alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, refDist);
86            // TODO: 500 is very magical here. Derive something better
87            alSourcei(this->audioSource_, AL_MAX_DISTANCE, refDist * 500);
88        }
89        this->tick(0); // update position, orientation and velocity
90    }
91
92    void WorldSound::tick(float dt)
93    {
94        if (alIsSource(this->audioSource_))
95        {
96            const Vector3& pos = this->getWorldPosition();
97            alSource3f(this->audioSource_, AL_POSITION, pos.x, pos.y, pos.z);
98            ALenum error = alGetError();
99            if (error == AL_INVALID_VALUE)
100                COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
101
102            const Vector3& vel = this->getVelocity();
103            alSource3f(this->audioSource_, AL_VELOCITY, vel.x, vel.y, vel.z);
104            error = alGetError();
105            if (error == AL_INVALID_VALUE)
106                COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
107
108            const Vector3& direction = -this->getWorldOrientation().zAxis();
109            alSource3f(this->audioSource_, AL_DIRECTION, direction.x, direction.y, direction.z);
110            error = alGetError();
111            if (error == AL_INVALID_VALUE)
112                COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
113        }
114    }
115
116    void WorldSound::changedActivity() 
117    {
118        SUPER(WorldSound, changedActivity);
119        if (this->isActive())
120            this->play();
121        else 
122            this->stop();
123    }
124
125    float WorldSound::getRealVolume()
126    {
127        return SoundManager::getInstance().getRealVolume(SoundType::Effects);
128    }
129}
Note: See TracBrowser for help on using the repository browser.