From 0d6bb3e70aadcfc16d4437470705b6ec006a9de0 Mon Sep 17 00:00:00 2001 From: theGreatWhiteShark Date: Sat, 5 Nov 2022 09:25:18 +0100 Subject: [PATCH] Random: introduce dedicated randomization class to more elegantly make the random number generating function of the AudioEngine available to other parts of the code as well. It looks quite empty but will likely be filled with life at some future point in time when I have time to look at #627 again --- src/core/AudioEngine/AudioEngine.cpp | 28 +++------------- src/core/Helpers/Random.cpp | 38 +++++++++++++++++++++ src/core/Helpers/Random.h | 50 ++++++++++++++++++++++++++++ src/core/Hydrogen.h | 2 -- 4 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 src/core/Helpers/Random.cpp create mode 100644 src/core/Helpers/Random.h diff --git a/src/core/AudioEngine/AudioEngine.cpp b/src/core/AudioEngine/AudioEngine.cpp index ffa98d5eab..94a6189974 100644 --- a/src/core/AudioEngine/AudioEngine.cpp +++ b/src/core/AudioEngine/AudioEngine.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -65,33 +66,12 @@ #include #include -#include namespace H2Core { const int AudioEngine::nMaxTimeHumanize = 2000; -inline int randomValue( int max ) -{ - return rand() % max; -} - -inline float getGaussian( float z ) -{ - // gaussian distribution -- dimss - float x1, x2, w; - do { - x1 = 2.0 * ( ( ( float ) rand() ) / static_cast(RAND_MAX) ) - 1.0; - x2 = 2.0 * ( ( ( float ) rand() ) / static_cast(RAND_MAX) ) - 1.0; - w = x1 * x1 + x2 * x2; - } while ( w >= 1.0 ); - - w = sqrtf( ( -2.0 * logf( w ) ) / w ); - return x1 * w * z + 0.0; // tunable -} - - /** Gets the current time. * \return Current time obtained by gettimeofday()*/ inline timeval currentTime2() @@ -1158,12 +1138,12 @@ void AudioEngine::processPlayNotes( unsigned long nframes ) if ( pSong->getHumanizeVelocityValue() != 0 ) { pNote->set_velocity( pNote->get_velocity() + pSong->getHumanizeVelocityValue() * - getGaussian( AudioEngine::fHumanizeVelocitySD ) ); + Random::getGaussian( AudioEngine::fHumanizeVelocitySD ) ); } float fPitch = pNote->get_pitch() + pNote->get_instrument()->get_pitch_offset(); if ( pNote->get_instrument()->get_random_pitch_factor() != 0. ) { - fPitch += getGaussian( AudioEngine::fHumanizePitchSD ) * + fPitch += Random::getGaussian( AudioEngine::fHumanizePitchSD ) * pNote->get_instrument()->get_random_pitch_factor(); } pNote->set_pitch( fPitch ); @@ -2382,7 +2362,7 @@ int AudioEngine::updateNoteQueue( unsigned nIntervalLengthInFrames ) */ if ( pSong->getHumanizeTimeValue() != 0 ) { nOffset += (int) ( - getGaussian( AudioEngine::fHumanizeTimingSD ) * + Random::getGaussian( AudioEngine::fHumanizeTimingSD ) * pSong->getHumanizeTimeValue() * AudioEngine::nMaxTimeHumanize ); } diff --git a/src/core/Helpers/Random.cpp b/src/core/Helpers/Random.cpp new file mode 100644 index 0000000000..90e329241c --- /dev/null +++ b/src/core/Helpers/Random.cpp @@ -0,0 +1,38 @@ +/* + * Hydrogen + * Copyright(c) 2008-2021 The hydrogen development team [hydrogen-devel@lists.sourceforge.net] + * + * http://www.hydrogen-music.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY, without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses + * + */ + +#include + +namespace H2Core { + +float Random::getGaussian( float fStandardDeviation ) { + // gaussian distribution -- dimss + float x1, x2, w; + do { + x1 = 2.0 * ( ( ( float ) rand() ) / static_cast(RAND_MAX) ) - 1.0; + x2 = 2.0 * ( ( ( float ) rand() ) / static_cast(RAND_MAX) ) - 1.0; + w = x1 * x1 + x2 * x2; + } while ( w >= 1.0 ); + + w = sqrtf( ( -2.0 * logf( w ) ) / w ); + return x1 * w * fStandardDeviation + 0.0; // tunable +} +}; diff --git a/src/core/Helpers/Random.h b/src/core/Helpers/Random.h new file mode 100644 index 0000000000..5fe5d61347 --- /dev/null +++ b/src/core/Helpers/Random.h @@ -0,0 +1,50 @@ +/* + * Hydrogen + * Copyright(c) 2008-2021 The hydrogen development team [hydrogen-devel@lists.sourceforge.net] + * + * http://www.hydrogen-music.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY, without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see https://www.gnu.org/licenses + * + */ + +#ifndef H2C_RANDOM_H +#define H2C_RANDOM_H + +#include + +namespace H2Core +{ + +/** + * Container for functions generating random number. + * + * \ingroup docCore + */ +class Random : public H2Core::Object +{ + H2_OBJECT(Random) +public: + /** + * Draws an uncorrelated random value from a Gaussian distribution + * of mean 0 and @a fStandardDeviation. + * + * @param fStandardDeviation Defines the width of the distribution used. + */ + static float getGaussian( float fStandardDeviation ); +}; + +}; + +#endif // H2C_RANDOM_H diff --git a/src/core/Hydrogen.h b/src/core/Hydrogen.h index 3f42f6d729..8fbd4d5b37 100644 --- a/src/core/Hydrogen.h +++ b/src/core/Hydrogen.h @@ -39,8 +39,6 @@ #include #include -inline int randomValue( int max ); - namespace H2Core { class CoreActionController;