From 324502145b2a5fec38928509c8b3d9f8eb8bf47d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 23 Mar 2016 22:38:44 +0100 Subject: Settings. --- src/instrument.cc | 205 ++++++++++++++++++++++++++---------------------------- 1 file changed, 100 insertions(+), 105 deletions(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index 96a6bfd..eeaa956 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -34,147 +34,142 @@ #include "sample.h" #include "configuration.h" -Instrument::Instrument() +Instrument::Instrument(Settings& settings) + : settings(settings) { - DEBUG(instrument, "new %p\n", this); - mod = 1.0; - lastpos = 0; + DEBUG(instrument, "new %p\n", this); + mod = 1.0; + lastpos = 0; - magic = this; + magic = this; } Instrument::~Instrument() { - magic = NULL; - - DEBUG(instrument, "delete %p\n", this); - std::vector::iterator i = audiofiles.begin(); - while(i != audiofiles.end()) { - delete *i; - i++; - } + magic = NULL; + + DEBUG(instrument, "delete %p\n", this); + std::vector::iterator i = audiofiles.begin(); + while(i != audiofiles.end()) + { + delete *i; + i++; + } } bool Instrument::isValid() { - return this == magic; + return this == magic; } Sample *Instrument::sample(level_t level, size_t pos) { - Sample *sample = NULL; - - if(Conf::enable_velocity_modifier == false) { - mod = 1.0; - lastpos = 0; - } - - 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] - level += r; - if(level > 1.0) level = 1.0; - if(level < 0.0) level = 0.0; - } - - if(Conf::enable_velocity_modifier) { - mod += (pos - lastpos) / - (Conf::samplerate * Conf::velocity_modifier_falloff); - if(mod > 1.0) mod = 1.0; - } - - if(version >= VersionStr("2.0")) { - // Version 2.0 - sample = powerlist.get(level * mod); - } else { - // Version 1.0 - std::vector s = samples.get(level * mod); - if(s.size() == 0) return NULL; - size_t idx = rand()%(s.size()); - sample = s[idx]; - } - - if(Conf::enable_velocity_modifier) { - lastpos = pos; - mod *= Conf::velocity_modifier_weight; - } - - return sample; + Sample *sample = NULL; + + // Read out all values from settings. + auto enable_velocity_randomiser = settings.enable_velocity_randomiser.load(); + auto velocity_randomiser_weight = settings.velocity_randomiser_weight.load(); + auto samplerate = settings.samplerate.load(); + auto velocity_modifier_falloff = settings.velocity_modifier_falloff.load(); + auto enable_velocity_modifier = settings.enable_velocity_modifier.load(); + auto velocity_modifier_weight = settings.velocity_modifier_weight.load(); + + if(enable_velocity_modifier == false) + { + mod = 1.0; + lastpos = 0; + } + + if(enable_velocity_randomiser) + { + float r = (float)rand() / (float)RAND_MAX; // random number: [0;1] + r -= 0.5; // random number [-0.5;0.5] + r *= velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1] + level += r; + if(level > 1.0) + { + level = 1.0; + } + + if(level < 0.0) + { + level = 0.0; + } + } + + if(enable_velocity_modifier) + { + mod += (pos - lastpos) / + (samplerate * velocity_modifier_falloff); + if(mod > 1.0) + { + mod = 1.0; + } + } + + if(version >= VersionStr("2.0")) + { + // Version 2.0 + sample = powerlist.get(level * mod); + } + else + { + // Version 1.0 + std::vector s = samples.get(level * mod); + if(s.size() == 0) + { + return NULL; + } + + size_t idx = rand()%(s.size()); + sample = s[idx]; + } + + if(enable_velocity_modifier) + { + lastpos = pos; + mod *= velocity_modifier_weight; + } + + return sample; } void Instrument::addSample(level_t a, level_t b, Sample *s) { - samples.insert(a, b, s); + samples.insert(a, b, s); } void Instrument::finalise() { - if(version >= VersionStr("2.0")) { - std::vector::iterator s = samplelist.begin(); - while(s != samplelist.end()) { - powerlist.add(*s); - s++; - } - - powerlist.finalise(); - } + if(version >= VersionStr("2.0")) + { + std::vector::iterator s = samplelist.begin(); + while(s != samplelist.end()) + { + powerlist.add(*s); + s++; + } + + powerlist.finalise(); + } } std::string Instrument::name() { - return _name; + return _name; } std::string Instrument::description() { - return _description; + return _description; } std::string Instrument::group() { - return _group; + return _group; } void Instrument::setGroup(std::string g) { - _group = g; + _group = g; } - -#ifdef TEST_INSTRUMENT -//deps: channel.cc sample.cc audiofile.cc -//cflags: $(SNDFILE_CFLAGS) -//libs: $(SNDFILE_LIBS) -#include "test.h" - -TEST_BEGIN; - -Instrument i("test"); - -Sample *a = new Sample(); -i.addSample(0.0, 1.0, a); - -Sample *b = new Sample(); -i.addSample(0.0, 1.0, b); - -Sample *c = new Sample(); -i.addSample(1.5, 1.7, c); - -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); - -TEST_EQUAL(i.sample(2.0), NULL, "?"); - -TEST_EQUAL(i.sample(1.6), c, "?"); -TEST_EQUAL(i.sample(1.6), c, "?"); -TEST_EQUAL(i.sample(1.6), c, "?"); - -TEST_END; - -#endif/*TEST_INSTRUMENT*/ -- cgit v1.2.3 From c2997b9b1a5b831e76b1779aa957f2312a6e5089 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 23 Mar 2016 22:38:44 +0100 Subject: Settings. --- src/instrument.cc | 76 ++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 49 deletions(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index 9f18233..3348a4f 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -34,7 +34,8 @@ #include "sample.h" #include "configuration.h" -Instrument::Instrument() +Instrument::Instrument(Settings& settings) + : settings(settings) { DEBUG(instrument, "new %p\n", this); mod = 1.0; @@ -65,28 +66,41 @@ Sample* Instrument::sample(level_t level, size_t pos) { Sample *sample = NULL; - if(Conf::enable_velocity_modifier == false) { + // Read out all values from settings. + auto enable_velocity_randomiser = settings.enable_velocity_randomiser.load(); + auto velocity_randomiser_weight = settings.velocity_randomiser_weight.load(); + auto samplerate = settings.samplerate.load(); + auto velocity_modifier_falloff = settings.velocity_modifier_falloff.load(); + auto enable_velocity_modifier = settings.enable_velocity_modifier.load(); + auto velocity_modifier_weight = settings.velocity_modifier_weight.load(); + + if(enable_velocity_modifier == false) + { mod = 1.0; lastpos = 0; } - if(Conf::enable_velocity_randomiser) { - float r = rand.floatInRange(-1.0*Conf::velocity_randomiser_weight, - Conf::velocity_randomiser_weight); + if(enable_velocity_randomiser) + { + float r = (float)rand() / (float)RAND_MAX; // random number: [0;1] + r -= 0.5; // random number [-0.5;0.5] + r *= velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1] level += r; if(level > 1.0) { level = 1.0; } + if(level < 0.0) { level = 0.0; } } - if(Conf::enable_velocity_modifier) { + if(enable_velocity_modifier) + { mod += (pos - lastpos) / - (Conf::samplerate * Conf::velocity_modifier_falloff); + (samplerate * velocity_modifier_falloff); if(mod > 1.0) { mod = 1.0; @@ -98,7 +112,8 @@ Sample* Instrument::sample(level_t level, size_t pos) // Version 2.0 sample = powerlist.get(level * mod); } - else { + else + { // Version 1.0 std::vector s = samples.get(level * mod); if(s.size() == 0) @@ -106,13 +121,14 @@ Sample* Instrument::sample(level_t level, size_t pos) return NULL; } - sample = rand.choose(s); + size_t idx = rand()%(s.size()); + sample = s[idx]; } - if(Conf::enable_velocity_modifier) + if(enable_velocity_modifier) { lastpos = pos; - mod *= Conf::velocity_modifier_weight; + mod *= velocity_modifier_weight; } return sample; @@ -157,41 +173,3 @@ void Instrument::setGroup(const std::string& g) { _group = g; } - -#ifdef TEST_INSTRUMENT -// deps: channel.cc sample.cc audiofile.cc -// cflags: $(SNDFILE_CFLAGS) -// libs: $(SNDFILE_LIBS) -#include "test.h" - -TEST_BEGIN; - -Instrument i("test"); - -Sample* a = new Sample(); -i.addSample(0.0, 1.0, a); - -Sample* b = new Sample(); -i.addSample(0.0, 1.0, b); - -Sample* c = new Sample(); -i.addSample(1.5, 1.7, c); - -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), b, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); -TEST_EQUAL(i.sample(0.0), a, "?"); - -TEST_EQUAL(i.sample(2.0), NULL, "?"); - -TEST_EQUAL(i.sample(1.6), c, "?"); -TEST_EQUAL(i.sample(1.6), c, "?"); -TEST_EQUAL(i.sample(1.6), c, "?"); - -TEST_END; - -#endif /*TEST_INSTRUMENT*/ -- cgit v1.2.3 From 6e6af33f00a17e842da81e4a04ea9c3421c55adb Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 31 Mar 2016 21:26:42 +0200 Subject: Fix issues from rebasing. --- src/instrument.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index 3348a4f..ee778d7 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -26,8 +26,8 @@ */ #include "instrument.h" -#include -#include +#include +//#include #include @@ -82,7 +82,7 @@ Sample* Instrument::sample(level_t level, size_t pos) if(enable_velocity_randomiser) { - float r = (float)rand() / (float)RAND_MAX; // random number: [0;1] + float r = (float)std::rand() / (float)RAND_MAX; // random number: [0;1] r -= 0.5; // random number [-0.5;0.5] r *= velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1] level += r; @@ -121,7 +121,7 @@ Sample* Instrument::sample(level_t level, size_t pos) return NULL; } - size_t idx = rand()%(s.size()); + size_t idx = std::rand()%(s.size()); sample = s[idx]; } -- cgit v1.2.3 From 2e4175d84cc0ca765fdc0b03bf5c8420deeec268 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 31 Mar 2016 21:50:28 +0200 Subject: Use new Random class. --- src/instrument.cc | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index d246c09..ee225d5 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -26,9 +26,6 @@ */ #include "instrument.h" -#include -//#include - #include #include "sample.h" @@ -82,8 +79,7 @@ Sample* Instrument::sample(level_t level, size_t pos) if(enable_velocity_randomiser) { - float r = (float)rand() / (float)RAND_MAX; // random number: [0;1] - r -= 0.5; // random number [-0.5;0.5] + float r = rand.floatInRange(-0.5f, 0.5f); r *= velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1] level += r; if(level > 1.0) @@ -121,8 +117,8 @@ Sample* Instrument::sample(level_t level, size_t pos) return NULL; } - size_t idx = rand()%(s.size()); - sample = s[idx]; + //size_t idx = ::rand()%(s.size()); + sample = rand.choose(s); } if(enable_velocity_modifier) -- cgit v1.2.3 From 19691923340e5984e3ace837d39938f718fee608 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 31 Mar 2016 22:11:07 +0200 Subject: Removed the last Conf:: namespace usage and replaced it with Settings. --- src/instrument.cc | 1 - 1 file changed, 1 deletion(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index ee225d5..f187de0 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -29,7 +29,6 @@ #include #include "sample.h" -#include "configuration.h" Instrument::Instrument(Settings& settings) : settings(settings) -- cgit v1.2.3 From 057ef1d83ba263fb2adf1aa86f8e281ab0065c43 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 8 Apr 2016 00:15:32 +0200 Subject: Refactoring to finally get rid of MessageHandler/Receiver in favor of the new Settings mechanism. --- src/instrument.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'src/instrument.cc') diff --git a/src/instrument.cc b/src/instrument.cc index f187de0..cc052e9 100644 --- a/src/instrument.cc +++ b/src/instrument.cc @@ -42,7 +42,7 @@ Instrument::Instrument(Settings& settings) Instrument::~Instrument() { - magic = NULL; + magic = nullptr; DEBUG(instrument, "delete %p\n", this); std::vector::iterator i = audiofiles.begin(); @@ -60,8 +60,6 @@ bool Instrument::isValid() const Sample* Instrument::sample(level_t level, size_t pos) { - Sample *sample = NULL; - // Read out all values from settings. auto enable_velocity_randomiser = settings.enable_velocity_randomiser.load(); auto velocity_randomiser_weight = settings.velocity_randomiser_weight.load(); @@ -70,6 +68,8 @@ Sample* Instrument::sample(level_t level, size_t pos) auto enable_velocity_modifier = settings.enable_velocity_modifier.load(); auto velocity_modifier_weight = settings.velocity_modifier_weight.load(); + Sample *sample = nullptr; + if(enable_velocity_modifier == false) { mod = 1.0; @@ -78,14 +78,13 @@ Sample* Instrument::sample(level_t level, size_t pos) if(enable_velocity_randomiser) { - float r = rand.floatInRange(-0.5f, 0.5f); - r *= velocity_randomiser_weight * 2; // ex. random number [-0.1;0.1] + float r = rand.floatInRange(-1.0 * velocity_randomiser_weight, + velocity_randomiser_weight); level += r; if(level > 1.0) { level = 1.0; } - if(level < 0.0) { level = 0.0; @@ -94,8 +93,7 @@ Sample* Instrument::sample(level_t level, size_t pos) if(enable_velocity_modifier) { - mod += (pos - lastpos) / - (samplerate * velocity_modifier_falloff); + mod += (pos - lastpos) / (samplerate * velocity_modifier_falloff); if(mod > 1.0) { mod = 1.0; @@ -113,11 +111,10 @@ Sample* Instrument::sample(level_t level, size_t pos) std::vector s = samples.get(level * mod); if(s.size() == 0) { - return NULL; + return nullptr; } - //size_t idx = ::rand()%(s.size()); - sample = rand.choose(s); + sample = rand.choose(s); } if(enable_velocity_modifier) -- cgit v1.2.3