Skip to content

Commit

Permalink
# - modernized white noise randomizer
Browse files Browse the repository at this point in the history
  • Loading branch information
en-software committed Jan 6, 2024
1 parent 2c12f57 commit 17419de
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
26 changes: 9 additions & 17 deletions moosic/random-noise.cpp
@@ -1,29 +1,21 @@
#include "pch.h"
#include "random-noise.h"
#include <time.h>
#include <random>

namespace blooDot::Oscillator
namespace blooDot::Music
{
RandomSource::RandomSource(unsigned int randomizerSeed)
{
this->InitializeRandomizer(randomizerSeed);
}
extern std::mt19937 gen;
extern std::uniform_real_distribution<> noise;
}

void RandomSource::InitializeRandomizer(unsigned int randomSeed)
{
if (randomSeed == 0)
{
::srand(static_cast<unsigned int>(::time(NULL)));
}
else
{
::srand(randomSeed);
}
}
namespace blooDot::Oscillator
{
using namespace blooDot::Music;

double RandomSource::Render(void)
{
return this->m_lastFrame[0] = static_cast<double>(2. * ::rand() / (RAND_MAX + 1.) - 1.);
return this->m_lastFrame[0] = noise(gen);
}

AudioFrame& RandomSource::Render(AudioFrame& frames, unsigned int channel)
Expand Down
4 changes: 1 addition & 3 deletions moosic/random-noise.h
Expand Up @@ -7,9 +7,7 @@ namespace blooDot::Oscillator
class RandomSource : public FlameSource
{
public:
RandomSource(unsigned int randomizerSeed = 0);

void InitializeRandomizer(unsigned int randomSeed = 0);
RandomSource() {}

double Render(void);
AudioFrame& Render(AudioFrame& frames, unsigned int channel = 0);
Expand Down
1 change: 1 addition & 0 deletions moosic/string-shaper.cpp
Expand Up @@ -155,6 +155,7 @@ namespace blooDot::Oscillator
preProcessed = this->m_secondOrder[3].Render(preProcessed);
preProcessed = this->m_noiseGate.Render(preProcessed);
preProcessed = this->m_delayLineFilter.Render(preProcessed);

return this->m_lastFrame[0] = preProcessed - this->m_delayLineLinear.Render(preProcessed);
}

Expand Down
22 changes: 11 additions & 11 deletions moosic/string-shaper.h
Expand Up @@ -30,18 +30,18 @@ namespace blooDot::Oscillator
AudioFrame& Render(AudioFrame& frames, unsigned int channel = 0);

private:
double m_baseGain;
double m_loopGain;
double m_mrLen;
double m_mrTension;
double m_pickPosition;
double m_pickExcitation;
double m_mrFreq;
double m_baseGain;
double m_loopGain;
double m_mrLen;
double m_mrTension;
double m_pickPosition;
double m_pickExcitation;
double m_mrFreq;

RandomSource m_noiseSource;
NoseGate m_noiseGate;
DelayLineFilter m_delayLineFilter;
RandomSource m_noiseSource;
NoseGate m_noiseGate;
DelayLineFilter m_delayLineFilter;
DelayLine m_delayLineLinear;
Derivative m_secondOrder[4];
Derivative m_secondOrder[4];
};
}
3 changes: 1 addition & 2 deletions music.cpp
Expand Up @@ -12,6 +12,7 @@ namespace blooDot::Music
std::random_device seed;
std::mt19937 gen{ seed() };
std::uniform_int_distribution<> dist{ 40, 88 };
std::uniform_real_distribution<> noise{ -1., 1. };

bool StartMusic()
{
Expand Down Expand Up @@ -51,8 +52,6 @@ namespace blooDot::Music
for (size_t i = 0; i < currentFrame.size(); ++i)
{
const auto frame = oscillator.Render();

//std::cout << frame;

currentFrame[i] = static_cast<int>(frame * INT_MAX); //dist(gen);
}
Expand Down

0 comments on commit 17419de

Please sign in to comment.