diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index b6ede56..08de151 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -42,7 +42,7 @@ target_sources(grit-eurorack INTERFACE "grit/math/hermite_interpolation.hpp" "grit/math/ilog2.hpp" "grit/math/ipow.hpp" - "grit/math/range.hpp" + "grit/math/remap.hpp" "grit/testing/approx.hpp" diff --git a/lib/grit/audio/oscillator/oscillator.hpp b/lib/grit/audio/oscillator/oscillator.hpp index fcc8ed3..c81b3d7 100644 --- a/lib/grit/audio/oscillator/oscillator.hpp +++ b/lib/grit/audio/oscillator/oscillator.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include diff --git a/lib/grit/audio/oscillator/variable_shape_oscillator.hpp b/lib/grit/audio/oscillator/variable_shape_oscillator.hpp index 1c44fb7..63dbc1f 100644 --- a/lib/grit/audio/oscillator/variable_shape_oscillator.hpp +++ b/lib/grit/audio/oscillator/variable_shape_oscillator.hpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include diff --git a/lib/grit/audio/oscillator/wavetable_oscillator.hpp b/lib/grit/audio/oscillator/wavetable_oscillator.hpp index a2ad6aa..aee4ba5 100644 --- a/lib/grit/audio/oscillator/wavetable_oscillator.hpp +++ b/lib/grit/audio/oscillator/wavetable_oscillator.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/lib/grit/math/range.hpp b/lib/grit/math/range.hpp deleted file mode 100644 index 63de048..0000000 --- a/lib/grit/math/range.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include -#include - -namespace grit { - -template -[[nodiscard]] constexpr auto mapToRange(Float in, Float min, Float max) -> Float -{ - return etl::clamp(min + in * (max - min), min, max); -} - -} // namespace grit diff --git a/lib/grit/math/remap.hpp b/lib/grit/math/remap.hpp new file mode 100644 index 0000000..aea171a --- /dev/null +++ b/lib/grit/math/remap.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include +#include + +namespace grit { + +template +[[nodiscard]] constexpr auto remap(Float in, Float min, Float max) -> Float +{ + return etl::clamp(min + in * (max - min), min, max); +} + +template +[[nodiscard]] constexpr auto remap(Float in, Float srcMin, Float srcMax, Float destMin, Float destMax) -> Float +{ + return destMin + ((destMax - destMin) * (in - srcMin)) / (srcMax - srcMin); +} + +} // namespace grit diff --git a/lib/grit/math/static_lookup_table_transform.hpp b/lib/grit/math/static_lookup_table_transform.hpp index 171b570..8cafd8f 100644 --- a/lib/grit/math/static_lookup_table_transform.hpp +++ b/lib/grit/math/static_lookup_table_transform.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -32,7 +33,7 @@ struct StaticLookupTableTransform _offset = -min * _scaler; _lut.initialize([func, min, max](size_t i) { - auto idx = map(Float(i), Float(0), Float(Size - 1), min, max); + auto idx = remap(Float(i), Float(0), Float(Size - 1), min, max); return func(etl::clamp(idx, min, max)); }); } @@ -50,11 +51,6 @@ struct StaticLookupTableTransform [[nodiscard]] static constexpr auto size() -> etl::size_t { return Size; } private: - static constexpr auto map(Float x, Float srcMin, Float srcMax, Float min, Float max) -> Float - { - return min + ((max - min) * (x - srcMin)) / (srcMax - srcMin); - } - StaticLookupTable _lut{}; Float _min{}; Float _max{}; diff --git a/src/astra/main.cpp b/src/astra/main.cpp index 6d30721..eea541b 100644 --- a/src/astra/main.cpp +++ b/src/astra/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include @@ -30,8 +30,8 @@ auto audioCallback( auto const gainLeftKnob = patch.GetAdcValue(daisy::patch_sm::CV_1); auto const gainRightKnob = patch.GetAdcValue(daisy::patch_sm::CV_2); - auto const gainLeft = grit::fromDecibels(grit::mapToRange(gainLeftKnob, -30.0F, 6.0F)); - auto const gainRight = grit::fromDecibels(grit::mapToRange(gainRightKnob, -30.0F, 6.0F)); + auto const gainLeft = grit::fromDecibels(grit::remap(gainLeftKnob, -30.0F, 6.0F)); + auto const gainRight = grit::fromDecibels(grit::remap(gainRightKnob, -30.0F, 6.0F)); for (size_t i = 0; i < size; ++i) { auto const inLeft = input(0, i); diff --git a/src/kyma/main.cpp b/src/kyma/main.cpp index bdc98b9..1639a77 100644 --- a/src/kyma/main.cpp +++ b/src/kyma/main.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include @@ -47,18 +47,18 @@ auto audioCallback(daisy::AudioHandle::InputBuffer in, daisy::AudioHandle::Outpu auto const subGainCv = patch.GetAdcValue(daisy::patch_sm::CV_7); auto const subMorphCv = patch.GetAdcValue(daisy::patch_sm::CV_8); - auto const pitch = grit::mapToRange(pitchKnob, 36.0F, 96.0F); - auto const voltsPerOctave = grit::mapToRange(vOctCv, 0.0F, 60.0F); + auto const pitch = grit::remap(pitchKnob, 36.0F, 96.0F); + auto const voltsPerOctave = grit::remap(vOctCv, 0.0F, 60.0F); auto const note = etl::clamp(pitch + voltsPerOctave, 0.0F, 127.0F); auto const morph = etl::clamp(morphKnob + morphCv, 0.0F, 1.0F); auto const subOffset = subOctaveToggle.Pressed() ? 12.0F : 24.0F; auto const subNoteNumber = etl::clamp(note - subOffset, 0.0F, 127.0F); auto const subMorph = etl::clamp(subMorphCv, 0.0F, 1.0F); - auto const subGain = grit::mapToRange(subGainCv, 0.0F, 1.0F); + auto const subGain = grit::remap(subGainCv, 0.0F, 1.0F); - auto const attack = grit::mapToRange(attackKnob, 0.0F, 0.750F); - auto const release = grit::mapToRange(releaseKnob, 0.0F, 2.5F); + auto const attack = grit::remap(attackKnob, 0.0F, 0.750F); + auto const release = grit::remap(releaseKnob, 0.0F, 2.5F); // oscillator.setWavetable(SineWavetable); // subOscillator.setWavetable(SineWavetable);