I often need access to information such as sample rate, number of inputs, etc, in several of the classes I have written for my projects, and initialising them for every class in a function such as foo.init(int sampleRate, int numInputs, etc.) seems redundant. While defining macros in my render.cpp filer or a header file could do the trick, I feel it would be neater to be able to initialise the values during compilation and would allow for the possibility of changing values during runtime.
I tried creating a header file, assigning the variables as public, and then pass the sample rate from render.cpp (I use JUCE for developing code so I haven't actually tested this on the Bela yet, so render.cpp would equal the MainComponent file in a JUCE project):
`
class GlobalInfo {
public:
GlobalInfo(){}
void setup(int sampleRate){
g_SampleRate = sampleRate;
g_SampleDur = 1.0 / sampleRate;
g_Nyquist = sampleRate / 2;
};
int g_SampleRate;
double g_SampleDur;
int g_Nyquist;
};
`
I then make my classes inherit from GlobalInfo, like this (header file):
class RMS : public GlobalInfo {
//code
}
In the RMS.cpp file:
void RMS::init(float frequency)
{
double sampleDurTest = g_sampleDur;
int sampleRateTest = g_sampleRate;
ym1_ = 0.0f;
b1_ = expf(-2.0f * (float)M_PI * (frequency * g_sampleDur));
a0_ = 1.0f - b1_;
}
But despite initialising the variables in setup, in the child classes, they are 0-initialised.