Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 11, 2009, 2:15:43 PM (15 years ago)
Author:
rgrieder
Message:

Added audio source management. This should reduce the problems when loading too many sounds.
However if there are too many players shooting at the same time, some sounds may still not play.

File:
1 edited

Legend:

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

    r6320 r6322  
    4343{
    4444    BaseSound::BaseSound()
    45         : audioSource_(0)
    46         , bPooling_(false)
     45        : bPooling_(false)
    4746        , volume_(1.0)
    4847        , bLooping_(false)
     
    5251        RegisterRootObject(BaseSound);
    5352
    54         if (GameMode::playsSound())
    55         {
    56             alGenSources(1, &this->audioSource_);
    57             if (!alIsSource(this->audioSource_))
    58                 COUT(1) << "Sound: Source generation failed: " << SoundManager::getALErrorString(alGetError()) << std::endl;
    59 
    60             if (alIsSource(this->audioSource_))
    61             {
    62                 alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20);
    63                 alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000);
    64             }
    65         }
     53        // Initialise audioSource_ to a value that is not a source
     54        // 0 is unfortunately not guaranteed to be no source ID.
     55        this->audioSource_ = 123456789;
     56        while (alIsSource(++this->audioSource_));
    6657    }
    6758
    6859    BaseSound::~BaseSound()
    6960    {
    70         this->setSource(std::string());
    71         if (GameMode::playsSound() && alIsSource(this->audioSource_))
    72             alDeleteSources(1, &this->audioSource_);
     61        this->stop();
    7362    }
    7463
     
    8473    {
    8574        this->state_ = Playing;
    86         if (GameMode::playsSound() && alIsSource(this->audioSource_) && this->getSourceState() != AL_PLAYING)
    87         {
     75        if (GameMode::playsSound() && this->getSourceState() != AL_PLAYING && this->soundBuffer_ != NULL)
     76        {
     77            if (!alIsSource(this->audioSource_))
     78                this->audioSource_ = SoundManager::getInstance().getSoundSource();
     79            if (!alIsSource(this->audioSource_))
     80                return;
     81            this->initialiseSource();
     82
    8883            alSourcePlay(this->audioSource_);
    89 
    9084            if (int error = alGetError())
    91                 COUT(2) << "Sound: Error playing sound: " << error << std::endl;
     85                COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl;
    9286        }
    9387    }
     
    9690    {
    9791        this->state_ = Stopped;
    98         if (GameMode::playsSound() && alIsSource(this->audioSource_))
     92        if (alIsSource(this->audioSource_))
     93        {
    9994            alSourceStop(this->audioSource_);
     95            // Release buffer
     96            alSourcei(this->audioSource_, AL_BUFFER, AL_NONE);
     97            // Release source again
     98            SoundManager::getInstance().releaseSoundSource(this->audioSource_);
     99            // Get a no source ID
     100            this->audioSource_ += 123455;
     101            while (alIsSource(++this->audioSource_));
     102        }
    100103    }
    101104
     
    105108            return;
    106109        this->state_ = Paused;
    107         if (GameMode::playsSound() && alIsSource(this->audioSource_))
     110        if (alIsSource(this->audioSource_))
    108111            alSourcePause(this->audioSource_);
    109112    }
     
    111114    ALint BaseSound::getSourceState() const
    112115    {
    113         if (GameMode::playsSound() && alIsSource(this->audioSource_))
     116        if (alIsSource(this->audioSource_))
    114117        {
    115118            ALint state;
     
    121124    }
    122125
     126    void BaseSound::initialiseSource()
     127    {
     128        this->updateVolume();
     129        this->setPitch(this->getPitch());
     130        this->setLooping(this->getLooping());
     131        alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
     132        alSource3f(this->audioSource_, AL_VELOCITY,  0, 0, 0);
     133        alSource3f(this->audioSource_, AL_DIRECTION, 0, 0, 0);
     134        alSourcei(this->audioSource_, AL_REFERENCE_DISTANCE, 20);
     135        alSourcei(this->audioSource_, AL_MAX_DISTANCE, 10000);
     136        if (ALint error = alGetError())
     137            COUT(2) << "Sound Warning: Setting source parameters to 0 failed: "
     138                    << SoundManager::getALErrorString(error) << std::endl;
     139        assert(this->soundBuffer_ != NULL);
     140        alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
     141        if (ALuint error = alGetError())
     142            COUT(1) << "Sound Error: Could not set buffer \"" << this->source_ << "\": " << SoundManager::getALErrorString(error) << std::endl;
     143    }
     144
    123145    void BaseSound::setVolume(float vol)
    124146    {
     
    136158    float BaseSound::getVolumeGain()
    137159    {
     160        assert(GameMode::playsSound());
    138161        return SoundManager::getInstance().getVolume(SoundType::none);
    139162    }
     
    143166        if (alIsSource(this->audioSource_))
    144167        {
    145             alSourcef(this->audioSource_, AL_GAIN, this->volume_*this->getVolumeGain());
     168            float volume = this->volume_ * this->getVolumeGain();
     169            alSourcef(this->audioSource_, AL_GAIN, volume);
    146170            if (int error = alGetError())
    147                 COUT(2) << "Sound: Error setting volume: " << error << std::endl;
     171                COUT(2) << "Sound: Error setting volume to " << volume
     172                        << ": " << SoundManager::getALErrorString(error) << std::endl;
    148173        }
    149174    }
     
    152177    {
    153178        this->bLooping_ = val;
    154         if (GameMode::playsSound() && alIsSource(this->audioSource_))
     179        if (alIsSource(this->audioSource_))
    155180            alSourcei(this->audioSource_, AL_LOOPING, (val ? AL_TRUE : AL_FALSE));
    156181    }
     
    165190        }       
    166191        this->pitch_ = pitch;
    167         if (GameMode::playsSound() && alIsSource(this->audioSource_))
     192        if (alIsSource(this->audioSource_))
    168193        {
    169194            if (int error = alGetError())
    170                 COUT(2) << "Sound: Error setting pitch: " << error << std::endl;
     195                COUT(2) << "Sound: Error setting pitch: " << SoundManager::getALErrorString(error) << std::endl;
    171196            alSourcef(this->audioSource_, AL_PITCH, pitch);
    172197        }
     
    183208        if (this->soundBuffer_ != NULL)
    184209        {
     210            // Stopping is imperative here!
    185211            if (alIsSource(this->audioSource_))
    186212            {
    187213                alSourceStop(this->audioSource_);
    188                 // Unload old buffer first
    189                 alSourcei(this->audioSource_, AL_BUFFER, 0);
     214                alSourcei(this->audioSource_, AL_BUFFER, AL_NONE);
    190215            }
    191216            SoundManager::getInstance().releaseSoundBuffer(this->soundBuffer_, this->bPooling_);
     
    194219
    195220        this->source_ = source;
    196         if (source_.empty() || !alIsSource(this->audioSource_))
     221        if (source_.empty())
    197222            return;
    198223
     
    201226            return;
    202227
    203         alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
    204         if (ALuint error = alGetError())
    205         {
    206             COUT(1) << "Sound Error: Could not load file \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl;
    207             return;
    208         }
    209 
    210         alSource3f(this->audioSource_, AL_POSITION,  0, 0, 0);
    211         this->updateVolume();
    212         this->setPitch(this->getPitch());
    213         this->setLooping(getLooping());
    214         if (this->isPlaying() || this->isPaused())
    215         {
    216             alSourcePlay(this->audioSource_);
    217             if (int error = alGetError())
    218                 COUT(2) << "Sound: Error playing sound: " << error << std::endl;
    219         }
    220         if (this->isPaused())
    221             alSourcePause(this->audioSource_);
     228        if (alIsSource(this->audioSource_))
     229        {
     230            alSourcei(this->audioSource_, AL_BUFFER, this->soundBuffer_->getBuffer());
     231            if (ALuint error = alGetError())
     232            {
     233                COUT(1) << "Sound Error: Could not set buffer \"" << source << "\": " << SoundManager::getALErrorString(error) << std::endl;
     234                return;
     235            }
     236
     237            if (this->isPlaying() || this->isPaused())
     238            {
     239                alSourcePlay(this->audioSource_);
     240                if (int error = alGetError())
     241                    COUT(2) << "Sound: Error playing sound: " << SoundManager::getALErrorString(error) << std::endl;
     242            }
     243            if (this->isPaused())
     244                alSourcePause(this->audioSource_);
     245        }
    222246    }
    223247   
Note: See TracChangeset for help on using the changeset viewer.