I'm trying to get an OLED display to update what it's showing based on the value of a potentiometer (in this case, showing the location of panned audio). I'm struggling to figure out how to send the pot value to the display function in O2O-main. Below is the main project:
#include <Bela.h>
#include <libraries/Scope/Scope.h>
#include "u8g2/U8g2LinuxI2C.h"
#include <libraries/OscSender/OscSender.h>
// Analog inputs
float gPot1;
// Bela oscilloscope
Scope gScope;
int gAudioFramesPerAnalogFrame = 0;
float amplitudeLeft;
float amplitudeRight;
float value = 0;
void sendToDisplayBackground(void *);
OscSender gOscSender;
size_t gRefreshSamples;
constexpr size_t kColumns = 128; // number of columns on your display
float gDisplayData[kColumns];
size_t gDisplayPtr = 0;
AuxiliaryTask gSendToDisplayTask;
//void sendToDisplay(void*) {
//gOscSender.newMessage("/parameters");
//for(size_t n = 0; n < kColumns; ++n) {
// the value in the buffer was between -1 and 1. Here we make it between 0 and 1
//float value = gDisplayData[n];
//gOscSender.add(value);
//}
//gOscSender.send();
//}
void sendToDisplay(float value) {
gOscSender.newMessage("/panner");
gOscSender.add(value);
gOscSender.send();
}
bool setup(BelaContext *context, void *userData)
{
// calculate no. audio frames per analog frame
if(context->analogFrames)
gAudioFramesPerAnalogFrame = context->audioFrames / context->analogFrames;
// Initialise the oscilloscope
gScope.setup(4, context->audioSampleRate);
gRefreshSamples = context->audioSampleRate * 0.05; // 50ms refresh rate
gOscSender.setup(7562);
gSendToDisplayTask = Bela_createAuxiliaryTask(sendToDisplayBackground, 1, "sendToDisplay");
return true;
}
void sendToDisplayBackground(void *){
sendToDisplay(value);
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; n++) {
// read analog inputs and update amplitudes
gPot1 = analogRead(context, n/gAudioFramesPerAnalogFrame, 0);
amplitudeLeft = map(gPot1, 0, 0.824, 1, 0);
amplitudeRight = map(gPot1, 0, 0.824, 0, 1);
// read input
float in = 0.5*audioRead(context, n, 0) + 0.5*audioRead(context, n, 1);
// pan audio
float outLeft = amplitudeLeft*in;
float outRight = amplitudeRight*in;
audioWrite(context, n, 0, outLeft);
audioWrite(context, n, 1, outRight);
gScope.log(amplitudeLeft, amplitudeRight, outLeft, outRight);
value = amplitudeLeft;
Bela_scheduleAuxiliaryTask(gSendToDisplayTask);
}
}
void cleanup(BelaContext *context, void *userData)
{
}
And here is the "panner" function within parseMessage in O2O:
else if (msg.match("/panner")){
if(!args.popFloat(param1Value).isOkNoMoreArgs())
error = kWrongArguments;
else {
printf("received /panner\n");
u8g2.drawCircle(displayWidth*param1Value, displayHeight*0.5, 10, U8G2_DRAW_ALL);
}
}
In this current form, the audio panning works as expected, but the circle on the display seems to have a mind of its own, mostly staying to one side of the screen and sometimes moving to the other and jittering about. It's also worth noting that running the panner project uses a surprisingly high amount of the Bela's CPU (~50%, whereas before I tried to include the display stuff it was more like 15-20%), and Error while sending to pipe from OscSndrTsk_127.0.0.17562: (12) Cannot allocate memory (size: 16) appears repeatedly in the IDE's console.
Any help that anyone can offer would be greatly appreciated.