home/ atoms/ circular-buffer-delay-line

A delay line is a circular buffer read behind a rotating write pointer

A delay line stores audio in a fixed-size circular (ring) buffer and replays it after a set time using two pointers. The write (record) head advances every sample, storing the newest value and overwriting the oldest; the read (playback) head lags by the delay length in samples. When either pointer reaches the buffer end it wraps to the start. Varying the read-head offset in real time changes the delay time; mixing the delayed output back into the input before writing adds feedback, creating echo. Feedback below 1.0 decays, exactly 1.0 sustains, above 1.0 diverges. The buffer must be at least max-delay-time × sample-rate samples long. The core off-by-one error is a read pointer that laps the write pointer, returning audio not yet written; correct wrapping is read = (write − delaySamples + bufferSize) % bufferSize. Multiple read heads on one buffer give multitap echo, and an LFO on the read offset yields modulated delays (chorus, flanger) and pitch shifting.

Examples

500 ms delay at 44100 Hz needs a 22050-sample buffer. In Max, tapin~ 2000 allocates a 2-second buffer (the write head) and tapout~ 500 reads 500 ms behind it; two tapout~ at 250 ms and 750 ms give multitap echo, and modulating a tapout~ delay time in real time pitch-shifts by changing read-head speed.

Assessment

Sketch circular-buffer code for a 300 ms delay at 44100 Hz: what minimum array size is required, and what is the pointer-wrap formula? Draw an 8-slot buffer showing write and read heads for a 3-sample delay, advance the write head 2 samples, and redraw. What happens perceptually if feedback exceeds 1.0?

“create delays and delay-based audio effects such as echo, chorus and flanger effects”
“reads the same circular buffer at a certain distance (given by the argument) with respect to the record head of the tapin~ object it is”
corpus · electronic-music-and-sound-design-vol-2-max-8-cipriani-and-g · chunk 17