This example records the inputs and outputs of Bela to an audio file of a fixed length on disk.
The program processes the input audio and it stores the input samples into the array gInputs[]. In order to generate something to send to the outputs we apply a filter to the input audio and store this as the output samples into the array gOutputs.
 
#include <libraries/AudioFile/AudioFile.h>
#include <libraries/Biquad/Biquad.h>
#include <vector>
#include <string>
#include <algorithm>
 
std::vector<std::vector<float>> gInputs;
std::vector<std::vector<float>> gOutputs;
std::string gFilenameOutputs = "outputs.wav";
std::string gFilenameInputs = "inputs.wav";
const double gDurationSec = 20; 
unsigned int gWrittenFrames = 0; 
std::vector<Biquad> gBiquads; 
 
{
        
        
        
        try {
                for(auto& c : gInputs)
                        c.resize(numFrames);
                for(auto& c : gOutputs)
                        c.resize(numFrames);
        } catch (std::exception& e) {
                fprintf(stderr, "Error while allocating memory. Maybe you are asking to record too many frames and/or too many channels\n");
                return false;
        }
                .type = Biquad::lowpass,
                .cutoff = 200,
                .q = 0.707,
                .peakGainDb = 0,
        };
        
        return true;
}
 
{
        {
 
                
                        gInputs[c][gWrittenFrames] = 
audioRead(context, n, c);
 
                unsigned int c;
                
                for(c = 0; c < gBiquads.size(); ++c) {
                        float out = gBiquads[c].process(in);
                        gOutputs[c][gWrittenFrames] = out;
                }
 
                ++gWrittenFrames;
                if(gWrittenFrames >= gOutputs[0].size()) {
                        
                        
                        return;
                }
        }
}
 
{
        
        
        
        for(auto& i : gInputs)
                i.resize(gWrittenFrames);
        for(auto& o : gOutputs)
                o.resize(gWrittenFrames);
}
void Bela_requestStop()
Tell the Bela program to stop.
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
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
int write(const std::string &filename, float *buf, unsigned int channels, unsigned int frames, unsigned int samplerate)
Definition AudioFileUtilities.cpp:79
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
const uint32_t audioInChannels
The number of audio input channels.
Definition Bela.h:324
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328