[Orxonox-commit 1353] r6071 - in code/branches/sound3/src/orxonox: gamestates sound
rgrieder at orxonox.net
rgrieder at orxonox.net
Sun Nov 15 22:36:45 CET 2009
Author: rgrieder
Date: 2009-11-15 22:36:45 +0100 (Sun, 15 Nov 2009)
New Revision: 6071
Modified:
code/branches/sound3/src/orxonox/gamestates/GSMainMenu.cc
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
Log:
Improved synchronisability of the sound classes (not yet Synchronisable though!).
Modified: code/branches/sound3/src/orxonox/gamestates/GSMainMenu.cc
===================================================================
--- code/branches/sound3/src/orxonox/gamestates/GSMainMenu.cc 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/gamestates/GSMainMenu.cc 2009-11-15 21:36:45 UTC (rev 6071)
@@ -101,8 +101,8 @@
if (GameMode::playsSound())
{
- this->ambient_->setLoop(true);
- this->ambient_->setPlayOnLoad(true);
+ this->ambient_->setLooping(true);
+ this->ambient_->play(); // works without source
}
this->setConfigValues();
@@ -135,7 +135,7 @@
void GSMainMenu::reloadSound() {
if (GameMode::playsSound())
{
- this->ambient_->setSource(soundPathMain_);
+ this->ambient_->setAmbientSource(soundPathMain_);
}
}
Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.cc 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.cc 2009-11-15 21:36:45 UTC (rev 6071)
@@ -31,8 +31,10 @@
#include "core/CoreIncludes.h"
#include "core/EventIncludes.h"
#include "core/GameMode.h"
+#include "core/Resource.h"
#include "core/XMLPort.h"
#include "SoundManager.h"
+#include "MoodManager.h"
namespace orxonox
{
@@ -55,9 +57,9 @@
{
SUPER(AmbientSound, XMLPort, 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);
+ XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLooping, getLooping, xmlelement, mode);
+ XMLPortParamExtern(AmbientSound, BaseSound, this, "play", play, isPlaying, xmlelement, mode);
+ XMLPortParam(AmbientSound, "source", setAmbientSource, getAmbientSource, xmlelement, mode);
}
void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
@@ -106,17 +108,17 @@
BaseSound::pause();
}
- void AmbientSound::setSource(const std::string& source)
+ void AmbientSound::setAmbientSource(const std::string& source)
{
+ this->ambientSource_ = source;
if (GameMode::playsSound())
{
- std::string filePath = SoundManager::getInstance().getAmbientPath(source);
- if (!filePath.empty())
- {
- BaseSound::setSource(filePath);
- return;
- }
- COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;
+ std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
+ shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
+ if (fileInfo != NULL)
+ this->setSource(path);
+ else
+ COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;
}
}
Modified: code/branches/sound3/src/orxonox/sound/AmbientSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/AmbientSound.h 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/AmbientSound.h 2009-11-15 21:36:45 UTC (rev 6071)
@@ -58,12 +58,15 @@
virtual void stop();
virtual void pause();
- virtual void setSource(const std::string& source);
+ virtual void setAmbientSource(const std::string& source);
+ const std::string& getAmbientSource() const { return this->ambientSource_; }
private:
- void doPlay(); // Continue playing without re-registering the sound
+ void doPlay();
void doStop();
void doPause();
+
+ std::string ambientSource_; //!< Analogous to source_, but mood independent
};
}
Modified: code/branches/sound3/src/orxonox/sound/BaseSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.cc 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.cc 2009-11-15 21:36:45 UTC (rev 6071)
@@ -28,6 +28,7 @@
#include "BaseSound.h"
+#include <cassert>
#include <vector>
#include <AL/alut.h>
#include <vorbis/vorbisfile.h>
@@ -41,72 +42,53 @@
BaseSound::BaseSound()
: audioSource_(0)
, audioBuffer_(0)
- , bPlayOnLoad_(false)
, bLoop_(false)
+ , state_(Stopped)
{
RegisterRootObject(BaseSound);
if (GameMode::playsSound())
+ {
alGenSources(1, &this->audioSource_);
+ assert(this->audioSource_ != 0);
+ }
}
BaseSound::~BaseSound()
{
this->setSource("");
- if (this->audioSource_)
+ if (GameMode::playsSound())
alDeleteSources(1, &this->audioSource_);
}
void BaseSound::play()
{
- if (alIsSource(this->audioSource_))
+ if (!this->isPlaying() && GameMode::showsGraphics())
{
- if (this->bLoop_)
- alSourcei(this->audioSource_, AL_LOOPING, AL_TRUE);
- else
- alSourcei(this->audioSource_, AL_LOOPING, AL_FALSE);
+ this->state_ = Playing;
alSourcePlay(this->audioSource_);
if (alGetError() != AL_NO_ERROR)
- {
COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
- }
}
}
void BaseSound::stop()
{
- if (alIsSource(this->audioSource_))
+ this->state_ = Stopped;
+ if (GameMode::playsSound())
alSourceStop(this->audioSource_);
}
void BaseSound::pause()
{
- if (alIsSource(this->audioSource_))
+ if (this->isStopped())
+ return;
+ this->state_ = Paused;
+ if (GameMode::playsSound())
alSourcePause(this->audioSource_);
}
- bool BaseSound::isPlaying()
- {
- if (alIsSource(this->audioSource_))
- return getSourceState() == AL_PLAYING;
- return false;
- }
-
- bool BaseSound::isPaused()
- {
- if (alIsSource(this->audioSource_))
- return getSourceState() == AL_PAUSED;
- return false;
- }
-
- bool BaseSound::isStopped()
- {
- if (alIsSource(this->audioSource_))
- return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
- return true;
- }
-
void BaseSound::setVolume(float vol)
{
if (vol > 1 || vol < 0)
@@ -120,6 +102,13 @@
alSourcef(this->audioSource_, AL_GAIN, vol);
}
+ void BaseSound::setLooping(bool val)
+ {
+ this->bLoop_ = val;
+ if (GameMode::playsSound())
+ alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
+ }
+
void BaseSound::setSource(const std::string& source)
{
if (!GameMode::playsSound() || source == this->source_)
@@ -127,10 +116,10 @@
this->source_ = source;
return;
}
-
- if (alIsSource(this->audioBuffer_))
+
+ if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
{
- this->stop();
+ alSourceStop(this->audioSource_);
// Unload old sound first
alSourcei(this->audioSource_, AL_BUFFER, 0);
alDeleteBuffers(1, &this->audioBuffer_);
@@ -182,20 +171,17 @@
}
alSource3f(this->audioSource_, AL_POSITION, 0, 0, 0);
+ alSourcef (this->audioSource_, AL_GAIN, this->volume_);
+ alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
+ if (this->isPlaying() || this->isPaused())
+ alSourcePlay(this->audioSource_);
+ if (this->isPaused())
+ alSourcePause(this->audioSource_);
- this->setVolume(this->volume_);
-
- if (this->bPlayOnLoad_)
- this->play();
+ if (alGetError() != AL_NO_ERROR)
+ COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
}
- ALint BaseSound::getSourceState()
- {
- ALint state;
- alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
- return state;
- }
-
size_t readVorbis(void* ptr, size_t size, size_t nmemb, void* datasource)
{
return static_cast<Ogre::DataStream*>(datasource)->read(ptr, size * nmemb);
Modified: code/branches/sound3/src/orxonox/sound/BaseSound.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/BaseSound.h 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/BaseSound.h 2009-11-15 21:36:45 UTC (rev 6071)
@@ -52,9 +52,9 @@
virtual void stop();
virtual void pause();
- bool isPlaying();
- bool isPaused();
- bool isStopped();
+ bool isPlaying() { return this->state_ = Playing; }
+ bool isPaused() { return this->state_ = Paused; }
+ bool isStopped() { return this->state_ = Stopped; }
virtual void setSource(const std::string& source);
virtual const std::string& getSource() const { return this->source_; }
@@ -62,26 +62,29 @@
void setVolume(float vol);
float getVolume() const { return this->volume_; }
- bool getPlayOnLoad() const { return this->bPlayOnLoad_; }
- void setPlayOnLoad(bool val) { this->bPlayOnLoad_ = val; }
+ bool getLooping() const { return this->bLoop_; }
+ void setLooping(bool val);
- bool getLoop() const { return this->bLoop_; }
- void setLoop(bool val) { this->bLoop_ = val; }
-
//ALuint getALAudioSource(void);
protected:
ALuint loadOggFile();
- ALint getSourceState();
ALuint audioSource_;
ALuint audioBuffer_;
private:
+ enum State
+ {
+ Stopped,
+ Playing,
+ Paused
+ };
+
std::string source_;
float volume_;
- bool bPlayOnLoad_;
bool bLoop_;
+ State state_;
DataStreamPtr dataStream_;
};
}
Modified: code/branches/sound3/src/orxonox/sound/SoundManager.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.cc 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.cc 2009-11-15 21:36:45 UTC (rev 6071)
@@ -39,10 +39,8 @@
#include "util/Clock.h"
#include "core/GameMode.h"
#include "core/ScopedSingletonManager.h"
-#include "core/Resource.h"
#include "core/ConfigValueIncludes.h"
#include "BaseSound.h"
-#include "MoodManager.h"
#include "AmbientSound.h"
namespace orxonox
@@ -222,18 +220,6 @@
}
}
- //! Get the current mood and return the full path string to the requested sound.
- std::string SoundManager::getAmbientPath(const std::string& source)
- {
- std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
- shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
- if (fileInfo == NULL)
- {
- return "";
- }
- return path;
- }
-
void SoundManager::fadeIn(AmbientSound* sound)
{
// If we're already fading out --> remove that
Modified: code/branches/sound3/src/orxonox/sound/SoundManager.h
===================================================================
--- code/branches/sound3/src/orxonox/sound/SoundManager.h 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/SoundManager.h 2009-11-15 21:36:45 UTC (rev 6071)
@@ -59,7 +59,6 @@
void registerAmbientSound(AmbientSound* newAmbient);
void unregisterAmbientSound(AmbientSound* oldAmbient);
void pauseAmbientSound(AmbientSound* ambient);
- std::string getAmbientPath(const std::string& source);
private:
void processCrossFading(float dt);
Modified: code/branches/sound3/src/orxonox/sound/WorldSound.cc
===================================================================
--- code/branches/sound3/src/orxonox/sound/WorldSound.cc 2009-11-15 14:53:12 UTC (rev 6070)
+++ code/branches/sound3/src/orxonox/sound/WorldSound.cc 2009-11-15 21:36:45 UTC (rev 6071)
@@ -53,8 +53,8 @@
{
SUPER(WorldSound, XMLPort, 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, "loop", setLooping, getLooping, xmlelement, mode);
+ XMLPortParamExtern(WorldSound, BaseSound, this, "play", play, isPlaying, xmlelement, mode);
XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
}
More information about the Orxonox-commit
mailing list