modded preset chooser so random selection changes the position iterator

other very minor non behavioral changes


git-svn-id: https://projectm.svn.sourceforge.net/svnroot/projectm/trunk@472 6778bc44-b910-0410-a7a0-be141de4315d
This commit is contained in:
w1z7ard
2007-09-22 19:20:32 +00:00
parent fa900a0deb
commit ff10424a33
5 changed files with 34 additions and 23 deletions

View File

@ -56,11 +56,11 @@ float PrefunExpr::eval_prefun_expr ( int mesh_i, int mesh_j )
assert ( func_ptr );
/* This is slightly less than safe, since
who knows if the passed argument is valid. For
speed purposes we'll go with this */
float * arg_list = new float[this->num_args];
assert(arg_list);
#ifdef EVAL_DEBUG_DOUBLE
DWRITE ( "fn[" );
#endif

View File

@ -109,7 +109,7 @@ public:
/// \param WeightFuncstor a functor that returns a probability for each index (see UniformRandomFunctor)
/// \returns an auto pointer of the newly allocated preset
template <class WeightFunctor>
std::auto_ptr<Preset> weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs, WeightFunctor & weightFunctor);
iterator weightedRandom(WeightFunctor & weightFunctor);
/// Do a weighted sample given a weight functor and default construction (ie. element size) of the weight functor
@ -118,7 +118,7 @@ public:
/// \param WeightFunctor a functor that returns a probability for each index (see UniformRandomFunctor)
/// \returns an auto pointer of the newly allocated preset
template <class WeightFunctor>
std::auto_ptr<Preset> weightedRandom(const PresetInputs & presetInputs, PresetOutputs & preseOutputs);
iterator weightedRandom();
@ -127,7 +127,7 @@ public:
private:
template <class WeightFunctor>
std::auto_ptr<Preset> doWeightedSample(WeightFunctor & weightFunctor, const PresetInputs & presetInputs, PresetOutputs & presetOutputs);
iterator doWeightedSample(WeightFunctor & weightFunctor);
const PresetLoader * m_presetLoader;
};
@ -191,20 +191,19 @@ inline PresetIterator PresetChooser::end() const {
}
template <class WeightFunctor>
std::auto_ptr<Preset> PresetChooser::weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs, WeightFunctor & weightFunctor) {
typename PresetChooser::iterator PresetChooser::weightedRandom(WeightFunctor & weightFunctor) {
return doWeightedSample(weightFunctor);
}
inline bool PresetChooser::empty() const {
return m_presetLoader->getNumPresets() == 0;
}
template <class WeightFunctor>
inline std::auto_ptr<Preset> PresetChooser::weightedRandom(const PresetInputs & presetInputs, PresetOutputs & presetOutputs){
inline PresetChooser::iterator PresetChooser::weightedRandom() {
WeightFunctor weightFunctor(m_presetLoader->getNumPresets());
return doWeightedSample(weightFunctor, presetInputs, presetOutputs);
return doWeightedSample(weightFunctor);
}
@ -215,9 +214,9 @@ inline std::auto_ptr<Preset> PresetChooser::directoryIndex(std::size_t index, co
}
template <class WeightFunctor>
inline std::auto_ptr<Preset> PresetChooser::doWeightedSample(WeightFunctor & weightFunctor, const PresetInputs & presetInputs, PresetOutputs & presetOutputs) {
inline PresetChooser::iterator PresetChooser::doWeightedSample(WeightFunctor & weightFunctor) {
#ifdef WIN32
// Choose a random bounded mass between 0 and 1
float cutoff = ((float)(rand())) / RAND_MAX;
@ -228,20 +227,25 @@ inline std::auto_ptr<Preset> PresetChooser::doWeightedSample(WeightFunctor & wei
float cutoff = ((float)(random())) / RAND_MAX;
#endif
#ifdef MACOS
// Choose a random bounded mass between 0 and 1
float cutoff = ((float)(rand())) / RAND_MAX;
#endif
// Sum up mass, stopping when cutoff is reached. This is the typical
// weighted sampling algorithm.
float mass = 0;
for (PresetIterator pos = this->begin();pos != this->end() ; ++pos) {
mass += weightFunctor(*pos);
if (mass >= cutoff)
return pos.allocate(presetInputs, presetOutputs);
return pos;
}
// Just in case something slips through the cracks
PresetIterator pos = this->end();
--pos;
return pos.allocate(presetInputs, presetOutputs);
return pos;
}
#endif

View File

@ -59,7 +59,7 @@ public:
}
int current() {
return (current_element);
return (current_element % RING_BUFFER_SIZE);
}
};
#endif

View File

@ -186,8 +186,10 @@ void projectM::default_key_handler( projectMEvent event, projectMKeycode keycode
if (m_presetChooser->empty())
break;
m_activePreset = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor>
(this->presetInputs, this->presetOutputs);
*m_presetPos = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor>();
m_activePreset = m_presetPos->allocate(this->presetInputs, this->presetOutputs);
assert(m_activePreset.get());
renderer->setPresetName(m_activePreset->presetName());

View File

@ -219,11 +219,14 @@ DLLEXPORT void projectM::renderFrame()
presetInputs.progress=0.0;
presetInputs.frame = 1;
m_activePreset2 = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor>
(presetInputs, &m_activePreset->presetOutputs() == &presetOutputs ? presetOutputs2 : presetOutputs);
*m_presetPos = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor>();
m_activePreset2 = m_presetPos->allocate
(presetInputs, &m_activePreset->presetOutputs() == &presetOutputs ? presetOutputs2 : presetOutputs);
assert(m_activePreset2.get());
renderer->setPresetName(m_activePreset2->presetName());
nohard=(int)(presetInputs.fps*3.5);
smoothFrame = (int)(presetInputs.fps * smoothDuration);
@ -233,8 +236,10 @@ DLLEXPORT void projectM::renderFrame()
{
// printf("%f %d %d\n", beatDetect->bass-beatDetect->bass_old,this->frame,this->avgtime);
printf("HARD CUT");
m_activePreset = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor>
(presetInputs, presetOutputs);
*m_presetPos = m_presetChooser->weightedRandom<PresetChooser::UniformRandomFunctor> ();
m_activePreset = m_presetPos->allocate(presetInputs, presetOutputs);
assert(m_activePreset.get());
nohard=presetInputs.fps*5;
smoothFrame=0;
@ -278,7 +283,7 @@ DLLEXPORT void projectM::renderFrame()
{
if (smoothFrame == 1)
{
m_activePreset = m_activePreset2;
m_activePreset = m_activePreset2;
smoothFrame=0;
printf("Smooth Finished\n");
}