Common Audio Clicking Questions

Hello All,

I’m a pretty new programmer, but I have a question about audio clicks.
I’m hearing some audio clicks in my plugin, and I was curious about some reasons why clicking would happen.

To my understanding, clicks can occur on edge conditions for a circular buffer. For example, let’s say you have a delay buffer 10 samples long. At this moment, the circular buffer’s pointer is at the 9th position. You write your current sample to buffer then you increment your bufferIndex (now at the 10th position, which doesn’t exist). Then you have to check in your code if your buffer if the writeIndex and the bufferSize relationship is in bounds:

if (m_nLeftWriteIndex > m_nLeftBufferSize) m_nLeftWriteIndex = m_nLeftWriteIndex - m_nLeftBufferSize; // checking if buffer sample is in bounds

When I originally wrote the code is used the “>” symbol instead of the “>=” and I heard clicks because I was writing to the buffer at the 10th which doesn’t exist.

Why does this click occur? Here are my guesses/questions I have:

  1. There’s a big discontinuity the between the buffer at the 9th position and buffer at the 0th position. The physical speaker can’t respond to the discontinuity change so fast that it clicks?
  2. When the program tries to write to the buffer at the 10th position, why doesn’t the program crash? It’s trying to access memory outside array’s location where it’s undefined; it should crash. Is the click basically a “small crash”?

Also, when do other common clicks occur? It would help me prevent some coding pitfalls.

C and C++ don’t provide any guarantees about that, it’s “undefined behavior”. What will “usually” happen is that you will be accessing garbage values from the memory. (Or perhaps memory that belongs to some other array, variable or object…) If the conditions are just right, you might get an actual crash, but you must not count on that happening to tell you something went wrong.

What you should do is to 100% ensure your array accesses are within the allocated array memory. If this is too tricky to ensure for some reason, you could overallocate your array a bit, and initialise its contents to zeros or some other suitable value that will work. But for example delay lines should not usually need anything like that.