Skip to content

Commit

Permalink
Merged revision(s) 20302 from trunk/OpenMPT:
Browse files Browse the repository at this point in the history
[Fix] Chorus/Flanger: Avoid occasional popping once the mix rate exceeds ~136.5 kHz (https://bugs.openmpt.org/view.php?id=1752).
[Mod] Chorus/Flanger: Rename Triangle waveshape to Square, because that's what those convoluted computations really end up doing in the end.
........


git-svn-id: https://source.openmpt.org/svn/openmpt/branches/OpenMPT-1.31@20303 56274372-70c3-4bfc-bfc3-4c3a0b034d27
  • Loading branch information
sagamusix committed Mar 8, 2024
1 parent 5c01e25 commit 66316b5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions soundlib/plugins/dmo/Chorus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "stdafx.h"

#ifndef NO_PLUGINS
#include "../../Sndfile.h"
#include "Chorus.h"
#include "../../Sndfile.h"
#include "mpt/base/numbers.hpp"
#endif // !NO_PLUGINS

Expand Down Expand Up @@ -51,7 +51,7 @@ int32 Chorus::GetBufferIntOffset(int32 fpOffset) const
if(fpOffset < 0)
fpOffset += m_bufSize * 4096;
MPT_ASSERT(fpOffset >= 0);
return (fpOffset / 4096) % m_bufSize;
return (m_bufPos + (fpOffset / 4096)) % m_bufSize;
}


Expand All @@ -63,7 +63,7 @@ void Chorus::Process(float *pOutL, float *pOutR, uint32 numFrames)
const float *in[2] = { m_mixBuffer.GetInputBuffer(0), m_mixBuffer.GetInputBuffer(1) };
float *out[2] = { m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1) };

const bool isTriangle = IsTriangle();
const bool isSquare = IsSquare();
const float feedback = Feedback() / 100.0f;
const float wetDryMix = WetDryMix();
const uint32 phase = Phase();
Expand All @@ -74,8 +74,8 @@ void Chorus::Process(float *pOutL, float *pOutR, uint32 numFrames)
const float leftIn = *(in[0])++;
const float rightIn = *(in[1])++;

const int32 readOffset = GetBufferIntOffset(m_bufPos + m_delayOffset);
const int32 writeOffset = GetBufferIntOffset(m_bufPos);
const int32 readOffset = GetBufferIntOffset(m_delayOffset);
const int32 writeOffset = m_bufPos;
if(m_isFlanger)
{
m_DryBufferL[m_dryWritePos] = leftIn;
Expand All @@ -89,7 +89,7 @@ void Chorus::Process(float *pOutL, float *pOutR, uint32 numFrames)

float waveMin;
float waveMax;
if(isTriangle)
if(isSquare)
{
m_waveShapeMin += m_waveShapeVal;
m_waveShapeMax += m_waveShapeVal;
Expand All @@ -110,14 +110,14 @@ void Chorus::Process(float *pOutL, float *pOutR, uint32 numFrames)
const float leftDelayIn = m_isFlanger ? m_DryBufferL[(m_dryWritePos + 2) % 3] : leftIn;
const float rightDelayIn = m_isFlanger ? m_DryBufferR[(m_dryWritePos + 2) % 3] : rightIn;

float left1 = m_bufferL[GetBufferIntOffset(m_bufPos + m_delayL)];
float left2 = m_bufferL[GetBufferIntOffset(m_bufPos + m_delayL + 4096)];
float left1 = m_bufferL[GetBufferIntOffset(m_delayL)];
float left2 = m_bufferL[GetBufferIntOffset(m_delayL + 4096)];
float fracPos = static_cast<float>(m_delayL & 0xFFF) * (1.0f / 4096.0f);
float leftOut = (left2 - left1) * fracPos + left1;
*(out[0])++ = leftDelayIn + (leftOut - leftDelayIn) * wetDryMix;

float right1 = bufferR[GetBufferIntOffset(m_bufPos + m_delayR)];
float right2 = bufferR[GetBufferIntOffset(m_bufPos + m_delayR + 4096)];
float right1 = bufferR[GetBufferIntOffset(m_delayR)];
float right2 = bufferR[GetBufferIntOffset(m_delayR + 4096)];
fracPos = static_cast<float>(m_delayR & 0xFFF) * (1.0f / 4096.0f);
float rightOut = (right2 - right1) * fracPos + right1;
*(out[1])++ = rightDelayIn + (rightOut - rightDelayIn) * wetDryMix;
Expand All @@ -131,8 +131,8 @@ void Chorus::Process(float *pOutL, float *pOutR, uint32 numFrames)
m_delayR = m_delayOffset + (phase < 2 ? -1 : 1) * static_cast<int32>(((phase % 2u) ? waveMax : waveMin) * m_depthDelay);

if(m_bufPos <= 0)
m_bufPos += m_bufSize * 4096;
m_bufPos -= 4096;
m_bufPos += m_bufSize;
m_bufPos--;
}

ProcessMixOps(pOutL, pOutR, m_mixBuffer.GetOutputBuffer(0), m_mixBuffer.GetOutputBuffer(1), numFrames);
Expand Down Expand Up @@ -179,16 +179,16 @@ void Chorus::Resume()

m_isResumed = true;
m_waveShapeMin = 0.0f;
m_waveShapeMax = IsTriangle() ? 0.5f : 1.0f;
m_waveShapeMax = IsSquare() ? 0.5f : 1.0f;
m_delayL = m_delayR = m_delayOffset;
m_bufPos = 0;
m_dryWritePos = 0;
}


void Chorus::PositionChanged()
{
m_bufSize = Util::muldiv(m_SndFile.GetSampleRate(), 3840, 1000);
m_bufPos = 0;
try
{
m_bufferL.assign(m_bufSize, 0.0f);
Expand Down Expand Up @@ -255,7 +255,7 @@ CString Chorus::GetParamDisplay(PlugParamIndex param)
value = FrequencyInHertz();
break;
case kChorusWaveShape:
return (value < 1) ? _T("Triangle") : _T("Sine");
return (value < 1) ? _T("Square") : _T("Sine");
break;
case kChorusPhase:
switch(Phase())
Expand Down Expand Up @@ -289,7 +289,7 @@ void Chorus::RecalculateChorusParams()
m_delayOffset = mpt::saturate_round<int32>(4096.0f * (delaySamples + 2.0f));
m_frequency = FrequencyInHertz();
const float frequencySamples = m_frequency / sampleRate;
if(IsTriangle())
if(IsSquare())
m_waveShapeVal = frequencySamples * 2.0f;
else
m_waveShapeVal = std::sin(frequencySamples * mpt::numbers::pi_v<float>) * 2.0f;
Expand Down
2 changes: 1 addition & 1 deletion soundlib/plugins/dmo/Chorus.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Chorus : public IMixPlugin
int32 GetBufferIntOffset(int32 fpOffset) const;

virtual float WetDryMix() const { return m_param[kChorusWetDryMix]; }
virtual bool IsTriangle() const { return m_param[kChorusWaveShape] < 1; }
virtual bool IsSquare() const { return m_param[kChorusWaveShape] < 1; }
virtual float Depth() const { return m_param[kChorusDepth]; }
virtual float Feedback() const { return -99.0f + m_param[kChorusFeedback] * 198.0f; }
virtual float Delay() const { return m_param[kChorusDelay] * 20.0f; }
Expand Down
4 changes: 2 additions & 2 deletions soundlib/plugins/dmo/Flanger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#include "stdafx.h"

#ifndef NO_PLUGINS
#include "../../Sndfile.h"
#include "Flanger.h"
#endif // !NO_PLUGINS
#include "../../Sndfile.h"
#endif // !NO_PLUGINS

OPENMPT_NAMESPACE_BEGIN

Expand Down
2 changes: 1 addition & 1 deletion soundlib/plugins/dmo/Flanger.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Flanger final : public Chorus

protected:
float WetDryMix() const override { return m_param[kFlangerWetDryMix]; }
bool IsTriangle() const override { return m_param[kFlangerWaveShape] < 1; }
bool IsSquare() const override { return m_param[kFlangerWaveShape] < 1; }
float Depth() const override { return m_param[kFlangerDepth]; }
float Feedback() const override { return -99.0f + m_param[kFlangerFeedback] * 198.0f; }
float Delay() const override { return m_param[kFlangerDelay] * 4.0f; }
Expand Down

0 comments on commit 66316b5

Please sign in to comment.