diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-23 22:38:44 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2016-03-23 22:38:44 +0100 |
commit | 569636bbd766710f5ac60a66c402acc70cdf77a8 (patch) | |
tree | 95712842bb1550c879796ce8444eaecf0e3741e9 /src | |
parent | 571e5a5feab478f6481db96343dfaf566bd2ab6d (diff) |
Settings.
Diffstat (limited to 'src')
-rw-r--r-- | src/drumgizmo.cc | 10 | ||||
-rw-r--r-- | src/drumgizmo.h | 8 | ||||
-rw-r--r-- | src/drumkitloader.cc | 16 | ||||
-rw-r--r-- | src/drumkitloader.h | 4 | ||||
-rw-r--r-- | src/drumkitparser.cc | 7 | ||||
-rw-r--r-- | src/drumkitparser.h | 3 | ||||
-rw-r--r-- | src/instrument.cc | 205 | ||||
-rw-r--r-- | src/instrument.h | 50 | ||||
-rw-r--r-- | src/settings.h | 94 |
9 files changed, 218 insertions, 179 deletions
diff --git a/src/drumgizmo.cc b/src/drumgizmo.cc index 2599f9f..6ca3dc8 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 55f4590..5d40917 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<event_t> events; + Settings& settings; }; diff --git a/src/drumkitloader.cc b/src/drumkitloader.cc index 82b177b..33ddd05 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 8819d71..320677a 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 f6dd553..f7737a0 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 98ea875..6b9a470 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 463f10d..a6015f7 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<AudioFile*>::iterator i = audiofiles.begin(); - while(i != audiofiles.end()) { - delete *i; - i++; - } + magic = NULL; + + DEBUG(instrument, "delete %p\n", this); + std::vector<AudioFile*>::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<Sample*> 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<Sample*> 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<Sample*>::iterator s = samplelist.begin(); - while(s != samplelist.end()) { - powerlist.add(*s); - s++; - } - - powerlist.finalise(); - } + if(version >= VersionStr("2.0")) + { + std::vector<Sample*>::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 07aba9c..2a70a95 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<std::string, std::string> channelmap; + // std::map<std::string, std::string> channelmap; - std::vector<AudioFile*> audiofiles; + std::vector<AudioFile*> 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<level_t, Sample*> samples; + PowerList powerlist; - RangeMap<level_t, Sample*> 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<Sample*> samplelist; - std::vector<Sample*> 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 6396fc4..7cf5282 100644 --- a/src/settings.h +++ b/src/settings.h @@ -27,14 +27,40 @@ #pragma once #include <atomic> +#include <string> #include <cassert> +class MyString { +public: + std::string value; +}; + +//! Engine settings +struct Settings +{ + std::atomic<bool> enable_velocity_modifier; + std::atomic<float> velocity_modifier_falloff; + std::atomic<float> velocity_modifier_weight; + + std::atomic<bool> enable_velocity_randomiser; + std::atomic<float> velocity_randomiser_weight; + + std::atomic<double> samplerate; + + std::atomic<bool> enable_resampling; + + std::atomic<int> number_of_files; + std::atomic<int> number_of_files_loaded; + //std::atomic<std::string> current_file; + +}; + +//! Getter utility class. template <typename T> class SettingRef { public: SettingRef(std::atomic<T>& value) - : value{value} - , cache{} + : value(value) { // string isn't lock free either assert((std::is_same<T, std::string>::value || value.is_lock_free())); @@ -57,20 +83,7 @@ private: std::atomic<T> cache; }; -struct Settings -{ - std::atomic<bool> enable_velocity_modifier; - std::atomic<float> velocity_modifier_falloff; - std::atomic<float> velocity_modifier_weight; - - std::atomic<bool> enable_velocity_randomiser; - std::atomic<float> velocity_randomiser_weight; - - std::atomic<int> samplerate; - - std::atomic<bool> enable_resampling; -}; - +//! Combined getter class. struct SettingsGetter { SettingRef<bool> enable_velocity_modifier; @@ -80,10 +93,14 @@ struct SettingsGetter SettingRef<bool> enable_velocity_randomiser; SettingRef<float> velocity_randomiser_weight; - SettingRef<int> samplerate; + SettingRef<double> samplerate; SettingRef<bool> enable_resampling; + SettingRef<int> number_of_files; + SettingRef<int> number_of_files_loaded; + //SettingRef<std::string> 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<std::atomic<int>, 5> ints; +struct Settings +{ + std::array<std::atomic<int>, 5> ints; - Settings() - : ints{} { - //get(IntParams::Foo).store(3); - } + Settings() + : ints{} + { + //get(IntParams::Foo).store(3); + } - std::atomic<int>& get(IntParams param) { - return ints[(size_t)param]; - } + std::atomic<int>& get(IntParams param) + { + return ints[(size_t)param]; + } }; -struct SettingsGetter { - std::vector<SettingRef<int>> ints; +struct SettingsGetter +{ + std::vector<SettingRef<int>> 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); + } + } }; */ |