· 8 min read
Delay in code: ring buffer, time, feedback, mix and tone
Delay is the simplest audio effect to write in code, which makes it the right place to start. The whole effect is one array of numbers, two indexes and four parameters. Here we take it apart piece by piece: what it does, what the memory it lives in looks like, and what the code looks like, with numbers you can change and hear right away.
What a delay is
A delay records what you play and plays it back a little later, like an echo in the mountains. The first delays were tape machines, with tape running past a record head and then a playback head. Analog BBD chips came next, passing the signal along a row of capacitors like buckets along a line of people, and today most delays are digital: the sound is turned into numbers, the numbers are stored in memory and read back later.
Digital audio is just a list of numbers. At 48 kHz, 48,000 numbers (samples) arrive every second, each one saying how far the speaker cone is pushed at that moment. A delay of half a second means: the output is whatever arrived 24,000 samples ago.
Four parameters
- Time (DELAY_MS): how late the repeat comes. Under 15 ms you do not hear an echo but a metallic color, 20–40 ms sounds like a doubled guitar, 80–150 ms is rock ’n’ roll slapback, and 300–600 ms gives classic repeats. For a delay in time with the song: ms = 60000 / BPM (a quarter note at 120 BPM is 500 ms).
- Feedback: how much of the output goes back into the memory. At 0 you get one repeat, at 0.5 each repeat is half as loud as the last, and close to 1 the repeats barely stop.
- Mix: the balance of the clean signal (dry) and the repeats (wet). At 0 you hear only the guitar, at 1 only the repeats.
- Tone: a low-pass filter in the feedback loop. Every repeat goes through it once more, so each one is darker than the last. That is how tape and analog delays sound, and it keeps the repeats from fighting the guitar for the highs.
The ring buffer: memory that goes round
A delay needs memory for the last two seconds of sound, and the sound never stops coming. The answer is an array of fixed length used in a circle: a ring buffer. At 48 kHz two seconds are 96,000 cells. One index, write, points to where the next sample goes, and after every sample it moves one place on. When it reaches the end of the array it jumps back to the start (that is what % N, the remainder, does) and overwrites the oldest sound, which we no longer need.
The other index, read, sits exactly d cells behind write. Whatever was written d samples ago is read now. That is the whole trick: the delay time is nothing but the gap between the two heads. The picture above runs the same algorithm in slow motion on 40 cells: send a note and watch the read head catch up with it while feedback writes it in again, quieter each time.
The code, line by line
The pseudocode above is the whole effect. For every sample that arrives:
- Read the past.
yis the sample written d steps ago: that is the repeat. - Darken it.
lpis the simplest low-pass filter: each time it moves only part of the way (a) towards the new value, so fast changes, the highs, are smoothed out. A lower TONE_HZ gives a smalleraand a darker sound. - Write the input plus some of the repeat. That is why a repeat repeats: what was read goes back into the memory multiplied by FEEDBACK and waits for the read head to reach it again.
- Move the head one place on, around the circle.
- Mix the clean signal and the repeat by MIX.
Why is FEEDBACK capped at 0.95? At 1 every repeat would be as loud as the one before and the sound would never die out, and above 1 it would grow until it hits the maximum. Some pedals allow that on purpose for “self-oscillation”, but in code it is a limit you have to guard yourself.
Try changing DELAY_MS while the sound plays: you will hear a click. When d jumps, the read head lands somewhere else in the memory and the output wave is cut. Real pedals move the head gradually instead, so you hear a short pitch “stretch”. That same idea, a head that keeps moving gently, is what chorus and flanger are made of, and it is the topic of the next article.
And inside a pedal?
A pedal does exactly the same, only on a small chip. Memory is the real cost there: 2 seconds at 48 kHz are 96,000 samples, and at 32 bits per sample that is about 384 KB, more than many small microcontrollers have. That is why delay pedals carry extra memory or save on resolution. The popular PT2399 chip, found in many “analog” delay pedals, is actually digital and does just this with rougher samples, which is why its repeats sound warm and dirty.
A microcontroller does not process one sample the moment it arrives but small blocks, say 32 or 64 samples, while the next block is already filling up. The loop from the code above simply runs 64 times in a row. Everything else (setting up the chip, interrupts, copying blocks) is technique; the math stays the same.
How it looks on real hardware: DMA, the double buffer and the callback on an STM32 →


