That is a opaque pointer used to pass arguments around. That pointer is passed to the Bela backend when calling the int Bela_initAudio(BelaInitSettings *settings, void *userData)
function. Any Bela project that does not have a custom main.cpp
file uses under the hood core/default_main.cpp
, which contains the call to Bela_init()
, and passes a NULL
pointer as userData
. This means that if you do not have a custom main.cpp
, then userData
is not used.
Some of the example projects provide a custom main()
function and use userData
to pass some data retrieved from the command-ilne to the setup()
function:
./04-Audio/FFT-phase-vocoder/render.cpp: gSampleData = *(SampleData *)userData;
./10-Instruments/d-box/render.cpp: int oscBankHopSize = *(int *)userData;
./11-Extras/second-pru/render.cpp: int pruNumber; // comes from userData
./11-Extras/second-pru/render.cpp: // Which PRU to use is in userData, assuming this project
./11-Extras/second-pru/render.cpp: if(userData == 0) {
./11-Extras/second-pru/render.cpp: pruNumber = *((int *)userData);
./11-Extras/second-pru/render.cpp: int pruNumber = *((int *)userData);
./11-Extras/userdata/render.cpp: if(userData != 0)
./11-Extras/userdata/render.cpp: gFrequency = *(float *)userData;
./terminal-only/filter-FIR/render.cpp: gSampleData = *(SampleData *)userData;
./terminal-only/filter-IIR/render.cpp: gSampleData = *(SampleData *)userData;
./terminal-only/samples/render.cpp: gSampleData = *(SampleData *)userData;
However, similar results could be achieved - in the context of a Bela project - using global variables.
The most powerful use of userData
, however, is when embedding Bela into an other application. For instance, in the code for the Bela backend for SuperCollider, userData
is a pointer to a driver object instance:
Bela_initAudio(&settings, this)
at that point, every time the audio callback function (sc_belaRender()
) is called, it is passed a reference to the driver object and it can (after appropriate casting) access its members:
void sc_belaRender(BelaContext *belaContext, void *userData)
{
SC_BelaDriver *driver = (SC_BelaDriver*)userData;
driver->BelaAudioCallback(belaContext);
}