I have this piece of code for a video game which updates the gain for a music loop based on how far away the player is from the end door
public void updateMusic()
{
int endX = currentMap.getEndPointX() * TILE_WIDTH;
int endY = currentMap.getEndPointY() * TILE_HEIGHT;
double dist = Math.hypot(endX - playerX, endY - playerY);
if(dist < 300)
{
double gain = 600 / dist;
FloatControl gainControl = (FloatControl) raveLoop.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(Math.min(0, Math.max(gainControl.getMinimum(), (float) (-10 + gain))));
}
else
{
FloatControl gainControl = (FloatControl) raveLoop.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-20f);
}
The gain of the audio is updated, but very choppily. I have a loop that updates it 20 times a second but instead of updating continuously it will suddenly jump/drop in gain randomly. Is there a way to force the audio system to update? Or is this the wrong way to do dynamic audio like this?
New contributor
love4code is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.