Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

c++ - 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++; }}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you aware of the fact that return exits the function immediately? So the code after that which stores values to your buffers, is never executed.

Instead, you should place the return call at the end of the function.

Some other notes:

  • It is unclear to me why you need pointers to buffers if you only ever use the first element.
  • You have duplicate code in the -L and -R functions. Instead, use two instances of a 'mono' class, so you only store data for a single channel per class.
  • You (almost) never need to use this->. Just leave it out.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...