Skip to content

Instantly share code, notes, and snippets.

@hollance
Created June 26, 2022 18:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hollance/b7219e49c6fc16fa2af05c4a72c186ef to your computer and use it in GitHub Desktop.
Save hollance/b7219e49c6fc16fa2af05c4a72c186ef to your computer and use it in GitHub Desktop.
For testing audio code with headphones on...
#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;
}
}
}
@Rincewind34
Copy link

The silence command is inside the loop. Is this by design or a mistake? I think it doesn't really make sense to do this inside the loop. Otherwise, thanks for providing this!

@hollance
Copy link
Author

No point in continuing the loop if a nan or inf is detected. It immediately sets the entire buffer to zeros and then returns.

@Rincewind34
Copy link

Rincewind34 commented Jun 30, 2022

Oh, I completely skipped over the return statement... Now I feel like an idiot thanks! :D

@anr-ableton
Copy link

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....

@hollance
Copy link
Author

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.

@anr-ableton
Copy link

@hollance interesting, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment