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

Sending from the GUI to Bela

This project is a minimal example on how to send data buffers from p5js to Bela. Bela (render.cpp) receives a buffer of data from p5js (sketch.js) containing three floats: two corresponding to slider values and the third one correponding to a button. These values are then mapped as parameters of an oscillator object. The mapping is: slider1-->pitch slider2-->amplitude button-->play/stop

HOW TO RECEIVE A BUFFER IN BELA

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', 3);

In this case we are expecting to receive a buffer of floats with a maximum of 3 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();

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/Oscillator/Oscillator.h>
#include <libraries/Gui/Gui.h>
#include <cmath>
//create a gui and an oscillator object
Gui gui;
Oscillator oscillator;
bool setup(BelaContext *context, void *userData)
{
// Set up oscillator and GUI
oscillator.setup(context->audioSampleRate);
gui.setup(context->projectName);
//Set the buffer to receive from the GUI
gui.setBuffer('f', 3);
return true;
}
void render(BelaContext *context, void *userData)
{
//We store the DataBuffer in 'buffer'
DataBuffer& buffer = gui.getDataBuffer(0);
// Retrieve contents of the buffer as floats
float* data = buffer.getAsFloat();
//store each element of the array in local variables
float pitch = data[0]; //range of values is [0,1],
pitch = map(pitch,0,1,40,80); // we map it to [20,80] for MIDI pitches
float amplitude = data[1]; //range is [0,1]
float play = data[2]; //0.0 or 1.0
//for mapping
float frequency = 440 * powf(2, (pitch-69)/12); // compute the frequency based on the MIDI pitch
oscillator.setFrequency(frequency);
// notice: no smoothing for amplitude and frequency, you will get clicks when the values change
for(unsigned int n = 0; n < context->audioFrames; n++) {
float out = oscillator.process() * amplitude * play;
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
// Write the sample to every audio output channel
audioWrite(context, n, channel, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{}
Main Bela public API.
float * getAsFloat()
Definition DataBuffer.h:103
Definition Gui.h:15
Definition Oscillator.h:3
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
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