There are two overloaded subscript operator methods. One is for reading the other for writing. The reading version takes a floating point index. This allows you to modulate reads using fractional indexes. It uses linear interpolation to calculate the results.
I don't like the int cast or the use of the modulo operator. If I could somehow restrict the delay line length to a power of two, I could use a mask instead of modulo. I'm not sure how to get around the int cast. I don't want to switch to a fixed point scheme, so I think I'm stuck with it.
Code: Select all
#ifndef DELAY_LINE_H
#define DELAY_LINE_H
#include <cassert>
#include <cstring>
template<int Length>
class DelayLine
{
private:
float data[Length];
public:
DelayLine()
{
Clear();
}
float operator[](float index) const
{
assert((index >= 0.0f) && (index < Length));
int x1 = (int)index;
int x2 = (x1 + 1) % Length;
float y1 = data[x1];
float y2 = data[x2];
float f = index - x1;
return (y2 - y1) * f + y1;
}
float &operator[](int index)
{
assert((index >= 0) && (index < Length));
return data[index];
}
void Clear()
{
memset(data, 0, sizeof(data));
}
int GetLength() const
{
return Length;
}
};
#endif
