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

A simple tremolo effect

This sketch demonstrates how to make a simple tremolo effect with one potiometer to control the rate of the effect. A tremolo effect is a simple type of amplitude modulation where the amplitude of one signal is continuous modulated by the amplitude of another. This is achieved by multiplying two signals together.

In this example we want to create a tremolo effect like that you would find in a guitar effects box so our first signal will be our audio input into which we could plug a guitar or external sound source. This will be our 'carrier' signal.

The second signal that we will use, the 'modulator', will be a low freqeuncy oscillator (LFO), in this case a sinetone which we will generate in the same way as the 01-Basic/sinetone example. The frequency of this sinetone is determined by a global variable, gFrequency. Again, the sinetone is produced by incrementing the phase of a sine function on every audio frame.

In render() you'll see two nested for loop structures, one for audio and the other for the analogs. You should be pretty familiar with this structure by now. In the first of these loops we deal with all the audio – in the second with reading the analog input channels. We read the value of analog input 0 and map it to an appropriate range for controlling the frequency of the sine tone.

The lfo is then mulitplied together with the audio input and sent to the audio output.

Hardware

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <cmath>
#include <algorithm>
unsigned int gAudioChannelNum; // number of audio channels to iterate over
float gFrequency = 4.0;
float gPhase;
float gInverseSampleRate;
int gAudioFramesPerAnalogFrame = 0;
bool setup(BelaContext *context, void *userData)
{
// If the amout of audio input and output channels is not the same
// we will use the minimum between input and output
gAudioChannelNum = std::min(context->audioInChannels, context->audioOutChannels);
// Check that we have the same number of inputs and outputs.
if(context->audioInChannels != context->audioOutChannels){
printf("Different number of audio outputs and inputs available. Using %d channels.\n", gAudioChannelNum);
}
if(context->analogSampleRate > context->audioSampleRate)
{
fprintf(stderr, "Error: for this project the sampling rate of the analog inputs has to be <= the audio sample rate\n");
return false;
}
if(context->audioFrames)
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
return true;
}
void render(BelaContext *context, void *userData)
{
// Nested for loops for audio channels
for(unsigned int n = 0; n < context->audioFrames; n++) {
if(gAudioFramesPerAnalogFrame && !(n % gAudioFramesPerAnalogFrame)) {
// Read analog channel 0 and map the range from 0-1 to 0.25-20
// use this to set the value of gFrequency
gFrequency = map(analogRead(context, n, 0), 0.0, 1.0, 0.25, 20.0);
}
// Generate a sinewave with frequency set by gFrequency
// and amplitude from -0.5 to 0.5
float lfo = sinf(gPhase) * 0.5f;
// Keep track and wrap the phase of the sinewave
gPhase += 2.0f * (float)M_PI * gFrequency * gInverseSampleRate;
if(gPhase > M_PI)
gPhase -= 2.0f * (float)M_PI;
for(unsigned int channel = 0; channel < gAudioChannelNum; channel++) {
// Read the audio input and half the amplitude
float input = audioRead(context, n, channel) * 0.5f;
// Write to audio output the audio input multiplied by the sinewave
audioWrite(context, n, channel, (input*lfo));
}
}
}
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
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 uint32_t audioInChannels
The number of audio input channels.
Definition Bela.h:324
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 float analogSampleRate
Analog sample rate in Hz.
Definition Bela.h:362