Bela

From EMC Electronic Music Coders
Jump to navigation Jump to search
____  _____ _        _    
| __ )| ____| |      / \   
|  _ \|  _| | |     / _ \  
| |_) | |___| |___ / ___ \ 
|____/|_____|_____/_/   \_\

Bela is an embedded computing platform for creating beautifully responsive interactive applications. Bela provides ultra-low latency, high quality audio, analog and digital I/O in a tiny self-contained package that can be easily embedded. Built upon the BeagleBone family of open-source embedded computers, Bela combines the processing power of an embedded computer with the timing precision and connectivity of a microcontroller.

Mini.jpg

Software

#include <Bela.h>

     #include <cmath>
     float gFrequency = 440.0;
     float gPhase;
     float gInverseSampleRate;
     bool setup(BelaContext *context, void *userData)
     {
       gInverseSampleRate = 1.0 / context->audioSampleRate;
       gPhase = 0.0;
       return true;
     }
     void render(BelaContext *context, void *userData)
     {
        for(unsigned int n = 0; n < context->audioFrames; n++) {
               float out = 0.8f * sinf(gPhase);
               gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
               if(gPhase > M_PI)
                     gPhase -= 2.0f * (float)M_PI;
               for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
                      audioWrite(context, n, channel, out);
               }
         }
     }
     void cleanup(BelaContext *context, void *userData)
    {
    }

example sinetone/render.cpp

Producing your first bleep!


This sketch is the hello world of embedded interactive audio. Better known as bleep, it produces a sine tone.

The frequency of the sine tone is determined by a global variable, `gFrequency`. The sine tone is produced by incrementing the phase of a sin function on every audio frame.

In render() you'll see a nested for loop structure. You'll see this in all Bela projects. The first for loop cycles through 'audioFrames', the second through 'audioChannels' (in this case left 0 and right 1). It is good to familiarise yourself with this structure as it's fundamental to producing sound with the system.

Hardware

Notes

  • Running Pd comes with some overhead, so whereas an empty C++ project, is about 9%, an empty Pd project is about 17% (at 16 samples per block).