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

GUI Mouse Tracker

In this project you can find a sketch.js file which is a p5.js file that is rendered in a browser tab. Click the GUI button (next to the Scope button) in the IDE to see the rendering of this file.

This example receives a buffer of data from the GUI with the X-Y coordinates of the mouse (normalised to the size of the window). The Y axis controls the pitch of an oscillator while the X axis controls L/R panning. In order to receive a buffer from the GUI, the type of the buffer and maximum number of values that it will hold need to be specified in setup:

myGUi.setBuffer('f', 2);

In this case we are expecting to receive a buffer of floats with a maximum of 2 elements. This function will return the index of the buffer (which is given automatically based on the order they are set on). This buffer will have index = 0. The buffer can then be accessed using this index:

DataBuffer& buffer = myGui.getDataBuffer(0);
Definition DataBuffer.h:12

Notice that the Gui::getDataBuffer() method returns a DataBuffer& (that is: a reference to a DataBuffer). You should in turn always be storing the return value in a DataBuffer& variable to minimize copy overhead, and ensure that it can be used safely from within a real-time context.

And its contents retrieved in the desired format (floats in this case): float* data = buffer.getAsFloat();

Additionally, two One-pole low pass filters are used to smooth the values read from the GUI and avoid glitches.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/Gui/Gui.h>
#include <libraries/OnePole/OnePole.h>
#include <cmath>
// GUI object declaration
Gui myGui;
// One Pole filter object declaration
OnePole freqFilt, ampFilt;
// Default frequency and panning values for the sinewave
float gFrequency = 440.0;
float gAmpL = 1.0;
float gAmpR = 1.0;
float gPhase;
float gInverseSampleRate;
bool setup(BelaContext *context, void *userData)
{
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
// Setup GUI. By default, the Bela GUI runs on port 5555 and address 'gui'
myGui.setup(context->projectName);
// Setup buffer of floats (holding a maximum of 2 values)
myGui.setBuffer('f', 2); // Index = 0
// Setup low pass filters for smoothing frequency and amplitude
freqFilt.setup(1, context->audioSampleRate); // Cut-off frequency = 1Hz
ampFilt.setup(1, context->audioSampleRate); // Cut-off frequency = 1Hz
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
// Get buffer 0
DataBuffer& buffer = myGui.getDataBuffer(0);
// Retrieve contents of the buffer as floats
float* data = buffer.getAsFloat();
// Map Y-axis (2nd element in the buffer) to a frequency range
gFrequency = map(data[1], 0, 1, 600, 320);
// Constrain frequency
gFrequency = constrain(gFrequency, 320, 600);
// Smooth frequency using low-pass filter
gFrequency = freqFilt.process(gFrequency);
// Constrain amplitude (given by the X-axis, 1st element in the buffer)
// between 0 and 1.
// Smooth value using low-pass filter
float amplitude = ampFilt.process(constrain(data[0], 0, 1));
// Calculate amplitude of left and right channels
gAmpL = 1 - amplitude;
gAmpR = amplitude;
float out = 0.4f * sinf(gPhase);
// Write sinewave to left and right channels
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
if(channel == 0) {
audioWrite(context, n, channel, gAmpL*out);
} else if (channel == 1) {
audioWrite(context, n, channel, gAmpR*out);
}
}
// Update and wrap phase of sine tone
gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
}
}
void cleanup(BelaContext *context, void *userData)
{
}
Main Bela public API.
float * getAsFloat()
Definition DataBuffer.h:103
Definition Gui.h:15
Definition OnePole.h:12
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
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Linearly rescale a number from one range of values to another.
Definition Utilities.h:71
static float constrain(float x, float min_val, float max_val)
Constrain a number to stay within a given range.
Definition Utilities.h:81
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
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