Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Shep #134

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions include/sst/voice-effects/delay/Chorus.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ template <typename VFXConfig> struct Chorus : core::VoiceEffectTemplateBase<VFXC
ipStereo,
};

Chorus(const SincTable &st) : sSincTable(st), core::VoiceEffectTemplateBase<VFXConfig>()
{
std::fill(mLastParam.begin(), mLastParam.end(), -188888.f);
}
Chorus(const SincTable &st) : sSincTable(st), core::VoiceEffectTemplateBase<VFXConfig>() {}

~Chorus()
{
Expand Down Expand Up @@ -394,8 +391,6 @@ template <typename VFXConfig> struct Chorus : core::VoiceEffectTemplateBase<VFXC
std::array<details::DelayLineSupport, 2> lineSupport;
bool isShort{true};

std::array<float, numFloatParams> mLastParam{};

sst::basic_blocks::dsp::lipol_sse<VFXConfig::blockSize, true> feedbackLerp, timeLerp[2];
};

Expand Down
2 changes: 0 additions & 2 deletions include/sst/voice-effects/dynamics/AutoWah.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ template <typename VFXConfig> struct AutoWah : core::VoiceEffectTemplateBase<VFX
bool getKeytrack() const { return keytrackOn; }

protected:
std::array<float, numFloatParams> mLastParam{};
std::array<int, numIntParams> mLastIParam{};
bool first = true;
bool keytrackOn = false;
sst::basic_blocks::dsp::SlewLimiter speedLimiter;
Expand Down
16 changes: 6 additions & 10 deletions include/sst/voice-effects/filter/CytomicSVF.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ template <typename VFXConfig> struct CytomicSVF : core::VoiceEffectTemplateBase<
return pmd().withName("error");
}

void initVoiceEffect() {}
void initVoiceEffect()
{
cySvf[0].init();
cySvf[1].init();
}
void initVoiceEffectParams() { this->initToParamMetadataDefault(this); }

void calc_coeffs(float pitch = 0.f)
Expand All @@ -191,20 +195,12 @@ template <typename VFXConfig> struct CytomicSVF : core::VoiceEffectTemplateBase<

if (diff || idiff)
{
if (idiff)
{
cySvf[0].init();
if (iparam[2])
{
cySvf[1].init();
}
}
auto mode = (sst::filters::CytomicSVF::Mode)(iparam[0]);

auto res = std::clamp(param[2], 0.f, 1.f);
if (iparam[2] == true)
{
res *= .885f; // I just checked on a specrum analyzer.
res *= .885f; // I just checked peak heights on a spectrum analyzer.
}

auto shelf = this->dbToLinear(param[3]);
Expand Down
2 changes: 0 additions & 2 deletions include/sst/voice-effects/modulation/Phaser.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ template <typename VFXConfig> struct Phaser : core::VoiceEffectTemplateBase<VFXC

protected:
bool keytrackOn{false};
std::array<float, numFloatParams> mLastParam{};
std::array<int, numIntParams> mLastIParam{};
std::array<sst::filters::CytomicSVF, 4> filters;
float fbAmt[2]{0.f, 0.f};
sst::basic_blocks::dsp::lipol_sse<VFXConfig::blockSize, true> feedbackLerp;
Expand Down
8 changes: 1 addition & 7 deletions include/sst/voice-effects/modulation/RingMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ template <typename VFXConfig> struct RingMod : core::VoiceEffectTemplateBase<VFX
ipDenom
};

RingMod() : core::VoiceEffectTemplateBase<VFXConfig>()
{
std::fill(mLastIParam.begin(), mLastIParam.end(), -1);
std::fill(mLastParam.begin(), mLastParam.end(), -188888.f);
}
RingMod() : core::VoiceEffectTemplateBase<VFXConfig>() {}

~RingMod() {}

Expand Down Expand Up @@ -159,8 +155,6 @@ template <typename VFXConfig> struct RingMod : core::VoiceEffectTemplateBase<VFX

protected:
bool keytrackOn{true};
std::array<float, numFloatParams> mLastParam{};
std::array<int, numIntParams> mLastIParam{};
sst::basic_blocks::dsp::QuadratureOscillator<float> qosc;
int priorNum = -1;
int priorDenom = -1;
Expand Down
185 changes: 133 additions & 52 deletions include/sst/voice-effects/modulation/ShepardPhaser.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,18 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
return pmd().asInt().withName("Error");
}

void initVoiceEffect() {}
void initVoiceEffect()
{
for (int i = 0; i < 24; ++i)
{
filters[i].init();
if (i < 12)
{
priorLevelL[i] = -12345.f;
priorLevelR[i] = -12345.f;
}
}
}

void initVoiceEffectParams() { this->initToParamMetadataDefault(this); }

Expand Down Expand Up @@ -124,6 +135,7 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
if (peaks != priorPeaks)
{
logOfPeaks = std::log2f(peaks);
gainScale = 1 / logOfPeaks;
priorPeaks = peaks;
}
auto lfoRate = this->getFloatParam(fpRate) - logOfPeaks;
Expand All @@ -140,50 +152,107 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
mech::clear_block<VFXConfig::blockSize>(dataoutL);
mech::clear_block<VFXConfig::blockSize>(dataoutR);

for (int i = 0; i < peaks; ++i)
if (stereo)
{
auto offset = static_cast<double>(i) / static_cast<double>(peaks);
auto halfway = 0.5 / static_cast<double>(peaks);
auto iPhaseL = std::fmod(phasorValue + offset, 1.0);
auto iPhaseR = !stereo ? iPhaseL : std::fmod(phasorValue + offset + halfway, 1.0);

auto iTriL = triangle(iPhaseL);
auto iTriR = triangle(iPhaseR);

auto freqL = 440.f * this->note_to_pitch_ignoring_tuning(
this->getFloatParam(fpStartFreq) + (range * iPhaseL));
auto freqR = 440.f * this->note_to_pitch_ignoring_tuning(
this->getFloatParam(fpStartFreq) + (range * iPhaseR));

filters[i].template setCoeffForBlock<VFXConfig::blockSize>(
sst::filters::CytomicSVF::Mode::BP, freqL, freqR, res, res,
this->getSampleRateInv(), 1.f, 1.f);

float tmpL alignas(16)[VFXConfig::blockSize];
float tmpR alignas(16)[VFXConfig::blockSize];
mech::copy_from_to<VFXConfig::blockSize>(datainL, tmpL);
mech::copy_from_to<VFXConfig::blockSize>(datainR, tmpR);

for (int k = 0; k < VFXConfig::blockSize; ++k)
for (int i = 0; i < peaks; ++i)
{
filters[i].processBlockStep(tmpL[k], tmpR[k]);
auto offset = static_cast<double>(i) / static_cast<double>(peaks);
auto halfway = 0.5 / static_cast<double>(peaks);

// ramp for frequency
auto iPhaseL = std::fmod(phasorValue + offset, 1.0);
auto iPhaseR = std::fmod(phasorValue + offset + halfway, 1.0);

// triangle for amplitude
auto iTriL = triangle(iPhaseL);
auto iTriR = triangle(iPhaseR);
iTriL = iTriL * iTriL * iTriL;
iTriR = iTriR * iTriR * iTriR;
if (priorLevelL[i] < 0) // true on first block
{
priorLevelL[i] = iTriL; // start from the first value
}
if (priorLevelR[i] < 0) // same for the right side
{
priorLevelR[i] = iTriR;
}
// set the smoothers to start on the prior value
levelLerpL.set_target_instant(priorLevelL[i]);
levelLerpR.set_target_instant(priorLevelR[i]);
// and aim for the current value
levelLerpL.set_target(iTriL);
levelLerpR.set_target(iTriR);
// and save the index's value to start from on the next block
priorLevelL[i] = iTriL;
priorLevelR[i] = iTriR;
// A float per filter is a lot less memory than a lipol_sse per filter

auto freqL = 440.f * this->note_to_pitch_ignoring_tuning(
this->getFloatParam(fpStartFreq) + (range * iPhaseL));
auto freqR = 440.f * this->note_to_pitch_ignoring_tuning(
this->getFloatParam(fpStartFreq) + (range * iPhaseR));

filters[i].template setCoeffForBlock<VFXConfig::blockSize>(
sst::filters::CytomicSVF::Mode::BP, freqL, freqR, res, res,
this->getSampleRateInv(), 1.f, 1.f);

float tmpL alignas(16)[VFXConfig::blockSize];
float tmpR alignas(16)[VFXConfig::blockSize];
mech::copy_from_to<VFXConfig::blockSize>(datainL, tmpL);
mech::copy_from_to<VFXConfig::blockSize>(datainR, tmpR);

for (int k = 0; k < VFXConfig::blockSize; ++k)
{
filters[i].processBlockStep(tmpL[k], tmpR[k]);
}

levelLerpL.multiply_block(tmpL);
levelLerpR.multiply_block(tmpR);

mech::scale_accumulate_from_to<VFXConfig::blockSize>(tmpL, tmpR, gainScale,
dataoutL, dataoutR);
}

if (!stereo)
{
lipolLevel[i].set_target(iTriL * iTriL * iTriL);
lipolLevel[i].multiply_2_blocks(tmpL, tmpR);
}
else
}
else
{
for (int i = 0; i < peaks; ++i)
{
lipolLevel[i].set_target(iTriL * iTriL * iTriL);
lipolLevel[i].multiply_block(tmpL);
lipolLevel[i + 12].set_target(iTriR * iTriR * iTriR);
lipolLevel[i + 12].multiply_block(tmpR);
auto offset = static_cast<double>(i) / static_cast<double>(peaks);
auto halfway = 0.5 / static_cast<double>(peaks);
auto iPhase = std::fmod(phasorValue + offset, 1.0);

auto iTri = triangle(iPhase);
iTri = iTri * iTri * iTri;
if (priorLevelL[i] < 0)
{
priorLevelL[i] = iTri;
}
levelLerpL.set_target_instant(priorLevelL[i]);
levelLerpL.set_target(iTri);
priorLevelL[i] = iTri;

auto freq = 440.f * this->note_to_pitch_ignoring_tuning(
this->getFloatParam(fpStartFreq) + (range * iPhase));

filters[i].template setCoeffForBlock<VFXConfig::blockSize>(
sst::filters::CytomicSVF::Mode::BP, freq, freq, res, res,
this->getSampleRateInv(), 1.f, 1.f);

float tmpL alignas(16)[VFXConfig::blockSize];
float tmpR alignas(16)[VFXConfig::blockSize];
mech::copy_from_to<VFXConfig::blockSize>(datainL, tmpL);
mech::copy_from_to<VFXConfig::blockSize>(datainR, tmpR);

for (int k = 0; k < VFXConfig::blockSize; ++k)
{
filters[i].processBlockStep(tmpL[k], tmpR[k]);
}

levelLerpL.multiply_2_blocks(tmpL, tmpR);

mech::scale_accumulate_from_to<VFXConfig::blockSize>(tmpL, tmpR, gainScale,
dataoutL, dataoutR);
}

mech::scale_accumulate_from_to<VFXConfig::blockSize>(tmpL, tmpR, 0.5f, dataoutL,
dataoutR);
}
}

Expand All @@ -196,6 +265,7 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
if (peaks != priorPeaks)
{
logOfPeaks = std::log2f(peaks);
gainScale = 1 / logOfPeaks;
priorPeaks = peaks;
}
auto lfoRate = this->getFloatParam(fpRate) - logOfPeaks;
Expand All @@ -213,13 +283,23 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa

for (int i = 0; i < peaks; ++i)
{
auto offset = static_cast<double>(i) / static_cast<double>(peaks);
auto iPhase = std::fmod(phasorValue + offset, 1.0);

float iPhase = phasorValue + (i / static_cast<float>(peaks));
if (iPhase > 1)
float iTri = triangle(iPhase);
iTri = iTri * iTri * iTri;

if (isFirst)
{
iPhase -= 1;
priorLevelL[i] = iTri;
if (i == peaks - 1)
{
isFirst = false;
}
}
float iTri = triangle(iPhase);
levelLerpL.set_target_instant(priorLevelL[i]);
levelLerpL.set_target(iTri);
priorLevelL[i] = iTri;

auto freqMod = this->getFloatParam(fpStartFreq) + (range * iPhase);
auto freq = 440.f * this->note_to_pitch_ignoring_tuning(freqMod);
Expand All @@ -234,10 +314,9 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
filters[i].processBlockStep(tmp[k]);
}

lipolLevel[i].set_target(iTri * iTri * iTri);
lipolLevel[i].multiply_block(tmp);
levelLerpL.multiply_block(tmp);

mech::scale_accumulate_from_to<VFXConfig::blockSize>(tmp, 0.5f, dataoutL);
mech::scale_accumulate_from_to<VFXConfig::blockSize>(tmp, gainScale, dataoutL);
}
}

Expand All @@ -249,14 +328,16 @@ template <typename VFXConfig> struct ShepardPhaser : core::VoiceEffectTemplateBa
bool getMonoToStereoSetting() const { return this->getIntParam(ipStereo) > 0; }

protected:
std::array<float, numFloatParams> mLastParam{};
std::array<int, numIntParams> mLastIParam{};

std::array<sst::filters::CytomicSVF, 12> filters;

sst::basic_blocks::dsp::lipol_sse<VFXConfig::blockSize, true> lipolLevel[24];
float logOfPeaks{0.f};
sst::basic_blocks::dsp::lipol_sse<VFXConfig::blockSize, true> levelLerpL, levelLerpR;
float priorLevelL[12];
float priorLevelR[12];

int priorPeaks{0};
float logOfPeaks{0.f};
float gainScale{1.f};

bool isFirst{true};
};
} // namespace sst::voice_effects::modulation
Expand Down
Loading