From 045828d744d8628010b6ff9da799e61dadb86429 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Tue, 16 Jun 2015 12:46:19 -0400 Subject: [PATCH] Define class NumberScale and enum NumberScaleType --- src/Makefile.am | 1 + src/NumberScale.h | 164 ++++++++++++++++++ win/Projects/Audacity/Audacity.vcxproj | 1 + .../Audacity/Audacity.vcxproj.filters | 3 + 4 files changed, 169 insertions(+) create mode 100644 src/NumberScale.h diff --git a/src/Makefile.am b/src/Makefile.am index 0df9b9025..023f1ec44 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -171,6 +171,7 @@ audacity_SOURCES = \ MixerBoard.h \ ModuleManager.cpp \ ModuleManager.h \ + NumberScale.h \ PitchName.cpp \ PitchName.h \ PlatformCompatibility.cpp \ diff --git a/src/NumberScale.h b/src/NumberScale.h new file mode 100644 index 000000000..a4cdfb192 --- /dev/null +++ b/src/NumberScale.h @@ -0,0 +1,164 @@ +/********************************************************************** + +Audacity: A Digital Audio Editor + +NumberScale.h + +Paul Licameli + +**********************************************************************/ + +#ifndef __AUDACITY_NUMBER_SCALE__ +#define __AUDACITY_NUMBER_SCALE__ + +#include +#include +#include +#include + +enum NumberScaleType { + nstLinear, + nstLogarithmic, + + nstNumScaleTypes, +}; + + +class NumberScale +{ +public: + NumberScale(NumberScaleType type, + float value0, float value1, float unit) + : mType(type) + { + switch (mType) { + case nstLinear: + { + mValue0 = value0 / unit; + mValue1 = value1 / unit; + mUnit = 1.0; + } + break; + case nstLogarithmic: + { + mValue0 = logf(value0 / unit); + mValue1 = logf(value1 / unit); + mUnit = 1.0; + } + break; + default: + wxASSERT(false); + } + } + + NumberScale Reversal() const + { + NumberScale result(*this); + std::swap(result.mValue0, result.mValue1); + return result; + } + + bool operator == (const NumberScale& other) const + { + return mType == other.mType + && mValue0 == other.mValue0 + && mValue1 == other.mValue1 + && mUnit == other.mUnit; + } + + bool operator != (const NumberScale &other) const + { + return !(*this == other); + } + + // Random access + float PositionToValue(float pp) const + { + switch (mType) { + default: + wxASSERT(false); + case nstLinear: + return mValue0 + pp * (mValue1 - mValue0); + case nstLogarithmic: + return exp(mValue0 + pp * (mValue1 - mValue0)); + } + } + + // STL-idiom iteration + + class Iterator + { + public: + Iterator(NumberScaleType type, float step, float value, float unit) + : mType(type), mStep(step), mValue(value), mUnit(unit) + { + } + + float operator * () const + { + switch (mType) { + default: + wxASSERT(false); + case nstLinear: + case nstLogarithmic: + return mValue; + } + } + + Iterator &operator ++() + { + switch (mType) { + case nstLinear: + mValue += mStep; + break; + case nstLogarithmic: + mValue *= mStep; + break; + default: + wxASSERT(false); + } + return *this; + } + + private: + const NumberScaleType mType; + const float mStep; + float mValue; + float mUnit; + }; + + Iterator begin(float nPositions) const + { + switch (mType) { + default: + wxASSERT(false); + case nstLinear: + return Iterator + (mType, (mValue1 - mValue0) / nPositions, mValue0, mUnit); + case nstLogarithmic: + return Iterator + (mType, exp((mValue1 - mValue0) / nPositions), exp(mValue0), mUnit); + } + } + + // Inverse + float ValueToPosition(float val) const + { + switch (mType) { + default: + wxASSERT(false); + case nstLinear: + return ((val - mValue0) / (mValue1 - mValue0)); + case nstLogarithmic: + return ((log(val) - mValue0) / (mValue1 - mValue0)); + } + } + +private: + const NumberScaleType mType; + float mValue0; + float mValue1; + float mUnit; +}; + +#endif diff --git a/win/Projects/Audacity/Audacity.vcxproj b/win/Projects/Audacity/Audacity.vcxproj index 6ce73f628..30aeb05e2 100755 --- a/win/Projects/Audacity/Audacity.vcxproj +++ b/win/Projects/Audacity/Audacity.vcxproj @@ -533,6 +533,7 @@ + diff --git a/win/Projects/Audacity/Audacity.vcxproj.filters b/win/Projects/Audacity/Audacity.vcxproj.filters index 566f52817..e767fbf09 100755 --- a/win/Projects/Audacity/Audacity.vcxproj.filters +++ b/win/Projects/Audacity/Audacity.vcxproj.filters @@ -1685,6 +1685,9 @@ src/prefs + + src +