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

This example shows you how to read from the extra audio channels of the audio expander capelet.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <cmath>
float gPhase[10] = {0};
float gFreq[10];
float inAcc[10] = {0};
unsigned int inAccCount[10] = {0};
bool setup(BelaContext *context, void *userData)
{
float baseFreq = 123;
for(unsigned int ch = 0; ch < context->audioOutChannels + context->analogOutChannels; ++ch)
gFreq[ch] = baseFreq * (1 + ch);
return true;
}
void render(BelaContext *context, void *userData)
{
// process the audio channels
for(unsigned int n = 0; n < context->audioFrames; ++n)
{
for(unsigned int ch = 0; ch < context->audioOutChannels; ++ch)
{
float in = audioRead(context, n, ch);
inAcc[ch] += in;
++inAccCount[ch];
float out = 0.4f * sinf(gPhase[ch]);
gPhase[ch] += 2.f * (float)M_PI * gFreq[ch] / context->audioSampleRate;
if(gPhase[ch] > M_PI)
gPhase[ch] -= 2.f * (float)M_PI;
audioWrite(context, n, ch, out);
}
}
// process the analog channels
for(unsigned int n = 0; n < context->analogFrames; ++n)
{
for(unsigned int ch = 0; ch < context->analogInChannels; ++ch)
{
int idx = ch + context->audioOutChannels;
float in = analogRead(context, n, ch);
inAcc[idx] += in;
++inAccCount[idx];
// when using the capelet, the analog output is AC-coupled,
// so we center it around 0, exactly the same as for the audio out
float out = 0.4f * sinf(gPhase[idx]);
// use the analogSampleRate instead
gPhase[idx] += 2.f * (float)M_PI * gFreq[idx] / context->analogSampleRate;
if(gPhase[idx] > (float)M_PI)
gPhase[idx] -= 2.f * (float)M_PI;
analogWriteOnce(context, n, ch, out);
}
}
static int count = 0;
for(unsigned int n = 0; n < context->audioFrames; ++n)
{
count += 1;
if(count % (int)(context->audioSampleRate * 0.5f) == 0)
{
rt_printf("Average input:\n");
for(unsigned int n = 0; n < 10; ++n)
{
rt_printf("[%d]:\t%.3f\t", n, inAcc[n]/inAccCount[n]);
if(n % 2 == 1)
rt_printf("\n");
}
}
}
}
void cleanup(BelaContext *context, void *userData)
{
}
Main Bela public API.
static float audioRead(BelaContext *context, int frame, int channel)
Read an audio input, specifying the frame number (when to read) and the channel.
Definition Bela.h:1458
static float analogRead(BelaContext *context, int frame, int channel)
Read an analog input, specifying the frame number (when to read) and the channel.
Definition Bela.h:1480
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
static void analogWriteOnce(BelaContext *context, int frame, int channel, float value)
Write an analog output, specifying the frame number (when to write) and the channel.
Definition Bela.h:1491
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
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
const uint32_t analogFrames
The number of analog frames per block.
Definition Bela.h:341
const uint32_t analogOutChannels
The number of analog output channels.
Definition Bela.h:351
const uint32_t analogInChannels
The number of analog input channels.
Definition Bela.h:346
const float analogSampleRate
Analog sample rate in Hz.
Definition Bela.h:362