Skip to content

Commit

Permalink
dc removal and safety saturation and more
Browse files Browse the repository at this point in the history
  • Loading branch information
LostRobotMusic committed Jun 25, 2024
1 parent ab3f5ed commit a96666d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions plugins/GranularPitchShifter/GranularPitchShifterEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool GranularPitchShifterEffect::processAudioBuffer(sampleFrame* buf, const fpp_
const float glide = m_granularpitchshifterControls.m_glideModel.value();
const int minLatency = m_granularpitchshifterControls.m_minLatencyModel.value() * m_sampleRate;
const float densityInvRoot = std::sqrt(1.f / density);
const float feedback = m_granularpitchshifterControls.m_feedbackModel.value() * densityInvRoot;
const float feedback = m_granularpitchshifterControls.m_feedbackModel.value();
const float fadeLength = 1.f / m_granularpitchshifterControls.m_fadeLengthModel.value();
const bool prefilter = m_granularpitchshifterControls.m_prefilterModel.value();

Expand Down Expand Up @@ -201,7 +201,7 @@ bool GranularPitchShifterEffect::processAudioBuffer(sampleFrame* buf, const fpp_
if (m_grains[i].readPoint[0] >= m_ringBufLength) { m_grains[i].readPoint[0] -= m_ringBufLength; }
if (m_grains[i].readPoint[1] >= m_ringBufLength) { m_grains[i].readPoint[1] -= m_ringBufLength; }

const float fadePos = std::clamp((-std::fabs(-2.f * static_cast<float>(m_grains[i].phase) + 1.f) + 0.5f) * fadeLength + 0.5f, 0.f, 1.f);
const float fadePos = std::clamp((-std::abs(-2.f * static_cast<float>(m_grains[i].phase) + 1.f) + 0.5f) * fadeLength + 0.5f, 0.f, 1.f);
const float windowVal = cosHalfWindowApprox(fadePos, shapeK);
s[0] += getHermiteSample(m_grains[i].readPoint[0], 0) * windowVal;
s[1] += getHermiteSample(m_grains[i].readPoint[1], 1) * windowVal;
Expand All @@ -211,6 +211,17 @@ bool GranularPitchShifterEffect::processAudioBuffer(sampleFrame* buf, const fpp_
s[0] *= densityInvRoot;
s[1] *= densityInvRoot;

// 1-pole highpass for DC offset removal, to make feedback safer
s[0] -= (m_dcVal[0] = (1.f - m_dcCoeff) * s[0] + m_dcCoeff * m_dcVal[0]);
s[1] -= (m_dcVal[1] = (1.f - m_dcCoeff) * s[1] + m_dcCoeff * m_dcVal[1]);

// cheap safety saturator to protect against infinite feedback
if (feedback > 0)
{
s[0] = safetySaturate(s[0]);
s[1] = safetySaturate(s[1]);
}

if (++m_writePoint >= m_ringBufLength)
{
m_writePoint = 0;
Expand Down Expand Up @@ -261,6 +272,8 @@ void GranularPitchShifterEffect::changeSampleRate()
m_grains.clear();
m_grainCount = 0;
m_grains.reserve(8);// arbitrary

m_dcCoeff = std::exp(-2.0 * M_PI * DcRemovalHz / m_sampleRate);
}


Expand Down
10 changes: 10 additions & 0 deletions plugins/GranularPitchShifter/GranularPitchShifterEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ constexpr float PrefilterBandwidth = 0.96f;// 96% of nyquist
constexpr double GlideSnagRadius = 0.001;
constexpr int SafetyLatency = 3;
constexpr float RangeSeconds[5] = {5, 10, 40, 40, 120};
constexpr float DcRemovalHz = 7;


class GranularPitchShifterEffect : public Effect
Expand Down Expand Up @@ -94,6 +95,13 @@ class GranularPitchShifterEffect : public Effect
return -6.0026608f + p * (6.8773512f - 1.5838104f * p);
}

// designed to use minimal CPU if the input isn't loud
float safetySaturate(float input)
{
float absInput = std::abs(input);
return absInput <= 16 ? input : std::copysign((absInput - 16) / (1 + (absInput - 16) * 0.001f) + 16, input);
}

void sampleRateNeedsUpdate() { m_sampleRateNeedsUpdate = true; }

void changeSampleRate();
Expand Down Expand Up @@ -147,10 +155,12 @@ class GranularPitchShifterEffect : public Effect
std::array<PrefilterLowpass, 2> m_prefilter;
std::array<double, 2> m_speed = {1, 1};
std::array<double, 2> m_truePitch = {0, 0};
std::array<float, 2> m_dcVal = {0, 0};

float m_sampleRate;
float m_nyquist;
float m_nextWaitRandomization = 1;
float m_dcCoeff;

int m_ringBufLength = 0;
int m_writePoint = 0;
Expand Down

0 comments on commit a96666d

Please sign in to comment.