Hearing protection in audio plugins

I’ve nearly finished my VST3 plugin, but before releasing it I’ve got a concern I wanted to ask about. What if the source code has some bug in it that would lead to accidental reproduction of some very loud sound? Are there some mechanisms that could help preventing this from happening?

I understand that the output buffer is limited to 1.0f value and theoretically I can’t write anything higher than that even by accident. Moreover, I don’t touch gain in my code, so the effective loudness should depend only on the gain parameter that user has selected in their DAW. So I think it should be rather safe, but I want to double-check.

Welcome to my nightmare!

Oooh you definitely can! There is no limit to what you can put in the audio buffer.

I generally put a limiter at the end of the signal chain that silences the output if anything goes over [-10, 10] (which is +20 dB) or has nan/inf in the buffer.

I’ve had a report for one plugin I worked on blew up someone’s speakers. This was before that limiter was installed. If true that it was my plugin indeed, this is really bad. The only thing worse is blowing someone’s ear drums out. However, I have not been able to reproduce this issue even with extensive testing. So is it really my plugin’s fault?

In any case, this is why pretty much all software has a EULA that says the software is delivered as-is without warranties of any kind. Users use it at their own risk. Even so, you’ll want to test your software as much as possible to avoid these situations; you don’t want to get a reputation for harming users or their equipment.

Dang, that sounds scary :worried:

Can you elaborate a bit on the limiter part? Is it some built-in JUCE thing or I should develop it myself?

Also, would it be enough if I add a logic before writing output samples to the buffer that truncates them to range [-1.0f…+1.0f] if the sample exceeds this range?

I wouldn’t truncate to [-1, 1] because it’s OK for sound levels to go over 0 dBFS on individual tracks, as long as the user dials it down on the master bus again (or on the next plugin in the track). In fact, on Mac you can go much louder even on output and it will sound fine.

The limiter I mentioned isn’t a real limiter with lookahead and stuff. I’m simply clipping to [-10, 10] since that’s louder than reasonable, so if that happens you’re probably hitting a bug somewhere and something is blowing up in your audio processing.

For an example implementation, see here.

I also do something like this during development to protect my own ears: gist link.

Thank you!

Would be a cool feature for JUCE though, wouldn’t it folks?

If you only want to clip samples between two values, FloatVectorOperations may be faster.

for (size_t channel = 0; channel < block.getNumChannels(); ++channel) {
    juce::FloatVectorOperations::clip(block.getChannelPointer(channel),
                                      block.getChannelPointer(channel),
                                      static_cast<FloatType>(-1.0),
                                      static_cast<FloatType>(1.0),
                                      static_cast<int>(block.getNumSamples()));
}
2 Likes

FYI : < Extreme danger of perforating eardrum from audioDeviceIOCallback >

1 Like

a good way to prevent this from happening is to ensure filters are always modulated as smoothly as possible