This example demonstrates how to receive and transmit serial data from Bela. When a 'k' or 's' are received on the specified serial port, a kick or snare sound, respectively, are generated.
The sound generator is very simple and "retro". IT is using a decaying noise burst for the snare sound and a decaying sine sweep for the kick
 
#include <libraries/Pipe/Pipe.h>
#include <libraries/Serial/Serial.h>
#include <cmath>
 
unsigned int gSnareDuration;
int gSnareTime = 0;
unsigned int gKickDuration;
int gKickTime = 0;
float gKickPhase = 0;
 
void serialIo(void* arg) {
        {
                unsigned int maxLen = 128;
                char serialBuffer[maxLen];
                
                int ret = gSerial.read(serialBuffer, maxLen, 100);
                if (ret > 0) {
                        printf("Received: %.*s\n", ret, serialBuffer);
                        for(int n = 0; n < ret; ++n)
                        {
                                
                                
                                
                                
                                if('k' == serialBuffer[n])
                                {
                                        gPipe.writeNonRt('k');
                                        gSerial.write("kick!\n\r");
                                } else if('s' == serialBuffer[n])
                                {
                                        gPipe.writeNonRt('s');
                                        gSerial.write("snare!\n\r");
                                }
                        }
                }
        }
}
 
        gSerial.
setup (
"/dev/ttyUSB0", 19200);
 
        gPipe.setup("serialpipe", 1024);
 
        return true;
}
 
        char c;
        
        while(gPipe.readRt(c) > 0)
        {
                
                if('s' == c)
                        gSnareTime = gSnareDuration;
                if('k' == c)
                        gKickTime = gKickDuration;
        }
        {
                
                float snareOut = 0;
                float kickOut = 0;
                if(gSnareTime)
                {
                        
                        float noise = 2.f * (rand() / (float)RAND_MAX) - 1.f;
                        float env = gSnareTime / (float)gSnareDuration;
                        snareOut = 0.4f * noise * env;
                        --gSnareTime;
                }
                if(gKickTime)
                {
                        
                        float frequency = 
map(gKickTime, gKickDuration, 0, 150, 20);
 
                        float env = gKickTime / (float)gKickDuration;
                        if(gKickPhase > M_PI)
                                gKickPhase -= 2.f * (float)M_PI;
                        kickOut = env * sinf(gKickPhase);
                        --gKickTime;
                }
                float out = snareOut + kickOut;
                {
                }
        }
}
 
bool setup(const std::string &pipeName=defaultName, size_t size=65536 *128, bool newBlockingRt=false, bool newBlockingNonRt=false)
Definition Pipe.cpp:13
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.
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
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Linearly rescale a number from one range of values to another.
Definition Utilities.h:71
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 float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328