Using optimised filter banks
This example creates a bank of filters using the QuadBiquad class.
The QuadBiquad class is an extension of the Biquad filter class which allows you to process four channels in parallel. To learn more about Biquad filters and how to use the Biquad library, make sure to check Nigel Redmon's fantastic blog: https://www.earlevel.com/main/category/digital-audio/filters/iir-filters/biquads/
In this example we create 150 instances the QuadBiquad filter using bs(150). These instances are connected in series to create some very very long filterbanks, each of which contains lowpass and highpass filters.
In setup() we set the type of filters and their cutoff frequency.
As each QuadBiquad object contains four filters (in parallel), which are completely independent of each other you can set each to a different type of filter, cutoff, Q, etc. Here we give to each of these four filters an increasing cutoff. This will result, in the right channel being brighter than the left channel.
We then process (up to) four audio inputs through the filterbanks and output the result through all available output channels.
 
#include <vector>
#include <libraries/Biquad/QuadBiquad.h>
 
std::vector<QuadBiquad> bs(150);
 
{
                .q = 0.8,
                .peakGainDb = 0,
        };
        
        for(unsigned int b = 0; b < bs.size(); ++b)
        {
                if(b < bs.size() / 2) {
                        
                        s.
type = BiquadCoeff::lowpass;
                } else {
                        
                        s.
type = BiquadCoeff::highpass;
                }
                for(unsigned int q = 0; q < 4; ++q)
                {
                        
                        
                        
                        
                        
                        
                        bs[b].filters[q].setup(s);
                }
                
                
                
                bs[b].update();
        }
        return true;
}
 
{
        unsigned int len = bs.size() & (~3);
        {
                const unsigned int datasz = 4;
                float data[datasz];
                
                
                
                for(unsigned int b = 0; b < len; ++b) {
                         bs[b].process(data);
                         
                         
                }
                
        }
}
 
{}
static float audioRead(BelaContext *context, int frame, int channel)
Read an audio input, specifying the frame number (when to read) and the channel.
Definition Bela.h:1458
static void audioWrite(BelaContext *context, int frame, int channel, float value)
Write an audio output, specifying the frame number (when to write) and the channel.
Definition Bela.h:1469
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t audioOutChannels
The number of audio output channels.
Definition Bela.h:326
const uint32_t audioFrames
The number of audio frames per block.
Definition Bela.h:322
const uint32_t audioInChannels
The number of audio input channels.
Definition Bela.h:324
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328
double cutoff
Cutoff in Hz.
Definition Biquad.h:37
Type type
Filter type.
Definition Biquad.h:36