visualizer works in a basic manner, disposing needed, gui needs

tweaking, visualizer bar rate could be adjusted;
This commit is contained in:
2018-08-11 02:21:05 -05:00
parent 4a7f4962e2
commit f2a60ea490
11 changed files with 104 additions and 70 deletions

View File

@@ -203,4 +203,5 @@ public class RhythmBullet extends Game {
}
super.dispose();
}
}

View File

@@ -1,15 +0,0 @@
package zero1hd.rhythmbullet.audio.visualizer;
import edu.emory.mathcs.jtransforms.fft.FloatFFT_1D;
public class BasicFFT {
private FloatFFT_1D fft;
public BasicFFT(int window) {
fft = new FloatFFT_1D(window);
}
public void fft(float[] PCM) {
fft.realForward(PCM);
}
}

View File

@@ -3,21 +3,22 @@ package zero1hd.rhythmbullet.audio.visualizer;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.utils.Disposable;
import zero1hd.rhythmbullet.audio.MusicController;
public class DoubleHorizontalVisualizer {
public class DoubleHorizontalVisualizer implements Disposable {
private int width, height, barCount, barWidth, spaceBetweenBars;
private int x, y;
private float barRate = 0.75f;
private ShapeRenderer shapeRenderer;
private PCMSystem pcm;
private int smoothRange;
private int multiplier = 300;
private int[] amplitudes;
private float[] amplitudes;
private int[] barHeights;
private int binsPerBar;
private float normalFactor = 20;
private float barChangeRate = 4f;
private int smoothRange = 1;
/**
*
* @param barCount amount of bars this visualizer should have.
@@ -27,14 +28,14 @@ public class DoubleHorizontalVisualizer {
public DoubleHorizontalVisualizer(int barCount, int width, int height, float spacePercentage, MusicController musicController, PCMSystem PCMSystem) {
this.barCount = barCount;
this.barWidth = width/barCount;
this.spaceBetweenBars = (int) (barWidth * spacePercentage);
this.spaceBetweenBars = MathUtils.round(barWidth * spacePercentage);
this.barWidth -= spaceBetweenBars;
pcm = PCMSystem;
if (barWidth < 1) throw new IllegalArgumentException("The arguments you passed caused the bar width to be 0.");
binsPerBar = (pcm.getFrequencyBins().length/barCount);
this.width = width;
this.height = height;
amplitudes = new int[barCount];
amplitudes = new float[barCount];
barHeights = new int[barCount];
shapeRenderer = new ShapeRenderer();
}
@@ -45,26 +46,29 @@ public class DoubleHorizontalVisualizer {
for (int freq = bar*binsPerBar; freq < (bar*binsPerBar) + binsPerBar; freq++) {
normalizedAmplitude += Math.abs(pcm.getFrequencyBins()[freq]);
}
amplitudes[bar] = (int) (normalizedAmplitude*multiplier);
amplitudes[bar] += normalizedAmplitude * normalFactor;
amplitudes[bar] /= binsPerBar;
barHeights[bar] += Math.max(0, (amplitudes[bar] - barHeights[bar]) * barRate * delta);
if (barHeights[bar] > amplitudes[bar]) barHeights[bar] = amplitudes[bar];
}
for (int bar = 0; bar < barHeights.length; bar++) {
int smoothCount = 1;
for (int range = 0; range < smoothRange; range++) {
if (bar+range < amplitudes.length) {
barHeights[bar] += amplitudes[bar+range];
amplitudes[bar] += amplitudes[bar+range];
smoothCount++;
}
if (bar-range > 0) {
barHeights[bar] += amplitudes[bar-range];
amplitudes[bar] += amplitudes[bar-range];
smoothCount++;
}
}
barHeights[bar] /= smoothCount;
amplitudes[bar] /= smoothCount;
int pixelsMoved = MathUtils.round(amplitudes[bar] - barHeights[bar]);
pixelsMoved = MathUtils.floor(pixelsMoved*delta*barChangeRate);
barHeights[bar] += pixelsMoved;
if (barHeights[bar] < 0) barHeights[bar] = 0;
if (barHeights[bar] > height) barHeights[bar] = height;
}
}
@@ -113,4 +117,9 @@ public class DoubleHorizontalVisualizer {
public int getY() {
return y;
}
@Override
public void dispose() {
pcm.dispose();
}
}

View File

@@ -1,6 +1,8 @@
package zero1hd.rhythmbullet.audio.visualizer;
public interface PCMSystem {
import com.badlogic.gdx.utils.Disposable;
public interface PCMSystem extends Disposable {
float[] getFrequencyBins();