Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
Gui/frequency-response/render.cpp

Adjustable filter parameters

This is an example of a more complex use of the GUI as a controller. The GUI here is used to set a filter's parameters and display its frequency response.

This example uses four libraries:

The best way to exerience this example is to play some audio into Bela's audio inputs. A low pass filter is applied to the audio input. If you press the GUI button you will find a control interface which allows you to control the Cutoff frequency (in Hertz) and Q (or resonance) of the filter applied to the audio input. You will hear the effect of the filter.

There are also various controls which allow you to adjust the visualisation of the filter's frequency response. The audio filter is applied in render() and we listen for changes to the settings of the GUI with the guiCallback function.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <vector>
#include <libraries/Fft/Fft.h>
#include <libraries/Gui/Gui.h>
#include <libraries/Biquad/Biquad.h>
#include <libraries/Pipe/Pipe.h>
#include <stdexcept>
Pipe gPipe;
Gui gui;
std::vector<Biquad> gAudioBiquads;
unsigned int kFftLen = 2048;
static bool existsAndIsNumber(JSONObject& json, const std::wstring& str)
{
return (json.find(str) != json.end() && json[str]->IsNumber());
}
static double retrieveAsNumber(JSONObject& json, const std::string& str)
{
std::wstring ws = JSON::s2ws(str);
if(existsAndIsNumber(json, ws))
{
double ret = json[ws]->AsNumber();
printf("Received parameter \"%s\": %f\n", str.c_str(), ret);
return ret;
}
else
throw(std::runtime_error("Value " + str + "not found\n"));
}
// when the settings change from the GUI, this function is called
bool guiCallback(JSONObject& json, void*)
{
static Biquad::Settings settings;
settings.type = Biquad::lowpass;
settings.fs = 44100; // this is only used for the visualisation and should match the one in sketch.js
try {
settings.cutoff = retrieveAsNumber(json, "cutoff");
} catch (std::exception& e) {}
try {
settings.q = retrieveAsNumber(json, "q");
} catch (std::exception& e) {}
try {
settings.peakGainDb = retrieveAsNumber(json, "peakGainDb");
} catch (std::exception& e) {}
// send the new settings to the audio thread
gPipe.writeNonRt(settings);
// now compute the frequency response and send it to the GUI
static Fft fft(kFftLen);
static std::vector<float> ir(kFftLen);
static std::vector<float> buf(kFftLen / 2);
fft.setup(kFftLen);
Biquad analysisBiquad(settings);
for(unsigned int n = 0; n < ir.size(); ++n)
{
// process an impulse through the filter to obtain the
// impulse response
ir[n] = analysisBiquad.process(0 == n ? 1 : 0);
}
fft.fft(ir);
float mx = -100000;
for(unsigned int n = 0; n < buf.size(); ++n)
{
buf[n] = fft.fda(n);
mx = mx < buf[n] ? buf[n] : mx;
}
gui.sendBuffer(0, buf);
return false;
}
bool setup(BelaContext *context, void *userData)
{
gPipe.setup("guiToLoop");
gAudioBiquads.resize(
std::min(context->audioInChannels, context->audioOutChannels),
.fs = context->audioSampleRate,
.type = Biquad::lowpass,
.cutoff = 6000,
.q = 0.707,
.peakGainDb = 6,
})
);
gui.setup(context->projectName);
gui.setControlDataCallback(guiCallback, nullptr);
return true;
}
void render(BelaContext *context, void *userData)
{
// receive any command from the gui through the pipe
Biquad::Settings settings;
bool newSettingsReceived = false;
while(1 == gPipe.readRt(settings))
{
newSettingsReceived = true;
// in case more than one came through, we only care about the
// most recent
continue;
}
if(newSettingsReceived) {
settings.fs = context->audioSampleRate;
for(auto& b : gAudioBiquads)
{
b.setup(settings);
b.clean(); // clean the internal filter state to avoid blowing up the filter
}
}
for(unsigned int n = 0; n < context->audioFrames; ++n)
{
for(unsigned int c = 0; c < gAudioBiquads.size(); ++c)
{
float in = audioRead(context, n, c);
// filter the audio input ...
float out = gAudioBiquads[c].process(in);
// ... and write it to the output
audioWrite(context, n, c, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}
Main Bela public API.
Definition Biquad.h:99
Definition Fft.h:12
Definition Gui.h:15
Definition Pipe.h:10
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
char projectName[MAX_PROJECTNAME_LENGTH]
Name of running project.
Definition Bela.h:417
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328
Definition Biquad.h:34
double fs
Sample rate in Hz.
Definition Biquad.h:35
double q
Quality factor.
Definition Biquad.h:38
double cutoff
Cutoff in Hz.
Definition Biquad.h:37
Type type
Filter type.
Definition Biquad.h:36
double peakGainDb
Maximum filter gain.
Definition Biquad.h:39