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
RevLine 
[3060]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:
[5896]23 *      Erwin 'vaiursch' Herrsche
24 *      Reto Grieder
[3060]25 *   Co-authors:
26 *      ...
27 *
28 */
[3196]29
[5896]30#include "WorldSound.h"
[3196]31
[3060]32#include <AL/alut.h>
[3196]33#include "util/Math.h"
[5914]34#include "core/CoreIncludes.h"
[5896]35#include "core/EventIncludes.h"
36#include "core/XMLPort.h"
[6372]37#include "Scene.h"
[6186]38#include "SoundManager.h"
[6307]39#include <core/ConsoleCommandCompilation.h>
[3060]40
[5738]41namespace orxonox
[3060]42{
[5896]43    CreateFactory(WorldSound);
44
45    WorldSound::WorldSound(BaseObject* creator)
46        : StaticEntity(creator)
[3060]47    {
[5896]48        RegisterObject(WorldSound);
[6254]49        // WorldSound buffers should be pooled when they're not used anymore
50        this->bPooling_ = true;
[6307]51        this->registerVariables();
[5896]52    }
[3060]53
[5896]54    WorldSound::~WorldSound()
55    {
[3060]56    }
[6307]57   
58    void WorldSound::registerVariables()
59    {
[6370]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));
[6307]65    }
[3060]66
[5896]67    void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[3060]68    {
[5896]69        SUPER(WorldSound, XMLPort, xmlelement, mode);
[6117]70        BaseSound::XMLPortExtern(xmlelement, mode);
[3060]71    }
72
[5896]73    void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
[3060]74    {
[5896]75        SUPER(WorldSound, XMLEventPort, xmlelement, mode);
76        XMLPortEventState(WorldSound, BaseObject, "play", play, xmlelement, mode);
[3060]77    }
78
[6322]79    void WorldSound::initialiseSource()
80    {
81        BaseSound::initialiseSource();
[6372]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        }
[6322]89        this->tick(0); // update position, orientation and velocity
90    }
91
[5896]92    void WorldSound::tick(float dt)
93    {
[5899]94        if (alIsSource(this->audioSource_))
[5896]95        {
96            const Vector3& pos = this->getWorldPosition();
[5899]97            alSource3f(this->audioSource_, AL_POSITION, pos.x, pos.y, pos.z);
[3060]98            ALenum error = alGetError();
[5896]99            if (error == AL_INVALID_VALUE)
[3060]100                COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
101
[5896]102            const Vector3& vel = this->getVelocity();
[5899]103            alSource3f(this->audioSource_, AL_VELOCITY, vel.x, vel.y, vel.z);
[3060]104            error = alGetError();
[5896]105            if (error == AL_INVALID_VALUE)
[3060]106                COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
107
[6269]108            const Vector3& direction = -this->getWorldOrientation().zAxis();
109            alSource3f(this->audioSource_, AL_DIRECTION, direction.x, direction.y, direction.z);
[3060]110            error = alGetError();
[5896]111            if (error == AL_INVALID_VALUE)
[3060]112                COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
113        }
114    }
115
[6117]116    void WorldSound::changedActivity() 
117    {
118        SUPER(WorldSound, changedActivity);
119        if (this->isActive())
120            this->play();
121        else 
122            this->stop();
123    }
[6204]124
[6370]125    float WorldSound::getRealVolume()
[6186]126    {
[6370]127        return SoundManager::getInstance().getRealVolume(SoundType::Effects);
[6186]128    }
[5896]129}
Note: See TracBrowser for help on using the repository browser.