Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 6, 2009, 11:12:01 PM (15 years ago)
Author:
rgrieder
Message:

Added some basic sound classes: WorldSound (to be used for 3D sound, is a WorldEntity) and AmbientSound (BaseObject, just add it somewhere and it will play with playOnLoad=true).
Also moved the sound listener device updating to the HumanController. We might want to modify this again so that the listener is at the camera position again, not at the space ship.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • code/branches/core5/src/orxonox/sound/BaseSound.cc

    r5891 r5896  
    2121 *
    2222 *   Author:
    23  *       Erwin 'vaiursch' Herrsche
     23 *      Erwin 'vaiursch' Herrsche
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder
    2626 *
    2727 */
    2828
    29 #include "SoundBase.h"
    30 
    31 #include <string>
     29#include "BaseSound.h"
     30
    3231#include <vector>
    3332#include <AL/alut.h>
    3433#include <vorbis/vorbisfile.h>
    3534
    36 #include "util/Math.h"
     35#include "core/CoreIncludes.h"
     36#include "core/GameMode.h"
    3737#include "core/Resource.h"
    38 #include "worldentities/WorldEntity.h"
    39 #include "SoundManager.h"
    4038
    4139namespace orxonox
    4240{
    43     SoundBase::SoundBase(WorldEntity* entity)
    44     {
    45         this->source_ = 0;
    46         this->buffer_ = 0;
    47         this->entity_ = entity;
    48 
    49         SoundManager::getInstance().addSound(this);
    50     }
    51 
    52     SoundBase::~SoundBase()
    53     {
    54         alSourcei(this->source_, AL_BUFFER, 0);
    55         alDeleteSources(1, &this->source_);
    56         alDeleteBuffers(1, &this->buffer_);
    57     }
    58 
    59     void SoundBase::attachToEntity(WorldEntity* entity)
    60     {
    61         this->entity_ = entity;
    62         this->update();
    63     }
    64 
    65     void SoundBase::update() {
    66         if(this->entity_ != NULL && alIsSource(this->source_)) {
    67             const Vector3& pos = this->entity_->getPosition();
    68             alSource3f(this->source_, AL_POSITION, pos.x, pos.y, pos.z);
    69             ALenum error = alGetError();
    70             if(error == AL_INVALID_VALUE)
    71                 COUT(2) << "Sound: OpenAL: Invalid sound position" << std::endl;
    72 
    73             const Vector3& vel = this->entity_->getVelocity();
    74             alSource3f(this->source_, AL_VELOCITY, vel.x, vel.y, vel.z);
    75             error = alGetError();
    76             if(error == AL_INVALID_VALUE)
    77                 COUT(2) << "Sound: OpenAL: Invalid sound velocity" << std::endl;
    78 
    79             const Quaternion& orient = this->entity_->getOrientation();
    80             Vector3 at = orient.zAxis();
    81             alSource3f(this->source_, AL_DIRECTION, at.x, at.y, at.z);
    82             error = alGetError();
    83             if(error == AL_INVALID_VALUE)
    84                 COUT(2) << "Sound: OpenAL: Invalid sound direction" << std::endl;
    85         }
    86     }
    87 
    88     void SoundBase::play(bool loop) {
    89         if(alIsSource(this->source_)) {
    90             if(loop)
     41    BaseSound::BaseSound()
     42        : source_(0)
     43        , buffer_(0)
     44        , bPlayOnLoad_(false)
     45        , bLoop_(false)
     46    {
     47        RegisterRootObject(BaseSound);
     48    }
     49
     50    BaseSound::~BaseSound()
     51    {
     52        this->setSoundFile("");
     53    }
     54
     55    void BaseSound::play()
     56    {
     57        if (alIsSource(this->source_))
     58        {
     59            if (this->bLoop_)
    9160                alSourcei(this->source_, AL_LOOPING, AL_TRUE);
    9261            else
     
    9463            alSourcePlay(this->source_);
    9564
    96             if(alGetError() != AL_NO_ERROR)
     65            if (alGetError() != AL_NO_ERROR)
    9766            {
    9867                 COUT(2) << "Sound: OpenAL: Error playin sound " << this->source_ << std::endl;
     
    10170    }
    10271
    103     void SoundBase::stop() {
    104         if(alIsSource(this->source_)) {
     72    void BaseSound::stop()
     73    {
     74        if (alIsSource(this->source_))
    10575            alSourceStop(this->source_);
    106         }
    107     }
    108 
    109     void SoundBase::pause() {
    110         if(alIsSource(this->source_)) {
     76    }
     77
     78    void BaseSound::pause()
     79    {
     80        if (alIsSource(this->source_))
    11181            alSourcePause(this->source_);
    112         }
    113     }
    114 
    115     bool SoundBase::isPlaying() {
    116         if(alIsSource(this->source_)) {
     82    }
     83
     84    bool BaseSound::isPlaying()
     85    {
     86        if (alIsSource(this->source_))
    11787            return getSourceState() == AL_PLAYING;
    118         }
    11988        return false;
    12089    }
    12190
    122     bool SoundBase::isPaused() {
    123         if(alIsSource(this->source_)) {
     91    bool BaseSound::isPaused()
     92    {
     93        if (alIsSource(this->source_))
    12494            return getSourceState() == AL_PAUSED;
    125         }
    12695        return true;
    12796    }
    12897
    129     bool SoundBase::isStopped() {
    130         if(alIsSource(this->source_)) {
     98    bool BaseSound::isStopped()
     99    {
     100        if (alIsSource(this->source_))
    131101            return getSourceState() == AL_INITIAL || getSourceState() == AL_STOPPED;
    132         }
    133102        return true;
    134103    }
    135104
    136     bool SoundBase::loadFile(const std::string& filename) {
    137         if(!SoundManager::getInstance().isSoundAvailable())
    138         {
    139             COUT(3) << "Sound: not available, skipping " << filename << std::endl;
    140             return false;
    141         }
    142 
    143         COUT(3) << "Sound: OpenAL ALUT: loading file " << filename << std::endl;
     105    void BaseSound::setPlayOnLoad(bool val)
     106    {
     107        this->bPlayOnLoad_ = true;
     108        this->play();
     109    }
     110
     111    void BaseSound::setSoundFile(const std::string& soundFile)
     112    {
     113        this->soundFile_ = soundFile;
     114        if (!GameMode::playsSound())
     115            return;
     116
     117        if (soundFile.empty() && alIsSource(this->source_))
     118        {
     119            // Unload sound
     120            alSourcei(this->source_, AL_BUFFER, 0);
     121            alDeleteSources(1, &this->source_);
     122            alDeleteBuffers(1, &this->buffer_);
     123            return;
     124        }
     125
     126        COUT(3) << "Sound: OpenAL ALUT: loading file " << soundFile << std::endl;
    144127        // Get DataStream from the resources
    145         shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(filename);
    146         if (fileInfo == NULL) {
    147             COUT(2) << "Warning: Sound file '" << filename << "' not found" << std::endl;
    148             return false;
    149         }
    150         DataStreamPtr stream = Resource::open(filename);
     128        shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(soundFile);
     129        if (fileInfo == NULL)
     130        {
     131            COUT(2) << "Warning: Sound file '" << soundFile << "' not found" << std::endl;
     132            return;
     133        }
     134        DataStreamPtr stream = Resource::open(soundFile);
    151135        // Read everything into a temporary buffer
    152136        char* buffer = new char[fileInfo->size];
     
    156140        delete[] buffer;
    157141
    158         if(this->buffer_ == AL_NONE) {
     142        if (this->buffer_ == AL_NONE)
     143        {
    159144            COUT(2) << "Sound: OpenAL ALUT: " << alutGetErrorString(alutGetError()) << std::endl;
    160             if(filename.find("ogg", 0) != std::string::npos)
    161             {
    162                 COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
    163                 this->buffer_ = loadOggFile(filename);
    164             }
    165 
    166             if(this->buffer_ == AL_NONE)
    167             {
    168                 COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
    169                 return false;
    170             }
     145            return;
     146            //if (filename.find("ogg", 0) != std::string::npos)
     147            //{
     148            //    COUT(2) << "Sound: Trying fallback ogg loader" << std::endl;
     149            //    this->buffer_ = loadOggFile(filename);
     150            //}
     151
     152            //if (this->buffer_ == AL_NONE)
     153            //{
     154            //    COUT(2) << "Sound: fallback ogg loader failed: " << alutGetErrorString(alutGetError()) << std::endl;
     155            //    return;
     156            //}
    171157        }
    172158
    173159        alGenSources(1, &this->source_);
    174160        alSourcei(this->source_, AL_BUFFER, this->buffer_);
    175         if(alGetError() != AL_NO_ERROR) {
    176             COUT(2) << "Sound: OpenAL: Error loading sample file: " << filename << std::endl;
    177             return false;
    178         }
    179         return true;
    180     }
    181 
    182     ALint SoundBase::getSourceState() {
     161        if (alGetError() != AL_NO_ERROR)
     162        {
     163            COUT(2) << "Sound: OpenAL: Error loading sample file: " << soundFile << std::endl;
     164            return;
     165        }
     166
     167        alSource3f(this->source_, AL_POSITION,  0, 0, 0);
     168
     169        if (this->bPlayOnLoad_)
     170            this->play();
     171    }
     172
     173    ALint BaseSound::getSourceState()
     174    {
    183175        ALint state;
    184176        alGetSourcei(this->source_, AL_SOURCE_STATE, &state);
     
    186178    }
    187179
    188     ALuint SoundBase::loadOggFile(const std::string& filename)
     180#if 0 // Not yet supported because of missing resource implementation
     181    ALuint BaseSound::loadOggFile(const std::string& filename)
    189182    {
    190183        char inbuffer[4096];
     
    199192        FILE* f = fopen(filename.c_str(), "rb");
    200193
    201         if(ov_open(f, &vf, NULL, 0) < 0)
     194        if (ov_open(f, &vf, NULL, 0) < 0)
    202195        {
    203196            COUT(2) << "Sound: libvorbisfile: File does not seem to be an Ogg Vorbis bitstream" << std::endl;
     
    206199        }
    207200
    208         while(!eof)
     201        while (!eof)
    209202        {
    210203            long ret = ov_read(&vf, inbuffer, sizeof(inbuffer), 0, 2, 1, &current_section);
     
    226219
    227220        vorbisInfo = ov_info(&vf, -1);
    228         if(vorbisInfo->channels == 1)
     221        if (vorbisInfo->channels == 1)
    229222            format = AL_FORMAT_MONO16;
    230223        else
     
    237230        return buffer;
    238231    }
     232#endif
     233
    239234} // namespace: orxonox
Note: See TracChangeset for help on using the changeset viewer.