#include "DelayLine.h"
DelayLine::DelayLine(int M, float g)
: M(static_cast<size_t>(M)), g(g), writeIndex(0)
{
// Find next power of two >= M+1 and compute mask
size_t bufferSize = 1;
while (bufferSize < M + 1)
bufferSize <<= 1;
mask = bufferSize - 1;
buffer.resize(bufferSize, 0.0f);
}
float DelayLine::read() const
{
size_t readIndex = (writeIndex - M) & mask;
return buffer[readIndex];
}
float DelayLine::read(int tau) const
{
size_t readIndex = (writeIndex - tau) & mask;
return buffer[readIndex];
}
void DelayLine::write(float input)
{
size_t readIndex = (writeIndex - M) & mask;
float delayedSample = buffer[readIndex];
buffer[writeIndex] = input + g * delayedSample;
writeIndex = (writeIndex + 1) & mask;
}
void DelayLine::process(float* block, int blockSize)
{
for (int i = 0; i < blockSize; ++i)
{
float x = block[i];
float y = read();
write(x);
block[i] = y;
}
}
for (int ch = 0; ch < buffer.getNumChannels(); ++ch) {
float* channelData = buffer.getWritePointer(ch);
for (int sample = 0; sample < buffer.getNumSamples(); ++sample) {
float x = channelData[sample];
float y = 0;
for (int m = 0; m < k.size(); ++m) {
y += z[ch]->read(k[m]);
}
z[ch]->write(x);
channelData[sample] = y * (1.0f / 600);
}
}
This code implements a multi-tap delay effect applied to a multi-channel audio buffer. For each channel, it reads delayed samples from the delay line at multiple offsets specified in the vector k before writing the current input sample. The problem is that this code does not scale efficiently. For example, when the number of delay taps (k) grows very large (e.g., around 3000), significant audio glitches and performance issues occur. How can I fix this?
