Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 6409


Ignore:
Timestamp:
Dec 24, 2009, 8:54:33 AM (14 years ago)
Author:
rgrieder
Message:

Better sound source management: Sources are created on demand with a minimum of minSources_ (default: 16) and deleted again when more than 50% more sources are available than used.

Location:
code/branches/presentation2/src/orxonox/sound
Files:
2 edited

Legend:

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

    r6394 r6409  
    147147            this->availableSoundSources_.push_back(source);
    148148        else
    149             ThrowException(InitialisationFailed, "Sound Error: Could not even create a single source");
    150         // Get the rest of the sources
    151         alGenSources(1, &source);
    152         unsigned int count = 1;
    153         while (alIsSource(source) && !alGetError() && count <= this->maxSources_)
    154         {
    155             this->availableSoundSources_.push_back(source);
    156             alGenSources(1, &source);
    157             ++count;
    158         }
     149            ThrowException(InitialisationFailed, "Sound Error: Could not create even a single source");
     150        // Create a few initial sources
     151        this->createSoundSources(this->minSources_ - 1);
    159152
    160153        // Disarm guards
     
    190183        if (!alutExit())
    191184            COUT(1) << "Sound Error: Closing ALUT failed: " << alutGetErrorString(alutGetError()) << std::endl;
    192     }
    193 
    194     void SoundManager::preUpdate(const Clock& time)
    195     {
    196         this->processCrossFading(time.getDeltaTime());
    197 
    198         // Check whether a sound object has stopped playing
    199         for (unsigned int i = 0; i < this->usedSoundSources_.size(); ++i)
    200         {
    201             ALint state;
    202             alGetSourcei(this->usedSoundSources_[i].first, AL_SOURCE_STATE, &state);
    203             if (state == AL_STOPPED)
    204             {
    205                 this->usedSoundSources_[i].second->stop();
    206                 --i;
    207             }
    208         }
    209185    }
    210186
     
    225201            .callback(this, &SoundManager::checkEffectsVolumeValidity);
    226202
     203        SetConfigValue(minSources_, 16)
     204            .description("Minimum number of sources being generated (if possible)");
    227205        SetConfigValue(maxSources_, 1024)
    228206            .description("Maximum number of sources to be made available");
     207    }
     208
     209    void SoundManager::preUpdate(const Clock& time)
     210    {
     211        this->processCrossFading(time.getDeltaTime());
     212
     213        // Check whether a sound object has stopped playing
     214        for (unsigned int i = 0; i < this->usedSoundSources_.size(); ++i)
     215        {
     216            ALint state;
     217            alGetSourcei(this->usedSoundSources_[i].first, AL_SOURCE_STATE, &state);
     218            if (state == AL_STOPPED)
     219            {
     220                this->usedSoundSources_[i].second->stop();
     221                --i;
     222            }
     223        }
    229224    }
    230225
     
    559554        else
    560555        {
     556            if (this->usedSoundSources_.size() < this->maxSources_)
     557            {
     558                ALuint source;
     559                alGenSources(1, &source);
     560                // Try to create new sources (50% more, but at least one)
     561                if (alIsSource(source) && !alGetError())
     562                {
     563                    this->usedSoundSources_.push_back(std::make_pair(source, object));
     564                    return source;
     565                }
     566            }
    561567            // Return no source ID
    562568            ALuint source = 123456789;
     
    582588            }
    583589        }
     590        int used = std::max(this->usedSoundSources_.size(), this->minSources_);
     591        // Subtract those we added in the statement above trough std::max
     592        int available = (int)this->availableSoundSources_.size() - (used - (int)this->usedSoundSources_.size());
     593        // Delete sources again to free resources if appropriate (more than 50% more available than used)
     594        int toDelete = available - used / 2;
     595        while (toDelete-- > 0)
     596        {
     597            alDeleteSources(1, &this->availableSoundSources_.back());
     598            if (alGetError())
     599                COUT(1) << "Sound Error: Failed to delete a source --> lost forever" << std::endl;
     600            this->availableSoundSources_.pop_back();
     601        }
     602    }
     603
     604    unsigned int SoundManager::createSoundSources(unsigned int n)
     605    {
     606        unsigned int count = this->availableSoundSources_.size() + this->usedSoundSources_.size();
     607        while (count < this->maxSources_ && count <= n)
     608        {
     609            ALuint source;
     610            alGenSources(1, &source);
     611            if (alIsSource(source) && !alGetError())
     612                this->availableSoundSources_.push_back(source);
     613            else
     614                break;
     615            ++count;
     616        }
     617        return count - this->availableSoundSources_.size() - this->usedSoundSources_.size();
    584618    }
    585619}
  • code/branches/presentation2/src/orxonox/sound/SoundManager.h

    r6391 r6409  
    114114        void updateVolume(SoundType::Value type);
    115115
     116        unsigned int createSoundSources(unsigned int n);
     117
    116118        // OpenAL device/context related
    117119        std::vector<std::string> deviceNames_;
     
    140142
    141143        // Sound source related
     144        unsigned int minSources_;
    142145        unsigned int maxSources_;
    143146        std::vector<ALuint> availableSoundSources_;
Note: See TracChangeset for help on using the changeset viewer.