This is more of a c++ problem, but I've not hit it before so maybe someone here can shed some light.
I have a simple biquad class:
class Biquad
{
protected:
int state_ptr;
float coeffs[5];
float states_in[2];
float states_out[2];
public:
Biquad()
{
state_ptr= 0;
states_in[0] = states_in[1] = 0;
states_out[0] = states_out[1] = 0;
coeffs[0] = 1.0;
coeffs[1] = 0.0;
coeffs[2] = 0.0;
coeffs[3] = 0.0;
coeffs[4] = 0.0;
}
float filterSample(float in)
{
float out = in * coeffs[0] +
states_in[state_ptr] * coeffs[1] +
states_out[state_ptr] * coeffs[3];
state_ptr = state_ptr ^ 0x1;
out += states_in[state_ptr] * coeffs[2] +
states_out[state_ptr] * coeffs[4];
states_in[state_ptr] = in;
states_out[state_ptr] = out;
return out;
}
};
And if I use it in this simple application, all is well (well, it compiles and runs at least - i'm not really concerned with the functionality right now):
Biquad* filters;
bool setup(BelaContext *context, void *userData)
{
filters = new Biquad[num_voices];
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++)
{
float in = audioWrite(context, n, 0);
float out = 0;
for (int k = 0; k < num_voices; ++k)
float out += filters[k].filterSample(in);
audioWrite(context, n, 0, out);
}
}
But I have more than one channel, so I want to make Biquads a 2D array. Now I get a segmentation fault:
Biquad** filters;
bool setup(BelaContext *context, void *userData)
{
filters = new Biquad*[num_channels];
for (int ch = 0; ch < num_channels; ++ch)
filters[ch] = new Biquad[num_voices];
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++)
{
for (int ch = 0; ch < num_channels; ++ch)
{
float in = audioWrite(context, n, ch);
float out = 0;
for (int k = 0; k < num_voices; ++k)
float out += filters[ch][k].filterSample(in);
audioWrite(context, n, ch, out);
}
}
}
I'm a bit lost as to why. I've googled around and many online examples work just as I have done this.