Introduction to Bela Code - BelaPlatform/Bela GitHub Wiki

The Bela Wiki has moved to learn.bela.io. This material has been superseded. Click here for the maintained version.

Coding in Bela

An introduction to audio and sensor processing on Bela

Audio and analog data are in interleaved format.

Sample alignment between audio, analog and digital. Unlike Arduino, the I/O functions require the frame number at which the read or write should take place, since all I/O happens synchronously with the audio clock.

Caveat: if you're using PureData to build projects in Bela then you can get by without worrying about the following too much, but if you are serious about building great projects then it's very important to know. The concepts of Blocks, Frames and Samples that we will introduce below is fundamental to all digital signal processing applications, from realtime plugins to stand alone synthesisers, so it's definitely worthwhile familiarising yourself with these concepts and code structures.

Blocks, Frames and Samples

Blocks

We gather, process and output signals in chunks known as blocks. Block size is the set by audio block size - the amount of samples across all channels.

Frames

A frame is the slice of time across all channels.

Samples

A sample is the individual value for each channel. Like frame but for a specific channel.

Nested for loop structure

In render() you'll see a nested for loop structure in almost all Bela projects. The first for loop cycles through audioFrames (up to the number of audioFrames in each block), the second through audioOutChannels (in this case left 0 and right 1).

for(unsigned int n = 0; n < context->audioFrames; n++) 
{
	for(unsigned int ch = 0; ch < context->audioOutChannels; ch++)
    {
		// Audio code here
		input = audioRead(context, n, ch);
		audioWrite(context, n, ch, input);
	}
}

The above example is for audio but the same holds for analog inputs and outputs. In this case we cycle through analogFrames (up to the number of analogFrames in each block), the second through analogOutChannels (in this case 0-8).

for(unsigned int n = 0; n < context->analogFrames; n++) 
{
	for(unsigned int ch = 0; ch < context->analogOutChannels; ch++)
    {
		// Analog code here
		analogInput[ch] = analogRead(context, n, ch);
	}
}
⚠️ **GitHub.com Fallback** ⚠️