Beginner programming student here. I’ve decided to take what I’ve done outside the classroom and have started fiddling around with java programming, a raspberry pi and some electronics. I’ve written a program using the minim library that takes the sum of the loudness of both audio channels (audio output from whatever is playing on the pi) and uses it to manipulate an led bar graph to create a sort of audio visualizer (kind of like the digital vu meters that could be seen on some hifis, just a lot less scientific) along with an accompanying display in a window on the monitor. The code works and the result is pretty cool, but a little problem is that latency quickly builds up between the led and monitor displays and the actual audio being played. For example, when audio is paused, the leds will still be changing for a few seconds before they all go dark. This problem doesn’t occur at first, and i’m 99% sure the problem begins when the pi lags for a bit and proceeds to display the frames it missed; essentially setting it behind. I was wondering if there was a way to prevent the pi from doing this? I want the program to skip frames that have been lagged out and instead jump back to being live. I have read on the forums that problems like this are because there is too much going on at once for the pi to handle and that code should be made more efficient and frame rate reduced but it certainly seems that my program is running okay until there is an occasional lag spike. I’m okay with the brief lag spikes as long as there is a way for the program to “reset” once the lag stops and produce results based on current data. I will attach the code. Thanks!
import java.lang.Math;
import java.lang.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
import processing.io.*;
Minim minim;
AudioInput input;
double volume;
int heightValue;
int lightValue;
float volumeStringLength;
int leds[] = {17, 18, 27, 22, 23, 24, 25, 2, 3, 8};
void setup()
{
size(500, 500);
frameRate(60);
for (int i=0; i < 10; i++)
{
GPIO.pinMode(leds[i], GPIO.OUTPUT);
}
minim = new Minim(this);
input = minim.getLineIn();
}
void draw()
{
volume = (double)(input.left.level() + input.right.level());
heightValue = (int)(volume * 350);
fill(204, 102, 0);
clear();
rect((width / 2) - (50 / 2), (height / 2) - (heightValue / 2), 50, heightValue);
text("" + volume, (width / 2) - (textWidth(Double.toString(volume)) / 2), 10);
text("" + heightValue, (width / 2) - (textWidth(String.valueOf(heightValue)) / 2), 25);
if (heightValue >= 450)
{
lightValue = 9;
}
else if (heightValue < 450 && heightValue >= 400)
{
lightValue = 8;
}
else if (heightValue < 400 && heightValue >= 350)
{
lightValue = 7;
}
else if (heightValue < 350 && heightValue >= 300)
{
lightValue = 6;
}
else if (heightValue < 300 && heightValue >= 250)
{
lightValue = 5;
}
else if (heightValue < 250 && heightValue >= 200)
{
lightValue = 4;
}
else if (heightValue < 200 && heightValue >= 150)
{
lightValue = 3;
}
else if (heightValue < 150 && heightValue >= 100)
{
lightValue = 2;
}
else if (heightValue < 100 && heightValue >= 50)
{
lightValue = 1;
}
else if (heightValue < 50 && heightValue > 0)
{
lightValue = 0;
}
else if (heightValue == 0)
{
lightValue = -1;
}
for (int i = 0; i <= lightValue; i++)
{
GPIO.digitalWrite(leds[i], GPIO.LOW);
}
for (int i = lightValue + 1; i <= leds.length - 1; i++)
{
GPIO.digitalWrite(leds[i], GPIO.HIGH);
}
}