summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndré Nusser <andre.nusser@googlemail.com>2016-03-24 00:11:28 +0100
committerAndré Nusser <andre.nusser@googlemail.com>2016-03-29 22:18:48 +0200
commit866b09992668f97af063dcd77dc5dd0e3a512b94 (patch)
treedfeffaa3eb1a559d746d04f3fbb7402e74d1b290 /src
parentefe93864d53f72be4fa4dfe003f0f7578fc558e2 (diff)
New Random class for convenient random number generation.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am.drumgizmo1
-rw-r--r--src/instrument.cc8
-rw-r--r--src/instrument.h3
-rw-r--r--src/powerlist.cc14
-rw-r--r--src/powerlist.h3
-rw-r--r--src/random.cc58
-rw-r--r--src/random.h62
-rw-r--r--src/velocity.cc2
-rw-r--r--src/velocity.h3
9 files changed, 135 insertions, 19 deletions
diff --git a/src/Makefile.am.drumgizmo b/src/Makefile.am.drumgizmo
index e41bacc..8f648d0 100644
--- a/src/Makefile.am.drumgizmo
+++ b/src/Makefile.am.drumgizmo
@@ -28,6 +28,7 @@ DRUMGIZMO_SOURCES = \
$(top_srcdir)/src/mutex.cc \
$(top_srcdir)/src/path.cc \
$(top_srcdir)/src/powerlist.cc \
+ $(top_srcdir)/src/random.cc \
$(top_srcdir)/src/sample.cc \
$(top_srcdir)/src/semaphore.cc \
$(top_srcdir)/src/saxparser.cc \
diff --git a/src/instrument.cc b/src/instrument.cc
index 96a6bfd..21371f0 100644
--- a/src/instrument.cc
+++ b/src/instrument.cc
@@ -70,9 +70,8 @@ Sample *Instrument::sample(level_t level, size_t pos)
}
if(Conf::enable_velocity_randomiser) {
- float r = (float)rand() / (float)RAND_MAX; // random number: [0;1]
- r -= 0.5; // random number [-0.5;0.5]
- r *= Conf::velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1]
+ float r = rand.floatInRange(-1.0*Conf::velocity_randomiser_weight,
+ Conf::velocity_randomiser_weight);
level += r;
if(level > 1.0) level = 1.0;
if(level < 0.0) level = 0.0;
@@ -91,8 +90,7 @@ Sample *Instrument::sample(level_t level, size_t pos)
// Version 1.0
std::vector<Sample*> s = samples.get(level * mod);
if(s.size() == 0) return NULL;
- size_t idx = rand()%(s.size());
- sample = s[idx];
+ sample = rand.choose(s);
}
if(Conf::enable_velocity_modifier) {
diff --git a/src/instrument.h b/src/instrument.h
index e880d8d..62f8d01 100644
--- a/src/instrument.h
+++ b/src/instrument.h
@@ -35,6 +35,7 @@
#include "sample.h"
#include "versionstr.h"
+#include "random.h"
class InstrumentParser;
class Instrument {
@@ -76,6 +77,8 @@ private:
size_t lastpos;
float mod;
+
+ Random rand;
};
//typedef std::map< std::string, Instrument > Instruments;
diff --git a/src/powerlist.cc b/src/powerlist.cc
index efb1f97..b6640e9 100644
--- a/src/powerlist.cc
+++ b/src/powerlist.cc
@@ -51,18 +51,6 @@
#define SIZE 500
-// Box–Muller transform.
-// See: http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
-static float box_muller_transform(float mean, float stddev)
-{
- float U1 = (float)rand() / (float)RAND_MAX;
- float U2 = (float)rand() / (float)RAND_MAX;
-
- float x = sqrt(-2.0 * log(U1)) * cos(2.0 * M_PI * U2);
-
- return mean + stddev * x;
-}
-
PowerList::PowerList()
{
power_max = 0;
@@ -242,7 +230,7 @@ Sample *PowerList::get(level_t level)
again:
// Select normal distributed value between
// (stddev/2) and (power_span-stddev/2)
- float lvl = box_muller_transform(mean, stddev);
+ float lvl = rand.normalDistribution(mean, stddev);
// Adjust this value to be in range
// (power_min+stddev/2) and (power_max-stddev/2)
diff --git a/src/powerlist.h b/src/powerlist.h
index 43f51d2..1077d8c 100644
--- a/src/powerlist.h
+++ b/src/powerlist.h
@@ -30,6 +30,7 @@
#include <vector>
#include "sample.h"
+#include "random.h"
class PowerList {
public:
@@ -47,6 +48,8 @@ private:
float power;
};
+ Random rand;
+
std::vector<PowerListItem> samples;
float power_max;
float power_min;
diff --git a/src/random.cc b/src/random.cc
new file mode 100644
index 0000000..1df9a62
--- /dev/null
+++ b/src/random.cc
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * random.cc
+ *
+ * Wed Mar 23 19:17:24 CET 2016
+ * Copyright 2016 André Nusser
+ * andre.nusser@googlemail.com
+ ****************************************************************************/
+
+/*
+ * This file is part of DrumGizmo.
+ *
+ * DrumGizmo is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * DrumGizmo 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with DrumGizmo; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "random.h"
+
+#include <chrono>
+
+Random::Random()
+ : Random(std::chrono::system_clock::now().time_since_epoch().count())
+{
+
+}
+
+Random::Random(unsigned int seed)
+{
+ generator.seed(seed);
+}
+
+int Random::intInRange(int lower_bound, int upper_bound)
+{
+ std::uniform_int_distribution<int> distribution(lower_bound, upper_bound);
+ return distribution(generator);
+}
+
+float Random::floatInRange(float lower_bound, float upper_bound)
+{
+ std::uniform_real_distribution<float> distribution(lower_bound, upper_bound);
+ return distribution(generator);
+}
+
+float Random::normalDistribution(float mean, float stddev)
+{
+ std::normal_distribution<float> distribution(mean, stddev);
+ return distribution(generator);
+}
diff --git a/src/random.h b/src/random.h
new file mode 100644
index 0000000..9eaefad
--- /dev/null
+++ b/src/random.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * random.h
+ *
+ * Wed Mar 23 19:17:24 CET 2016
+ * Copyright 2016 André Nusser
+ * andre.nusser@googlemail.com
+ ****************************************************************************/
+
+/*
+ * This file is part of DrumGizmo.
+ *
+ * DrumGizmo is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * DrumGizmo 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with DrumGizmo; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#pragma once
+
+#include <random>
+#include <vector>
+
+class Random
+{
+public:
+ Random();
+ Random(unsigned int seed);
+
+ //! \return random int in range [<lower_bound>, <upper_bound>].
+ int intInRange(int lower_bound, int upper_bound);
+
+ //! \return random float in range [<lower_bound>, <upper_bound>].
+ float floatInRange(float lower_bound, float upper_bound);
+
+ //! \return random float drawn from a normal distribution with mean <mean>
+ //! and standard deviation <stddev>.
+ float normalDistribution(float mean, float stddev);
+
+ //! \return uniformly at random chosen element from <vec>.
+ template <typename T>
+ T& choose(std::vector<T>& vec);
+
+private:
+ std::default_random_engine generator;
+};
+
+template <typename T>
+T& Random::choose(std::vector<T>& vec)
+{
+ std::uniform_int_distribution<size_t> distribution(0, vec.size()-1);
+ size_t rand_index = distribution(generator);
+ return vec[rand_index];
+}
diff --git a/src/velocity.cc b/src/velocity.cc
index 04d0475..0ee00df 100644
--- a/src/velocity.cc
+++ b/src/velocity.cc
@@ -47,7 +47,7 @@ Sample *Velocity::getSample()
{
Sample *sample = NULL;
- float x = (float)rand() / (float)RAND_MAX;
+ float x = rand.floatInRange(0, 1);
float sum = 0.0;
Samples::iterator i = samples.begin();
diff --git a/src/velocity.h b/src/velocity.h
index 439ca68..c62ddad 100644
--- a/src/velocity.h
+++ b/src/velocity.h
@@ -30,6 +30,7 @@
#include <map>
#include "sample.h"
+#include "random.h"
class Velocity {
public:
@@ -44,6 +45,8 @@ public:
private:
typedef std::map< Sample *, float > Samples;
Samples samples;
+
+ Random rand;
};
#endif/*__DRUMGIZMO_VELOCITY_H__*/