// MyDelay.h
class MyDelay
{
public:
MyDelay();
~MyDelay();
double process(double input, double FB); // FB = feedback amount
void setDelay(int);
private:
double mBuffer2[44100] = {}; // array of doubles
double* wptr = mBuffer2; // pointer to adress of first element of array
double* rptr = mBuffer2;
double out;
};
// MyDelay.cpp
#include "MyDelay.h"
double MyDelay::process(double input, double FB) {
*wptr++ = input + out * FB; // move to next element
out = *rptr++;
// wrap around when end of array is reached
if ((wptr - mBuffer2) >= 44100) { wptr -= 44100; }
if ((rptr - mBuffer2) >= 44100) { rptr -= 44100; }
return out;
}
void MyDelay::setDelay(int delayTime) {
rptr = wptr - delayTime;
// wrap around
while (rptr < mBuffer2) { rptr += 44100; }
}
This works fine.
But actually I'd like to use a vector of doubles for the buffer instead of an array because the buffer has to be resizeable dynamically, which I can't do with an array afaik.
As I'm quite new to vectors (and actually C++ in general) how would I go about this ?
Can I perform the same pointer arithmetic using a vector instead of an array ?
http://en.cppreference.com/w/cpp/container/vector/data will give you a pointer to the start of the buffer.
Keep in mind that changing the size may reallocate the buffer, so you'll need to update your base pointers (or use an index instead).
> wrap around when end of array is reached
consider to use boost::circular_buffer