Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6117


Ignore:
Timestamp:
Nov 22, 2009, 4:01:16 PM (14 years ago)
Author:
rgrieder
Message:

Merged sound3 branch to presentation2.

Location:
code/branches/presentation2
Files:
14 edited
3 copied

Legend:

Unmodified
Added
Removed
  • code/branches/presentation2

  • code/branches/presentation2/data/levels/templates/spaceship_assff.oxt

    r5781 r6117  
    7676      <Light mainstate=visibility position="-8, 0, 8" diffuse="0.3, 0.6, 1.0" specular="0.3, 0.6, 1.0" attenuation="600, 1.0, 0.007, 0.0002" type=point />
    7777-->
     78      <!--WorldSound mainstate=activity source="sounds/Engine_low.ogg" oop=1 /-->
    7879    </active>
    7980    <forward>
    8081      <Backlight mainstate=activity active=false scale=0.4 name=bltest position=" 7.6, 0, 6" colour="0.2, 0.65, 1.0, 1.0" width=15 length=1500 lifetime=2 elements=50 trailmaterial="Trail/backlighttrail" turnontime=1 turnofftime=1 material="Flares/ThrusterFlare1" />
    8182      <Backlight mainstate=activity active=false scale=0.4 name=bltest position="-7.6, 0, 6" colour="0.2, 0.65, 1.0, 1.0" width=15 length=1500 lifetime=2 elements=50 trailmaterial="Trail/backlighttrail" turnontime=1 turnofftime=1 material="Flares/ThrusterFlare1" />
     83      <WorldSound mainstate=activity source="sounds/Engine_low.ogg" oop=1 />
    8284    </forward>
    8385    <boost>
    8486      <Backlight mainstate=activity active=false scale=0.4 name=bltest position=" 7.6, 0, 6" colour="0.6, 0.75, 0.8, 0.7" width=40 length=1000 lifetime=1 elements=30 trailmaterial="Trail/backlighttrail" turnontime=1 turnofftime=1 material="Examples/Flare" />
    8587      <Backlight mainstate=activity active=false scale=0.4 name=bltest position="-7.6, 0, 6" colour="0.6, 0.75, 0.8, 0.7" width=40 length=1000 lifetime=1 elements=30 trailmaterial="Trail/backlighttrail" turnontime=1 turnofftime=1 material="Examples/Flare" />
     88      <!--WorldSound mainstate=activity source="sounds/Engine_high.ogg" oop=1 /-->
    8689    </boost>
    8790    <brake>
  • code/branches/presentation2/src/libraries/core/XMLPort.h

    r5929 r6117  
    184184        ClassIdentifier<classname>::getIdentifier()->addXMLPortParamContainer(paramname, containername); \
    185185    } \
    186     containername->port(static_cast<BaseObject*>(this), object, xmlelement, mode)
     186    containername->port(dynamic_cast<BaseObject*>(this), object, xmlelement, mode)
    187187
    188188// --------------------
  • code/branches/presentation2/src/orxonox/CMakeLists.txt

    r5929 r6117  
    2828  LevelManager.cc
    2929  Main.cc
     30  MoodManager.cc
    3031  PawnManager.cc
    3132  PlayerManager.cc
     
    5556  TOLUA_FILES
    5657    LevelManager.h
     58    MoodManager.h
    5759    pickup/BaseItem.h
    5860    pickup/PickupInventory.h
  • code/branches/presentation2/src/orxonox/gamestates/GSMainMenu.cc

    r6105 r6117  
    3636#include "core/Game.h"
    3737#include "core/ConsoleCommand.h"
     38#include "core/ConfigValueIncludes.h"
    3839#include "core/GraphicsManager.h"
    3940#include "core/GUIManager.h"
     
    4950        , inputState_(0)
    5051    {
     52        RegisterRootObject(GSMainMenu);
    5153        inputState_ = InputManager::getInstance().createInputState("mainMenu");
    5254        inputState_->setMouseMode(MouseMode::Nonexclusive);
     
    6466            // Load sound
    6567            this->ambient_ = new AmbientSound(0);
    66             this->ambient_->setSource("ambient/mainmenu.wav");
    6768        }
    6869    }
     
    9192        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&GSMainMenu::startDedicated), "startDedicated"));
    9293        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&GSMainMenu::startMainMenu), "startMainMenu"));
     94       
     95        // create command to change sound path
     96        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(createFunctor(&GSMainMenu::setMainMenuSoundPath, this), "setMMSoundPath"));
    9397
    9498        KeyBinderManager::getInstance().setToDefault();
     
    97101        if (GameMode::playsSound())
    98102        {
    99             this->ambient_->setLoop(true);
    100             this->ambient_->play();
     103            this->ambient_->setLooping(true);
     104            this->ambient_->play(); // works without source
    101105        }
     106
     107        this->setConfigValues();
    102108    }
    103109
     
    117123    void GSMainMenu::update(const Clock& time)
    118124    {
     125    }
     126
     127    void GSMainMenu::setConfigValues()
     128    {
     129        SetConfigValue(soundPathMain_, "mainmenu.ogg")
     130            .description("Contains the path to the main menu sound file.")
     131            .callback(this, &GSMainMenu::reloadSound);
     132    }
     133
     134    void GSMainMenu::reloadSound()
     135    {
     136        if (GameMode::playsSound())
     137        {
     138            this->ambient_->setAmbientSource(soundPathMain_);
     139        }
     140    }
     141
     142    const std::string& GSMainMenu::getMainMenuSoundPath()
     143    {
     144        return soundPathMain_;
     145    }
     146
     147    void GSMainMenu::setMainMenuSoundPath(const std::string& path)
     148    {
     149        ModifyConfigValue(soundPathMain_, set, path);
    119150    }
    120151
  • code/branches/presentation2/src/orxonox/gamestates/GSMainMenu.h

    r5929 r6117  
    3434#include "util/OgreForwardRefs.h"
    3535#include "core/GameState.h"
     36#include "core/OrxonoxClass.h"
    3637
    3738namespace orxonox
    3839{
    39     class _OrxonoxExport GSMainMenu : public GameState
     40    class _OrxonoxExport GSMainMenu : public GameState, public OrxonoxClass
    4041    {
    4142    public:
     
    4647        void deactivate();
    4748        void update(const Clock& time);
     49
     50        void setConfigValues();
     51        void reloadSound();
     52        const std::string& getMainMenuSoundPath();
     53        void setMainMenuSoundPath(const std::string& path);
    4854
    4955        static void startStandalone();
     
    6167        // ambient sound for the main menu
    6268        AmbientSound*     ambient_;
     69        std::string       soundPathMain_;
    6370    };
    6471}
  • code/branches/presentation2/src/orxonox/sound/AmbientSound.cc

    r5929 r6117  
    3131#include "core/CoreIncludes.h"
    3232#include "core/EventIncludes.h"
     33#include "core/GameMode.h"
     34#include "core/Resource.h"
    3335#include "core/XMLPort.h"
     36#include "SoundManager.h"
     37#include "MoodManager.h"
    3438
    3539namespace orxonox
     
    4145    {
    4246        RegisterObject(AmbientSound);
     47
     48        // Ambient sounds always fade in
     49        this->setVolume(0);
    4350    }
    4451
     
    5057    {
    5158        SUPER(AmbientSound, XMLPort, xmlelement, mode);
    52         XMLPortParamExtern(AmbientSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
    53         XMLPortParamExtern(AmbientSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    54         XMLPortParamExtern(AmbientSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
     59        BaseSound::XMLPortExtern(xmlelement, mode);
     60        XMLPortParam(AmbientSound, "ambientsource", setAmbientSource, getAmbientSource, xmlelement, mode);
    5561    }
    5662
     
    6066        XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode);
    6167    }
     68
     69    void AmbientSound::play()
     70    {
     71        if (GameMode::playsSound())
     72        {
     73            COUT(3) << "Sound: " << this->getSource() << ": Playing" << std::endl;
     74            SoundManager::getInstance().registerAmbientSound(this);
     75        }
     76    }
     77
     78    void AmbientSound::doPlay()
     79    {
     80        BaseSound::play();
     81    }
     82
     83    void AmbientSound::stop()
     84    {
     85        if (GameMode::playsSound())
     86        {
     87            SoundManager::getInstance().unregisterAmbientSound(this);
     88        }
     89    }
     90
     91    void AmbientSound::doStop()
     92    {
     93        BaseSound::stop();
     94    }
     95
     96    void AmbientSound::pause()
     97    {
     98        if (GameMode::playsSound())
     99        {
     100            SoundManager::getInstance().pauseAmbientSound(this);
     101        }
     102    }
     103
     104    void AmbientSound::doPause()
     105    {
     106        BaseSound::pause();
     107    }
     108
     109    void AmbientSound::setAmbientSource(const std::string& source)
     110    {
     111        this->ambientSource_ = source;
     112        if (GameMode::playsSound())
     113        {
     114            std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source;
     115            shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path);
     116            if (fileInfo != NULL)
     117                this->setSource(path);
     118            else
     119                COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl;       
     120        }
     121    }
     122
     123    void AmbientSound::changedActivity()
     124    {
     125        SUPER(AmbientSound, changedActivity);
     126        if (this->isActive())
     127            this->play();
     128        else
     129            this->stop();
     130    }
    62131}
  • code/branches/presentation2/src/orxonox/sound/AmbientSound.h

    r5929 r6117  
    2222 *   Author:
    2323 *      Reto Grieder
     24 *      Kevin Young
    2425 *   Co-authors:
    2526 *      ...
    2627 *
    2728 */
     29
    2830#ifndef _AmbientSound_H__
    2931#define _AmbientSound_H__
     
    4345    class _OrxonoxExport AmbientSound : public BaseSound, public BaseObject
    4446    {
     47        friend class SoundManager;
     48
    4549    public:
    4650        AmbientSound(BaseObject* creator);
     
    4953        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    5054        virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
     55        virtual void changedActivity();
     56
     57        virtual void play();
     58        virtual void stop();
     59        virtual void pause();
     60
     61        virtual void setAmbientSource(const std::string& source);
     62        const std::string& getAmbientSource() const { return this->ambientSource_; }
    5163
    5264    private:
     65        void doPlay();
     66        void doStop();
     67        void doPause();
     68
     69        std::string ambientSource_; //!< Analogous to source_, but mood independent
    5370    };
    5471}
  • code/branches/presentation2/src/orxonox/sound/BaseSound.cc

    r5929 r6117  
    2929#include "BaseSound.h"
    3030
     31#include <cassert>
    3132#include <vector>
    3233#include <AL/alut.h>
     
    3637#include "core/GameMode.h"
    3738#include "core/Resource.h"
     39#include "core/XMLPort.h"
    3840
    3941namespace orxonox
     
    4244        : audioSource_(0)
    4345        , audioBuffer_(0)
    44         , bPlayOnLoad_(false)
    4546        , bLoop_(false)
     47        , state_(Stopped)
    4648    {
    4749        RegisterRootObject(BaseSound);
     50
     51        if (GameMode::playsSound())
     52        {
     53            alGenSources(1, &this->audioSource_);
     54            assert(this->audioSource_ != 0);
     55        }
    4856    }
    4957
     
    5159    {
    5260        this->setSource("");
     61        if (GameMode::playsSound())
     62            alDeleteSources(1, &this->audioSource_);
     63    }
     64
     65    void BaseSound::XMLPortExtern(Element& xmlelement, XMLPort::Mode mode)
     66    {
     67        XMLPortParam(BaseSound, "volume", setVolume,  getVolume,  xmlelement, mode);
     68        XMLPortParam(BaseSound, "loop",   setLooping, getLooping, xmlelement, mode);
     69        XMLPortParam(BaseSound, "play",   play,       isPlaying,  xmlelement, mode);
     70        XMLPortParam(BaseSound, "source", setSource,  getSource,  xmlelement, mode);
    5371    }
    5472
    5573    void BaseSound::play()
    5674    {
     75        if (!this->isPlaying() && GameMode::showsGraphics())
     76        {
     77            this->state_ = Playing;
     78            alSourcePlay(this->audioSource_);
     79
     80            if (alGetError() != AL_NO_ERROR)
     81                 COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
     82        }
     83    }
     84
     85    void BaseSound::stop()
     86    {
     87        this->state_ = Stopped;
     88        if (GameMode::playsSound())
     89            alSourceStop(this->audioSource_);
     90    }
     91
     92    void BaseSound::pause()
     93    {
     94        if (this->isStopped())
     95            return;
     96        this->state_ = Paused;
     97        if (GameMode::playsSound())
     98            alSourcePause(this->audioSource_);
     99    }
     100
     101    void BaseSound::setVolume(float vol)
     102    {
     103        if (vol > 1 || vol < 0)
     104        {
     105            COUT(2) << "Sound warning: volume out of range, cropping value." << std::endl;
     106            vol = vol > 1 ? 1 : vol;
     107            vol = vol < 0 ? 0 : vol;
     108        }
     109        this->volume_ = vol;
    57110        if (alIsSource(this->audioSource_))
    58         {
    59             if (this->bLoop_)
    60                 alSourcei(this->audioSource_, AL_LOOPING, AL_TRUE);
    61             else
    62                 alSourcei(this->audioSource_, AL_LOOPING, AL_FALSE);
    63             alSourcePlay(this->audioSource_);
    64 
    65             if (alGetError() != AL_NO_ERROR)
    66             {
    67                  COUT(2) << "Sound: OpenAL: Error playin sound " << this->audioSource_ << std::endl;
    68             }
    69         }
    70     }
    71 
    72     void BaseSound::stop()
    73     {
    74         if (alIsSource(this->audioSource_))
     111            alSourcef(this->audioSource_, AL_GAIN, vol);
     112    }
     113
     114    void BaseSound::setLooping(bool val)
     115    {
     116        this->bLoop_ = val;
     117        if (GameMode::playsSound())
     118            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
     119    }
     120
     121    void BaseSound::setSource(const std::string& source)
     122    {
     123        if (!GameMode::playsSound() || source == this->source_)
     124        {
     125            this->source_ = source;
     126            return;
     127        }
     128
     129        if (this->audioBuffer_ != 0 && alIsBuffer(this->audioBuffer_))
     130        {
    75131            alSourceStop(this->audioSource_);
    76     }
    77 
    78     void BaseSound::pause()
    79     {
    80         if (alIsSource(this->audioSource_))
    81             alSourcePause(this->audioSource_);
    82     }
    83 
    84     bool BaseSound::isPlaying()
    85     {
    86         if (alIsSource(this->audioSource_))
    87             return getSourceState() == AL_PLAYING;
    88         return false;
    89     }
    90 
    91     bool BaseSound::isPaused()
    92     {
    93         if (alIsSource(this->audioSource_))
    94             return getSourceState() == AL_PAUSED;
    95         return true;
    96     }
    97 
    98     bool BaseSound::isStopped()
    99     {
    100         if (alIsSource(this->audioSource_))
    101             return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
    102         return true;
    103     }
    104 
    105     void BaseSound::setPlayOnLoad(bool val)
    106     {
    107         this->bPlayOnLoad_ = true;
    108         this->play();
    109     }
    110 
    111     void BaseSound::setSource(const std::string& source)
    112     {
     132            // Unload old sound first
     133            alSourcei(this->audioSource_, AL_BUFFER, 0);
     134            alDeleteBuffers(1, &this->audioBuffer_);
     135            this->audioBuffer_ = 0;
     136        }
     137
    113138        this->source_ = source;
    114         if (!GameMode::playsSound())
    115             return;
    116 
    117         if (source.empty() && alIsSource(this->audioSource_))
    118         {
    119             // Unload sound
    120             alSourcei(this->audioSource_, AL_BUFFER, 0);
    121             alDeleteSources(1, &this->audioSource_);
    122             alDeleteBuffers(1, &this->audioBuffer_);
    123             return;
    124         }
     139        if (source_.empty())
     140            return;
    125141
    126142        COUT(3) << "Sound: OpenAL ALUT: loading file " << source << std::endl;
     
    129145        if (fileInfo == NULL)
    130146        {
    131             COUT(2) << "Warning: Sound file '" << source << "' not found" << std::endl;
     147            COUT(2) << "Sound: Warning: Sound file '" << source << "' not found" << std::endl;
    132148            return;
    133149        }
     
    147163            {
    148164                COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
    149                 this->audioBuffer_ = loadOggFile();
     165                this->audioBuffer_ = this->loadOggFile();
    150166            }
    151167
     
    157173        }
    158174
    159         alGenSources(1, &this->audioSource_);
    160175        alSourcei(this->audioSource_, AL_BUFFER, this->audioBuffer_);
    161176        if (alGetError() != AL_NO_ERROR)
     
    166181
    167182        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    168 
    169         if (this->bPlayOnLoad_)
    170             this->play();
    171     }
    172 
    173     ALint BaseSound::getSourceState()
    174     {
    175         ALint state;
    176         alGetSourcei(this->audioSource_, AL_SOURCE_STATE, &state);
    177         return state;
     183        alSourcef (this->audioSource_, AL_GAIN, this->volume_);
     184        alSourcei (this->audioSource_, AL_LOOPING, (this->bLoop_ ? AL_TRUE : AL_FALSE));
     185        if (this->isPlaying() || this->isPaused())
     186            alSourcePlay(this->audioSource_);
     187        if (this->isPaused())
     188            alSourcePause(this->audioSource_);
     189
     190        if (alGetError() != AL_NO_ERROR)
     191            COUT(2) << "Sound: OpenAL: Error playing sound " << this->audioSource_ << std::endl;
    178192    }
    179193
     
    259273        return buffer;
    260274    }
    261 
    262 } // namespace: orxonox
     275}
  • code/branches/presentation2/src/orxonox/sound/BaseSound.h

    r5929 r6117  
    2626 *
    2727 */
     28
    2829#ifndef _BaseSound_H__
    2930#define _BaseSound_H__
     
    3233
    3334#include <string>
    34 #include <OgreSharedPtr.h>
    3535#include <OgreDataStream.h>
    3636#include "core/OrxonoxClass.h"
     
    4949        virtual ~BaseSound();
    5050
    51         void play();
    52         void stop();
    53         void pause();
     51        void XMLPortExtern(Element& xmlelement, XMLPort::Mode mode);
    5452
    55         bool isPlaying();
    56         bool isPaused();
    57         bool isStopped();
     53        virtual void play();
     54        virtual void stop();
     55        virtual void pause();
    5856
    59         void setSource(const std::string& source);
    60         const std::string& getSource() { return this->source_; }
     57        bool isPlaying() { return this->state_ == Playing; }
     58        bool isPaused()  { return this->state_ == Paused; }
     59        bool isStopped() { return this->state_ == Stopped; }
    6160
    62         bool getPlayOnLoad() { return this->bPlayOnLoad_; }
    63         void setPlayOnLoad(bool val);
     61        virtual void setSource(const std::string& source);
     62        virtual const std::string& getSource() const { return this->source_; }
    6463
    65         bool getLoop() { return this->bLoop_; }
    66         void setLoop(bool val) { this->bLoop_ = val; }
     64        void setVolume(float vol);
     65        float getVolume() const { return this->volume_; }
     66
     67        bool getLooping() const   { return this->bLoop_; }
     68        void setLooping(bool val);
     69
     70        //ALuint getALAudioSource(void);
    6771
    6872    protected:
    6973        ALuint loadOggFile();
    70         ALint getSourceState();
    7174
    7275        ALuint audioSource_;
     
    7477
    7578    private:
    76         std::string source_;
    77         bool bPlayOnLoad_;
    78         bool bLoop_;
    79         DataStreamPtr dataStream_;
     79        enum State
     80        {
     81            Stopped,
     82            Playing,
     83            Paused
     84        };
     85
     86        std::string     source_;
     87        float           volume_;
     88        bool            bLoop_;
     89        State           state_;
     90        DataStreamPtr   dataStream_;
    8091    };
    8192}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.cc

    r5929 r6117  
    2222 *   Author:
    2323 *       Erwin 'vaiursch' Herrsche
     24 *       Kevin Young
    2425 *   Co-authors:
    2526 *      ...
     
    3031
    3132#include <AL/alut.h>
     33#include <utility>
    3234
    3335#include "util/Exception.h"
    3436#include "util/Math.h"
    3537#include "util/ScopeGuard.h"
     38#include "util/StringUtils.h"
     39#include "util/Clock.h"
    3640#include "core/GameMode.h"
    3741#include "core/ScopedSingletonManager.h"
     42#include "core/ConfigValueIncludes.h"
     43#include "BaseSound.h"
     44#include "AmbientSound.h"
    3845
    3946namespace orxonox
     
    4451    SoundManager::SoundManager()
    4552    {
    46         if (!alutInitWithoutContext(NULL,NULL))
    47             ThrowException(InitialisationFailed, "OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
     53        RegisterRootObject(SoundManager);
     54
     55        if (!alutInitWithoutContext(NULL, NULL))
     56            ThrowException(InitialisationFailed, "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()));
    4857        Loki::ScopeGuard alutExitGuard = Loki::MakeGuard(&alutExit);
    4958
    50         COUT(3) << "OpenAL: Opening sound device..." << std::endl;
     59        COUT(3) << "Sound: OpenAL: Opening sound device..." << std::endl;
    5160        this->device_ = alcOpenDevice(NULL);
    5261        if (this->device_ == NULL)
    5362        {
    54             COUT(0) << "OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;
     63            COUT(0) << "Sound: OpenaAL: Could not open sound device. Have you installed OpenAL?" << std::endl;
    5564#ifdef ORXONOX_PLATFORM_WINDOWS
    56             COUT(0) << "Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
     65            COUT(0) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
    5766#endif
    58             ThrowException(InitialisationFailed, "OpenAL error: Could not open sound device.");
     67            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
    5968        }
    6069        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
    6170
    62         COUT(3) << "OpenAL: Sound device opened" << std::endl;
     71        COUT(3) << "Sound: OpenAL: Sound device opened" << std::endl;
    6372        this->context_ = alcCreateContext(this->device_, NULL);
    6473        if (this->context_ == NULL)
    65             ThrowException(InitialisationFailed, "OpenAL error: Could not create sound context");
     74            ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not create sound context");
    6675        Loki::ScopeGuard desroyContextGuard = Loki::MakeGuard(&alcDestroyContext, this->context_);
    6776
    6877        if (alcMakeContextCurrent(this->context_) == AL_TRUE)
    69             COUT(3) << "OpenAL: Context " << this->context_ << " loaded" << std::endl;
     78            COUT(3) << "Sound: OpenAL: Context " << this->context_ << " loaded" << std::endl;
    7079
    7180        COUT(4) << "Sound: OpenAL ALUT version: " << alutGetMajorVersion() << "." << alutGetMinorVersion() << std::endl;
     
    7382        const char* str = alutGetMIMETypes(ALUT_LOADER_BUFFER);
    7483        if (str == NULL)
    75             COUT(2) << "OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
     84            COUT(2) << "Sound: OpenAL ALUT error: " << alutGetErrorString(alutGetError()) << std::endl;
    7685        else
    77             COUT(4) << "OpenAL ALUT supported MIME types: " << str << std::endl;
     86            COUT(4) << "Sound: OpenAL ALUT supported MIME types: " << str << std::endl;
    7887
    7988        GameMode::setPlaysSound(true);
     
    8291        closeDeviceGuard.Dismiss();
    8392        desroyContextGuard.Dismiss();
     93
     94        this->setConfigValues();
    8495    }
    8596
     
    92103    }
    93104
     105    void SoundManager::update(const Clock& time)
     106    {
     107        this->processCrossFading(time.getDeltaTime());
     108    }
     109
     110    void SoundManager::setConfigValues()
     111    {
     112        SetConfigValue(crossFadeStep_, 0.2f)
     113            .description("Determines how fast sounds should fade, per second.")
     114            .callback(this, &SoundManager::checkFadeStepValidity);
     115    }
     116
     117    void SoundManager::checkFadeStepValidity()
     118    {
     119        if (crossFadeStep_ <= 0.0 || crossFadeStep_ >= 1.0 )
     120        {
     121            COUT(2) << "Sound warning: Sound step out of range, ignoring change." << std::endl;
     122            ResetConfigValue(crossFadeStep_);
     123        }
     124        COUT(3) << "SoundManager: fade step set to " << crossFadeStep_ << std::endl;
     125        return;
     126    }
     127
    94128    void SoundManager::setListenerPosition(const Vector3& position)
    95129    {
     
    114148            COUT(2) << "Sound: OpenAL: Invalid listener orientation" << std::endl;
    115149    }
     150
     151    void SoundManager::registerAmbientSound(AmbientSound* newAmbient)
     152    {
     153        if (newAmbient != NULL)
     154        {
     155            for (AmbientList::const_iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     156            {
     157                if (it->first == newAmbient)
     158                {
     159                    COUT(2) << "Sound warning: Will not play an AmbientSound twice." << std::endl;
     160                    return;
     161                }
     162            }
     163
     164            if (!this->ambientSounds_.empty())
     165            {
     166                this->fadeOut(ambientSounds_.front().first);
     167            }
     168            this->ambientSounds_.push_front(std::make_pair(newAmbient, false));
     169            newAmbient->doPlay();
     170            this->fadeIn(newAmbient);
     171        }
     172    }
     173
     174    void SoundManager::unregisterAmbientSound(AmbientSound* oldAmbient)
     175    {
     176        if (oldAmbient == NULL || ambientSounds_.empty())
     177        {
     178            return;
     179        }
     180        if (this->ambientSounds_.front().first == oldAmbient)
     181        {
     182            this->fadeOut(oldAmbient);
     183            this->ambientSounds_.pop_front();
     184            if (!this->ambientSounds_.empty())
     185            {
     186                if (!this->ambientSounds_.front().second) // Not paused before
     187                {
     188                    this->ambientSounds_.front().first->doPlay();
     189                }
     190                this->fadeIn(this->ambientSounds_.front().first);
     191            }
     192        }
     193        else
     194        {
     195            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     196            {
     197                if (it->first == oldAmbient)
     198                {
     199                    this->fadeOut(oldAmbient);
     200                    this->ambientSounds_.erase(it);
     201                    break;
     202                }
     203            }
     204        }
     205    }
     206
     207    void SoundManager::pauseAmbientSound(AmbientSound* ambient)
     208    {
     209        if (ambient != NULL)
     210        {
     211            for (AmbientList::iterator it = this->ambientSounds_.begin(); it != this->ambientSounds_.end(); ++it)
     212            {
     213                if (it->first == ambient)
     214                {
     215                    it->second = true;
     216                    this->fadeOut(it->first);
     217                    return;
     218                }
     219            }
     220        }
     221    }
     222
     223    void SoundManager::fadeIn(AmbientSound* sound)
     224    {
     225        // If we're already fading out --> remove that
     226        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it++)
     227        {
     228            if (*it == sound)
     229            {
     230                this->fadeOutList_.erase(it);
     231                break;
     232            }
     233        }
     234        // No duplicate entries
     235        if (std::find(this->fadeInList_.begin(), this->fadeInList_.end(), sound) == this->fadeInList_.end())
     236            this->fadeInList_.push_back(sound);
     237    }
     238
     239    void SoundManager::fadeOut(AmbientSound* sound)
     240    {
     241        // If we're already fading in --> remove that
     242        for (std::list<AmbientSound*>::iterator it = this->fadeInList_.begin(); it != this->fadeInList_.end(); it++)
     243        {
     244            if (*it == sound)
     245            {
     246                this->fadeInList_.erase(it);
     247                break;
     248            }
     249        }
     250        // No duplicate entries
     251        if (std::find(this->fadeOutList_.begin(), this->fadeOutList_.end(), sound) == this->fadeOutList_.end())
     252            this->fadeOutList_.push_back(sound);
     253    }
     254
     255    void SoundManager::processCrossFading(float dt)
     256    {
     257       
     258        // Hacky solution to the fade delay while loading a level.
     259        if(dt > 0.2)
     260        {
     261            return;
     262        }
     263       
     264        // FADE IN
     265        for (std::list<AmbientSound*>::iterator it= this->fadeInList_.begin(); it != this->fadeInList_.end(); it)
     266        {
     267            if ((*it)->getVolume() + this->crossFadeStep_*dt > 1.0f)
     268            {
     269                (*it)->setVolume(1.0f);
     270                this->fadeInList_.erase(it++);
     271            }
     272            else
     273            {
     274                (*it)->setVolume((*it)->getVolume() + this->crossFadeStep_*dt);
     275                ++it;
     276            }
     277        }
     278
     279        // FADE OUT
     280        for (std::list<AmbientSound*>::iterator it = this->fadeOutList_.begin(); it != this->fadeOutList_.end(); it)
     281        {
     282            if ((*it)->getVolume() - this->crossFadeStep_*dt < 0.0f)
     283            {
     284                (*it)->setVolume(0.0f);
     285
     286                // If sound is in the ambient list --> pause
     287                for (AmbientList::const_iterator it2 = this->ambientSounds_.begin(); it2 != this->ambientSounds_.end(); ++it2)
     288                {
     289                    if (it2->first == *it)
     290                    {
     291                        (*it)->doPause();
     292                        break;
     293                    }
     294                }
     295                // If not pause (by loop above for instance) --> stop
     296                if (!(*it)->isPaused())
     297                    (*it)->doStop();
     298
     299                this->fadeOutList_.erase(it++);
     300            }
     301            else
     302            {
     303                (*it)->setVolume((*it)->getVolume() - this->crossFadeStep_*dt);
     304                ++it;
     305            }
     306        }
     307    }
    116308}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.h

    r5929 r6117  
    2222 *   Author:
    2323 *       Erwin 'vaiursch' Herrsche
     24 *       Kevin Young
    2425 *   Co-authors:
    2526 *      ...
    2627 */
     28
    2729#ifndef _SoundManager_H__
    2830#define _SoundManager_H__
     
    3032#include "OrxonoxPrereqs.h"
    3133
    32 #include <cassert>
    3334#include <list>
     35#include <string>
    3436#include "util/Singleton.h"
    35 #include "tools/interfaces/Tickable.h"
    3637
    3738namespace orxonox
     
    4243     *
    4344     */
    44     class _OrxonoxExport SoundManager : public Singleton<SoundManager>
     45    class _OrxonoxExport SoundManager : public Singleton<SoundManager>, public OrxonoxClass
    4546    {
    4647        friend class Singleton<SoundManager>;
     48
    4749    public:
    4850        SoundManager();
    4951        ~SoundManager();
    5052
     53        void update(const Clock& time);
     54        void setConfigValues();
     55
    5156        void setListenerPosition(const Vector3& position);
    5257        void setListenerOrientation(const Quaternion& orientation);
    5358
     59        void registerAmbientSound(AmbientSound* newAmbient);
     60        void unregisterAmbientSound(AmbientSound* oldAmbient);
     61        void pauseAmbientSound(AmbientSound* ambient);
     62
    5463    private:
     64        void processCrossFading(float dt);
     65        void fadeIn(AmbientSound* sound);
     66        void fadeOut(AmbientSound* sound);
     67
     68        void checkFadeStepValidity();
     69
    5570        ALCdevice* device_;
    5671        ALCcontext* context_;
    57 
     72       
     73        typedef std::list<std::pair<AmbientSound*, bool> > AmbientList;
     74        AmbientList ambientSounds_;
     75       
     76        float crossFadeStep_;       //!< Absolute change per second (0.1 means 10% of the nominal volume) for cross fading
     77        std::list<AmbientSound*> fadeInList_;
     78        std::list<AmbientSound*> fadeOutList_;
     79       
    5880        static SoundManager* singletonPtr_s;
    5981    };
  • code/branches/presentation2/src/orxonox/sound/WorldSound.cc

    r5929 r6117  
    5353    {
    5454        SUPER(WorldSound, XMLPort, xmlelement, mode);
    55         XMLPortParamExtern(WorldSound, BaseSound, this, "source", setSource, getSource, xmlelement, mode);
    56         XMLPortParamExtern(WorldSound, BaseSound, this, "loop", setLoop, getLoop, xmlelement, mode);
    57         XMLPortParamExtern(WorldSound, BaseSound, this, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode);
     55        BaseSound::XMLPortExtern(xmlelement, mode);
    5856    }
    5957
     
    8987    }
    9088
     89    void WorldSound::changedActivity()
     90    {
     91        SUPER(WorldSound, changedActivity);
     92        if (this->isActive())
     93            this->play();
     94        else
     95            this->stop();
     96    }
    9197}
  • code/branches/presentation2/src/orxonox/sound/WorldSound.h

    r5929 r6117  
    2626 *
    2727 */
     28
    2829#ifndef _WorldSound_H__
    2930#define _WorldSound_H__
     
    4950        virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode);
    5051        virtual void XMLEventPort(Element& xmlelement, XMLPort::Mode mode);
     52        virtual void changedActivity();
    5153
    5254        virtual void tick(float dt);
Note: See TracChangeset for help on using the changeset viewer.