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/SoundManager.cc

    r6298 r6322  
    8787            COUT(1) << "Sound: Just getting the DLL with the dependencies is not enough for Windows (esp. Windows 7)!" << std::endl;
    8888#endif
    89             ThrowException(InitialisationFailed, "Sound: OpenAL error: Could not open sound device.");
     89            ThrowException(InitialisationFailed, "Sound Error: Could not open sound device.");
    9090        }
    9191        Loki::ScopeGuard closeDeviceGuard = Loki::MakeGuard(&alcCloseDevice, this->device_);
     
    100100
    101101        GameMode::setPlaysSound(true);
     102        Loki::ScopeGuard resetPlaysSoundGuard = Loki::MakeGuard(&GameMode::setPlaysSound, false);
    102103
    103104        // Get some information about the sound
     
    110111        else
    111112            COUT(2) << "Sound Warning: MIME Type retrieval failed: " << alutGetErrorString(alutGetError()) << std::endl;
     113       
     114        this->setVolumeInternal(1.0, SoundType::none);
     115        this->setVolumeInternal(1.0, SoundType::ambient);
     116        this->setVolumeInternal(1.0, SoundType::effects);
     117       
     118        this->mute_[SoundType::none] = false;
     119        this->mute_[SoundType::ambient] = false;
     120        this->mute_[SoundType::effects] = false;
     121
     122        this->setConfigValues();
     123
     124        // Try to get at least one source
     125        ALuint source;
     126        alGenSources(1, &source);
     127        if (!alGetError() && alIsSource(source))
     128            this->soundSources_.push_back(source);
     129        else
     130            ThrowException(InitialisationFailed, "Sound Error: Could not even create a single source");
     131        // Get the rest of the sources
     132        alGenSources(1, &source);
     133        unsigned int count = 1;
     134        while (alIsSource(source) && !alGetError() && count <= this->maxSources_)
     135        {
     136            this->soundSources_.push_back(source);
     137            alGenSources(1, &source);
     138            ++count;
     139        }
    112140
    113141        // Disarm guards
     
    115143        closeDeviceGuard.Dismiss();
    116144        desroyContextGuard.Dismiss();
    117        
    118         this->setVolumeInternal(1.0, SoundType::none);
    119         this->setVolumeInternal(1.0, SoundType::ambient);
    120         this->setVolumeInternal(1.0, SoundType::effects);
    121        
    122         this->mute_[SoundType::none] = false;
    123         this->mute_[SoundType::ambient] = false;
    124         this->mute_[SoundType::effects] = false;
    125 
    126         this->setConfigValues();
     145        resetPlaysSoundGuard.Dismiss();
    127146
    128147        COUT(4) << "Sound: Initialisation complete" << std::endl;
     
    164183            .description("Determines how fast sounds should fade, per second.")
    165184            .callback(this, &SoundManager::checkFadeStepValidity);
    166            
     185
    167186        SetConfigValue(soundVolume_, 1.0f)
    168187            .description("Defines the overall volume.")
    169188            .callback(this, &SoundManager::checkSoundVolumeValidity);
    170            
     189
    171190        SetConfigValue(ambientVolume_, 1.0f)
    172191            .description("Defines the ambient volume.")
    173192            .callback(this, &SoundManager::checkAmbientVolumeValidity);
    174            
     193
    175194        SetConfigValue(effectsVolume_, 1.0f)
    176195            .description("Defines the effects volume.")
    177196            .callback(this, &SoundManager::checkEffectsVolumeValidity);
     197
     198        SetConfigValue(maxSources_, 1024)
     199            .description("Maximum number of sources to be made available");
    178200    }
    179201
     
    595617        }
    596618    }
     619
     620    ALuint SoundManager::getSoundSource()
     621    {
     622        if (!this->soundSources_.empty())
     623        {
     624            ALuint source = this->soundSources_.back();
     625            this->soundSources_.pop_back();
     626            return source;
     627        }
     628        else
     629        {
     630            // Return no source ID
     631            ALuint source = 123456789;
     632            while (alIsSource(++source));
     633            return source;
     634        }
     635    }
     636
     637    void SoundManager::releaseSoundSource(ALuint source)
     638    {
     639#ifndef NDEBUG
     640        for (std::vector<ALuint>::const_iterator it = this->soundSources_.begin(); it != this->soundSources_.end(); ++it)
     641            assert((*it) != source);
     642#endif
     643        this->soundSources_.push_back(source);
     644    }
    597645}
Note: See TracChangeset for help on using the changeset viewer.