Search code examples
c++functionobjectorientationvst

Storing values in buffer, within class function method


I am programming a VST DSP plugin in c++.

I am creating a series of band pass filters in a 'filterbank'. I have implemented a filter class in my header (including function) and built constructor/destructor correctly in .cpp.

I can pass values to the method and return them also. However, the issues lays in the area of storing data in buffers in the function. It seems that every time the function method is called that the values stored in the buffer are reset (or alternatively, are not stored correctly in the first place). Therefore, what is passed back is not 'complete'.

Any advice greatly appreciated!

n.b. This post has been update with new code:

Here's the classes:

class aFilterL

{

friend class Beat_to_Midi;

public: aFilterL(); ~aFilterL();

float fOut1_l;
float filterOut1_l;
float Out_1_l;
float Out_2_l;
float* buffer_Out_1_l;
float* buffer_Out_2_l;

virtual float aFilterMethodL (float a0, float a1, float a2, float b1, float b2, float inputL, float prevInput1L, float prevInput2L) {

Out_1_l = buffer_Out_1_l[0];
Out_2_l = buffer_Out_2_l[0];

filterOut1_l = (a0 * inputL) + (a1 * prevInput1L) + (a2 * prevInput2L) - (b1 * Out_1_l) - (b2 * Out_2_l) + 1.0E-25;

fOut1_l = filterOut1_l;
buffer_Out_2_l[0] = buffer_Out_1_l[0];
buffer_Out_1_l[0] = fOut1_l;  
return fOut1_l;

}

};

class aFilterR {

friend class Beat_to_Midi;

public: aFilterR(); ~aFilterR();

float fOut1_r;
float filterOut1_r;
float Out_1_r;
float Out_2_r;
float* buffer_Out_1_r;
float* buffer_Out_2_r;

virtual float aFilterMethodR (float a0, float a1, float a2, float b1, float b2, float inputR, float prevInput1R, float prevInput2R) {

Out_1_r = buffer_Out_1_r[0];
Out_2_r = buffer_Out_2_r[0];

filterOut1_r = (a0 * inputR) + (a1 * prevInput1R) + (a2 * prevInput2R) - (b1 * Out_1_r) - (b2 * Out_2_r) + 1.0E-25;

fOut1_r = filterOut1_r;
buffer_Out_2_r[0] = buffer_Out_1_r[0];
buffer_Out_1_r[0] = fOut1_r;
return fOut1_r;

} };

This is then constructed/destructed in the cpp as follows:

aFilterL::aFilterL()

{ fOut1_l = 0.f; filterOut1_l = 0.f;

Out_1_l = 0.f;
Out_2_l = 0.f;

buffer_Out_1_l = new float [1];
buffer_Out_2_l = new float [1];

buffer_Out_1_l[0] = 0.f;
buffer_Out_2_l[0] = 0.f;

}

aFilterL::~aFilterL() {

if (buffer_Out_1_l)
    delete[] buffer_Out_1_l;
if (buffer_Out_2_l)
    delete[] buffer_Out_2_l;

}

aFilterR::aFilterR() { fOut1_r = 0.f;

filterOut1_r = 0.f;

Out_1_r = 0.f;
Out_2_r = 0.f;

buffer_Out_1_r = new float [1];
buffer_Out_2_r = new float [1];

buffer_Out_1_r[0] = 0.f;
buffer_Out_2_r[0] = 0.f;

}

aFilterR::~aFilterR() {

if (buffer_Out_1_r)
    delete[] buffer_Out_1_r;
if (buffer_Out_2_r)
    delete [] buffer_Out_2_r;

}

Finally it is implemented in the processReplacing function as:

void myPlugin::processReplacing (float** inputs, float** outputs, VstInt32 sampleFrames) {

float* in1  =  inputs[0]; 
float* in2  =  inputs[1]; 
float* out1 = outputs[0]; 
float* out2 = outputs[1]; 

    aFilterL *my_aFilter1L = new aFilterL;
aFilterR *my_aFilter1R = new aFilterR;

while (--sampleFrames >= 0) {

// Filter Input

In_1_l = buffer_In_1_l[0];

In_1_r = buffer_In_1_r[0];

In_2_l = buffer_In_2_l[0];

In_2_r = buffer_In_2_r[0];

// Filter in management

buffer_In_2_l[0] = buffer_In_1_l[0];

buffer_In_2_r[0] = buffer_In_1_r[0];

buffer_In_1_l[0] = *in1;

buffer_In_1_r[0] = *in2;

// send to function for processing

returnedL = my_aFilter1L->aFilterMethodL(0.000171f, 0.0f, -0.000171f, -1.999911f, 0.999943f, *in1, In_1_l, In_2_l);

returnedR = my_aFilter1R->aFilterMethodR(0.000171f, 0.0f, -0.000171f, -1.999911f, 0.999943f, *in2, In_1_r, In_2_r);

// Send filter output to out

*out1 = returnedL;

*out2 = returnedR;

*in1++;

*in2++;

*out1++;

*out2++; }}


Solution

  • After creating my new question on nested classes found here the solution has been found.

    The filter classes are declared within the myPlugin class.

    From here constructors and destructors are built as:

    myPlugin::aFilterL::aFilterL()
    myPlugin::aFilterL::~aFilterL()
    

    in the myPlugin constructor the new instances are created:

    aFilterL *my_aFilter1L = new aFilterL();
    

    and the final piece in the puzzle is then to make sure they are added as an instance to the myPlugin effect:

    aFilterL my_aFilter1L;
    aFilterR my_aFilter1R;
    

    my_aFilter1L etc can now be accessed via processReplacing and seems to be functioning correctly.

    Many thanks to everyone for all your help in this matter.