Recording the audio input and output to file
This example records the inputs and outputs of Bela to an audio file of a fixed length on disk.
When the program begins it will attempt to allocate enough memory to store gDurationSec
seconds of audio data for each of the input and output channels. If you request an excessive amount of RAM then the program may fail when starting or while running.
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
.
Ã…fter running for gDurationSec
this program will automatically stop and write a two .wav
files to disk. If you have more audio input or output channels then these .wav
files will be multichannel.
These .wav
files will be created in the Resources section of the Project explorer amongst your other project files.
#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);
}