This scripts needs to be run in a terminal because it requires you to interact with Bela using your computer's keyboard. Note that it CAN NOT be run from within the IDE or the IDE's console.
This program shows how to playback audio samples from a buffer using key strokes.
Monitoring of the keyboard input is done in a low priority task to avoid interfering with the audio processing.
 
#include <sys/types.h>
#include <libraries/AudioFile/AudioFile.h>
#include <string>
#include <vector>
 
std::string fileName;           
int gReadPtr;                   
std::vector<float> data;
 
bool initialise_trigger();
void trigger_samples(void*);
 
{
        
        fileName = (const char *)userData;
        if(0 == data.size()) {
                fprintf(stderr, "Unable to load file\n");
                return false;
        }
 
        gReadPtr = -1;
        if(initialise_trigger() == false)
                return false;
 
        
 
        return true;
}
 
{
 
        for(
unsigned int n = 0; n < context->
audioFrames; n++) {
 
 
                float out = 0;
                
                if(gReadPtr != -1)
                        out += data[gReadPtr++];        
 
                if(gReadPtr >= data.size())
                        gReadPtr = -1;
 
                        
        }
}
 
 
bool initialise_trigger()
{
                return false;
 
        rt_printf("Press 'a' <enter> to trigger sample,\n"
                          "      's' <enter> to stop the current playing sample\n"
                          "      'q' <enter> or ctrl-C to quit\n");
 
        return true;
}
 
 
void trigger_samples(void*)
{
        
        
        
        
 
        char keyStroke = '.';
 
        fd_set readfds;
    struct timeval tv;
    int    fd_stdin;
        fd_stdin = fileno(stdin);
                FD_ZERO(&readfds);
                FD_SET(fileno(stdin), &readfds);
                tv.tv_sec = 0;
                tv.tv_usec = 1000;
                fflush(stdout);
                
                int num_readable = select(fd_stdin + 1, &readfds, NULL, NULL, &tv);
                
                if(num_readable > 0){
                        scanf("%c", &keyStroke);
                        if(keyStroke != '\n'){ 
                                switch (keyStroke)
                                {
                                        case 'a':
                                                gReadPtr = 0;
                                                break;
                                        case 's':
                                                gReadPtr = -1;
                                                break;
                                        case 'q':
                                                break;
                                        default:
                                                break;
                                }
 
                        }
                }
                usleep(1000);
        }
}
 
 
{
}
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.
void Bela_requestStop()
Tell the Bela program to stop.
int Bela_stopRequested()
Check whether the program should stop.
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< float > loadMono(const std::string &file)
Definition AudioFileUtilities.cpp:157
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