Ticket #3578: DeterministicSound.patch

File DeterministicSound.patch, 1.8 KB (added by Thomas, 8 years ago)
  • source/soundmanager/scripting/SoundGroup.cpp

     
    3838#include "ps/XML/Xeromyces.h"
    3939#include "soundmanager/items/ISoundItem.h"
    4040#include "soundmanager/SoundManager.h"
     41#include "simulation2/system/Component.h"
     42#include "simulation2/components/ICmpVisual.h"
    4143
     44#include <boost/random/mersenne_twister.hpp>
     45#include <boost/random/uniform_real.hpp>
     46
    4247#include <algorithm>
    4348
    4449
     
    4651
    4752#define PI 3.14126f
    4853
     54typedef boost::mt19937 rng_t;
    4955
    5056static const bool DISABLE_INTENSITY = true; // disable for now since it's broken
    5157
     
    96102}
    97103
    98104#if CONFIG2_AUDIO
    99 static float RandFloat(float min, float max)
    100 {
    101     return float(rand(min*100.0f, max*100.0f) / 100.0f);
     105static float RandFloat(rng_t& rng, float min, float max) {
     106    boost::uniform_real<> uni_dist(min, max);
     107    return float(uni_dist(rng));
    102108}
    103109#endif // CONFIG2_AUDIO
    104110
     
    203209        hSound->SetRollOff(itemRollOff);
    204210    }
    205211
     212
     213    rng_t rng;
     214    if (TestFlag(eRandPitch) || TestFlag(eRandGain)) {
     215        // Seed random number generator
     216        CmpPtr<ICmpVisual> cmpVisual(*g_Game->GetSimulation2(), source);
     217        rng.seed(cmpVisual ? cmpVisual->GetActorSeed() : rand(0, 1000));
     218    }
     219
    206220    if (TestFlag(eRandPitch))
    207         hSound->SetPitch(RandFloat(m_PitchLower, m_PitchUpper));
     221        hSound->SetPitch(RandFloat(rng, m_PitchLower, m_PitchUpper));
    208222    else
    209223        hSound->SetPitch(m_Pitch);
    210224
    211225    ALfloat theGain = m_Gain;
    212226    if (TestFlag(eRandGain))
    213         theGain = RandFloat(m_GainLower, m_GainUpper);
     227        theGain = RandFloat(rng, m_GainLower, m_GainUpper);
    214228
    215229    hSound->SetCone(m_ConeInnerAngle, m_ConeOuterAngle, m_ConeOuterGain);
    216230    ((CSoundManager*)g_SoundManager)->PlayGroupItem(hSound, theGain);