Created
June 26, 2022 18:45
-
-
Save hollance/b7219e49c6fc16fa2af05c4a72c186ef to your computer and use it in GitHub Desktop.
For testing audio code with headphones on...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <JuceHeader.h> | |
/** | |
Silences the buffer if bad or loud values are detected in the output buffer. | |
Use this during debugging to avoid blowing out your eardrums on headphones. | |
If the output value is out of the range [-1, +1] it will be hard clipped. | |
*/ | |
inline void protectYourEars(float *buffer, int sampleCount) | |
{ | |
if (buffer == nullptr) { return; } | |
bool firstWarning = true; | |
for (int i = 0; i < sampleCount; ++i) { | |
float x = buffer[i]; | |
bool silence = false; | |
if (std::isnan(x)) { | |
DBG("!!! WARNING: nan detected in audio buffer, silencing !!!"); | |
silence = true; | |
} else if (std::isinf(x)) { | |
DBG("!!! WARNING: inf detected in audio buffer, silencing !!!"); | |
silence = true; | |
} else if (x < -2.0f || x > 2.0f) { // screaming feedback | |
DBG("!!! WARNING: sample out of range, silencing !!!"); | |
silence = true; | |
} else if (x < -1.0f) { | |
if (firstWarning) { | |
DBG("!!! WARNING: sample out of range, clamping !!!"); | |
firstWarning = false; | |
} | |
buffer[i] = -1.0f; | |
} else if (x > 1.0f) { | |
if (firstWarning) { | |
DBG("!!! WARNING: sample out of range, clamping !!!"); | |
firstWarning = false; | |
} | |
buffer[i] = 1.0f; | |
} | |
if (silence) { | |
memset(buffer, 0, sampleCount * sizeof(float)); | |
return; | |
} | |
} | |
} |
You could in theory get an oscillating wave between -1.999 and +1.999 and it would still be very loud, even after clipping. Most of the time, though, when things go wrong it's because of values exploding and going to infinity. That's what this protects against.
@hollance interesting, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
does this work? You would think if there was a certain exceeded dB threshold? Suppose a sample overshot +/-1.0f, is clamping it enough to prevent the feedback? Curious....