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:
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:
Additionally, two One-pole low pass filters are used to smooth the values read from the GUI and avoid glitches.
#include <libraries/Gui/Gui.h>
#include <libraries/OnePole/OnePole.h>
#include <cmath>
float gFrequency = 440.0;
float gAmpL = 1.0;
float gAmpR = 1.0;
float gPhase;
float gInverseSampleRate;
{
gPhase = 0.0;
myGui.setBuffer('f', 2);
return true;
}
{
for(
unsigned int n = 0; n < context->
audioFrames; n++) {
gFrequency =
map(data[1], 0, 1, 600, 320);
gFrequency =
constrain(gFrequency, 320, 600);
gFrequency = freqFilt.process(gFrequency);
float amplitude = ampFilt.process(
constrain(data[0], 0, 1));
gAmpL = 1 - amplitude;
gAmpR = amplitude;
float out = 0.4f * sinf(gPhase);
if(channel == 0) {
} else if (channel == 1) {
}
}
gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
}
}
{
}
float * getAsFloat()
Definition DataBuffer.h:103
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