When dealing with large wav files it is usually not a good idea to load the entire file into memory. This example shows how to use two buffers to continually load chunks of the file, thus allowing playback of very large files. While one buffer is being used for playback the other buffer is being filled with the next chunk of samples.
Try uploading a large wav file into the project and playing it back. You will need specify the amount of channels (#``define NUM_CHANNELS) and the name of the file (gFilename).
 
#include <libraries/AudioFile/AudioFile.h>
#include <vector>
 
#define BUFFER_LEN 22050   
 
std::string gFilename = "waves.wav";
int gNumFramesInFile;
 
std::vector<std::vector<float> > gSampleBuf[2];
 
int gReadPtr = BUFFER_LEN;
int gBufferReadPtr = 0;
int gActiveBuffer = 0;
int gDoneLoadingBuffer = 1;
 
 
void fillBuffer(void*) {
 
    
    gBufferReadPtr+=BUFFER_LEN;
 
    
    if(gBufferReadPtr>=gNumFramesInFile)
        gBufferReadPtr=0;
 
    int endFrame = gBufferReadPtr + BUFFER_LEN;
    int zeroPad = 0;
 
    
    
    if((gBufferReadPtr+BUFFER_LEN)>=gNumFramesInFile-1) {
          endFrame = gNumFramesInFile-1;
          zeroPad = 1;
    }
 
    for(unsigned int ch = 0; ch < gSampleBuf[0].size(); ++ch) {
 
        
                    ,gBufferReadPtr,endFrame);
 
        
        if(zeroPad) {
            int numFramesToPad = BUFFER_LEN - (endFrame-gBufferReadPtr);
            for(int n=0;n<numFramesToPad;n++)
                gSampleBuf[!gActiveBuffer][ch][n+(BUFFER_LEN-numFramesToPad)] = 0;
        }
 
    }
 
    gDoneLoadingBuffer = 1;
 
    
 
}
 
{
 
    
                return false;
 
 
    if(gNumFramesInFile <= 0)
        return false;
 
    if(gNumFramesInFile <= BUFFER_LEN) {
        printf("Sample needs to be longer than buffer size. This example is intended to work with long samples.");
        return false;
    }
 
        gSampleBuf[1] = gSampleBuf[0]; 
 
        return true;
}
 
{
    for(
unsigned int n = 0; n < context->
audioFrames; n++) {
 
 
        
        if(++gReadPtr >= BUFFER_LEN) {
            if(!gDoneLoadingBuffer)
                rt_printf("Couldn't load buffer in time :( -- try increasing buffer size!");
            gDoneLoadingBuffer = 0;
            gReadPtr = 0;
            gActiveBuffer = !gActiveBuffer;
        }
 
            
                float out = gSampleBuf[gActiveBuffer][channel%gSampleBuf[0].size()][gReadPtr];
        }
 
    }
}
 
 
{
}
void * AuxiliaryTask
Definition Bela.h:561
int Bela_scheduleAuxiliaryTask(AuxiliaryTask task)
Run an auxiliary task which has previously been created.
AuxiliaryTask Bela_createAuxiliaryTask(void(*callback)(void *), int priority, const char *name, void *arg=NULL)
Create a new auxiliary task.
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 getSamples(const std::string &file, float *buf, unsigned int channel, unsigned int startFrame, unsigned int endFrame)
Definition AudioFileUtilities.cpp:9
int getNumFrames(const std::string &file)
Definition AudioFileUtilities.cpp:66
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