From 2f203fe1635d216e1ee6ff5bda714d55fd49d8b1 Mon Sep 17 00:00:00 2001 From: paulfd Date: Sun, 25 Aug 2019 00:46:31 +0200 Subject: [PATCH] Cosmetics --- sources/FilePool.cpp | 4 +-- sources/Helpers.h | 67 ++++++++++++++++++++++--------------------- sources/Parser.cpp | 44 ++++++++++++++-------------- sources/Region.cpp | 52 +++++++++++++++++++++------------ sources/SIMDDummy.cpp | 6 ---- sources/Voice.h | 5 ++-- 6 files changed, 94 insertions(+), 84 deletions(-) diff --git a/sources/FilePool.cpp b/sources/FilePool.cpp index a776488b..a8a62a83 100644 --- a/sources/FilePool.cpp +++ b/sources/FilePool.cpp @@ -49,14 +49,14 @@ void sfz::FilePool::loadingThread() DBG("Background thread error: voice is null."); continue; } - + DBG("Background loading of: " << fileToLoad.sample); std::filesystem::path file { rootDirectory / fileToLoad.sample }; if (!std::filesystem::exists(file)) { DBG("Background thread: no file " << fileToLoad.sample << " exists."); continue; } - + SndfileHandle sndFile(reinterpret_cast(file.c_str())); auto fileLoaded = std::make_unique>(fileToLoad.numFrames); auto readBuffer = std::make_unique>(fileToLoad.numFrames * 2); diff --git a/sources/Helpers.h b/sources/Helpers.h index 4cf48a6d..058c4a5f 100644 --- a/sources/Helpers.h +++ b/sources/Helpers.h @@ -3,14 +3,17 @@ #include #include -inline void trimInPlace(std::string_view& s) +inline void trimInPlace(std::string_view &s) { const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v"); - if (leftPosition != s.npos) { + if (leftPosition != s.npos) + { s.remove_prefix(leftPosition); const auto rightPosition = s.find_last_not_of(" \r\t\n\f\v"); s.remove_suffix(s.size() - rightPosition - 1); - } else { + } + else + { s.remove_suffix(s.size()); } } @@ -18,11 +21,14 @@ inline void trimInPlace(std::string_view& s) inline std::string_view trim(std::string_view s) { const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v"); - if (leftPosition != s.npos) { + if (leftPosition != s.npos) + { s.remove_prefix(leftPosition); const auto rightPosition = s.find_last_not_of(" \r\t\n\f\v"); s.remove_suffix(s.size() - rightPosition - 1); - } else { + } + else + { s.remove_suffix(s.size()); } return s; @@ -30,7 +36,7 @@ inline std::string_view trim(std::string_view s) inline constexpr unsigned int Fnv1aBasis = 0x811C9DC5; inline constexpr unsigned int Fnv1aPrime = 0x01000193; -inline constexpr unsigned int hash(const char* s, unsigned int h = Fnv1aBasis) +inline constexpr unsigned int hash(const char *s, unsigned int h = Fnv1aBasis) { return !*s ? h : hash(s + 1, static_cast((h ^ *s) * static_cast(Fnv1aPrime))); } @@ -106,10 +112,11 @@ inline constexpr Type mag2db(Type in) return static_cast(20.0) * std::log10(in); } -namespace Random { +namespace Random +{ static inline std::random_device randomDevice; -static inline std::mt19937 randomGenerator { randomDevice() }; -} +static inline std::mt19937 randomGenerator{randomDevice()}; +} // namespace Random inline float midiNoteFrequency(const int noteNumber) { @@ -117,64 +124,58 @@ inline float midiNoteFrequency(const int noteNumber) } template -constexpr Type pi { 3.141592653589793238462643383279502884 }; +constexpr Type pi{3.141592653589793238462643383279502884}; template -constexpr Type twoPi { 2 * pi }; +constexpr Type twoPi{2 * pi}; template -constexpr Type piTwo { pi / 2 }; +constexpr Type piTwo{pi / 2}; #include template -class LeakDetector { +class LeakDetector +{ public: LeakDetector() { - // auto currentCounter = objectCounter.count.load(); - // auto desiredCounter = currentCounter + 1; - // while(!objectCounter.count.compare_exchange_weak(currentCounter, desiredCounter)) - // desiredCounter = currentCounter + 1; objectCounter.count++; - // DBG("Counted " << desiredCounter << " " << Owner::getClassName()); } - LeakDetector(const LeakDetector&) + LeakDetector(const LeakDetector &) { objectCounter.count++; } ~LeakDetector() { objectCounter.count--; - // auto currentCounter = objectCounter.count.load(); - // auto desiredCounter = currentCounter - 1; - // while(!objectCounter.count.compare_exchange_weak(currentCounter, desiredCounter)) - // desiredCounter = currentCounter - 1; - // DBG("Counted " << desiredCounter << " " << Owner::getClassName() << " left after deletion"); - if (objectCounter.count.load() < 0) { + if (objectCounter.count.load() < 0) + { DBG("Deleted a dangling pointer for class " << Owner::getClassName()); // Deleted a dangling pointer! - // ASSERTFALSE; + ASSERTFALSE; } } private: - struct ObjectCounter { + struct ObjectCounter + { ObjectCounter() = default; ~ObjectCounter() { - if (auto residualCount = count.load() > 0) { + if (auto residualCount = count.load() > 0) + { DBG("Leaked " << residualCount << " instance(s) of class " << Owner::getClassName()); // Leaked ojects - // ASSERTFALSE; + ASSERTFALSE; } }; - std::atomic count { 0 }; + std::atomic count{0}; }; static inline ObjectCounter objectCounter; }; #ifndef NDEBUG -#define LEAK_DETECTOR(Class) \ - friend class LeakDetector; \ - static const char* getClassName() noexcept { return #Class; } \ +#define LEAK_DETECTOR(Class) \ + friend class LeakDetector; \ + static const char *getClassName() { return #Class; } \ LeakDetector leakDetector; #else #define LEAK_DETECTOR(Class) diff --git a/sources/Parser.cpp b/sources/Parser.cpp index 71146e21..01be13cf 100644 --- a/sources/Parser.cpp +++ b/sources/Parser.cpp @@ -8,38 +8,38 @@ using svregex_iterator = std::regex_iterator; using svmatch_results = std::match_results; -void removeCommentOnLine(std::string_view& line) +void removeCommentOnLine(std::string_view &line) { if (auto position = line.find("//"); position != line.npos) line.remove_suffix(line.size() - position); } -bool sfz::Parser::loadSfzFile(const std::filesystem::path& file) +bool sfz::Parser::loadSfzFile(const std::filesystem::path &file) { - const auto sfzFile = file.is_absolute() ? file : rootDirectory / file; + const auto sfzFile = file.is_absolute() ? file : rootDirectory / file; if (!std::filesystem::exists(sfzFile)) return false; - rootDirectory = file.parent_path(); + rootDirectory = file.parent_path(); std::vector lines; readSfzFile(file, lines); - aggregatedContent = absl::StrJoin(lines, " "); - const std::string_view aggregatedView { aggregatedContent }; + aggregatedContent = absl::StrJoin(lines, " "); + const std::string_view aggregatedView{aggregatedContent}; - svregex_iterator headerIterator(aggregatedView.cbegin(), aggregatedView.cend(), sfz::Regexes::headers); + svregex_iterator headerIterator(aggregatedView.cbegin(), aggregatedView.cend(), sfz::Regexes::headers); const auto regexEnd = svregex_iterator(); - std::vector currentMembers; + std::vector currentMembers; - for (; headerIterator != regexEnd; ++headerIterator) - { + for (; headerIterator != regexEnd; ++headerIterator) + { svmatch_results headerMatch = *headerIterator; // Can't use uniform initialization here because it generates narrowing conversions const std::string_view header(&*headerMatch[1].first, headerMatch[1].length()); const std::string_view members(&*headerMatch[2].first, headerMatch[2].length()); - auto paramIterator = svregex_iterator (members.cbegin(), members.cend(), sfz::Regexes::members); + auto paramIterator = svregex_iterator(members.cbegin(), members.cend(), sfz::Regexes::members); // Store or handle members for (; paramIterator != regexEnd; ++paramIterator) @@ -47,16 +47,16 @@ bool sfz::Parser::loadSfzFile(const std::filesystem::path& file) const svmatch_results paramMatch = *paramIterator; const std::string_view opcode(&*paramMatch[1].first, paramMatch[1].length()); const std::string_view value(&*paramMatch[2].first, paramMatch[2].length()); - currentMembers.emplace_back(opcode, value); + currentMembers.emplace_back(opcode, value); } callback(header, currentMembers); - currentMembers.clear(); + currentMembers.clear(); } - return true; + return true; } -void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector& lines) noexcept +void sfz::Parser::readSfzFile(const std::filesystem::path &fileName, std::vector &lines) noexcept { std::ifstream fileStream(fileName.c_str()); if (!fileStream) @@ -69,7 +69,7 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector std::string tmpString; while (std::getline(fileStream, tmpString)) { - std::string_view tmpView { tmpString }; + std::string_view tmpView{tmpString}; removeCommentOnLine(tmpView); trimInPlace(tmpView); @@ -82,7 +82,7 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector { auto includePath = includeMatch.str(1); std::replace(includePath.begin(), includePath.end(), '\\', '/'); - const auto newFile = rootDirectory / includePath; + const auto newFile = rootDirectory / includePath; auto alreadyIncluded = std::find(includedFiles.begin(), includedFiles.end(), newFile); if (std::filesystem::exists(newFile)) { @@ -110,13 +110,13 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector std::string newString; newString.reserve(tmpView.length()); std::string::size_type lastPos = 0; - std::string::size_type findPos = tmpView.find(sfz::config::defineCharacter, lastPos); + std::string::size_type findPos = tmpView.find(sfz::config::defineCharacter, lastPos); - while(findPos < tmpView.npos) + while (findPos < tmpView.npos) { newString.append(tmpView, lastPos, findPos - lastPos); - for (auto& definePair: defines) + for (auto &definePair : defines) { std::string_view candidate = tmpView.substr(findPos, definePair.first.length()); if (candidate == definePair.first) @@ -126,7 +126,7 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector break; } } - + if (lastPos <= findPos) { newString += sfz::config::defineCharacter; @@ -138,6 +138,6 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector // Copy the rest of the string newString += tmpView.substr(lastPos); - lines.push_back(std::move(newString)); + lines.push_back(std::move(newString)); } } \ No newline at end of file diff --git a/sources/Region.cpp b/sources/Region.cpp index d491b71c..467c2739 100644 --- a/sources/Region.cpp +++ b/sources/Region.cpp @@ -4,26 +4,27 @@ #include #include -bool sfz::Region::parseOpcode(const Opcode& opcode) +bool sfz::Region::parseOpcode(const Opcode &opcode) { - switch (hash(opcode.opcode)) { + switch (hash(opcode.opcode)) + { // Sound source: sample playback case hash("sample"): - sample = absl::StrReplaceAll(trim(opcode.value), { { "\\", "/" } }); + sample = absl::StrReplaceAll(trim(opcode.value), {{"\\", "/"}}); break; case hash("delay"): setValueFromOpcode(opcode, delay, Default::delayRange); break; case hash("delay_random"): setValueFromOpcode(opcode, delayRandom, Default::delayRange); - delayDistribution.param( std::uniform_real_distribution::param_type(0, delayRandom) ); + delayDistribution.param(std::uniform_real_distribution::param_type(0, delayRandom)); break; case hash("offset"): setValueFromOpcode(opcode, offset, Default::offsetRange); break; case hash("offset_random"): setValueFromOpcode(opcode, offsetRandom, Default::offsetRange); - offsetDistribution.param( std::uniform_int_distribution::param_type(0, offsetRandom) ); + offsetDistribution.param(std::uniform_int_distribution::param_type(0, offsetRandom)); break; case hash("end"): setValueFromOpcode(opcode, sampleEnd, Default::sampleEndRange); @@ -33,7 +34,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) break; case hash("loopmode"): case hash("loop_mode"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("no_loop"): loopMode = SfzLoopMode::no_loop; break; @@ -68,7 +70,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) setValueFromOpcode(opcode, offBy, Default::groupRange); break; case hash("off_mode"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("fast"): offMode = SfzOffMode::fast; break; @@ -112,7 +115,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) setRangeEndFromOpcode(opcode, bendRange, Default::bendRange); break; case hash("locc"): - if (opcode.parameter) { + if (opcode.parameter) + { setRangeStartFromOpcode(opcode, ccConditions[*opcode.parameter], Default::ccRange); } break; @@ -142,7 +146,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) previousKeySwitched = false; break; case hash("sw_vel"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("current"): velocityOverride = SfzVelocityOverride::current; break; @@ -181,7 +186,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) break; // Region logic: triggers case hash("trigger"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("attack"): trigger = SfzTrigger::attack; break; @@ -257,7 +263,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) gainDistribution.param(std::uniform_real_distribution::param_type(-ampRandom, ampRandom)); break; case hash("amp_velcurve_"): - if (opcode.parameter && Default::ccRange.containsWithEnd(*opcode.parameter)) { + if (opcode.parameter && Default::ccRange.containsWithEnd(*opcode.parameter)) + { if (auto value = readOpcode(opcode.value, Default::ampVelcurveRange); value) velocityPoints.emplace_back(*opcode.parameter, *value); } @@ -287,7 +294,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) setRangeEndFromOpcode(opcode, crossfadeVelOutRange, Default::velocityRange); break; case hash("xf_keycurve"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("power"): crossfadeKeyCurve = SfzCrossfadeCurve::power; break; @@ -299,7 +307,8 @@ bool sfz::Region::parseOpcode(const Opcode& opcode) } break; case hash("xf_velcurve"): - switch (hash(opcode.value)) { + switch (hash(opcode.value)) + { case hash("power"): crossfadeVelCurve = SfzCrossfadeCurve::power; break; @@ -414,8 +423,10 @@ bool sfz::Region::registerNoteOn(int channel, int noteNumber, uint8_t velocity, if (!chanOk) return false; - if (keyswitchRange.containsWithEnd(noteNumber)) { - if (keyswitch) { + if (keyswitchRange.containsWithEnd(noteNumber)) + { + if (keyswitch) + { if (*keyswitch == noteNumber) keySwitched = true; else @@ -430,7 +441,8 @@ bool sfz::Region::registerNoteOn(int channel, int noteNumber, uint8_t velocity, } const bool keyOk = keyRange.containsWithEnd(noteNumber); - if (keyOk) { + if (keyOk) + { // Update the number of notes playing for the region activeNotesInRange++; @@ -445,7 +457,8 @@ bool sfz::Region::registerNoteOn(int channel, int noteNumber, uint8_t velocity, if (trigger == SfzTrigger::release_key || velocityOverride == SfzVelocityOverride::previous) lastNoteVelocities[noteNumber] = velocity; - if (previousNote) { + if (previousNote) + { if (*previousNote == noteNumber) previousKeySwitched = true; else @@ -468,13 +481,14 @@ bool sfz::Region::registerNoteOn(int channel, int noteNumber, uint8_t velocity, return keyOk && velOk && chanOk && randOk && (attackTrigger || firstLegatoNote || notFirstLegatoNote); } -bool sfz::Region::registerNoteOff(int channel, int noteNumber, uint8_t velocity [[maybe_unused]], float randValue) +bool sfz::Region::registerNoteOff(int channel, int noteNumber, uint8_t velocity[[maybe_unused]], float randValue) { const bool chanOk = channelRange.containsWithEnd(channel); if (!chanOk) return false; - if (keyswitchRange.containsWithEnd(noteNumber)) { + if (keyswitchRange.containsWithEnd(noteNumber)) + { if (keyswitchDown && *keyswitchDown == noteNumber) keySwitched = false; diff --git a/sources/SIMDDummy.cpp b/sources/SIMDDummy.cpp index 85b10314..e8236686 100644 --- a/sources/SIMDDummy.cpp +++ b/sources/SIMDDummy.cpp @@ -13,12 +13,6 @@ void writeInterleaved(absl::Span inputLeft, absl::Span writeInterleaved(inputLeft, inputRight, output); } -// template -// void linearRamp(absl::Span output, Type start, Type step); - -// template -// void exponentialRamp(absl::Span output, Type start, Type step); - template<> void fill(absl::Span output, float value) noexcept { diff --git a/sources/Voice.h b/sources/Voice.h index 2a658072..d619294e 100644 --- a/sources/Voice.h +++ b/sources/Voice.h @@ -133,6 +133,7 @@ public: void fillWithData(StereoSpan buffer) { const StereoSpan source([&]() -> StereoBuffer& { + // TODO: shouldn't need to check fileData here, something is a bit strange... if (dataReady.load(std::memory_order_seq_cst) && fileData != nullptr) return *fileData; else @@ -191,12 +192,12 @@ public: void reset() { + dataReady.store(false); + state = State::idle; if (region != nullptr) { DBG("Reset voice with sample " << region->sample); } region = nullptr; - state = State::idle; - dataReady.store(false); noteIsOff = false; }