Skip to content

Commit

Permalink
Reset I/O processor if NaNs are detected in the audio buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Jan 2, 2024
1 parent 0916f18 commit 4693839
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/BYOD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void BYOD::processBlock (AudioBuffer<float>& buffer, MidiBuffer& midi)
// real processing here!
procs->processAudio (buffer, midi);

chowdsp::BufferMath::sanitizeBuffer<AudioBuffer<float>, float> (buffer);
if (! chowdsp::BufferMath::sanitizeBuffer<AudioBuffer<float>, float> (buffer))
procs->reset();
}

void BYOD::processBlockBypassed (AudioBuffer<float>& buffer, MidiBuffer&)
Expand Down
1 change: 1 addition & 0 deletions src/headless/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ target_sources(BYOD_headless PRIVATE

tests/AmpIRsSaveLoadTest.cpp
tests/BadModulationTest.cpp
tests/NaNResetTest.cpp
tests/ParameterSmoothTest.cpp
tests/PreBufferTest.cpp
tests/PresetsTest.cpp
Expand Down
40 changes: 40 additions & 0 deletions src/headless/tests/NaNResetTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "UnitTests.h"

class NaNResetTest : public UnitTest
{
public:
NaNResetTest() : UnitTest ("NaN Reset Test")
{
}

void runTest() override
{
beginTest ("NaN Reset Test");

static constexpr int blockSize = 512;

BYOD byod;
byod.prepareToPlay (48000.0, blockSize);

MidiBuffer midi;
AudioBuffer<float> buffer { 2, blockSize };

{
for (auto [_, data] : chowdsp::buffer_iters::channels (buffer))
std::fill (data.begin(), data.end(), std::numeric_limits<float>::quiet_NaN());
byod.processBlock (buffer, midi);
const auto mag = chowdsp::BufferMath::getMagnitude (buffer);
expectEquals (mag, 0.0f);
}

{
for (auto [_, data] : chowdsp::buffer_iters::channels (buffer))
std::fill (data.begin(), data.end(), 1.0f);
byod.processBlock (buffer, midi);
const auto mag = chowdsp::BufferMath::getMagnitude (buffer);
expectGreaterOrEqual (mag, 1.0f);
}
}
};

static NaNResetTest nanResetTest;
11 changes: 11 additions & 0 deletions src/processors/chain/ChainIOProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ void ChainIOProcessor::prepare (double sampleRate, int samplesPerBlock)
isPrepared = true;
}

void ChainIOProcessor::reset() noexcept
{
oversampling.reset();

inGain.reset();
outGain.reset();

ioBuffer.clear();
dryWetMixer.reset();
}

int ChainIOProcessor::getOversamplingFactor() const
{
if (! isPrepared)
Expand Down
1 change: 1 addition & 0 deletions src/processors/chain/ChainIOProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ChainIOProcessor

static void createParameters (Parameters& params);
void prepare (double sampleRate, int samplesPerBlock);
void reset() noexcept;

int getOversamplingFactor() const;
dsp::AudioBlock<float> processAudioInput (const AudioBuffer<float>& buffer, bool& sampleRateChanged);
Expand Down
5 changes: 5 additions & 0 deletions src/processors/chain/ProcessorChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ void ProcessorChain::prepare (double sampleRate, int samplesPerBlock)
initializeProcessors();
}

void ProcessorChain::reset() noexcept
{
ioProcessor.reset();
}

void ProcessorChain::runProcessor (BaseProcessor* proc, AudioBuffer<float>& buffer, bool& outProcessed)
{
TRACE_DSP();
Expand Down
1 change: 1 addition & 0 deletions src/processors/chain/ProcessorChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProcessorChain : private AudioProcessorValueTreeState::Listener
static void createParameters (Parameters& params);
void prepare (double sampleRate, int samplesPerBlock);
void processAudio (AudioBuffer<float>& buffer, const MidiBuffer& hostMidiBuffer);
void reset() noexcept;

auto& getProcessors() { return procs; }
const auto& getProcessors() const { return procs; }
Expand Down

0 comments on commit 4693839

Please sign in to comment.