Sample by Sample processing - simple example


Just learning JUCE and I want to implement a simple LPF filter as a test.
The approach is ‘sample by sample’, though I’m sure built in classes for full buffer already exists.

In Max/MSP or the like I could code the filter in a simple iterative way, as so:

a = 0.986; //A good coefficient value for LPF

y = (1 - a) * x + a * yn;
yn = y;

x is the ‘monofied’ input and y is the expected output.

I’ve tried this in JUCE and it failed:

float** channelDatas = buffer.getArrayOfWritePointers();
for (int i = 0; i<buffer.getNumSamples(); ++i)
	//Define x as the current sample value
	float x = 0.5f * (channelDatas[0][i] + channelDatas[1][i]);
	//a is the single coefficient
	float a = 0.986995;

	float y = (1 - a) * x + a * yn;

	channelDatas[0][i] = y;
	channelDatas[1][i] = y;

	float yn = y;

I think I’m not initializing y and yn correctly. Any comments please ?

It would be useful to know how it fails.

If I have to make a guess from what you posted, it looks like you are resetting the filter history at each call to processBlock, which won’t work. You need to have the filter history state as member variable(s) of your AudioProcessor subclass and reset that only when needed.

Well, it fails to ‘build’ on an error.
The error is due to yn being used before it is initialized or decalred (in the function code line).
But if I declare it before the ‘loop’, the output is simply attenuated.

So, the question is how to intiailize and declare yn while keeping it for next time the system enters this loop.

By having it as a class member variable of your AudioProcessor subclass.

Yes. This seems to have made work.