在类函数方法内将值存储在缓冲区中

发布于 2024-10-15 23:18:12 字数 3715 浏览 1 评论 0原文

我正在用 C++ 编写 VST DSP 插件。

我正在“滤波器组”中创建一系列带通滤波器。我已经在标头(包括函数)中实现了一个过滤器类,并在 .cpp 中正确构建了构造函数/析构函数。

我可以将值传递给该方法并返回它们。然而,问题在于函数缓冲区中存储数据的区域。似乎每次调用函数方法时,存储在缓冲区中的值都会被重置(或者,一开始就没有正确存储)。因此,传回的内容并不“完整”。

任何建议都非常感谢!

注:这篇文章已更新为新代码:

这是课程:

class aFilterL

{

friend class Beat_to_Midi;

民众: 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;

虚拟浮点aFilterMethodL(浮点a0,浮点a1,浮点a2,浮点b1,浮点b2,浮点inputL,浮点prevInput1L,浮点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;

}

};

aFilterR类 {

朋友类 Beat_to_Midi;

民众: 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;

虚拟浮点aFilterMethodR(浮点a0,浮点a1,浮点a2,浮点b1,浮点b2,浮点inputR,浮点prevInput1R,浮点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;

} };

然后在 cpp 中构造/销毁它,如下所示:

aFilterL::aFilterL()

{ fOut1_l = 0.f; 过滤输出1_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;

}

最后在 processReplacing 函数中实现为:

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

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) {

// 过滤器输入

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];

// 管理中的过滤器

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;

// 发送到函数进行处理

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

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

// 将过滤器输出发送到 out

*out1 = returnedL;

*out2=返回R;

*in1++;

*in2++;

*输出1++;

*输出2++; }}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2024-10-22 23:18:12

您是否知道 return 会立即退出该函数?因此,之后将值存储到缓冲区的代码永远不会执行。

相反,您应该将返回调用放在函数的末尾。

其他一些注意事项:

  • 我不清楚如果您只使用第一个元素,为什么需要指向缓冲区的指针。
  • -L 和 -R 函数中有重复的代码。相反,请使用“单声道”类的两个实例,这样您只需为每个类存储一个通道的数据。
  • 您(几乎)永远不需要使用 this->。把它留下吧。

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.
莫相离 2024-10-22 23:18:12

此处找到关于嵌套类的新问题后,解决方案是成立。

过滤器类在 myPlugin 类中声明。

从这里构建构造函数和析构函数如下:

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

在 myPlugin 构造函数中创建新实例:

aFilterL *my_aFilter1L = new aFilterL();

然后难题的最后一部分是确保它们作为实例添加到 myPlugin 效果中:

aFilterL my_aFilter1L;
aFilterR my_aFilter1R;

现在可以通过 processReplacing 访问 my_aFilter1L 等并且似乎运行正常。

非常感谢大家在此事上提供的所有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文