Skip to content

Commit 1277ae7

Browse files
committed
Rez Synth: fix NaN audio output during AUVal stress test
1 parent 0116cfb commit 1277ae7

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

rezsynth/rezsynthsubprocesses.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ To contact the author, use the contact form at http://destroyfx.org/
2323

2424
#include <cmath>
2525
#include <tuple>
26+
#include <type_traits>
2627

2728
//-----------------------------------------------------------------------------------------
2829
// this function tries to even out the wildly erratic resonant amplitudes
@@ -74,7 +75,7 @@ int RezSynth::calculateCoefficients(int currentNote)
7475
for (int bandcount = 0; bandcount < mNumBands; bandcount++)
7576
{
7677
// GET THE CURRENT BAND'S CENTER FREQUENCY
77-
mBandCenterFreq[currentNote][bandcount] = [this, baseFreq, bandcount]()
78+
mBandCenterFreq[currentNote][bandcount] = [this, baseFreq, bandcount]
7879
{
7980
// do logarithmic band separation, octave-style
8081
if (mSepMode == kSeparationMode_Octaval)
@@ -207,18 +208,33 @@ void RezSynth::processFilterOuts(float const* const* inAudio, float* const* outA
207208

208209
for (unsigned long ch = 0; ch < numChannels; ch++)
209210
{
211+
auto const clampInfinities = [](double value)
212+
{
213+
#if __GNUC__
214+
if (__builtin_expect(std::isinf(value), 0)) // TODO: C++20 [[unlikely]]
215+
#else
216+
if (std::isinf(value))
217+
#endif
218+
{
219+
return std::copysign(std::numeric_limits<decltype(value)>::max(), value);
220+
}
221+
return value;
222+
};
223+
210224
double bandOutputSum = 0.;
211225
for (int bandIndex = 0; bandIndex < numBands; bandIndex++)
212226
{
213227
// filter using the input, delayed values, and their filter coefficients
214-
double const curBandOutValue = (mInputAmp[bandIndex] * (inAudio[ch][sampleIndex] - mPrevPrevInCoeff[bandIndex] * mPrevPrevInValue[ch][currentNote]))
215-
+ (mPrevOutCoeff[bandIndex] * mPrevOutValue[ch][currentNote][bandIndex])
216-
- (mPrevPrevOutCoeff[bandIndex] * mPrevPrevOutValue[ch][currentNote][bandIndex]);
228+
double curBandOutValue = (mInputAmp[bandIndex] * (inAudio[ch][sampleIndex] - mPrevPrevInCoeff[bandIndex] * mPrevPrevInValue[ch][currentNote]))
229+
+ (mPrevOutCoeff[bandIndex] * mPrevOutValue[ch][currentNote][bandIndex])
230+
- (mPrevPrevOutCoeff[bandIndex] * mPrevPrevOutValue[ch][currentNote][bandIndex]);
231+
curBandOutValue = clampInfinities(curBandOutValue);
217232

218233
bandOutputSum += curBandOutValue;
219234
// very old outValue gets old outValue and old outValue gets current outValue (no longer current)
220235
mPrevPrevOutValue[ch][currentNote][bandIndex] = std::exchange(mPrevOutValue[ch][currentNote][bandIndex], curBandOutValue);
221236
}
237+
bandOutputSum = clampInfinities(bandOutputSum);
222238
auto const scaledBandOutputSum = static_cast<float>(bandOutputSum * ampEvener);
223239

224240
// add the latest resonator to the output collection, scaled by my evener and user gain

0 commit comments

Comments
 (0)