Tweak the crossfades

This commit is contained in:
Paul Fd 2021-05-20 20:18:52 +02:00
parent 5fd5a5a808
commit 262953d52a
2 changed files with 14 additions and 11 deletions

View file

@ -18,15 +18,17 @@ namespace sfz {
template <class T, bool C, class U>
float crossfadeIn(const sfz::Range<T, C>& crossfadeRange, U value, CrossfadeCurve curve)
{
constexpr float gapOffset { static_cast<T>(normalize7Bits(1)) };
if (value < crossfadeRange.getStart())
return 0.0f;
const auto length = static_cast<float>(crossfadeRange.length());
const auto length = static_cast<float>(crossfadeRange.length()) - gapOffset;
if (length <= 0.0f)
return 1.0f;
else if (value < crossfadeRange.getEnd()) {
const auto crossfadePosition = static_cast<float>(value - crossfadeRange.getStart()) / length;
const auto distanceFromStart = static_cast<float>(value - crossfadeRange.getStart());
const auto crossfadePosition = distanceFromStart / length;
if (curve == CrossfadeCurve::power)
return sqrt(crossfadePosition);
if (curve == CrossfadeCurve::gain)
@ -42,15 +44,16 @@ float crossfadeIn(const sfz::Range<T, C>& crossfadeRange, U value, CrossfadeCurv
template <class T, bool C, class U>
float crossfadeOut(const sfz::Range<T, C>& crossfadeRange, U value, CrossfadeCurve curve)
{
if (value > crossfadeRange.getEnd())
return 0.0f;
const auto length = static_cast<float>(crossfadeRange.length());
constexpr float gapOffset { static_cast<T>(normalize7Bits(1)) };
const auto length = static_cast<float>(crossfadeRange.length()) - gapOffset;
if (length <= 0.0f)
return 1.0f;
else if (value > crossfadeRange.getStart()) {
const auto crossfadePosition = static_cast<float>(value - crossfadeRange.getStart()) / length;
const auto distanceFromStart = static_cast<float>(value - crossfadeRange.getStart());
const auto crossfadePosition = distanceFromStart / length;
if (crossfadePosition > 1.0f)
return 0.0f;
if (curve == CrossfadeCurve::power)
return std::sqrt(1 - crossfadePosition);
if (curve == CrossfadeCurve::gain)

View file

@ -149,10 +149,10 @@ TEST_CASE("[Files] Group from AVL")
REQUIRE(synth.getRegionView(i)->keyRange == Range<uint8_t>(36, 36));
}
almostEqualRanges(synth.getRegionView(0)->velocityRange, { 1_norm, 26_norm });
almostEqualRanges(synth.getRegionView(1)->velocityRange, { 27_norm, 52_norm });
almostEqualRanges(synth.getRegionView(2)->velocityRange, { 53_norm, 77_norm });
almostEqualRanges(synth.getRegionView(3)->velocityRange, { 78_norm, 102_norm });
almostEqualRanges(synth.getRegionView(0)->velocityRange, { 1_norm, std::nextafter(27_norm, 0.0f) });
almostEqualRanges(synth.getRegionView(1)->velocityRange, { 27_norm, std::nextafter(53_norm, 0.0f) });
almostEqualRanges(synth.getRegionView(2)->velocityRange, { 53_norm, std::nextafter(78_norm, 0.0f) });
almostEqualRanges(synth.getRegionView(3)->velocityRange, { 78_norm, std::nextafter(103_norm, 0.0f) });
almostEqualRanges(synth.getRegionView(4)->velocityRange, { 103_norm, 127_norm });
}