Updating Apple’s FilterDemo to use the vDSP biquad

plugin

There was a poorly-documented and little known addition to OSX 10.9 and iOS6, which is the addition of a cascade-able biquad filter to the vDSP library (previously the only way to vDSP accelerate a biquad was to use  vDSP_deq22, which required hand-coding of a buffer and memory allocation, etc).   But despite the lack of fanfare: this is a HUGE deal for AudioUnit programmers since it saves a bunch of code, speeds up computations, reduces the CPU load, optimizes the feedback loop, and eases the process of cascading filters.  The usage is pretty simple too:  you set it up once, and then can call it up as many times as you need.  For cascaded filters, you specify the number of stages and it takes care of everything else for you.

I took the updated FilterDemo from my last blog post and spliced in the vDSP code to see how well it worked.  The result was a 18-22% reduction in CPU load!  Pretty nice when you get less complexity AND improved performance.  Here is the process:

Open up the FilterDemo and add in the accelerate framework:

framework

Include the Accelerate header at the top:

cpp header

Add this code into the kernel class at the top:

    // float setup delays for the biquad loop
    // array number = delays[2 * N + 2] where N=number of cascaded biquads
    // add a section to the {0.f} for each stage (i.e. for 2 biquads = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f}, etc)
    float delays[4] = {0.f, 0.f, 0.f, 0.f}; //For one biquad

kernel-class

Cut out this code at the bottom in the process section:

cut out code

Add in this code where you cut that out:

    // Filter coefficient array:
    // filterCoeffs array number = [5 * N] where N=number of cascaded biquads
    double filterCoeffs[5] = {mA0, mA1, mA2, mB1, mB2};

    vDSP_biquad_Setup setup = vDSP_biquad_CreateSetup(filterCoeffs, 1); // Setup (the number = number of stages)

    vDSP_biquad(setup, delays, inSourceP, 1, inDestP, 1, inFramesToProcess); // Operate

    vDSP_biquad_DestroySetup(setup); // Destroy

end code

Build the plugin and have fun!

-Alex

About alexkenis

Guitarist, philosopher, tinkerer

Leave a comment