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/drumgizmo.cc | 10 ++- src/drumgizmo.h | 8 +- src/drumkitloader.cc | 16 ++-- src/drumkitloader.h | 4 +- src/drumkitparser.cc | 7 +- src/drumkitparser.h | 3 +- src/instrument.cc | 205 +++++++++++++++++++++++++-------------------------- src/instrument.h | 50 +++++++------ src/settings.h | 94 ++++++++++++++--------- 9 files changed, 218 insertions(+), 179 deletions(-) (limited to 'src') diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 51cee18..2658754 100644 --- a/src/drumgizmo.cc +++ b/src/drumgizmo.cc @@ -46,18 +46,20 @@ #include "nolocale.h" -DrumGizmo::DrumGizmo(AudioOutputEngine *o, AudioInputEngine *i) +DrumGizmo::DrumGizmo(Settings& settings, + AudioOutputEngine *o, AudioInputEngine *i) : MessageReceiver(MSGRCV_ENGINE) - , loader() + , loader(settings) , oe(o) , ie(i) , framesize(0) , freewheel(false) , events{} + , settings(settings) { is_stopping = false; audioCache.init(10000); // start thread - + events.reserve(1000); } @@ -81,7 +83,7 @@ bool DrumGizmo::loadkit(std::string file) // Delete all Channels, Instruments, Samples and AudioFiles. kit.clear(); - DrumKitParser parser(kit); + DrumKitParser parser(settings, kit); if(parser.parseFile(file)) { ERR(drumgizmo, "Drumkit parser failed: %s\n", file.c_str()); diff --git a/src/drumgizmo.h b/src/drumgizmo.h index 632d5fc..63348da 100644 --- a/src/drumgizmo.h +++ b/src/drumgizmo.h @@ -50,6 +50,8 @@ #include "configfile.h" +#include "settings.h" + #define MAX_NUM_CHANNELS 64 #define REFSFILE "refs.conf" #define RESAMPLER_INPUT_BUFFER 64 @@ -58,7 +60,8 @@ class DrumGizmo : public MessageReceiver { public: - DrumGizmo(AudioOutputEngine *outputengine, AudioInputEngine *inputengine); + DrumGizmo(Settings& settings, + AudioOutputEngine *outputengine, AudioInputEngine *inputengine); virtual ~DrumGizmo(); bool loadkit(std::string kitfile); @@ -106,6 +109,7 @@ protected: size_t framesize; bool freewheel; - + std::vector events; + Settings& settings; }; diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index ff489ea..3cb4b68 100644 --- a/src/drumkitloader.cc +++ b/src/drumkitloader.cc @@ -31,9 +31,10 @@ #include "drumkitparser.h" #include "drumgizmo.h" -DrumKitLoader::DrumKitLoader() +DrumKitLoader::DrumKitLoader(Settings& settings) : semaphore("drumkitloader") , framesize(0) + , settings(settings) { run(); run_semaphore.wait(); // Wait for the thread to actually start. @@ -187,11 +188,14 @@ void DrumKitLoader::thread_main() if(loaded % fraction == 0 || loaded == total_num_audiofiles) { - LoadStatusMessage *ls = new LoadStatusMessage(); - ls->number_of_files = total_num_audiofiles; - ls->numer_of_files_loaded = loaded; - ls->current_file = filename; - msghandler.sendMessage(MSGRCV_UI, ls); + //LoadStatusMessage *ls = new LoadStatusMessage(); + //ls->number_of_files = total_num_audiofiles; + //ls->numer_of_files_loaded = loaded; + //ls->current_file = filename; + //msghandler.sendMessage(MSGRCV_UI, ls); + settings.number_of_files.store(total_num_audiofiles); + settings.number_of_files_loaded.store(loaded); + //settings.current_file.store(filename); } } diff --git a/src/drumkitloader.h b/src/drumkitloader.h index 3656839..2410074 100644 --- a/src/drumkitloader.h +++ b/src/drumkitloader.h @@ -34,6 +34,7 @@ #include "mutex.h" #include "drumkit.h" +#include "settings.h" //! This class is responsible for loading the drumkits in its own thread. //! All interaction calls are simply modifying queues and not doing any @@ -45,7 +46,7 @@ class DrumKitLoader { public: //! The constrcutor starts the loader thread. - DrumKitLoader(); + DrumKitLoader(Settings& settings); //! The destructor signals the thread to stop and waits to merge before //! returning (ie. deleting the object will garantuee that the thread has @@ -82,4 +83,5 @@ protected: size_t fraction{1}; size_t loaded{0}; size_t framesize{0}; + Settings& settings; }; diff --git a/src/drumkitparser.cc b/src/drumkitparser.cc index 92230cc..1210611 100644 --- a/src/drumkitparser.cc +++ b/src/drumkitparser.cc @@ -34,9 +34,10 @@ #include "path.h" #include "drumgizmo.h" -DrumKitParser::DrumKitParser(DrumKit& kit) - : kit(kit) +DrumKitParser::DrumKitParser(Settings& setting, DrumKit& k) + : kit(k) , refs(REFSFILE) + , settings(settings) { } @@ -182,7 +183,7 @@ void DrumKitParser::endTag(const std::string& name) { if(name == "instrument") { - Instrument* instrument = new Instrument(); + Instrument* instrument = new Instrument(settings); instrument->setGroup(instr_group); InstrumentParser parser(*instrument); diff --git a/src/drumkitparser.h b/src/drumkitparser.h index f857590..b3cf0a6 100644 --- a/src/drumkitparser.h +++ b/src/drumkitparser.h @@ -34,7 +34,7 @@ class DrumKitParser : public SAXParser { public: - DrumKitParser(DrumKit& kit); + DrumKitParser(Settings& setting, DrumKit& kit); virtual int parseFile(const std::string& filename) override; @@ -52,4 +52,5 @@ private: std::string instr_group; ConfigFile refs; + Settings& settings; }; 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*/ diff --git a/src/instrument.h b/src/instrument.h index e880d8d..549a5a4 100644 --- a/src/instrument.h +++ b/src/instrument.h @@ -36,46 +36,50 @@ #include "sample.h" #include "versionstr.h" +#include "settings.h" + class InstrumentParser; class Instrument { - friend class InstrumentParser; + friend class InstrumentParser; public: - Instrument(); - ~Instrument(); + Instrument(Settings& settings); + ~Instrument(); - Sample *sample(level_t level, size_t pos); + Sample *sample(level_t level, size_t pos); - std::string name(); - std::string description(); - std::string group(); + std::string name(); + std::string description(); + std::string group(); - void setGroup(std::string group); + void setGroup(std::string group); - // std::map channelmap; + // std::map channelmap; - std::vector audiofiles; + std::vector audiofiles; - bool isValid(); + bool isValid(); private: - void *magic; + void *magic; + + std::string _group; + std::string _name; + std::string _description; - std::string _group; - std::string _name; - std::string _description; + VersionStr version; - VersionStr version; + RangeMap samples; + PowerList powerlist; - RangeMap samples; - PowerList powerlist; + void addSample(level_t a, level_t b, Sample *s); + void finalise(); ///< Signal instrument that no more samples will be added. - void addSample(level_t a, level_t b, Sample *s); - void finalise(); ///< Signal instrument that no more samples will be added. + std::vector samplelist; - std::vector samplelist; + size_t lastpos; + float mod; - size_t lastpos; - float mod; + Settings& settings; }; //typedef std::map< std::string, Instrument > Instruments; diff --git a/src/settings.h b/src/settings.h index 74c432d..4eb7dba 100644 --- a/src/settings.h +++ b/src/settings.h @@ -27,14 +27,40 @@ #pragma once #include +#include #include +class MyString { +public: + std::string value; +}; + +//! Engine settings +struct Settings +{ + std::atomic enable_velocity_modifier; + std::atomic velocity_modifier_falloff; + std::atomic velocity_modifier_weight; + + std::atomic enable_velocity_randomiser; + std::atomic velocity_randomiser_weight; + + std::atomic samplerate; + + std::atomic enable_resampling; + + std::atomic number_of_files; + std::atomic number_of_files_loaded; + //std::atomic current_file; + +}; + +//! Getter utility class. template class SettingRef { public: SettingRef(std::atomic& value) - : value{value} - , cache{} + : value(value) { // string isn't lock free either assert((std::is_same::value || value.is_lock_free())); @@ -57,20 +83,7 @@ private: std::atomic cache; }; -struct Settings -{ - std::atomic enable_velocity_modifier; - std::atomic velocity_modifier_falloff; - std::atomic velocity_modifier_weight; - - std::atomic enable_velocity_randomiser; - std::atomic velocity_randomiser_weight; - - std::atomic samplerate; - - std::atomic enable_resampling; -}; - +//! Combined getter class. struct SettingsGetter { SettingRef enable_velocity_modifier; @@ -80,10 +93,14 @@ struct SettingsGetter SettingRef enable_velocity_randomiser; SettingRef velocity_randomiser_weight; - SettingRef samplerate; + SettingRef samplerate; SettingRef enable_resampling; + SettingRef number_of_files; + SettingRef number_of_files_loaded; + //SettingRef current_file; + SettingsGetter(Settings& settings) : enable_velocity_modifier{settings.enable_velocity_modifier} , velocity_modifier_falloff{settings.velocity_modifier_falloff} @@ -92,6 +109,9 @@ struct SettingsGetter , velocity_randomiser_weight{settings.velocity_randomiser_weight} , samplerate{settings.samplerate} , enable_resampling{settings.enable_resampling} + , number_of_files{settings.number_of_files} + , number_of_files_loaded{settings.number_of_files_loaded} + //, current_file{settings.current_file} { } }; @@ -99,29 +119,35 @@ struct SettingsGetter // lovely reminder: NO, GLOCKE. NOOOO!! /* enum class IntParams { - Foo = 0 + Foo = 0 }; -struct Settings { - std::array, 5> ints; +struct Settings +{ + std::array, 5> ints; - Settings() - : ints{} { - //get(IntParams::Foo).store(3); - } + Settings() + : ints{} + { + //get(IntParams::Foo).store(3); + } - std::atomic& get(IntParams param) { - return ints[(size_t)param]; - } + std::atomic& get(IntParams param) + { + return ints[(size_t)param]; + } }; -struct SettingsGetter { - std::vector> ints; +struct SettingsGetter +{ + std::vector> ints; - SettingsGetter(Settings& parent) { - for (auto& atomic: parent.ints) { - ints.emplace_back(atomic); - } - } + SettingsGetter(Settings& parent) + { + for(auto& atomic: parent.ints) + { + ints.emplace_back(atomic); + } + } }; */ -- cgit v1.2.3