so in my case I have:
main.cpp:
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include <libgen.h>
#include <signal.h>
#include <getopt.h>
#include "../include/Bela.h"
using namespace std;
// Handle Ctrl-C by requesting that the audio rendering stop
void interrupt_handler(int var)
{
gShouldStop = true;
}
// Print usage information
void usage(const char * processName)
{
cerr << "Usage: " << processName << " [options]" << endl;
Bela_usage();
cerr << " --help [-h]: Print this menu\n";
}
int main(int argc, char *argv[])
{
BelaInitSettings settings; // Standard audio settings
struct option customOptions[] =
{
{"help", 0, NULL, 'h'},
{NULL, 0, NULL, 0}
};
// Set default settings
Bela_defaultSettings(&settings);
// Parse command-line arguments
while (1) {
int c;
if ((c = Bela_getopt_long(argc, argv, "h", customOptions, &settings)) < 0)
break;
switch (c) {
case 'h':
usage(basename(argv[0]));
exit(0);
case '?':
default:
usage(basename(argv[0]));
exit(1);
}
}
for(int n = 0; n < 3; ++n)
{
// Initialise the PRU audio device
if(Bela_initAudio(&settings, 0) != 0) {
cout << "Error: unable to initialise audio" << endl;
return -1;
}
// Start the audio device running
if(Bela_startAudio()) {
cout << "Error: unable to start real-time audio" << endl;
// Stop the audio device
Bela_stopAudio();
// Clean up any resources allocated for audio
Bela_cleanupAudio();
return -1;
}
// Set up interrupt handler to catch Control-C and SIGTERM
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
// Run until told to stop
while(!gShouldStop) {
usleep(100000);
}
// Set up interrupt handler to restore Control-C and SIGTERM
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
// Stop the audio device
Bela_stopAudio();
// Clean up any resources allocated for audio
Bela_cleanupAudio();
}
// All done!
return 0;
}
render.cpp: (based on 03-Analog/scope-analog
, but using a Scope* scope
instead of Scope scope
, with new
in setup()
and delete
in cleanup()
).
#include <Bela.h>
#include <cmath>
#include <Scope.h>
Scope* scope;
int gAudioFramesPerAnalogFrame;
float gInverseSampleRate;
float gPhase;
float gAmplitude;
float gFrequency;
float gIn1;
float gIn2;
// For this example you need to set the Analog Sample Rate to
// 44.1 KHz which you can do in the settings tab.
bool setup(BelaContext *context, void *userData)
{
scope = new Scope();
// setup the scope with 3 channels at the audio sample rate
scope->setup(3, context->audioSampleRate);
// Check if analog channels are enabled
if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
return false;
}
// Check that we have the same number of inputs and outputs.
if(context->audioInChannels != context->audioOutChannels ||
context->analogInChannels != context-> analogOutChannels){
printf("Error: for this project, you need the same number of input and output channels.\n");
return false;
}
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
gInverseSampleRate = 1.0 / context->audioSampleRate;
gPhase = 0.0;
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
if(!(n % gAudioFramesPerAnalogFrame)) {
// On even audio samples: read analog inputs and update frequency and amplitude
gIn1 = analogRead(context, n/gAudioFramesPerAnalogFrame, 0);
gIn2 = analogRead(context, n/gAudioFramesPerAnalogFrame, 1);
gAmplitude = gIn1 * 0.8f;
gFrequency = map(gIn2, 0, 1, 100, 1000);
}
// generate a sine wave with the amplitude and frequency
float out = gAmplitude * sinf(gPhase);
gPhase += 2.0f * M_PI * gFrequency * gInverseSampleRate;
if(gPhase > 1.0f * M_PI)
gPhase -= 2.0f * M_PI;
// log the sine wave and sensor values on the scope
scope->log(out, gIn1, gIn2);
// pass the sine wave to the audio outputs
for(unsigned int channel = 0; channel < context->audioOutChannels; channel++) {
audioWrite(context, n, channel, out);
}
}
}
void cleanup(BelaContext *context, void *userData)
{
delete scope;
}