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.
In this project an audio recording processesd through an IIR filter.
To control the playback of the audio sample, use your computer keyboard, by pressing:
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <sys/types.h>
#include <libraries/AudioFile/AudioFile.h>
#include <string>
#include <vector>
std::string fileName;
int gReadPtr;
std::vector<float> data;
float gCutFreq = 200;
float gLastX[2];
float gLastY[2];
double lb0, lb1, lb2, la1, la2 = 0.0;
int gChangeCoeff = 0;
int gFreqDelta = 0;
void initialise_filter(float sample_rate, float freq);
void calculate_coeff(float cutFreq);
bool initialise_aux_tasks();
void check_coeff(void*);
void read_input(void*);
{
fileName = (const char *)userData;
if(0 == data.size()) {
fprintf(stderr, "Unable to load file\n");
return false;
}
gReadPtr = -1;
if(!initialise_aux_tasks())
return false;
return true;
}
{
for(
unsigned int n = 0; n < context->
audioFrames; n++) {
float sample = 0;
float out = 0;
if(gReadPtr != -1)
sample += data[gReadPtr++];
if(gReadPtr >= int(data.size()))
gReadPtr = -1;
out = lb0*sample+lb1*gLastX[0]+lb2*gLastX[1]-la1*gLastY[0]-la2*gLastY[1];
gLastX[1] = gLastX[0];
gLastX[0] = out;
gLastY[1] = gLastY[0];
gLastY[0] = out;
}
}
float gSampleRate = 1;
void initialise_filter(float sample_rate, float freq)
{
gSampleRate = sample_rate;
calculate_coeff(freq);
}
void calculate_coeff(float cutFreq)
{
float sampleRate = gSampleRate;
double f = 2*M_PI*cutFreq/sampleRate;
double denom = 4+2*sqrt(2)*f+f*f;
lb0 = f*f/denom;
lb1 = 2*lb0;
lb2 = lb0;
la1 = (2*f*f-8)/denom;
la2 = (f*f+4-2*sqrt(2)*f)/denom;
gLastX[0] = gLastX [1] = 0;
gLastY[0] = gLastY[1] = 0;
}
bool initialise_aux_tasks()
{
return false;
return false;
rt_printf("Cut-off frequency: %f\n", gCutFreq);
rt_printf("Press 'a' <enter> to start playing the sample, 's' to stop\n");
rt_printf(" 'z' <enter> to low down cut-off freq by 100 Hz, 'x' to raise it\n");
rt_printf("Press 'q' <enter> or ctrl-C to quit\n");
return true;
}
void check_coeff(void*)
{
if(gChangeCoeff == 1)
{
gCutFreq += gFreqDelta;
gCutFreq = gCutFreq < 0 ? 0 : gCutFreq;
gCutFreq = gCutFreq > 22050 ? 22050 : gCutFreq;
rt_printf("Cut-off frequency: %f\n", gCutFreq);
calculate_coeff(gCutFreq);
gChangeCoeff = 0;
}
}
void read_input(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 'z':
gChangeCoeff = 1;
gFreqDelta = -100;
break;
case 'x':
gChangeCoeff = 1;
gFreqDelta = 100;
break;
case 'q':
break;
default:
break;
}
}
}
usleep(1000);
}
}
{
}