Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
Audio/sample-loader/render.cpp

Simple Sample Loader

This example loads a specified range of samples from a file into a buffer using a helper function provided in libraries/AudioFile/AudioFile.h. This should be used when working with small wav files. See sampleStreamer and sampleStreamerMulti for more elaborate ways of loading and playing back larger files.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/AudioFile/AudioFile.h>
#include <vector>
std::string gFilename = "waves.wav";
int gStartFrame = 44100;
int gEndFrame = 88200;
std::vector<std::vector<float> > gSampleData;
unsigned int gReadPtr; // Position of last read sample from file
bool setup(BelaContext *context, void *userData)
{
gSampleData = AudioFileUtilities::load(gFilename, gEndFrame - gStartFrame, gStartFrame);
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
// Increment read pointer and reset to 0 when end of file is reached
if(++gReadPtr > gSampleData[0].size())
gReadPtr = 0;
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
// Wrap channel index in case there are more audio output channels than the file contains
float out = gSampleData[channel%gSampleData.size()][gReadPtr];
audioWrite(context, n, channel, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}
Main Bela public API.
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
std::vector< std::vector< float > > load(const std::string &filename, int maxCount=-1, unsigned int start=0)
Definition AudioFileUtilities.cpp:135
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