This example shows how to make an audio level meter with a set of ten LEDs.
You will also need to connect an audio adapter to the audio input pins and connect something that can produce an audio signal to this (a laptop, mp3 player, phone or keyboard). The code preforms peak detection on the audio input and lights the LEDs once the amplitude of the input signal passes a certain threshold stored in gThresholds[i]. Each LED has its own threshold, the values of which are set in setup() and are steps of -3dB. All digital pins are set to output pinMode(context, 0, i, OUTPUT); and they are toggled on and off in render() when the amplitude passes the threshold for that LED. The LEDs below the current peak value always remain lit to create a classic amplitude peak meter. The audio input is passed through to the output so you can listen as you watch the light show.
 
#include <cmath>
#include <algorithm>
 
 
#define NUMBER_OF_SEGMENTS      10
 
unsigned int gAudioChannelNum; 
 
float gAudioLocalLevel = 0, gAudioPeakLevel = 0;
 
float gLocalDecayRate = 0.99, gPeakDecayRate = 0.999;
 
float gThresholds[NUMBER_OF_SEGMENTS + 1];
int gSamplesToLight[NUMBER_OF_SEGMENTS];
 
float gLastX[2] = {0};
float gLastY[2] = {0};
 
double gB0 = 0.99949640;
double gB1 = -1.99899280;
double gB2 = gB0;
double gA1 = -1.99899254;
double gA2 = 0.99899305;
 
{
        
        
                rt_printf("Error: this project needs the audio and digital sample rates to be the same.\n");
                return false;
        }
 
        
        
 
        
        
        for(int i = 0; i < NUMBER_OF_SEGMENTS + 1; i++) {
                gThresholds[i] = powf(10.0f, (-1.0 * (NUMBER_OF_SEGMENTS - i)) * .05);
        }
 
        for(int i = 0; i < NUMBER_OF_SEGMENTS; i++) {
                gSamplesToLight[i] = 0;
        }
 
        return true;
}
 
{
        for(
unsigned int n = 0; n < context->
audioFrames; n++) {
 
 
                
                float sample = 0;
                }
 
                
                for(unsigned int ch = 0; ch < gAudioChannelNum; ch++)
 
                
                float out = gB0 * sample + gB1 * gLastX[0] + gB2 * gLastX[1]
                                                - gA1 * gLastY[0] - gA2 * gLastY[1];
 
                gLastX[1] = gLastX[0];
                gLastX[0] = sample;
                gLastY[1] = gLastY[0];
                gLastY[0] = out;
 
                out = fabsf(out / (float)gAudioChannelNum);
 
                
                if(out > gAudioLocalLevel)
                        gAudioLocalLevel = out;
                else
                        gAudioLocalLevel *= gLocalDecayRate;
 
                
                if(out > gAudioPeakLevel)
                        gAudioPeakLevel = out;
                else {
                        
                        
                                gAudioPeakLevel *= gPeakDecayRate;
                }
                
                for(int led = 0; led < NUMBER_OF_SEGMENTS; led++) {
                        
                        
                        int state = LOW;
 
                        if(gAudioLocalLevel > gThresholds[led]) {
                                state = HIGH;
                                gSamplesToLight[led] = 1000;
                        }
                        
                        else if(--gSamplesToLight[led] > 0)
                                state = HIGH;
 
                        
                }
        }
}
 
{
 
}
static float audioRead(BelaContext *context, int frame, int channel)
Read an audio input, specifying the frame number (when to read) and the channel.
Definition Bela.h:1458
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
static void digitalWriteOnce(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition Bela.h:1538
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition Bela.h:1548
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
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t digitalFrames
Number of digital frames per period.
Definition Bela.h:365
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
const uint32_t audioInChannels
The number of audio input channels.
Definition Bela.h:324
const uint64_t audioFramesElapsed
Number of elapsed audio frames since the start of rendering.
Definition Bela.h:379