[Orxonox-commit 1351] r6069 - code/branches/sound3/src/orxonox/sound
rgrieder at orxonox.net
rgrieder at orxonox.net
Sun Nov 15 15:43:06 CET 2009
Author: rgrieder
Date: 2009-11-15 15:43:06 +0100 (Sun, 15 Nov 2009)
New Revision: 6069
Modified:
code/branches/sound3/src/orxonox/sound/AmbientSound.cc
code/branches/sound3/src/orxonox/sound/AmbientSound.h
code/branches/sound3/src/orxonox/sound/BaseSound.cc
code/branches/sound3/src/orxonox/sound/BaseSound.h
code/branches/sound3/src/orxonox/sound/SoundManager.cc
code/branches/sound3/src/orxonox/sound/SoundManager.h
code/branches/sound3/src/orxonox/sound/WorldSound.cc
code/branches/sound3/src/orxonox/sound/WorldSound.h
Log:
Detail changes in the sound classes.
Plus fixed the level sound problem (order of XML parameters was wrong).
There is still a large issue when changing an ambient sound source though.
Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.cc 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.cc 2009-11-15 14:43:06 UTC (rev 6069)
@@ -30,6 +30,7 @@
#include "core/CoreIncludes.h"
#include "core/EventIncludes.h"
+#include "core/GameMode.h"
#include "core/XMLPort.h"
#include "SoundManager.h"
@@ -41,6 +42,9 @@
: BaseObject(creator)
{
RegisterObject(AmbientSound);
+
+ // Ambient sounds always fade in
+ this->setVolume(0);
}
AmbientSound::~AmbientSound()
@@ -50,9 +54,10 @@
void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
{
SUPER(AmbientSound, XMLPort, xmlelement, mode);
- XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
+ XMLPortParamExtern(AmbientSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
+ XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
}
void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
@@ -63,22 +68,21 @@
void AmbientSound::play()
{
- COUT(3) << this->getSource() << ": Playing" << std::endl;
- if(GameMode::playsSound())
+ if (GameMode::playsSound())
{
+ COUT(3) << "Sound: " << this->getSource() << ": Playing" << std::endl;
SoundManager::getInstance().registerAmbientSound(this);
- this->BaseSound::play();
}
}
- void AmbientSound::replay()
+ void AmbientSound::doPlay()
{
- this->BaseSound::play();
+ BaseSound::play();
}
void AmbientSound::stop()
{
- if(GameMode::playsSound())
+ if (GameMode::playsSound())
{
SoundManager::getInstance().unregisterAmbientSound(this);
}
@@ -86,28 +90,41 @@
void AmbientSound::doStop()
{
- this->BaseSound::stop();
+ BaseSound::stop();
}
+ void AmbientSound::pause()
+ {
+ if (GameMode::playsSound())
+ {
+ SoundManager::getInstance().pauseAmbientSound(this);
+ }
+ }
+
+ void AmbientSound::doPause()
+ {
+ BaseSound::pause();
+ }
+
void AmbientSound::setSource(const std::string& source)
{
- if(source.find('/') == std::string.npos && GameMode::playsSound())
+ if (GameMode::playsSound())
{
std::string filePath = SoundManager::getInstance().getAmbientPath(source);
- if(!(filePath.empty()))
+ if (!filePath.empty())
{
- this->BaseSound::setSource(filePath);
+ BaseSound::setSource(filePath);
return;
}
+ COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;
}
- COUT(3) << source << ": Not a valid name! Ambient sound will not change." << std::endl;
}
void AmbientSound::changedActivity()
{
- COUT(3) << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
+ COUT(3) << "Sound: " << this->getSource() << ": ChangedActivity: " << this->isActive() << std::endl;
this->BaseObject::changedActivity();
- if(this->isActive())
+ if (this->isActive())
{
this->play();
}
Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.h 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.h 2009-11-15 14:43:06 UTC (rev 6069)
@@ -21,10 +21,12 @@
*
* Author:
* Reto Grieder
+ * Kevin Young
* Co-authors:
* ...
*
*/
+
#ifndef _AmbientSound_H__
#define _AmbientSound_H__
@@ -42,23 +44,26 @@
*/
class _OrxonoxExport AmbientSound : public BaseSound, public BaseObject
{
+ friend class SoundManager;
+
public:
AmbientSound(BaseObject* creator);
virtual ~AmbientSound();
+ virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
+ virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
+ virtual void changedActivity();
+
virtual void play();
- void replay(); // Continue playing without re-registering the sound
virtual void stop();
- void doStop();
+ virtual void pause();
virtual void setSource(const std::string& source);
- virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
- virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
- virtual void changedActivity();
-
-
private:
+ void doPlay(); // Continue playing without re-registering the sound
+ void doStop();
+ void doPause();
};
}
Modified: code/branches/sound3/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.cc 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.cc 2009-11-15 14:43:06 UTC (rev 6069)
@@ -92,7 +92,7 @@
{
if (alIsSource(this->audioSource_))
return getSourceState() == AL_PAUSED;
- return true;
+ return false;
}
bool BaseSound::isStopped()
@@ -102,13 +102,17 @@
return true;
}
- void BaseSound::setPlayOnLoad(bool val)
+ void BaseSound::setVolume(float vol)
{
- this->bPlayOnLoad_ = val;
- if(val)
+ if (vol > 1 || vol < 0)
{
- this->play();
+ COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
+ vol = vol > 1 ? 1 : vol;
+ vol = vol < 0 ? 0 : vol;
}
+ this->volume_ = vol;
+ if (alIsSource(this->audioSource_))
+ alSourcef(this->audioSource_, AL_GAIN, vol);
}
void BaseSound::setSource(const std::string& source)
@@ -177,15 +181,12 @@
alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0);
+ this->setVolume(this->volume_);
+
if (this->bPlayOnLoad_)
this->play();
}
- ALuint BaseSound::getALAudioSource()
- {
- return audioSource_;
- }
-
ALint BaseSound::getSourceState()
{
ALint state;
@@ -274,5 +275,4 @@
return buffer;
}
-
-} // namespace: orxonox
+}
Modified: code/branches/sound3/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.h 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.h 2009-11-15 14:43:06 UTC (rev 6069)
@@ -25,13 +25,13 @@
* Reto Grieder
*
*/
+
#ifndef _BaseSound_H__
#define _BaseSound_H__
#include "OrxonoxPrereqs.h"
#include <string>
-#include <OgreSharedPtr.h>
#include <OgreDataStream.h>
#include "core/OrxonoxClass.h"
@@ -50,22 +50,25 @@
virtual void play();
virtual void stop();
- void pause();
+ virtual void pause();
bool isPlaying();
bool isPaused();
bool isStopped();
virtual void setSource(const std::string& source);
- const std::string& getSource() { return this->source_; }
+ virtual const std::string& getSource() const { return this->source_; }
- bool getPlayOnLoad() { return this->bPlayOnLoad_; }
- void setPlayOnLoad(bool val);
+ void setVolume(float vol);
+ float getVolume() const { return this->volume_; }
- bool getLoop() { return this->bLoop_; }
+ bool getPlayOnLoad() const { return this->bPlayOnLoad_; }
+ void setPlayOnLoad(bool val) { this->bPlayOnLoad_ = val; }
+
+ bool getLoop() const { return this->bLoop_; }
void setLoop(bool val) { this->bLoop_ = val; }
- ALuint getALAudioSource(void);
+ //ALuint getALAudioSource(void);
protected:
ALuint loadOggFile();
@@ -75,10 +78,11 @@
ALuint audioBuffer_;
private:
- std::string source_;
- bool bPlayOnLoad_;
- bool bLoop_;
- DataStreamPtr dataStream_;
+ std::string source_;
+ float volume_;
+ bool bPlayOnLoad_;
+ bool bLoop_;
+ DataStreamPtr dataStream_;
};
}
Modified: code/branches/sound3/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.cc 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.cc 2009-11-15 14:43:06 UTC (rev 6069)
@@ -21,6 +21,7 @@
*
* Author:
* Erwin 'vaiursch' Herrsche
+ * Kevin Young
* Co-authors:
* ...
*
@@ -29,6 +30,7 @@
#include "SoundManager.h"
#include <AL/alut.h>
+#include <utility>
#include "util/Exception.h"
#include "util/Math.h"
@@ -52,7 +54,7 @@
{
RegisterRootObject(SoundManager);
- if (!alutInitWithoutContext(NULL,NULL))
+ if (!alutInitWithoutContext(NULL, NULL))
ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
@@ -102,19 +104,29 @@
alutExit();
}
- void SoundManager::update(const Clock &time)
+ void SoundManager::update(const Clock& time)
{
- this->fadeInAmbientSound(time.getDeltaTime());
- this->fadeOutAmbientSound(time.getDeltaTime());
+ this->processCrossFading(time.getDeltaTime());
}
void SoundManager::setConfigValues()
{
- SetConfigValue(fadeStep_, 0.2f)
+ SetConfigValue(crossFadeStep_, 0.2f)
.description("Determines how fast sounds should fade, per second.")
.callback(this, &SoundManager::checkFadeStepValidity);
}
+ void SoundManager::checkFadeStepValidity()
+ {
+ if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
+ {
+ COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
+ ResetConfigValue(crossFadeStep_);
+ }
+ COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
+ return;
+ }
+
void SoundManager::setListenerPosition(const Vector3& position)
{
alListener3f(AL_POSITION, position.x, position.y, position.z);
@@ -140,39 +152,53 @@
void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
{
- if(newAmbient != NULL)
+ if (newAmbient != NULL)
{
- if (!(this->ambientSounds_.empty()))
+ for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
{
- this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
+ if (it->first == newAmbient)
+ {
+ COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
+ return;
+ }
}
- this->fadeInList_.push_front(std::make_pair(newAmbient, 0.0));
- this->ambientSounds_.push_front(newAmbient);
+
+ if (!this->ambientSounds_.empty())
+ {
+ this->fadeOut(ambientSounds_.front().first);
+ }
+ this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
+ newAmbient->doPlay();
+ this->fadeIn(newAmbient);
}
}
- void SoundManager::unregisterAmbientSound(AmbientSound* currentAmbient)
+ void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
{
- if(currentAmbient == NULL || ambientSounds_.empty())
+ if (oldAmbient == NULL || ambientSounds_.empty())
{
return;
}
- if(this->ambientSounds_.front() == currentAmbient)
+ if (this->ambientSounds_.front().first == oldAmbient)
{
- this->fadeOutList_.push_front(std::make_pair(this->ambientSounds_.front(), 1.0));
+ this->fadeOut(oldAmbient);
this->ambientSounds_.pop_front();
- if(!(this->ambientSounds_.empty()))
+ if (!this->ambientSounds_.empty())
{
- this->fadeInList_.push_front(std::make_pair(this->ambientSounds_.front(), 0.0));
+ if (!this->ambientSounds_.front().second) // Not paused before
+ {
+ this->ambientSounds_.front().first->doPlay();
+ }
+ this->fadeIn(this->ambientSounds_.front().first);
}
}
else
{
- for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
+ for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
{
- if(*it == currentAmbient)
+ if (it->first == oldAmbient)
{
- currentAmbient->doStop();
+ this->fadeOut(oldAmbient);
this->ambientSounds_.erase(it);
break;
}
@@ -180,75 +206,110 @@
}
}
- // Get the current mood and return the full path string to the requested sound.
- const std::string& SoundManager::getAmbientPath(const std::string& source)
+ void SoundManager::pauseAmbientSound(AmbientSound* ambient)
{
- lastReqPath_ = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
- shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(lastReqPath_);
- if(fileInfo == NULL)
+ if (ambient != NULL)
{
- return BLANKSTRING;
+ for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
+ {
+ if (it->first == ambient)
+ {
+ it->second = true;
+ this->fadeOut(it->first);
+ return;
+ }
+ }
}
- return lastReqPath_;
}
- void SoundManager::fadeInAmbientSound(float dt)
+ //! Get the current mood and return the full path string to the requested sound.
+ std::string SoundManager::getAmbientPath(const std::string& source)
{
- if(!(this->fadeInList_.empty()))
+ std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
+ shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
+ if (fileInfo == NULL)
{
- for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
+ return "";
+ }
+ return path;
+ }
+
+ void SoundManager::fadeIn(AmbientSound* sound)
+ {
+ // If we're already fading out --> remove that
+ for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
+ {
+ if (*it == sound)
{
- it->second += fadeStep_ * dt;
- alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second);
+ this->fadeOutList_.erase(it);
+ break;
}
- if(this->fadeInList_.back().second >= 1)
+ }
+ // No duplicate entries
+ if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
+ this->fadeInList_.push_back(sound);
+ }
+
+ void SoundManager::fadeOut(AmbientSound* sound)
+ {
+ // If we're already fading in --> remove that
+ for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
+ {
+ if (*it == sound)
{
- this->fadeInList_.pop_back();
+ this->fadeInList_.erase(it);
+ break;
}
}
+ // No duplicate entries
+ if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
+ this->fadeOutList_.push_back(sound);
}
- void SoundManager::fadeOutAmbientSound(float dt)
+ void SoundManager::processCrossFading(float dt)
{
- if(!(this->fadeInList_.empty()))
+ // FADE IN
+ for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it)
{
- for(std::list<std::pair<AmbientSound*, float> >::iterator it= this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
+ if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
{
- it->second -= fadeStep_ * dt;
- alSourcef(it->first->getALAudioSource(), AL_GAIN, it->second);
+ (*it)->setVolume(1.0f);
+ this->fadeInList_.erase(it++);
}
- if(this->fadeOutList_.back().second <= 0)
+ else
{
- bool pauseTest = false;
-
- for(std::list<AmbientSound*>::iterator it= this->ambientSounds_.begin(); it != this->ambientSounds_.end(); it++)
+ (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
+ ++it;
+ }
+ }
+
+ // FADE OUT
+ for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it)
+ {
+ if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
+ {
+ (*it)->setVolume(0.0f);
+
+ // If sound is in the ambient list --> pause
+ for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
{
- if(*it == this->fadeOutList_.back().first)
+ if (it2->first == *it)
{
- pauseTest = true;
+ (*it)->doPause();
break;
}
}
- if(pauseTest)
- {
- this->fadeOutList_.back().first->pause();
- }
- else
- {
- this->fadeOutList_.back().first->doStop();
- }
- this->fadeOutList_.pop_back();
+ // If not pause (by loop above for instance) --> stop
+ if (!(*it)->isPaused())
+ (*it)->doStop();
+
+ this->fadeOutList_.erase(it++);
}
+ else
+ {
+ (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
+ ++it;
+ }
}
}
-
- void SoundManager::checkFadeStepValidity()
- {
- if(fadeStep_ <= 0.0 || fadeStep_ >= 1.0 )
- {
- ResetConfigValue(fadeStep_);
- }
- COUT(0) << "SoundManager: fade step now set to " << fadeStep_ << std::endl;
- return;
- }
}
Modified: code/branches/sound3/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.h 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.h 2009-11-15 14:43:06 UTC (rev 6069)
@@ -21,18 +21,19 @@
*
* Author:
* Erwin 'vaiursch' Herrsche
+ * Kevin Young
* Co-authors:
* ...
*/
+
#ifndef _SoundManager_H__
#define _SoundManager_H__
#include "OrxonoxPrereqs.h"
-#include <cassert>
#include <list>
+#include <string>
#include "util/Singleton.h"
-#include "tools/interfaces/Tickable.h"
namespace orxonox
{
@@ -44,35 +45,39 @@
class _OrxonoxExport SoundManager : public Singleton<SoundManager>, public OrxonoxClass
{
friend class Singleton<SoundManager>;
+
public:
SoundManager();
~SoundManager();
- void update(const Clock &time);
- void setConfigValues(void);
+ void update(const Clock& time);
+ void setConfigValues();
void setListenerPosition(const Vector3& position);
void setListenerOrientation(const Quaternion& orientation);
void registerAmbientSound(AmbientSound* newAmbient);
- void unregisterAmbientSound(AmbientSound* currentAmbient);
- const std::string& getAmbientPath(const std::string& source);
- void fadeInAmbientSound(float dt);
- void fadeOutAmbientSound(float dt);
- void checkFadeStepValidity(void);
+ void unregisterAmbientSound(AmbientSound* oldAmbient);
+ void pauseAmbientSound(AmbientSound* ambient);
+ std::string getAmbientPath(const std::string& source);
private:
+ void processCrossFading(float dt);
+ void fadeIn(AmbientSound* sound);
+ void fadeOut(AmbientSound* sound);
+
+ void checkFadeStepValidity();
+
ALCdevice* device_;
ALCcontext* context_;
- std::list<AmbientSound*> ambientSounds_;
+ typedef std::list<std::pair<AmbientSound*, bool> > AmbientList;
+ AmbientList ambientSounds_;
- float fadeStep_; //per second
- std::list<std::pair<AmbientSound*, float> > fadeInList_;
- std::list<std::pair<AmbientSound*, float> > fadeOutList_;
+ float crossFadeStep_; //!< Absolute change per second (0.1 means 10% of the nominal volume) for cross fading
+ std::list<AmbientSound*> fadeInList_;
+ std::list<AmbientSound*> fadeOutList_;
- std::string lastReqPath_;
-
static SoundManager* singletonPtr_s;
};
}
Modified: code/branches/sound3/src/orxonox/sound/WorldSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/WorldSound.cc 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/WorldSound.cc 2009-11-15 14:43:06 UTC (rev 6069)
@@ -52,9 +52,10 @@
void WorldSound::XMLPort(Element& xmlelement, XMLPort::Mode mode)
{
SUPER(WorldSound, XMLPort, xmlelement, mode);
- XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
+ XMLPortParamExtern(WorldSound, BaseSound, this, "volume", setVolume, getVolume, xmlelement, mode);
XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
+ XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
}
void WorldSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
@@ -87,5 +88,4 @@
COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
}
}
-
}
Modified: code/branches/sound3/src/orxonox/sound/WorldSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/WorldSound.h 2009-11-15 12:47:05 UTC (rev 6068)
+++ code/branches/sound3/src/orxonox/sound/WorldSound.h 2009-11-15 14:43:06 UTC (rev 6069)
@@ -25,6 +25,7 @@
* ...
*
*/
+
#ifndef _WorldSound_H__
#define _WorldSound_H__
More information about the Orxonox-commit
mailing list