WIP fixes: replace optional, string_view, nullopt with abseil versions, temporary clamp alternative.

This commit is contained in:
redtide 2019-09-18 20:33:08 +02:00
parent 74ff0ea433
commit 024ac4d832
21 changed files with 221 additions and 177 deletions

View file

@ -37,7 +37,7 @@ public:
int getNumMasters() const noexcept { return numMasters; } int getNumMasters() const noexcept { return numMasters; }
int getNumCurves() const noexcept { return numCurves; } int getNumCurves() const noexcept { return numCurves; }
protected: protected:
void callback(std::string_view header, const std::vector<sfz::Opcode>& members [[maybe_unused]]) final void callback(absl::string_view header, const std::vector<sfz::Opcode>& members [[maybe_unused]]) final
{ {
switch (hash(header)) switch (hash(header))
{ {

View file

@ -24,7 +24,7 @@
#include "ADSREnvelope.h" #include "ADSREnvelope.h"
#include "Config.h" #include "Config.h"
#include "SIMDHelpers.h" #include "SIMDHelpers.h"
#include <algorithm> #include "compat/algorithm.h"
namespace sfz { namespace sfz {

View file

@ -72,7 +72,8 @@ public:
paddedData = static_cast<pointer>(newData); paddedData = static_cast<pointer>(newData);
normalData = static_cast<pointer>(std::align(Alignment, alignedSize, newData, tempSize)); normalData = static_cast<pointer>(std::align(Alignment, alignedSize, newData, tempSize));
normalEnd = normalData + alignedSize; normalEnd = normalData + alignedSize;
if (auto endMisalignment = (alignedSize & TypeAlignmentMask); endMisalignment != 0) auto endMisalignment = (alignedSize & TypeAlignmentMask);
if (endMisalignment != 0)
_alignedEnd = normalEnd + Alignment - endMisalignment; _alignedEnd = normalEnd + Alignment - endMisalignment;
else else
_alignedEnd = normalEnd; _alignedEnd = normalEnd;

View file

@ -39,114 +39,114 @@ namespace Default
// The categories match http://sfzformat.com/ // The categories match http://sfzformat.com/
// ******* SFZ 1 ******* // ******* SFZ 1 *******
// Sound source: sample playback // Sound source: sample playback
inline constexpr float delay { 0.0 }; constexpr float delay { 0.0 };
inline constexpr float delayRandom { 0.0 }; constexpr float delayRandom { 0.0 };
inline constexpr Range<float> delayRange { 0.0, 100.0 }; constexpr Range<float> delayRange { 0.0, 100.0 };
inline constexpr uint32_t offset { 0 }; constexpr uint32_t offset { 0 };
inline constexpr uint32_t offsetRandom { 0 }; constexpr uint32_t offsetRandom { 0 };
inline constexpr Range<uint32_t> offsetRange { 0, std::numeric_limits<uint32_t>::max() }; constexpr Range<uint32_t> offsetRange { 0, std::numeric_limits<uint32_t>::max() };
inline constexpr Range<uint32_t> sampleEndRange { 0, std::numeric_limits<uint32_t>::max() }; constexpr Range<uint32_t> sampleEndRange { 0, std::numeric_limits<uint32_t>::max() };
inline constexpr Range<uint32_t> sampleCountRange { 0, std::numeric_limits<uint32_t>::max() }; constexpr Range<uint32_t> sampleCountRange { 0, std::numeric_limits<uint32_t>::max() };
inline constexpr SfzLoopMode loopMode { SfzLoopMode::no_loop }; constexpr SfzLoopMode loopMode { SfzLoopMode::no_loop };
inline constexpr Range<uint32_t> loopRange { 0, std::numeric_limits<uint32_t>::max() }; constexpr Range<uint32_t> loopRange { 0, std::numeric_limits<uint32_t>::max() };
// Instrument setting: voice lifecycle // Instrument setting: voice lifecycle
inline constexpr uint32_t group { 0 }; constexpr uint32_t group { 0 };
inline constexpr Range<uint32_t> groupRange { 0, std::numeric_limits<uint32_t>::max() }; constexpr Range<uint32_t> groupRange { 0, std::numeric_limits<uint32_t>::max() };
inline constexpr SfzOffMode offMode { SfzOffMode::fast }; constexpr SfzOffMode offMode { SfzOffMode::fast };
// Region logic: key mapping // Region logic: key mapping
inline constexpr Range<uint8_t> keyRange { 0, 127 }; constexpr Range<uint8_t> keyRange { 0, 127 };
inline constexpr Range<uint8_t> velocityRange { 0, 127 }; constexpr Range<uint8_t> velocityRange { 0, 127 };
// Region logic: MIDI conditions // Region logic: MIDI conditions
inline constexpr Range<uint8_t> channelRange { 1, 16 }; constexpr Range<uint8_t> channelRange { 1, 16 };
inline constexpr Range<uint8_t> ccRange { 0, 127 }; constexpr Range<uint8_t> ccRange { 0, 127 };
inline constexpr uint8_t cc { 0 }; constexpr uint8_t cc { 0 };
inline constexpr Range<int> bendRange { -8192, 8192 }; constexpr Range<int> bendRange { -8192, 8192 };
inline constexpr int bend { 0 }; constexpr int bend { 0 };
inline constexpr SfzVelocityOverride velocityOverride { SfzVelocityOverride::current }; constexpr SfzVelocityOverride velocityOverride { SfzVelocityOverride::current };
// Region logic: internal conditions // Region logic: internal conditions
inline constexpr Range<float> randRange { 0.0, 1.0 }; constexpr Range<float> randRange { 0.0, 1.0 };
inline constexpr Range<uint8_t> aftertouchRange { 0, 127 }; constexpr Range<uint8_t> aftertouchRange { 0, 127 };
inline constexpr uint8_t aftertouch { 0 }; constexpr uint8_t aftertouch { 0 };
inline constexpr Range<float> bpmRange { 0.0, 500.0 }; constexpr Range<float> bpmRange { 0.0, 500.0 };
inline constexpr float bpm { 120.0 }; constexpr float bpm { 120.0 };
inline constexpr uint8_t sequenceLength{ 1 }; constexpr uint8_t sequenceLength{ 1 };
inline constexpr uint8_t sequencePosition{ 1 }; constexpr uint8_t sequencePosition{ 1 };
inline constexpr Range<uint8_t> sequenceRange { 1, 100 }; constexpr Range<uint8_t> sequenceRange { 1, 100 };
// Region logic: Triggers // Region logic: Triggers
inline constexpr SfzTrigger trigger { SfzTrigger::attack }; constexpr SfzTrigger trigger { SfzTrigger::attack };
inline constexpr Range<uint8_t> ccTriggerValueRange{ 0, 127 }; constexpr Range<uint8_t> ccTriggerValueRange{ 0, 127 };
// Performance parameters: amplifier // Performance parameters: amplifier
inline constexpr float volume { -3.0 }; constexpr float volume { -3.0 };
inline constexpr Range<float> volumeRange { -144.0, 6.0 }; constexpr Range<float> volumeRange { -144.0, 6.0 };
inline constexpr Range<float> volumeCCRange { -144.0, 6.0 }; constexpr Range<float> volumeCCRange { -144.0, 6.0 };
inline constexpr float amplitude { 100.0 }; constexpr float amplitude { 100.0 };
inline constexpr Range<float> amplitudeRange { 0.0, 100.0 }; constexpr Range<float> amplitudeRange { 0.0, 100.0 };
inline constexpr float pan { 0.0 }; constexpr float pan { 0.0 };
inline constexpr Range<float> panRange { -100.0, 100.0 }; constexpr Range<float> panRange { -100.0, 100.0 };
inline constexpr Range<float> panCCRange { -200.0, 200.0 }; constexpr Range<float> panCCRange { -200.0, 200.0 };
inline constexpr float position { 0.0 }; constexpr float position { 0.0 };
inline constexpr Range<float> positionRange { -100.0, 100.0 }; constexpr Range<float> positionRange { -100.0, 100.0 };
inline constexpr Range<float> positionCCRange { -200.0, 200.0 }; constexpr Range<float> positionCCRange { -200.0, 200.0 };
inline constexpr float width { 0.0 }; constexpr float width { 0.0 };
inline constexpr Range<float> widthRange { -100.0, 100.0 }; constexpr Range<float> widthRange { -100.0, 100.0 };
inline constexpr Range<float> widthCCRange { -200.0, 200.0 }; constexpr Range<float> widthCCRange { -200.0, 200.0 };
inline constexpr uint8_t ampKeycenter { 60 }; constexpr uint8_t ampKeycenter { 60 };
inline constexpr float ampKeytrack { 0.0 }; constexpr float ampKeytrack { 0.0 };
inline constexpr Range<float> ampKeytrackRange { -96, 12 }; constexpr Range<float> ampKeytrackRange { -96, 12 };
inline constexpr float ampVeltrack { 100.0 }; constexpr float ampVeltrack { 100.0 };
inline constexpr Range<float> ampVeltrackRange { -100.0, 100.0 }; constexpr Range<float> ampVeltrackRange { -100.0, 100.0 };
inline constexpr Range<float> ampVelcurveRange { 0.0, 1.0 }; constexpr Range<float> ampVelcurveRange { 0.0, 1.0 };
inline constexpr float ampRandom { 0.0 }; constexpr float ampRandom { 0.0 };
inline constexpr Range<float> ampRandomRange { 0.0, 24.0 }; constexpr Range<float> ampRandomRange { 0.0, 24.0 };
inline constexpr Range<uint8_t> crossfadeKeyInRange { 0, 0 }; constexpr Range<uint8_t> crossfadeKeyInRange { 0, 0 };
inline constexpr Range<uint8_t> crossfadeKeyOutRange { 127, 127 }; constexpr Range<uint8_t> crossfadeKeyOutRange { 127, 127 };
inline constexpr Range<uint8_t> crossfadeVelInRange { 0, 0 }; constexpr Range<uint8_t> crossfadeVelInRange { 0, 0 };
inline constexpr Range<uint8_t> crossfadeVelOutRange { 127, 127 }; constexpr Range<uint8_t> crossfadeVelOutRange { 127, 127 };
inline constexpr Range<uint8_t> crossfadeCCInRange { 0, 0 }; constexpr Range<uint8_t> crossfadeCCInRange { 0, 0 };
inline constexpr Range<uint8_t> crossfadeCCOutRange { 127, 127 }; constexpr Range<uint8_t> crossfadeCCOutRange { 127, 127 };
inline constexpr SfzCrossfadeCurve crossfadeKeyCurve { SfzCrossfadeCurve::power }; constexpr SfzCrossfadeCurve crossfadeKeyCurve { SfzCrossfadeCurve::power };
inline constexpr SfzCrossfadeCurve crossfadeVelCurve { SfzCrossfadeCurve::power }; constexpr SfzCrossfadeCurve crossfadeVelCurve { SfzCrossfadeCurve::power };
inline constexpr SfzCrossfadeCurve crossfadeCCCurve { SfzCrossfadeCurve::power }; constexpr SfzCrossfadeCurve crossfadeCCCurve { SfzCrossfadeCurve::power };
inline constexpr float rtDecay { 0.0f }; constexpr float rtDecay { 0.0f };
inline constexpr Range<float> rtDecayRange { 0.0f, 200.0f }; constexpr Range<float> rtDecayRange { 0.0f, 200.0f };
// Performance parameters: pitch // Performance parameters: pitch
inline constexpr uint8_t pitchKeycenter { 60 }; constexpr uint8_t pitchKeycenter { 60 };
inline constexpr int pitchKeytrack { 100 }; constexpr int pitchKeytrack { 100 };
inline constexpr Range<int> pitchKeytrackRange { -1200, 1200 }; constexpr Range<int> pitchKeytrackRange { -1200, 1200 };
inline constexpr int pitchRandom { 0 }; constexpr int pitchRandom { 0 };
inline constexpr Range<int> pitchRandomRange { 0, 9600 }; constexpr Range<int> pitchRandomRange { 0, 9600 };
inline constexpr int pitchVeltrack { 0 }; constexpr int pitchVeltrack { 0 };
inline constexpr Range<int> pitchVeltrackRange { -9600, 9600 }; constexpr Range<int> pitchVeltrackRange { -9600, 9600 };
inline constexpr int transpose { 0 }; constexpr int transpose { 0 };
inline constexpr Range<int> transposeRange { -127, 127 }; constexpr Range<int> transposeRange { -127, 127 };
inline constexpr int tune { 0 }; constexpr int tune { 0 };
inline constexpr Range<int> tuneRange { -100, 100 }; constexpr Range<int> tuneRange { -100, 100 };
// Envelope generators // Envelope generators
inline constexpr float attack { 0 }; constexpr float attack { 0 };
inline constexpr float decay { 0 }; constexpr float decay { 0 };
inline constexpr float delayEG { 0 }; constexpr float delayEG { 0 };
inline constexpr float hold { 0 }; constexpr float hold { 0 };
inline constexpr float release { 0 }; constexpr float release { 0 };
inline constexpr float start { 0.0 }; constexpr float start { 0.0 };
inline constexpr float sustain { 100.0 }; constexpr float sustain { 100.0 };
inline constexpr float vel2sustain { 0.0 }; constexpr float vel2sustain { 0.0 };
inline constexpr int depth { 0 }; constexpr int depth { 0 };
inline constexpr Range<float> egTimeRange { 0.0, 100.0 }; constexpr Range<float> egTimeRange { 0.0, 100.0 };
inline constexpr Range<float> egPercentRange { 0.0, 100.0 }; constexpr Range<float> egPercentRange { 0.0, 100.0 };
inline constexpr Range<int> egDepthRange { -12000, 12000 }; constexpr Range<int> egDepthRange { -12000, 12000 };
inline constexpr Range<float> egOnCCTimeRange { -100.0, 100.0 }; constexpr Range<float> egOnCCTimeRange { -100.0, 100.0 };
inline constexpr Range<float> egOnCCPercentRange { -100.0, 100.0 }; constexpr Range<float> egOnCCPercentRange { -100.0, 100.0 };
// ***** SFZ v2 ******** // ***** SFZ v2 ********
inline constexpr bool checkSustain { true }; // sustain_sw constexpr bool checkSustain { true }; // sustain_sw
inline constexpr bool checkSostenuto { true }; // sostenuto_sw constexpr bool checkSostenuto { true }; // sostenuto_sw
} }
} }

View file

@ -26,7 +26,7 @@
#include "Defaults.h" #include "Defaults.h"
#include "LeakDetector.h" #include "LeakDetector.h"
#include "SfzHelpers.h" #include "SfzHelpers.h"
#include <optional> #include <absl/types/optional.h>
namespace sfz namespace sfz
@ -55,13 +55,13 @@ struct EGDescription
float vel2sustain { Default::vel2sustain }; float vel2sustain { Default::vel2sustain };
int vel2depth { Default::depth }; int vel2depth { Default::depth };
std::optional<CCValuePair> ccAttack; absl::optional<CCValuePair> ccAttack;
std::optional<CCValuePair> ccDecay; absl::optional<CCValuePair> ccDecay;
std::optional<CCValuePair> ccDelay; absl::optional<CCValuePair> ccDelay;
std::optional<CCValuePair> ccHold; absl::optional<CCValuePair> ccHold;
std::optional<CCValuePair> ccRelease; absl::optional<CCValuePair> ccRelease;
std::optional<CCValuePair> ccStart; absl::optional<CCValuePair> ccStart;
std::optional<CCValuePair> ccSustain; absl::optional<CCValuePair> ccSustain;
float getAttack(const CCValueArray &ccValues, uint8_t velocity) const noexcept float getAttack(const CCValueArray &ccValues, uint8_t velocity) const noexcept
{ {

View file

@ -31,6 +31,7 @@
#include <mutex> #include <mutex>
#include <sndfile.hh> #include <sndfile.hh>
#include <thread> #include <thread>
#include <mutex>
using namespace std::chrono_literals; using namespace std::chrono_literals;
template <class T> template <class T>
@ -47,7 +48,7 @@ std::unique_ptr<AudioBuffer<T>> readFromFile(SndfileHandle& sndFile, int numFram
return std::move(returnedBuffer); return std::move(returnedBuffer);
} }
std::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(std::string_view filename, uint32_t offset) noexcept absl::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(absl::string_view filename, uint32_t offset) noexcept
{ {
std::filesystem::path file { rootDirectory / filename }; std::filesystem::path file { rootDirectory / filename };
if (!std::filesystem::exists(file)) if (!std::filesystem::exists(file))
@ -101,7 +102,7 @@ std::optional<sfz::FilePool::FileInformation> sfz::FilePool::getFileInformation(
return returnedValue; return returnedValue;
} }
void sfz::FilePool::enqueueLoading(Voice* voice, std::string_view sample, int numFrames, unsigned ticket) noexcept void sfz::FilePool::enqueueLoading(Voice* voice, absl::string_view sample, int numFrames, unsigned ticket) noexcept
{ {
if (!loadingQueue.try_enqueue({ voice, sample, numFrames, ticket })) { if (!loadingQueue.try_enqueue({ voice, sample, numFrames, ticket })) {
DBG("Problem enqueuing a file read for file " << sample); DBG("Problem enqueuing a file read for file " << sample);

View file

@ -31,7 +31,7 @@
#include "readerwriterqueue.h" #include "readerwriterqueue.h"
#include <absl/container/flat_hash_map.h> #include <absl/container/flat_hash_map.h>
#include <mutex> #include <mutex>
#include <optional> #include <absl/types/optional.h>
#include <string_view> #include <string_view>
#include <thread> #include <thread>
@ -60,14 +60,14 @@ public:
double sampleRate { config::defaultSampleRate }; double sampleRate { config::defaultSampleRate };
std::shared_ptr<AudioBuffer<float>> preloadedData; std::shared_ptr<AudioBuffer<float>> preloadedData;
}; };
std::optional<FileInformation> getFileInformation(std::string_view filename, uint32_t offset) noexcept; absl::optional<FileInformation> getFileInformation(absl::string_view filename, uint32_t offset) noexcept;
void enqueueLoading(Voice* voice, std::string_view sample, int numFrames, unsigned ticket) noexcept; void enqueueLoading(Voice* voice, absl::string_view sample, int numFrames, unsigned ticket) noexcept;
void clear(); void clear();
private: private:
std::filesystem::path rootDirectory; std::filesystem::path rootDirectory;
struct FileLoadingInformation { struct FileLoadingInformation {
Voice* voice; Voice* voice;
std::string_view sample; absl::string_view sample;
int numFrames; int numFrames;
unsigned ticket; unsigned ticket;
}; };
@ -80,7 +80,7 @@ private:
std::vector<std::shared_ptr<AudioBuffer<float>>> fileHandles; std::vector<std::shared_ptr<AudioBuffer<float>>> fileHandles;
std::mutex fileHandleMutex; std::mutex fileHandleMutex;
bool quitThread { false }; bool quitThread { false };
absl::flat_hash_map<std::string_view, std::shared_ptr<AudioBuffer<float>>> preloadedData; absl::flat_hash_map<absl::string_view, std::shared_ptr<AudioBuffer<float>>> preloadedData;
LEAK_DETECTOR(FilePool); LEAK_DETECTOR(FilePool);
}; };
} }

View file

@ -27,6 +27,7 @@
#include <absl/types/span.h> #include <absl/types/span.h>
#include <functional> #include <functional>
#include <type_traits> #include <type_traits>
#include <vector>
namespace sfz { namespace sfz {

View file

@ -24,13 +24,13 @@
#include "Opcode.h" #include "Opcode.h"
#include "StringViewHelpers.h" #include "StringViewHelpers.h"
sfz::Opcode::Opcode(std::string_view inputOpcode, std::string_view inputValue) sfz::Opcode::Opcode(absl::string_view inputOpcode, absl::string_view inputValue)
: opcode(inputOpcode) : opcode(inputOpcode)
, value(inputValue) , value(inputValue)
{ {
if (const auto lastCharIndex = inputOpcode.find_last_not_of("1234567890"); lastCharIndex != inputOpcode.npos) { if (const auto lastCharIndex = inputOpcode.find_last_not_of("1234567890"); lastCharIndex != inputOpcode.npos) {
int returnedValue; int returnedValue;
std::string_view parameterView = inputOpcode; absl::string_view parameterView = inputOpcode;
parameterView.remove_prefix(lastCharIndex + 1); parameterView.remove_prefix(lastCharIndex + 1);
if (absl::SimpleAtoi(parameterView, &returnedValue)) { if (absl::SimpleAtoi(parameterView, &returnedValue)) {
parameter = returnedValue; parameter = returnedValue;

View file

@ -27,7 +27,7 @@
#include "Range.h" #include "Range.h"
#include "SfzHelpers.h" #include "SfzHelpers.h"
#include "StringViewHelpers.h" #include "StringViewHelpers.h"
#include <optional> #include <absl/types/optional.h>
#include <string_view> #include <string_view>
// charconv support is still sketchy with clang/gcc so we use abseil's numbers // charconv support is still sketchy with clang/gcc so we use abseil's numbers
@ -36,16 +36,16 @@
namespace sfz { namespace sfz {
struct Opcode { struct Opcode {
Opcode() = delete; Opcode() = delete;
Opcode(std::string_view inputOpcode, std::string_view inputValue); Opcode(absl::string_view inputOpcode, absl::string_view inputValue);
std::string_view opcode {}; absl::string_view opcode {};
std::string_view value {}; absl::string_view value {};
// This is to handle the integer parameter of some opcodes // This is to handle the integer parameter of some opcodes
std::optional<uint8_t> parameter; absl::optional<uint8_t> parameter;
LEAK_DETECTOR(Opcode); LEAK_DETECTOR(Opcode);
}; };
template <class ValueType> template <class ValueType>
inline std::optional<ValueType> readOpcode(std::string_view value, const Range<ValueType>& validRange) inline absl::optional<ValueType> readOpcode(absl::string_view value, const Range<ValueType>& validRange)
{ {
if constexpr (std::is_integral<ValueType>::value) { if constexpr (std::is_integral<ValueType>::value) {
int64_t returnedValue; int64_t returnedValue;
@ -65,13 +65,13 @@ inline std::optional<ValueType> readOpcode(std::string_view value, const Range<V
} else { } else {
float returnedValue; float returnedValue;
if (!absl::SimpleAtof(value, &returnedValue)) if (!absl::SimpleAtof(value, &returnedValue))
return std::nullopt; return absl::nullopt;
return validRange.clamp(returnedValue); return validRange.clamp(returnedValue);
} }
} }
inline std::optional<bool> readBooleanFromOpcode(const Opcode& opcode) inline absl::optional<bool> readBooleanFromOpcode(const Opcode& opcode)
{ {
switch (hash(opcode.value)) { switch (hash(opcode.value)) {
case hash("off"): case hash("off"):
@ -94,7 +94,7 @@ inline void setValueFromOpcode(const Opcode& opcode, ValueType& target, const Ra
} }
template <class ValueType> template <class ValueType>
inline void setValueFromOpcode(const Opcode& opcode, std::optional<ValueType>& target, const Range<ValueType>& validRange) inline void setValueFromOpcode(const Opcode& opcode, absl::optional<ValueType>& target, const Range<ValueType>& validRange)
{ {
auto value = readOpcode(opcode.value, validRange); auto value = readOpcode(opcode.value, validRange);
if (!value) // Try and read a note rather than a number if (!value) // Try and read a note rather than a number
@ -124,7 +124,7 @@ inline void setRangeStartFromOpcode(const Opcode& opcode, Range<ValueType>& targ
} }
template <class ValueType> template <class ValueType>
inline void setCCPairFromOpcode(const Opcode& opcode, std::optional<CCValuePair>& target, const Range<ValueType>& validRange) inline void setCCPairFromOpcode(const Opcode& opcode, absl::optional<CCValuePair>& target, const Range<ValueType>& validRange)
{ {
auto value = readOpcode(opcode.value, validRange); auto value = readOpcode(opcode.value, validRange);
if (value && opcode.parameter && Default::ccRange.containsWithEnd(*opcode.parameter)) if (value && opcode.parameter && Default::ccRange.containsWithEnd(*opcode.parameter))

View file

@ -28,12 +28,13 @@
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
using svregex_iterator = std::regex_iterator<std::string_view::const_iterator>; using svregex_iterator = std::regex_iterator<absl::string_view::const_iterator>;
using svmatch_results = std::match_results<std::string_view::const_iterator>; using svmatch_results = std::match_results<absl::string_view::const_iterator>;
void removeCommentOnLine(std::string_view& line) void removeCommentOnLine(absl::string_view& line)
{ {
if (auto position = line.find("//"); position != line.npos) auto position = line.find("//");
if (position != line.npos)
line.remove_suffix(line.size() - position); line.remove_suffix(line.size() - position);
} }
@ -48,7 +49,7 @@ bool sfz::Parser::loadSfzFile(const std::filesystem::path& file)
readSfzFile(file, lines); readSfzFile(file, lines);
aggregatedContent = absl::StrJoin(lines, " "); aggregatedContent = absl::StrJoin(lines, " ");
const std::string_view aggregatedView { aggregatedContent }; const absl::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(); const auto regexEnd = svregex_iterator();
@ -59,15 +60,15 @@ bool sfz::Parser::loadSfzFile(const std::filesystem::path& file)
svmatch_results headerMatch = *headerIterator; svmatch_results headerMatch = *headerIterator;
// Can't use uniform initialization here because it generates narrowing conversions // Can't use uniform initialization here because it generates narrowing conversions
const std::string_view header(&*headerMatch[1].first, headerMatch[1].length()); const absl::string_view header(&*headerMatch[1].first, headerMatch[1].length());
const std::string_view members(&*headerMatch[2].first, headerMatch[2].length()); const absl::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 // Store or handle members
for (; paramIterator != regexEnd; ++paramIterator) { for (; paramIterator != regexEnd; ++paramIterator) {
const svmatch_results paramMatch = *paramIterator; const svmatch_results paramMatch = *paramIterator;
const std::string_view opcode(&*paramMatch[1].first, paramMatch[1].length()); const absl::string_view opcode(&*paramMatch[1].first, paramMatch[1].length());
const std::string_view value(&*paramMatch[2].first, paramMatch[2].length()); const absl::string_view value(&*paramMatch[2].first, paramMatch[2].length());
currentMembers.emplace_back(opcode, value); currentMembers.emplace_back(opcode, value);
} }
callback(header, currentMembers); callback(header, currentMembers);
@ -89,7 +90,7 @@ void sfz::Parser::readSfzFile(const std::filesystem::path& fileName, std::vector
std::string tmpString; std::string tmpString;
while (std::getline(fileStream, tmpString)) { while (std::getline(fileStream, tmpString)) {
std::string_view tmpView { tmpString }; absl::string_view tmpView { tmpString };
removeCommentOnLine(tmpView); removeCommentOnLine(tmpView);
trimInPlace(tmpView); trimInPlace(tmpView);

View file

@ -47,7 +47,7 @@ public:
void disableRecursiveIncludeGuard() { recursiveIncludeGuard = false; } void disableRecursiveIncludeGuard() { recursiveIncludeGuard = false; }
void enableRecursiveIncludeGuard() { recursiveIncludeGuard = true; } void enableRecursiveIncludeGuard() { recursiveIncludeGuard = true; }
protected: protected:
virtual void callback(std::string_view header, const std::vector<Opcode>& members) = 0; virtual void callback(absl::string_view header, const std::vector<Opcode>& members) = 0;
std::filesystem::path rootDirectory { std::filesystem::current_path() }; std::filesystem::path rootDirectory { std::filesystem::current_path() };
private: private:
bool recursiveIncludeGuard { false }; bool recursiveIncludeGuard { false };

View file

@ -22,7 +22,7 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once #pragma once
#include <algorithm> #include "compat/algorithm.h"
#include <initializer_list> #include <initializer_list>
#include <type_traits> #include <type_traits>

View file

@ -29,7 +29,7 @@
#include "Opcode.h" #include "Opcode.h"
#include "AudioBuffer.h" #include "AudioBuffer.h"
#include <bitset> #include <bitset>
#include <optional> #include <absl/types/optional.h>
#include <random> #include <random>
#include <string> #include <string>
#include <vector> #include <vector>
@ -73,13 +73,13 @@ struct Region {
uint32_t offset { Default::offset }; // offset uint32_t offset { Default::offset }; // offset
uint32_t offsetRandom { Default::offsetRandom }; // offset_random uint32_t offsetRandom { Default::offsetRandom }; // offset_random
uint32_t sampleEnd { Default::sampleEndRange.getEnd() }; // end uint32_t sampleEnd { Default::sampleEndRange.getEnd() }; // end
std::optional<uint32_t> sampleCount {}; // count absl::optional<uint32_t> sampleCount {}; // count
SfzLoopMode loopMode { Default::loopMode }; // loopmode SfzLoopMode loopMode { Default::loopMode }; // loopmode
Range<uint32_t> loopRange { Default::loopRange }; //loopstart and loopend Range<uint32_t> loopRange { Default::loopRange }; //loopstart and loopend
// Instrument settings: voice lifecycle // Instrument settings: voice lifecycle
uint32_t group { Default::group }; // group uint32_t group { Default::group }; // group
std::optional<uint32_t> offBy {}; // off_by absl::optional<uint32_t> offBy {}; // off_by
SfzOffMode offMode { Default::offMode }; // off_mode SfzOffMode offMode { Default::offMode }; // off_mode
// Region logic: key mapping // Region logic: key mapping
@ -91,10 +91,10 @@ struct Region {
Range<int> bendRange { Default::bendRange }; // hibend and lobend Range<int> bendRange { Default::bendRange }; // hibend and lobend
CCMap<Range<uint8_t>> ccConditions { Default::ccRange }; CCMap<Range<uint8_t>> ccConditions { Default::ccRange };
Range<uint8_t> keyswitchRange { Default::keyRange }; // sw_hikey and sw_lokey Range<uint8_t> keyswitchRange { Default::keyRange }; // sw_hikey and sw_lokey
std::optional<uint8_t> keyswitch {}; // sw_last absl::optional<uint8_t> keyswitch {}; // sw_last
std::optional<uint8_t> keyswitchUp {}; // sw_up absl::optional<uint8_t> keyswitchUp {}; // sw_up
std::optional<uint8_t> keyswitchDown {}; // sw_down absl::optional<uint8_t> keyswitchDown {}; // sw_down
std::optional<uint8_t> previousNote {}; // sw_previous absl::optional<uint8_t> previousNote {}; // sw_previous
SfzVelocityOverride velocityOverride { Default::velocityOverride }; // sw_vel SfzVelocityOverride velocityOverride { Default::velocityOverride }; // sw_vel
bool checkSustain { Default::checkSustain }; // sustain_sw bool checkSustain { Default::checkSustain }; // sustain_sw
bool checkSostenuto { Default::checkSostenuto }; // sostenuto_sw bool checkSostenuto { Default::checkSostenuto }; // sostenuto_sw
@ -116,11 +116,11 @@ struct Region {
float pan { Default::pan }; // pan float pan { Default::pan }; // pan
float width { Default::width }; // width float width { Default::width }; // width
float position { Default::position }; // position float position { Default::position }; // position
std::optional<CCValuePair> volumeCC; // volume_oncc absl::optional<CCValuePair> volumeCC; // volume_oncc
std::optional<CCValuePair> amplitudeCC; // amplitude_oncc absl::optional<CCValuePair> amplitudeCC; // amplitude_oncc
std::optional<CCValuePair> panCC; // pan_oncc absl::optional<CCValuePair> panCC; // pan_oncc
std::optional<CCValuePair> widthCC; // width_oncc absl::optional<CCValuePair> widthCC; // width_oncc
std::optional<CCValuePair> positionCC; // position_oncc absl::optional<CCValuePair> positionCC; // position_oncc
uint8_t ampKeycenter { Default::ampKeycenter }; // amp_keycenter uint8_t ampKeycenter { Default::ampKeycenter }; // amp_keycenter
float ampKeytrack { Default::ampKeytrack }; // amp_keytrack float ampKeytrack { Default::ampKeytrack }; // amp_keytrack
float ampVeltrack { Default::ampVeltrack }; // amp_keytrack float ampVeltrack { Default::ampVeltrack }; // amp_keytrack

View file

@ -24,7 +24,7 @@
#include "SfzHelpers.h" #include "SfzHelpers.h"
#include "StringViewHelpers.h" #include "StringViewHelpers.h"
std::optional<uint8_t> sfz::readNoteValue(const std::string_view& value) absl::optional<uint8_t> sfz::readNoteValue(const absl::string_view& value)
{ {
switch(hash(value)) switch(hash(value))
{ {

View file

@ -22,8 +22,9 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once #pragma once
#include <optional> #include <absl/types/optional.h>
#include <string> #include <absl/strings/string_view.h>
//#include <string>
#include <array> #include <array>
#include <cmath> #include <cmath>
@ -59,7 +60,7 @@ inline constexpr float normalizeNegativePercents(T percentValue)
return std::min(std::max(static_cast<float>(percentValue), -100.0f), 100.0f) / 100.0f; return std::min(std::max(static_cast<float>(percentValue), -100.0f), 100.0f) / 100.0f;
} }
inline float ccSwitchedValue(const CCValueArray& ccValues, const std::optional<CCValuePair>& ccSwitch, float value) noexcept inline float ccSwitchedValue(const CCValueArray& ccValues, const absl::optional<CCValuePair>& ccSwitch, float value) noexcept
{ {
if (ccSwitch) if (ccSwitch)
return value + ccSwitch->second * normalizeCC(ccValues[ccSwitch->first]); return value + ccSwitch->second * normalizeCC(ccValues[ccSwitch->first]);
@ -67,7 +68,7 @@ inline float ccSwitchedValue(const CCValueArray& ccValues, const std::optional<C
return value; return value;
} }
std::optional<uint8_t> readNoteValue(const std::string_view& value); absl::optional<uint8_t> readNoteValue(const absl::string_view& value);
} // namespace sfz } // namespace sfz

View file

@ -24,7 +24,7 @@
#pragma once #pragma once
#include <string_view> #include <string_view>
inline void trimInPlace(std::string_view& s) inline void trimInPlace(absl::string_view& s)
{ {
const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v"); const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v");
if (leftPosition != s.npos) { if (leftPosition != s.npos) {
@ -36,7 +36,7 @@ inline void trimInPlace(std::string_view& s)
} }
} }
inline std::string_view trim(std::string_view s) inline absl::string_view trim(absl::string_view s)
{ {
const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v"); const auto leftPosition = s.find_first_not_of(" \r\t\n\f\v");
if (leftPosition != s.npos) { if (leftPosition != s.npos) {
@ -52,7 +52,7 @@ inline std::string_view trim(std::string_view s)
inline constexpr uint64_t Fnv1aBasis = 0x811C9DC5; inline constexpr uint64_t Fnv1aBasis = 0x811C9DC5;
inline constexpr uint64_t Fnv1aPrime = 0x01000193; inline constexpr uint64_t Fnv1aPrime = 0x01000193;
inline constexpr uint64_t hash(std::string_view s, uint64_t h = Fnv1aBasis) inline constexpr uint64_t hash(absl::string_view s, uint64_t h = Fnv1aBasis)
{ {
if (s.length() > 0) if (s.length() > 0)
return hash( { s.data() + 1, s.length() - 1 }, (h ^ s.front()) * Fnv1aPrime ); return hash( { s.data() + 1, s.length() - 1 }, (h ^ s.front()) * Fnv1aPrime );

View file

@ -41,7 +41,7 @@ sfz::Synth::Synth()
voiceViewArray.reserve(config::numVoices); voiceViewArray.reserve(config::numVoices);
} }
void sfz::Synth::callback(std::string_view header, const std::vector<Opcode>& members) void sfz::Synth::callback(absl::string_view header, const std::vector<Opcode>& members)
{ {
switch (hash(header)) { switch (hash(header)) {
case hash("global"): case hash("global"):
@ -87,7 +87,7 @@ void sfz::Synth::buildRegion(const std::vector<Opcode>& regionOpcodes)
auto parseOpcodes = [&](const auto& opcodes) { auto parseOpcodes = [&](const auto& opcodes) {
for (auto& opcode : opcodes) { for (auto& opcode : opcodes) {
const auto unknown = absl::c_find_if(unknownOpcodes, [&](std::string_view sv) { return sv.compare(opcode.opcode) == 0; }); const auto unknown = absl::c_find_if(unknownOpcodes, [&](absl::string_view sv) { return sv.compare(opcode.opcode) == 0; });
if (unknown != unknownOpcodes.end()) { if (unknown != unknownOpcodes.end()) {
continue; continue;
} }
@ -113,7 +113,7 @@ void sfz::Synth::clear()
numMasters = 0; numMasters = 0;
numCurves = 0; numCurves = 0;
fileTicket = -1; fileTicket = -1;
defaultSwitch = std::nullopt; defaultSwitch = absl::nullopt;
for (auto& state : ccState) for (auto& state : ccState)
state = 0; state = 0;
ccNames.clear(); ccNames.clear();
@ -423,7 +423,7 @@ const sfz::Region* sfz::Synth::getRegionView(int idx) const noexcept
{ {
return (size_t)idx < regions.size() ? regions[idx].get() : nullptr; return (size_t)idx < regions.size() ? regions[idx].get() : nullptr;
} }
std::set<std::string_view> sfz::Synth::getUnknownOpcodes() const noexcept std::set<absl::string_view> sfz::Synth::getUnknownOpcodes() const noexcept
{ {
return unknownOpcodes; return unknownOpcodes;
} }

View file

@ -28,7 +28,7 @@
#include "LeakDetector.h" #include "LeakDetector.h"
#include "AudioSpan.h" #include "AudioSpan.h"
#include "absl/types/span.h" #include "absl/types/span.h"
#include <optional> #include <absl/types/optional.h>
#include <random> #include <random>
#include <set> #include <set>
#include <string_view> #include <string_view>
@ -46,7 +46,7 @@ public:
int getNumMasters() const noexcept; int getNumMasters() const noexcept;
int getNumCurves() const noexcept; int getNumCurves() const noexcept;
const Region* getRegionView(int idx) const noexcept; const Region* getRegionView(int idx) const noexcept;
std::set<std::string_view> getUnknownOpcodes() const noexcept; std::set<absl::string_view> getUnknownOpcodes() const noexcept;
size_t getNumPreloadedSamples() const noexcept; size_t getNumPreloadedSamples() const noexcept;
void setSamplesPerBlock(int samplesPerBlock) noexcept; void setSamplesPerBlock(int samplesPerBlock) noexcept;
@ -62,7 +62,7 @@ public:
void getNumActiveVoices() const noexcept; void getNumActiveVoices() const noexcept;
void garbageCollect() noexcept; void garbageCollect() noexcept;
protected: protected:
void callback(std::string_view header, const std::vector<Opcode>& members) final; void callback(absl::string_view header, const std::vector<Opcode>& members) final;
private: private:
bool hasGlobal { false }; bool hasGlobal { false };
@ -83,8 +83,8 @@ private:
CCValueArray ccState; CCValueArray ccState;
Voice* findFreeVoice() noexcept; Voice* findFreeVoice() noexcept;
std::vector<CCNamePair> ccNames; std::vector<CCNamePair> ccNames;
std::optional<uint8_t> defaultSwitch; absl::optional<uint8_t> defaultSwitch;
std::set<std::string_view> unknownOpcodes; std::set<absl::string_view> unknownOpcodes;
using RegionPtrVector = std::vector<Region*>; using RegionPtrVector = std::vector<Region*>;
using VoicePtrVector = std::vector<Voice*>; using VoicePtrVector = std::vector<Voice*>;
std::vector<std::unique_ptr<Region>> regions; std::vector<std::unique_ptr<Region>> regions;

39
sfizz/compat/algorithm.h Normal file
View file

@ -0,0 +1,39 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <algorithm>
#if __cplusplus <= 201402L
#include <cassert>
namespace std
{
template<class T>
constexpr const T& clamp( const T& v, const T& lo, const T& hi )
{
assert( !(hi < lo) );
return (v < lo) ? lo : (hi < v) ? hi : v;
}
}
#endif // lower than C++17

View file

@ -24,12 +24,12 @@
#pragma once #pragma once
#if defined(__cpp_lib_filesystem) || (defined(__has_include) && __has_include(<filesystem>)) #if defined(__cpp_lib_filesystem) || (defined(__has_include) && __has_include(<filesystem>))
#include <filesystem> #include <filesystem>
#elif defined(__cpp_lib_experimental_filesystem) || (defined(__has_include) && __has_include(<experimental/filesystem>)) #elif __cplusplus >= 201103L
#include <experimental/filesystem> #include <experimental/filesystem>
namespace std { namespace std {
namespace filesystem = std::experimental::filesystem; namespace filesystem = std::experimental::filesystem;
} }
#else #else
#error no filesystem support #error no filesystem support
#endif #endif