Skip to content

Commit

Permalink
Use algorithms in place of some loops
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed May 16, 2024
1 parent 6b4bbcc commit d24fc10
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions alc/effects/chorus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,35 +188,31 @@ void ChorusState::calcTriangleDelays(const size_t todo)
const float depth{mDepth};
const int delay{mDelay};

ASSUME(lfo_range > 0);
ASSUME(todo > 0);

auto gen_lfo = [lfo_scale,depth,delay](const uint offset) -> uint
{
const float offset_norm{static_cast<float>(offset) * lfo_scale};
return static_cast<uint>(fastf2i((1.0f-std::abs(2.0f-offset_norm)) * depth) + delay);
};

uint offset{mLfoOffset};
ASSUME(lfo_range > offset);
auto ldelays = mModDelays[0].begin();
for(size_t i{0};i < todo;)
{
size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
do {
mModDelays[0][i++] = gen_lfo(offset++);
} while(--rem);
if(offset == lfo_range)
offset = 0;
const size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
ldelays = std::generate_n(ldelays, rem, [&offset,gen_lfo] { return gen_lfo(offset++); });
if(offset == lfo_range) offset = 0;
i += rem;
}

offset = (mLfoOffset+mLfoDisp) % lfo_range;
auto rdelays = mModDelays[1].begin();
for(size_t i{0};i < todo;)
{
size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
do {
mModDelays[1][i++] = gen_lfo(offset++);
} while(--rem);
if(offset == lfo_range)
offset = 0;
const size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
rdelays = std::generate_n(rdelays, rem, [&offset,gen_lfo] { return gen_lfo(offset++); });
if(offset == lfo_range) offset = 0;
i += rem;
}

mLfoOffset = static_cast<uint>(mLfoOffset+todo) % lfo_range;
Expand All @@ -229,35 +225,31 @@ void ChorusState::calcSinusoidDelays(const size_t todo)
const float depth{mDepth};
const int delay{mDelay};

ASSUME(lfo_range > 0);
ASSUME(todo > 0);

auto gen_lfo = [lfo_scale,depth,delay](const uint offset) -> uint
{
const float offset_norm{static_cast<float>(offset) * lfo_scale};
return static_cast<uint>(fastf2i(std::sin(offset_norm)*depth) + delay);
};

uint offset{mLfoOffset};
ASSUME(lfo_range > offset);
auto ldelays = mModDelays[0].begin();
for(size_t i{0};i < todo;)
{
size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
do {
mModDelays[0][i++] = gen_lfo(offset++);
} while(--rem);
if(offset == lfo_range)
offset = 0;
const size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
ldelays = std::generate_n(ldelays, rem, [&offset,gen_lfo] { return gen_lfo(offset++); });
if(offset == lfo_range) offset = 0;
i += rem;
}

offset = (mLfoOffset+mLfoDisp) % lfo_range;
auto rdelays = mModDelays[1].begin();
for(size_t i{0};i < todo;)
{
size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
do {
mModDelays[1][i++] = gen_lfo(offset++);
} while(--rem);
if(offset == lfo_range)
offset = 0;
const size_t rem{std::min(todo-i, size_t{lfo_range-offset})};
rdelays = std::generate_n(rdelays, rem, [&offset,gen_lfo] { return gen_lfo(offset++); });
if(offset == lfo_range) offset = 0;
i += rem;
}

mLfoOffset = static_cast<uint>(mLfoOffset+todo) % lfo_range;
Expand Down

0 comments on commit d24fc10

Please sign in to comment.