Skip to content

Commit

Permalink
Rename mapToRange to remap
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiashienzsch committed Feb 10, 2024
1 parent 7d8d117 commit 718cecb
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion lib/grit/audio/oscillator/oscillator.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <grit/math/range.hpp>
#include <grit/math/remap.hpp>

#include <etl/algorithm.hpp>
#include <etl/concepts.hpp>
Expand Down
2 changes: 1 addition & 1 deletion lib/grit/audio/oscillator/variable_shape_oscillator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <grit/audio/mix/cross_fade.hpp>
#include <grit/audio/oscillator/oscillator.hpp>
#include <grit/math/range.hpp>
#include <grit/math/remap.hpp>

#include <etl/concepts.hpp>

Expand Down
2 changes: 1 addition & 1 deletion lib/grit/audio/oscillator/wavetable_oscillator.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <grit/math/buffer_interpolation.hpp>
#include <grit/math/range.hpp>
#include <grit/math/remap.hpp>

#include <etl/algorithm.hpp>
#include <etl/cmath.hpp>
Expand Down
14 changes: 0 additions & 14 deletions lib/grit/math/range.hpp

This file was deleted.

20 changes: 20 additions & 0 deletions lib/grit/math/remap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <etl/algorithm.hpp>
#include <etl/concepts.hpp>

namespace grit {

template<etl::floating_point Float>
[[nodiscard]] constexpr auto remap(Float in, Float min, Float max) -> Float
{
return etl::clamp(min + in * (max - min), min, max);
}

template<etl::floating_point Float>
[[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
8 changes: 2 additions & 6 deletions lib/grit/math/static_lookup_table_transform.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <grit/math/remap.hpp>
#include <grit/math/static_lookup_table.hpp>

#include <etl/algorithm.hpp>
Expand Down Expand Up @@ -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));
});
}
Expand All @@ -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<Float, Size> _lut{};
Float _min{};
Float _max{};
Expand Down
6 changes: 3 additions & 3 deletions src/astra/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <grit/unit/decibel.hpp>
#include <grit/math/range.hpp>
#include <grit/math/remap.hpp>

#include <daisy_patch_sm.h>

Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/kyma/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <grit/audio/oscillator/variable_shape_oscillator.hpp>
#include <grit/audio/oscillator/wavetable_oscillator.hpp>
#include <grit/unit/decibel.hpp>
#include <grit/math/range.hpp>
#include <grit/math/remap.hpp>

#include <daisy_patch_sm.h>

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 718cecb

Please sign in to comment.